diff --git a/changelog.txt b/changelog.txt index 214e0479..be2520a1 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,10 @@ -Version 0.22.3 (2022-01-16) +Version 0.23.0 (2022-01-16) - Make ["DRY", "RAIN"] sensor a binary_sensor +- Add converter to sensor value + - HmIP-SCTH230 CONCENTRATION to int + - Fix RSSI +- raise connection_checker interval to 60s +- Add sleep interval(120s) to wait with reconnect after successful connection check Version 0.22.2 (2022-01-15) - Rename hub extra_state_attributes to attributes diff --git a/hahomematic/central_unit.py b/hahomematic/central_unit.py index bee2d578..8784b091 100644 --- a/hahomematic/central_unit.py +++ b/hahomematic/central_unit.py @@ -416,13 +416,15 @@ async def is_connected(self) -> bool: self._available = True return True - async def reconnect(self) -> None: + async def reconnect(self, force_immediate: bool = False) -> None: """re-init all RPC clients.""" if await self.is_connected(): _LOGGER.warning( "CentralUnit.reconnect: re-connect to central_unit %s", self.instance_name, ) + if not force_immediate: + await asyncio.sleep(config.RECONNECT_WAIT) for client in self._clients.values(): await client.proxy_re_init() @@ -594,7 +596,7 @@ def stop(self) -> None: self._active = False async def _check_connection(self) -> None: - sleep_time = config.CONNECTION_CHECKER_INTERVAL + connection_checker_interval = config.CONNECTION_CHECKER_INTERVAL while self._active: _LOGGER.debug( "ConnectionCecker.check_connection: Checking connection to server %s", @@ -606,12 +608,12 @@ async def _check_connection(self) -> None: "ConnectionCecker.check_connection: No connection to server %s", self._central.instance_name, ) - await asyncio.sleep(sleep_time) + await asyncio.sleep(connection_checker_interval) await self._central.reconnect() - await asyncio.sleep(sleep_time) + await asyncio.sleep(connection_checker_interval) except NoConnection as nex: _LOGGER.exception("check_connection: no connection: %s", nex.args) - await asyncio.sleep(sleep_time) + await asyncio.sleep(connection_checker_interval) continue except Exception: _LOGGER.exception("check_connection: Exception") diff --git a/hahomematic/config.py b/hahomematic/config.py index ec594fdc..805f6425 100644 --- a/hahomematic/config.py +++ b/hahomematic/config.py @@ -5,6 +5,7 @@ from hahomematic.const import DEFAULT_INIT_TIMEOUT, DEFAULT_TIMEOUT -CONNECTION_CHECKER_INTERVAL = 30 +CONNECTION_CHECKER_INTERVAL = 60 +RECONNECT_WAIT = 120 INIT_TIMEOUT = DEFAULT_INIT_TIMEOUT TIMEOUT = DEFAULT_TIMEOUT diff --git a/hahomematic/platforms/sensor.py b/hahomematic/platforms/sensor.py index b1a68077..5561a7de 100644 --- a/hahomematic/platforms/sensor.py +++ b/hahomematic/platforms/sensor.py @@ -42,4 +42,47 @@ def value(self) -> Any | None: """Return the value.""" if self._value is not None and self._value_list is not None: return self._value_list[self._value] + if convert_func := self._get_converter_func(): + return convert_func(self._value) return self._value + + def _get_converter_func(self) -> Any: + """Return a converter based on sensor.""" + if convert_func := CONVERTERS_BY_DEVICE_PARAM.get( + (self.device_type, self.parameter) + ): + return convert_func + if convert_func := CONVERTERS_BY_PARAM.get(self.parameter): + return convert_func + + +def _convert_float_to_int(value: Any) -> int | None: + """Convert value to int.""" + if value is not None and isinstance(value, float): + return int(value) + return value + + +def _fix_rssi(value: Any) -> int | None: + """Fix rssi value.""" + if value is None or not isinstance(value, int): + return None + if -120 <= value <= 0: + return value + if 0 < value <= 120: + return value * -1 + if -256 <= value <= -146: + return (value * -1) - 256 + if 146 <= value <= 256: + return value - 256 + return None + + +CONVERTERS_BY_DEVICE_PARAM: dict[tuple[str, str], Any] = { + ("HmIP-SCTH230", "CONCENTRATION"): _convert_float_to_int, +} + +CONVERTERS_BY_PARAM: dict[str, Any] = { + "RSSI_PEER": _fix_rssi, + "RSSI_DEVICE": _fix_rssi, +} diff --git a/setup.py b/setup.py index 5af670ce..7af2b2cd 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ def readme(): }, PACKAGE_NAME = "hahomematic" HERE = os.path.abspath(os.path.dirname(__file__)) -VERSION = "0.22.3" +VERSION = "0.23.0" PACKAGES = find_packages(exclude=["tests", "tests.*", "dist", "build"])