Skip to content

Commit

Permalink
Collect config validation errors (#1311)
Browse files Browse the repository at this point in the history
* Collect config validation errors

* Update support.py
  • Loading branch information
SukramJ authored Dec 2, 2023
1 parent 191cb2c commit e8b562d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
27 changes: 10 additions & 17 deletions hahomematic/central/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
EVENT_DATA,
EVENT_INTERFACE_ID,
EVENT_TYPE,
IDENTIFIER_SEPARATOR,
Description,
DeviceFirmwareState,
EventType,
Expand Down Expand Up @@ -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__)

Expand Down Expand Up @@ -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."""
Expand Down
28 changes: 27 additions & 1 deletion hahomematic/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e8b562d

Please sign in to comment.