From 9a997e2df68a22ce13692ab2dd367d3e58a3fa96 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 17 Jan 2022 09:52:31 +0100 Subject: [PATCH] Fix raspihats callbacks (#64122) * Adjust callback registration * Register callbacks in the executor * Move read_dq to online_callback Co-authored-by: epenet --- .../components/raspihats/binary_sensor.py | 20 +++++++++--- homeassistant/components/raspihats/switch.py | 32 ++++++++++++------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/raspihats/binary_sensor.py b/homeassistant/components/raspihats/binary_sensor.py index f8fbc0d010f..2c0ce10a5f3 100644 --- a/homeassistant/components/raspihats/binary_sensor.py +++ b/homeassistant/components/raspihats/binary_sensor.py @@ -2,6 +2,7 @@ from __future__ import annotations import logging +from typing import TYPE_CHECKING import voluptuous as vol @@ -108,12 +109,20 @@ class I2CHatBinarySensor(BinarySensorEntity): self._device_class = device_class self._state = self.I2C_HATS_MANAGER.read_di(self._address, self._channel) + async def async_added_to_hass(self) -> None: + """Register callbacks.""" + if TYPE_CHECKING: + assert self.I2C_HATS_MANAGER + def online_callback(): """Call fired when board is online.""" self.schedule_update_ha_state() - self.I2C_HATS_MANAGER.register_online_callback( - self._address, self._channel, online_callback + await self.hass.async_add_executor_job( + self.I2C_HATS_MANAGER.register_online_callback, + self._address, + self._channel, + online_callback, ) def edge_callback(state): @@ -121,8 +130,11 @@ class I2CHatBinarySensor(BinarySensorEntity): self._state = state self.schedule_update_ha_state() - self.I2C_HATS_MANAGER.register_di_callback( - self._address, self._channel, edge_callback + await self.hass.async_add_executor_job( + self.I2C_HATS_MANAGER.register_di_callback, + self._address, + self._channel, + edge_callback, ) @property diff --git a/homeassistant/components/raspihats/switch.py b/homeassistant/components/raspihats/switch.py index c67a2b4ab8f..89ad641544e 100644 --- a/homeassistant/components/raspihats/switch.py +++ b/homeassistant/components/raspihats/switch.py @@ -2,6 +2,7 @@ from __future__ import annotations import logging +from typing import TYPE_CHECKING import voluptuous as vol @@ -101,6 +102,7 @@ class I2CHatSwitch(ToggleEntity): self._channel = channel self._name = name or DEVICE_DEFAULT_NAME self._invert_logic = invert_logic + self._state = initial_state if initial_state is not None: if self._invert_logic: state = not initial_state @@ -108,14 +110,27 @@ class I2CHatSwitch(ToggleEntity): state = initial_state self.I2C_HATS_MANAGER.write_dq(self._address, self._channel, state) - def online_callback(): - """Call fired when board is online.""" - self.schedule_update_ha_state() + async def async_added_to_hass(self) -> None: + """Register callbacks.""" + if TYPE_CHECKING: + assert self.I2C_HATS_MANAGER - self.I2C_HATS_MANAGER.register_online_callback( - self._address, self._channel, online_callback + await self.hass.async_add_executor_job( + self.I2C_HATS_MANAGER.register_online_callback, + self._address, + self._channel, + self.online_callback, ) + def online_callback(self): + """Call fired when board is online.""" + try: + self._state = self.I2C_HATS_MANAGER.read_dq(self._address, self._channel) + except I2CHatsException as ex: + _LOGGER.error(self._log_message(f"Is ON check failed, {ex!s}")) + self._state = False + self.schedule_update_ha_state() + def _log_message(self, message): """Create log message.""" string = f"{self._name} " @@ -136,12 +151,7 @@ class I2CHatSwitch(ToggleEntity): @property def is_on(self): """Return true if device is on.""" - try: - state = self.I2C_HATS_MANAGER.read_dq(self._address, self._channel) - return state != self._invert_logic - except I2CHatsException as ex: - _LOGGER.error(self._log_message(f"Is ON check failed, {ex!s}")) - return False + return self._state != self._invert_logic def turn_on(self, **kwargs): """Turn the device on."""