Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocking call on startup #69

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.0.10

- Fix blocking call on startup

## v3.0.9

- Fix async dispatcher send
Expand Down
83 changes: 63 additions & 20 deletions custom_components/shinobi/managers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.const import ATTR_ICON, ATTR_STATE
from homeassistant.core import Event
from homeassistant.core import Event, callback
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
Expand Down Expand Up @@ -92,24 +92,6 @@ def __init__(self, hass, config_manager: ConfigManager):
update_method=self._async_update_data,
)

entry = config_manager.entry

signal_handlers = {
SIGNAL_API_STATUS: self._on_api_status_changed,
SIGNAL_WS_STATUS: self._on_ws_status_changed,
SIGNAL_MONITOR_DISCOVERED: self._on_monitor_discovered,
SIGNAL_MONITOR_UPDATED: self._on_monitor_updated,
SIGNAL_MONITOR_TRIGGER: self._on_monitor_triggered,
SIGNAL_MONITOR_STATUS_CHANGED: self._on_monitor_status_changed,
SIGNAL_SERVER_DISCOVERED: self._on_server_discovered,
SIGNAL_WS_READY: self._on_ws_ready,
}

for signal in signal_handlers:
handler = signal_handlers[signal]

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

self._api = RestAPI(hass, config_manager)
self._websockets = WebSockets(hass, config_manager)

Expand All @@ -121,6 +103,8 @@ def __init__(self, hass, config_manager: ConfigManager):
self._last_heartbeat = 0
self._monitors = {}

self._load_signal_handlers()

@property
def monitors(self) -> dict[str, MonitorData]:
monitors = self._monitors
Expand Down Expand Up @@ -148,6 +132,65 @@ 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_monitor_discovered(entry_id: str, monitor: MonitorData):
loop.create_task(self._on_monitor_discovered(entry_id, monitor)).__await__()

@callback
def on_monitor_updated(entry_id: str, monitor: MonitorData):
loop.create_task(self._on_monitor_updated(entry_id, monitor)).__await__()

@callback
def on_monitor_triggered(
entry_id: str, monitor_id: str, event_type: str, value
):
loop.create_task(
self._on_monitor_triggered(entry_id, monitor_id, event_type, value)
).__await__()

@callback
def on_monitor_status_changed(entry_id: str, monitor_id: str, status_code: int):
loop.create_task(
self._on_monitor_status_changed(entry_id, monitor_id, status_code)
).__await__()

@callback
def on_server_discovered(entry_id: str):
loop.create_task(self._on_server_discovered(entry_id)).__await__()

@callback
def on_ws_ready(entry_id: str):
loop.create_task(self._on_ws_ready(entry_id)).__await__()

signal_handlers = {
SIGNAL_API_STATUS: on_api_status_changed,
SIGNAL_WS_STATUS: on_ws_status_changed,
SIGNAL_MONITOR_DISCOVERED: on_monitor_discovered,
SIGNAL_MONITOR_UPDATED: on_monitor_updated,
SIGNAL_MONITOR_TRIGGER: on_monitor_triggered,
SIGNAL_MONITOR_STATUS_CHANGED: on_monitor_status_changed,
SIGNAL_SERVER_DISCOVERED: on_server_discovered,
SIGNAL_WS_READY: on_ws_ready,
}

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 Expand Up @@ -307,7 +350,7 @@ async def _on_monitor_triggered(
await self.async_request_refresh()

async def _on_monitor_status_changed(
self, entry_id: str, monitor_id, status_code: int
self, entry_id: str, monitor_id: str, status_code: int
):
if entry_id == self.config_manager.entry_id:
_LOGGER.debug(f"Monitor '{monitor_id}' status changed to {status_code}")
Expand Down
2 changes: 1 addition & 1 deletion custom_components/shinobi/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/elad-bar/ha-shinobi/issues",
"requirements": [],
"version": "3.0.9"
"version": "3.0.10"
}
Loading