Skip to content

Commit

Permalink
Merge pull request #155 from elad-bar/fix-startup-block-call
Browse files Browse the repository at this point in the history
Fix blocking call on startup
  • Loading branch information
elad-bar authored Jun 16, 2024
2 parents 102ba01 + 253f638 commit cc9678c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.8

- Fix blocking call on startup

## 2.1.7

- Fix async dispatcher send
Expand Down
49 changes: 33 additions & 16 deletions custom_components/edgeos/managers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.components.device_tracker import ATTR_IP, ATTR_MAC
from homeassistant.components.homeassistant import SERVICE_RELOAD_CONFIG_ENTRY
from homeassistant.const import ATTR_STATE
from homeassistant.core import Event
from homeassistant.core import Event, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import (
Expand Down Expand Up @@ -87,21 +87,6 @@ def __init__(self, hass, config_manager: ConfigManager):

_LOGGER.debug("Initializing")

entry = config_manager.entry

signal_handlers = {
SIGNAL_API_STATUS: self._on_api_status_changed,
SIGNAL_WS_STATUS: self._on_ws_status_changed,
SIGNAL_DATA_CHANGED: self._on_data_changed,
}

_LOGGER.debug(f"Registering signals for {signal_handlers.keys()}")

for signal in signal_handlers:
handler = signal_handlers[signal]

entry.async_on_unload(async_dispatcher_connect(hass, signal, handler))

config_data = config_manager.config_data
entry_id = config_manager.entry_id

Expand Down Expand Up @@ -130,6 +115,8 @@ def __init__(self, hass, config_manager: ConfigManager):
DeviceTypes.INTERFACE: self._interface_processor,
}

self._load_signal_handlers()

_LOGGER.debug("Initializing done")

@property
Expand Down Expand Up @@ -159,6 +146,36 @@ def config_manager(self) -> ConfigManager:
async def on_home_assistant_start(self, _event_data: Event):
await self.initialize()

def _load_signal_handlers(self):
loop = self.hass.loop

@callback
def on_api_status_changed(entry_id: str, status: ConnectivityStatus):
loop.create_task(self._on_api_status_changed(entry_id, status)).__await__()

@callback
def on_ws_status_changed(entry_id: str, status: ConnectivityStatus):
loop.create_task(self._on_ws_status_changed(entry_id, status)).__await__()

@callback
def on_data_changed(entry_id: str):
loop.create_task(self._on_data_changed(entry_id)).__await__()

signal_handlers = {
SIGNAL_API_STATUS: on_api_status_changed,
SIGNAL_WS_STATUS: on_ws_status_changed,
SIGNAL_DATA_CHANGED: on_data_changed,
}

_LOGGER.debug(f"Registering signals for {signal_handlers.keys()}")

for signal in signal_handlers:
handler = signal_handlers[signal]

self._config_manager.entry.async_on_unload(
async_dispatcher_connect(self.hass, signal, handler)
)

async def initialize(self):
self._build_data_mapping()

Expand Down
2 changes: 1 addition & 1 deletion custom_components/edgeos/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/elad-bar/ha-edgeos/issues",
"requirements": ["aiohttp"],
"version": "2.1.7"
"version": "2.1.8"
}

0 comments on commit cc9678c

Please sign in to comment.