diff --git a/CHANGELOG.md b/CHANGELOG.md index 01620d0..e63faff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Add support for `inktank` cartridge type [#162](https://github.com/elad-bar/ha-hpprinter/issues/162) - Add sensor of total printed pages per consumable, available for toner only [#72](https://github.com/elad-bar/ha-hpprinter/issues/72) - Fix HTTPS requests [#160](https://github.com/elad-bar/ha-hpprinter/issues/160) +- Remove startup blocking call ## 2.0.2 diff --git a/custom_components/hpprinter/managers/ha_coordinator.py b/custom_components/hpprinter/managers/ha_coordinator.py index baf0589..9b8fec1 100644 --- a/custom_components/hpprinter/managers/ha_coordinator.py +++ b/custom_components/hpprinter/managers/ha_coordinator.py @@ -1,7 +1,7 @@ import logging import sys -from homeassistant.core import Event +from homeassistant.core import Event, callback from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send, @@ -52,11 +52,7 @@ def __init__( "Adapter": self.create_adapter_device, } - self.config_entry.async_on_unload( - async_dispatcher_connect( - hass, SIGNAL_HA_DEVICE_DISCOVERED, self._on_device_discovered - ) - ) + self._load_signal_handlers() @property def api(self) -> RestAPIv2: @@ -93,6 +89,25 @@ async def initialize(self): await self.async_config_entry_first_refresh() + def _load_signal_handlers(self): + loop = self.hass.loop + + @callback + def on_device_discovered( + entry_id: str, device_key: str, device_data: dict, device_config: dict + ): + loop.create_task( + self._on_device_discovered( + entry_id, device_key, device_data, device_config + ) + ).__await__() + + self.config_entry.async_on_unload( + async_dispatcher_connect( + self.hass, SIGNAL_HA_DEVICE_DISCOVERED, self._on_device_discovered + ) + ) + def create_main_device( self, device_key: str, device_data: dict, device_config: dict ):