mirror of
https://github.com/home-assistant/core.git
synced 2025-08-01 19:55:10 +02:00
Use dispatcher for KNX GroupMonitor instead of custom HassJob (#122384)
This commit is contained in:
@@ -3,7 +3,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from collections.abc import Callable
|
|
||||||
from typing import Final, TypedDict
|
from typing import Final, TypedDict
|
||||||
|
|
||||||
from xknx import XKNX
|
from xknx import XKNX
|
||||||
@@ -12,7 +11,7 @@ from xknx.exceptions import XKNXException
|
|||||||
from xknx.telegram import Telegram
|
from xknx.telegram import Telegram
|
||||||
from xknx.telegram.apci import GroupValueResponse, GroupValueWrite
|
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.dispatcher import async_dispatcher_send
|
||||||
from homeassistant.helpers.storage import Store
|
from homeassistant.helpers.storage import Store
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
@@ -68,7 +67,6 @@ class Telegrams:
|
|||||||
self._history_store = Store[list[TelegramDict]](
|
self._history_store = Store[list[TelegramDict]](
|
||||||
hass, STORAGE_VERSION, STORAGE_KEY
|
hass, STORAGE_VERSION, STORAGE_KEY
|
||||||
)
|
)
|
||||||
self._jobs: list[HassJob[[TelegramDict], None]] = []
|
|
||||||
self._xknx_telegram_cb_handle = (
|
self._xknx_telegram_cb_handle = (
|
||||||
xknx.telegram_queue.register_telegram_received_cb(
|
xknx.telegram_queue.register_telegram_received_cb(
|
||||||
telegram_received_cb=self._xknx_telegram_cb,
|
telegram_received_cb=self._xknx_telegram_cb,
|
||||||
@@ -100,24 +98,6 @@ class Telegrams:
|
|||||||
telegram_dict = self.telegram_to_dict(telegram)
|
telegram_dict = self.telegram_to_dict(telegram)
|
||||||
self.recent_telegrams.append(telegram_dict)
|
self.recent_telegrams.append(telegram_dict)
|
||||||
async_dispatcher_send(self.hass, SIGNAL_KNX_TELEGRAM, telegram, 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:
|
def telegram_to_dict(self, telegram: Telegram) -> TelegramDict:
|
||||||
"""Convert a Telegram to a dict."""
|
"""Convert a Telegram to a dict."""
|
||||||
|
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Final
|
|||||||
|
|
||||||
import knx_frontend as knx_panel
|
import knx_frontend as knx_panel
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
from xknx.telegram import Telegram
|
||||||
from xknxproject.exceptions import XknxProjectException
|
from xknxproject.exceptions import XknxProjectException
|
||||||
|
|
||||||
from homeassistant.components import panel_custom, websocket_api
|
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.const import CONF_ENTITY_ID, CONF_PLATFORM
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.typing import UNDEFINED
|
from homeassistant.helpers.typing import UNDEFINED
|
||||||
from homeassistant.util.ulid import ulid_now
|
from homeassistant.util.ulid import ulid_now
|
||||||
|
|
||||||
@@ -28,7 +30,7 @@ from .storage.entity_store_validation import (
|
|||||||
EntityStoreValidationSuccess,
|
EntityStoreValidationSuccess,
|
||||||
validate_entity_data,
|
validate_entity_data,
|
||||||
)
|
)
|
||||||
from .telegrams import TelegramDict
|
from .telegrams import SIGNAL_KNX_TELEGRAM, TelegramDict
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from . import KNXModule
|
from . import KNXModule
|
||||||
@@ -220,19 +222,19 @@ def ws_subscribe_telegram(
|
|||||||
msg: dict,
|
msg: dict,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Subscribe to incoming and outgoing KNX telegrams."""
|
"""Subscribe to incoming and outgoing KNX telegrams."""
|
||||||
knx: KNXModule = hass.data[DOMAIN]
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def forward_telegram(telegram: TelegramDict) -> None:
|
def forward_telegram(_telegram: Telegram, telegram_dict: TelegramDict) -> None:
|
||||||
"""Forward telegram to websocket subscription."""
|
"""Forward telegram to websocket subscription."""
|
||||||
connection.send_event(
|
connection.send_event(
|
||||||
msg["id"],
|
msg["id"],
|
||||||
telegram,
|
telegram_dict,
|
||||||
)
|
)
|
||||||
|
|
||||||
connection.subscriptions[msg["id"]] = knx.telegrams.async_listen_telegram(
|
connection.subscriptions[msg["id"]] = async_dispatcher_connect(
|
||||||
action=forward_telegram,
|
hass,
|
||||||
name="KNX GroupMonitor subscription",
|
signal=SIGNAL_KNX_TELEGRAM,
|
||||||
|
target=forward_telegram,
|
||||||
)
|
)
|
||||||
connection.send_result(msg["id"])
|
connection.send_result(msg["id"])
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user