diff --git a/homeassistant/components/knx/const.py b/homeassistant/components/knx/const.py index 3d1e3c62a34..aa48bcdf557 100644 --- a/homeassistant/components/knx/const.py +++ b/homeassistant/components/knx/const.py @@ -82,6 +82,9 @@ DATA_HASS_CONFIG: Final = "knx_hass_config" ATTR_COUNTER: Final = "counter" ATTR_SOURCE: Final = "source" +# dispatcher signal for KNX interface device triggers +SIGNAL_KNX_TELEGRAM_DICT: Final = "knx_telegram_dict" + AsyncMessageCallbackType = Callable[[Telegram], Awaitable[None]] MessageCallbackType = Callable[[Telegram], None] diff --git a/homeassistant/components/knx/device_trigger.py b/homeassistant/components/knx/device_trigger.py index 1abafb221db..867a7c075b0 100644 --- a/homeassistant/components/knx/device_trigger.py +++ b/homeassistant/components/knx/device_trigger.py @@ -9,11 +9,12 @@ from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEM from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback from homeassistant.helpers import selector +from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo from homeassistant.helpers.typing import ConfigType from . import KNXModule -from .const import DOMAIN +from .const import DOMAIN, SIGNAL_KNX_TELEGRAM_DICT from .project import KNXProject from .schema import ga_list_validator from .telegrams import TelegramDict @@ -87,7 +88,6 @@ async def async_attach_trigger( trigger_data = trigger_info["trigger_data"] dst_addresses: list[str] = config.get(EXTRA_FIELD_DESTINATION, []) job = HassJob(action, f"KNX device trigger {trigger_info}") - knx: KNXModule = hass.data[DOMAIN] @callback def async_call_trigger_action(telegram: TelegramDict) -> None: @@ -99,6 +99,8 @@ async def async_attach_trigger( {"trigger": {**trigger_data, **telegram}}, ) - return knx.telegrams.async_listen_telegram( - async_call_trigger_action, name="KNX device trigger call" + return async_dispatcher_connect( + hass, + signal=SIGNAL_KNX_TELEGRAM_DICT, + target=async_call_trigger_action, ) diff --git a/homeassistant/components/knx/telegrams.py b/homeassistant/components/knx/telegrams.py index 87c1a8b6052..95250d99f85 100644 --- a/homeassistant/components/knx/telegrams.py +++ b/homeassistant/components/knx/telegrams.py @@ -11,10 +11,11 @@ from xknx.telegram import Telegram from xknx.telegram.apci import GroupValueResponse, GroupValueWrite from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback +from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.storage import Store import homeassistant.util.dt as dt_util -from .const import DOMAIN +from .const import DOMAIN, SIGNAL_KNX_TELEGRAM_DICT from .project import KNXProject STORAGE_VERSION: Final = 1 @@ -87,6 +88,7 @@ class Telegrams: """Handle incoming and outgoing telegrams from xknx.""" telegram_dict = self.telegram_to_dict(telegram) self.recent_telegrams.append(telegram_dict) + async_dispatcher_send(self.hass, SIGNAL_KNX_TELEGRAM_DICT, telegram_dict) for job in self._jobs: self.hass.async_run_hass_job(job, telegram_dict) diff --git a/tests/components/knx/test_device_trigger.py b/tests/components/knx/test_device_trigger.py index e901fd7f29e..f3448947cf8 100644 --- a/tests/components/knx/test_device_trigger.py +++ b/tests/components/knx/test_device_trigger.py @@ -150,7 +150,6 @@ async def test_remove_device_trigger( }, ) - assert len(hass.data[DOMAIN].telegrams._jobs) == 1 await knx.receive_write("0/0/1", (0x03, 0x2F)) assert len(calls) == 1 assert calls.pop().data["catch_all"] == "telegram - 0/0/1" @@ -161,8 +160,6 @@ async def test_remove_device_trigger( {ATTR_ENTITY_ID: f"automation.{automation_name}"}, blocking=True, ) - - assert len(hass.data[DOMAIN].telegrams._jobs) == 0 await knx.receive_write("0/0/1", (0x03, 0x2F)) assert len(calls) == 0