From d7b0d1a50ece8deb0b7610e2b54dc4f027eff7ec Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Tue, 23 Jul 2024 17:47:45 +0200 Subject: [PATCH] Use dispatcher for KNX GroupMonitor instead of custom HassJob (#122384) --- homeassistant/components/knx/telegrams.py | 22 +--------------------- homeassistant/components/knx/websocket.py | 16 +++++++++------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/knx/telegrams.py b/homeassistant/components/knx/telegrams.py index 6945bb50746..82df78e748e 100644 --- a/homeassistant/components/knx/telegrams.py +++ b/homeassistant/components/knx/telegrams.py @@ -3,7 +3,6 @@ from __future__ import annotations from collections import deque -from collections.abc import Callable from typing import Final, TypedDict from xknx import XKNX @@ -12,7 +11,7 @@ from xknx.exceptions import XKNXException from xknx.telegram import Telegram from xknx.telegram.apci import GroupValueResponse, GroupValueWrite -from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback +from homeassistant.core import HomeAssistant from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.storage import Store import homeassistant.util.dt as dt_util @@ -68,7 +67,6 @@ class Telegrams: self._history_store = Store[list[TelegramDict]]( hass, STORAGE_VERSION, STORAGE_KEY ) - self._jobs: list[HassJob[[TelegramDict], None]] = [] self._xknx_telegram_cb_handle = ( xknx.telegram_queue.register_telegram_received_cb( telegram_received_cb=self._xknx_telegram_cb, @@ -100,24 +98,6 @@ class Telegrams: telegram_dict = self.telegram_to_dict(telegram) self.recent_telegrams.append(telegram_dict) async_dispatcher_send(self.hass, SIGNAL_KNX_TELEGRAM, telegram, telegram_dict) - for job in self._jobs: - self.hass.async_run_hass_job(job, telegram_dict) - - @callback - def async_listen_telegram( - self, - action: Callable[[TelegramDict], None], - name: str = "KNX telegram listener", - ) -> CALLBACK_TYPE: - """Register callback to listen for telegrams.""" - job = HassJob(action, name=name) - self._jobs.append(job) - - def remove_listener() -> None: - """Remove the listener.""" - self._jobs.remove(job) - - return remove_listener def telegram_to_dict(self, telegram: Telegram) -> TelegramDict: """Convert a Telegram to a dict.""" diff --git a/homeassistant/components/knx/websocket.py b/homeassistant/components/knx/websocket.py index bca1b119ef7..97758dc87c9 100644 --- a/homeassistant/components/knx/websocket.py +++ b/homeassistant/components/knx/websocket.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Final import knx_frontend as knx_panel import voluptuous as vol +from xknx.telegram import Telegram from xknxproject.exceptions import XknxProjectException from homeassistant.components import panel_custom, websocket_api @@ -13,6 +14,7 @@ from homeassistant.components.http import StaticPathConfig from homeassistant.const import CONF_ENTITY_ID, CONF_PLATFORM from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.typing import UNDEFINED from homeassistant.util.ulid import ulid_now @@ -28,7 +30,7 @@ from .storage.entity_store_validation import ( EntityStoreValidationSuccess, validate_entity_data, ) -from .telegrams import TelegramDict +from .telegrams import SIGNAL_KNX_TELEGRAM, TelegramDict if TYPE_CHECKING: from . import KNXModule @@ -220,19 +222,19 @@ def ws_subscribe_telegram( msg: dict, ) -> None: """Subscribe to incoming and outgoing KNX telegrams.""" - knx: KNXModule = hass.data[DOMAIN] @callback - def forward_telegram(telegram: TelegramDict) -> None: + def forward_telegram(_telegram: Telegram, telegram_dict: TelegramDict) -> None: """Forward telegram to websocket subscription.""" connection.send_event( msg["id"], - telegram, + telegram_dict, ) - connection.subscriptions[msg["id"]] = knx.telegrams.async_listen_telegram( - action=forward_telegram, - name="KNX GroupMonitor subscription", + connection.subscriptions[msg["id"]] = async_dispatcher_connect( + hass, + signal=SIGNAL_KNX_TELEGRAM, + target=forward_telegram, ) connection.send_result(msg["id"])