Consolidate error message constants into ErrorMsg class#1432
Conversation
Replace 19 individual *_ERROR_MSG / *_MSG constants with an ErrorMsg class in constants.py, reducing import clutter across bsblan.py and 14 test files.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1432 +/- ##
=======================================
Coverage 99.89% 99.89%
=======================================
Files 6 6
Lines 986 987 +1
Branches 139 139
=======================================
+ Hits 985 986 +1
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
There was a problem hiding this comment.
Pull request overview
This PR refactors error-message handling in the bsblan async client by consolidating scattered error message string constants into a single ErrorMsg namespace and updating call sites accordingly, aiming to improve consistency and maintainability.
Changes:
- Introduces
ErrorMsginsrc/bsblan/constants.pyand migrates existing error message strings into class attributes. - Updates
src/bsblan/bsblan.pyto raise errors usingErrorMsg.<NAME>instead of*_ERROR_MSGconstants. - Updates multiple tests to reference
ErrorMsgso assertions continue to validate the correct error messages.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/bsblan/constants.py | Replaces module-level error message constants with the new ErrorMsg class. |
| src/bsblan/bsblan.py | Updates all internal error raising to use ErrorMsg attributes. |
| tests/test_version_errors.py | Updates version-related error assertions to use ErrorMsg. |
| tests/test_thermostat.py | Updates multi-parameter error assertion to use ErrorMsg. |
| tests/test_temperature_validation.py | Updates temperature-range error assertion to use ErrorMsg. |
| tests/test_set_hotwater.py | Updates hot water validation error assertions to use ErrorMsg. |
| tests/test_initialization.py | Updates API-version error assertion to use ErrorMsg. |
| tests/test_include_parameter.py | Updates include-list validation assertions to use ErrorMsg. |
| tests/test_dhw_time_switch.py | Updates multi-parameter error assertion to use ErrorMsg. |
| tests/test_configuration.py | Updates _validate_single_parameter error assertions to use ErrorMsg. |
| tests/test_circuit.py | Updates multi-parameter error assertion to use ErrorMsg. |
| tests/test_bsblan_edge_cases.py | Updates API-data-not-initialized error assertion to use ErrorMsg. |
| tests/test_api_validation.py | Updates validator/data-not-initialized error assertions to use ErrorMsg. |
| tests/test_api_initialization.py | Updates API-version error assertion to use ErrorMsg. |
| # Error Messages | ||
| NO_STATE_ERROR_MSG: Final[str] = "No state provided." | ||
| NO_SCHEDULE_ERROR_MSG: Final[str] = "No schedule provided." | ||
| VERSION_ERROR_MSG: Final[str] = "Version not supported" | ||
| FIRMWARE_VERSION_ERROR_MSG: Final[str] = "Firmware version not available" | ||
| TEMPERATURE_RANGE_ERROR_MSG: Final[str] = "Temperature range not initialized" | ||
| API_VERSION_ERROR_MSG: Final[str] = "API version not set" | ||
| MULTI_PARAMETER_ERROR_MSG: Final[str] = "Only one parameter can be set at a time" | ||
| SESSION_NOT_INITIALIZED_ERROR_MSG: Final[str] = "Session not initialized" | ||
| API_DATA_NOT_INITIALIZED_ERROR_MSG: Final[str] = "API data not initialized" | ||
| API_VALIDATOR_NOT_INITIALIZED_ERROR_MSG: Final[str] = "API validator not initialized" | ||
| SECTION_NOT_FOUND_ERROR_MSG: Final[str] = "Section '{}' not found in API data" | ||
| INVALID_CIRCUIT_ERROR_MSG: Final[str] = "Invalid circuit number: {}. Must be 1 or 2." | ||
| INVALID_RESPONSE_ERROR_MSG: Final[str] = ( | ||
| "Invalid response format from BSB-LAN device: {}" | ||
| ) | ||
| EMPTY_SECTION_PARAMS_ERROR_MSG: Final[str] = ( | ||
| "No valid parameters found for section '{}'. " | ||
| "The device may not support this circuit or section." | ||
| ) | ||
| class ErrorMsg: | ||
| """Error message constants for BSBLAN.""" | ||
|
|
||
| NO_STATE = "No state provided." | ||
| NO_SCHEDULE = "No schedule provided." | ||
| VERSION = "Version not supported" | ||
| FIRMWARE_VERSION = "Firmware version not available" | ||
| TEMPERATURE_RANGE = "Temperature range not initialized" | ||
| API_VERSION = "API version not set" | ||
| MULTI_PARAMETER = "Only one parameter can be set at a time" | ||
| SESSION_NOT_INITIALIZED = "Session not initialized" |
There was a problem hiding this comment.
Replacing the module-level *_ERROR_MSG constants with ErrorMsg is a breaking API change for any downstream code importing those names from bsblan.constants. Consider keeping backward-compatible aliases (e.g., NO_STATE_ERROR_MSG = ErrorMsg.NO_STATE, etc.) and deprecating them over at least one release cycle to avoid unexpected runtime ImportErrors.



This pull request refactors how error messages are managed and referenced throughout the
bsblancodebase. Instead of using individual string constants for each error message, all error messages are now encapsulated in a newErrorMsgclass. All code that previously referenced the old constants has been updated to use the new class attributes, improving maintainability and consistency.Key changes include:
Error Message Refactoring:
ErrorMsgclass inconstants.pyto centralize all error message strings, replacing the previous pattern of individual error message constants.bsblan.pyto useErrorMsg.<ATTRIBUTE>instead of the old error message constants, ensuring consistent error handling throughout the codebase. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24]Test Updates:
ErrorMsgclass attributes instead of the old error message constants, ensuring tests remain valid and up to date. [1] [2] [3]This refactor improves code clarity and makes it easier to manage and update error messages in the future.