Skip to content

Commit

Permalink
add/use HaHomematicConfigException
Browse files Browse the repository at this point in the history
fix mypy
  • Loading branch information
SukramJ committed Dec 2, 2023
1 parent d59effc commit 2bd85b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
32 changes: 15 additions & 17 deletions hahomematic/central/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
)
from hahomematic.exceptions import (
BaseHomematicException,
HaHomematicConfigException,
HaHomematicException,
NoClients,
NoConnection,
Expand Down Expand Up @@ -1295,32 +1296,29 @@ def use_caches(self) -> bool:
"""Return if caches should be used."""
return self.start_direct is False

def check_config(self) -> bool:
def check_config(self) -> None:
"""Check config."""
if not self.username:
_LOGGER.warning("CHECK_CONFIG: Username must not be empty")
return False
raise HaHomematicConfigException("CHECK_CONFIG: Username must not be empty")
if self.password is None:
_LOGGER.warning("CHECK_CONFIG: Password is required") # type: ignore[unreachable]
return False
raise HaHomematicConfigException("CHECK_CONFIG: Password is required")
if not check_password(self.password):
return False
raise HaHomematicConfigException("CHECK_CONFIG: Password is not valid")
if IDENTIFIER_SEPARATOR in self.name:
_LOGGER.warning("CHECK_CONFIG: Name must not contain %s", IDENTIFIER_SEPARATOR)
return False
raise HaHomematicConfigException(
f"CHECK_CONFIG: Name must not contain {IDENTIFIER_SEPARATOR}"
)

try:
check_or_create_directory(self.storage_folder)
except BaseHomematicException:
_LOGGER.warning("CHECK_CONFIG: directory %s cannot be created", self.storage_folder)
return False
return True
check_or_create_directory(self.storage_folder)

def create_central(self) -> CentralUnit:
"""Return the central."""
if not self.check_config():
raise HaHomematicException("create_central: Config contains errors. See log files.")
return CentralUnit(self)
try:
self.check_config()
return CentralUnit(self)
except BaseHomematicException as bhex:
_LOGGER.warning("CREATE_CENTRAL: Not able to create a central: %s", bhex)
raise # HaHomematicException(bhex) from bhex

def create_json_rpc_client(self) -> JsonRpcAioHttpClient:
"""Return the json rpc client."""
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def get_virtual_remote(self) -> HmDevice | None:
async def get_all_device_descriptions(self) -> tuple[dict[str, Any]] | None:
"""Get device descriptions from CCU / Homegear."""
try:
return tuple(await self._proxy.listDevices()) # type: ignore[return-value]
return tuple(await self._proxy.listDevices())
except BaseHomematicException as ex:
_LOGGER.warning(
"GET_ALL_DEVICE_DESCRIPTIONS failed: %s [%s]", ex.name, reduce_args(args=ex.args)
Expand Down
8 changes: 8 additions & 0 deletions hahomematic/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def __init__(self, *args: Any) -> None:
super().__init__("HaHomematicException", *args)


class HaHomematicConfigException(BaseHomematicException):
"""hahomematic HaHomematicConfigException exception."""

def __init__(self, *args: Any) -> None:
"""Init the HaHomematicConfigException."""
super().__init__("HaHomematicConfigException", *args)


class InternalBackendException(BaseHomematicException):
"""hahomematic InternalBackendException exception."""

Expand Down
19 changes: 10 additions & 9 deletions hahomematic/platforms/generic/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ def _fix_rssi(value: Any) -> int | None:
See https://github.com/danielperna84/hahomematic/blob/devel/docs/rssi_fix.md.
"""
if value is None or not isinstance(value, int):
if value is None:
return None
if -127 < value < 0:
return value
if 1 < value < 127:
return value * -1
if -256 < value < -129:
return (value * -1) - 256
if 129 < value < 256:
return value - 256
if isinstance(value, int):
if -127 < value < 0:
return value
if 1 < value < 127:
return value * -1
if -256 < value < -129:
return (value * -1) - 256
if 129 < value < 256:
return value - 256
return None


Expand Down

0 comments on commit 2bd85b2

Please sign in to comment.