Use dispatcher for KNX GroupMonitor instead of custom HassJob (#122384)
parent
42b9c0448c
commit
d7b0d1a50e
|
@ -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."""
|
||||
|
|
|
@ -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"])
|
||||
|
||||
|
|
Loading…
Reference in New Issue