Skip to content

Commit

Permalink
Add converter to sensor value (#194)
Browse files Browse the repository at this point in the history
* Add converter to sensor value

* raise connection_checker interval

Add sleep interval to wait with reconnect after successful connection check

* Update version
  • Loading branch information
SukramJ authored Jan 16, 2022
1 parent 2ceb7b3 commit 63a573b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
7 changes: 6 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 7 additions & 5 deletions hahomematic/central_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion hahomematic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 43 additions & 0 deletions hahomematic/platforms/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down

0 comments on commit 63a573b

Please sign in to comment.