From e8b562dce18968ae5880006e95606191ce5e6bff Mon Sep 17 00:00:00 2001 From: SukramJ Date: Sat, 2 Dec 2023 21:00:07 +0100 Subject: [PATCH] Collect config validation errors (#1311) * Collect config validation errors * Update support.py --- changelog.md | 1 + hahomematic/central/__init__.py | 27 ++++++++++----------------- hahomematic/support.py | 28 +++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/changelog.md b/changelog.md index 208a09d1..56e0b687 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,7 @@ # Version 2023.12.0 (2023-12-01) - Add support for away_mode and classic homematic thermostats +- Collect config validation errors # Version 2023.11.4 (2023-11-22) diff --git a/hahomematic/central/__init__.py b/hahomematic/central/__init__.py index 7bae7448..c5bc5f45 100644 --- a/hahomematic/central/__init__.py +++ b/hahomematic/central/__init__.py @@ -35,7 +35,6 @@ EVENT_DATA, EVENT_INTERFACE_ID, EVENT_TYPE, - IDENTIFIER_SEPARATOR, Description, DeviceFirmwareState, EventType, @@ -65,12 +64,7 @@ from hahomematic.platforms.hub import Hub from hahomematic.platforms.hub.button import HmProgramButton from hahomematic.platforms.hub.entity import GenericHubEntity, GenericSystemVariable -from hahomematic.support import ( - check_or_create_directory, - check_password, - get_device_address, - reduce_args, -) +from hahomematic.support import check_config, get_device_address, reduce_args _LOGGER: Final = logging.getLogger(__name__) @@ -1298,16 +1292,15 @@ def use_caches(self) -> bool: def check_config(self, extended_validation: bool = True) -> None: """Check config. Throws BaseHomematicException on failure.""" - if extended_validation and IDENTIFIER_SEPARATOR in self.name: - raise HaHomematicConfigException(f"Name must not contain {IDENTIFIER_SEPARATOR}") - - if not self.username: - raise HaHomematicConfigException("Username must not be empty") - if self.password is None: - raise HaHomematicConfigException("Password is required") - if not check_password(self.password): - raise HaHomematicConfigException("Password is not valid") - check_or_create_directory(self.storage_folder) + if config_failures := check_config( + central_name=self.name, + username=self.username, + password=self.password, + storage_folder=self.storage_folder, + extended_validation=extended_validation, + ): + failures = ", ".join(config_failures) + raise HaHomematicConfigException(failures) def create_central(self, extended_validation: bool = True) -> CentralUnit: """Create the central. Throws BaseHomematicException on validation failure.""" diff --git a/hahomematic/support.py b/hahomematic/support.py index 33b3dfe3..ec4beecd 100644 --- a/hahomematic/support.py +++ b/hahomematic/support.py @@ -18,12 +18,13 @@ CCU_PASSWORD_PATTERN, FILE_DEVICES, FILE_PARAMSETS, + IDENTIFIER_SEPARATOR, INIT_DATETIME, MAX_CACHE_AGE, NO_CACHE_ENTRY, SysvarType, ) -from hahomematic.exceptions import HaHomematicException +from hahomematic.exceptions import BaseHomematicException, HaHomematicException _LOGGER: Final = logging.getLogger(__name__) @@ -62,6 +63,31 @@ def build_headers( return [("Authorization", f"Basic {base64_message}")] +def check_config( + central_name: str | None, + username: str | None, + password: str | None, + storage_folder: str, + extended_validation: bool = True, +) -> list[str]: + """Check config. Throws BaseHomematicException on failure.""" + config_failures: list[str] = [] + if extended_validation and central_name and IDENTIFIER_SEPARATOR in central_name: + config_failures.append(f"Instance name must not contain {IDENTIFIER_SEPARATOR}") + if not username: + config_failures.append("Username must not be empty") + if password is None: + config_failures.append("Password is required") + if not check_password(password): + config_failures.append("Password is not valid") + try: + check_or_create_directory(storage_folder) + except BaseHomematicException as haex: + config_failures.append(reduce_args(haex.args)[0]) + + return config_failures + + def check_or_create_directory(directory: str) -> bool: """Check / create directory.""" if not directory: