diff --git a/homeassistant/components/zwave_js/triggers/event.py b/homeassistant/components/zwave_js/triggers/event.py index 5cecf7096f2..b8b8662c0b5 100644 --- a/homeassistant/components/zwave_js/triggers/event.py +++ b/homeassistant/components/zwave_js/triggers/event.py @@ -256,6 +256,11 @@ async def async_attach_trigger( class EventTrigger(Trigger): """Z-Wave JS event trigger.""" + def __init__(self, hass: HomeAssistant, config: ConfigType) -> None: + """Initialize trigger.""" + self._config = config + self._hass = hass + @classmethod async def async_validate_trigger_config( cls, hass: HomeAssistant, config: ConfigType @@ -263,13 +268,12 @@ class EventTrigger(Trigger): """Validate config.""" return await async_validate_trigger_config(hass, config) - @classmethod async def async_attach_trigger( - cls, - hass: HomeAssistant, - config: ConfigType, + self, action: TriggerActionType, trigger_info: TriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" - return await async_attach_trigger(hass, config, action, trigger_info) + return await async_attach_trigger( + self._hass, self._config, action, trigger_info + ) diff --git a/homeassistant/components/zwave_js/triggers/value_updated.py b/homeassistant/components/zwave_js/triggers/value_updated.py index ba856344a03..a50053fa2db 100644 --- a/homeassistant/components/zwave_js/triggers/value_updated.py +++ b/homeassistant/components/zwave_js/triggers/value_updated.py @@ -207,6 +207,11 @@ async def async_attach_trigger( class ValueUpdatedTrigger(Trigger): """Z-Wave JS value updated trigger.""" + def __init__(self, hass: HomeAssistant, config: ConfigType) -> None: + """Initialize trigger.""" + self._config = config + self._hass = hass + @classmethod async def async_validate_trigger_config( cls, hass: HomeAssistant, config: ConfigType @@ -214,13 +219,12 @@ class ValueUpdatedTrigger(Trigger): """Validate config.""" return await async_validate_trigger_config(hass, config) - @classmethod async def async_attach_trigger( - cls, - hass: HomeAssistant, - config: ConfigType, + self, action: TriggerActionType, trigger_info: TriggerInfo, ) -> CALLBACK_TYPE: """Attach a trigger.""" - return await async_attach_trigger(hass, config, action, trigger_info) + return await async_attach_trigger( + self._hass, self._config, action, trigger_info + ) diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 73e3c4a0852..62aebdf6fd7 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -53,6 +53,9 @@ DATA_PLUGGABLE_ACTIONS: HassKey[defaultdict[tuple, PluggableActionsEntry]] = Has class Trigger(abc.ABC): """Trigger class.""" + def __init__(self, hass: HomeAssistant, config: ConfigType) -> None: + """Initialize trigger.""" + @classmethod @abc.abstractmethod async def async_validate_trigger_config( @@ -60,12 +63,9 @@ class Trigger(abc.ABC): ) -> ConfigType: """Validate config.""" - @classmethod @abc.abstractmethod async def async_attach_trigger( - cls, - hass: HomeAssistant, - config: ConfigType, + self, action: TriggerActionType, trigger_info: TriggerInfo, ) -> CALLBACK_TYPE: @@ -370,17 +370,15 @@ async def async_initialize_triggers( trigger_data=trigger_data, ) + action_wrapper = _trigger_action_wrapper(hass, action, conf) if hasattr(platform, "async_get_triggers"): trigger_descriptors = await platform.async_get_triggers(hass) - attach_fn = trigger_descriptors[conf[CONF_PLATFORM]].async_attach_trigger + trigger = trigger_descriptors[conf[CONF_PLATFORM]](hass, conf) + coro = trigger.async_attach_trigger(action_wrapper, info) else: - attach_fn = platform.async_attach_trigger + coro = platform.async_attach_trigger(hass, conf, action_wrapper, info) - triggers.append( - create_eager_task( - attach_fn(hass, conf, _trigger_action_wrapper(hass, action, conf), info) - ) - ) + triggers.append(create_eager_task(coro)) attach_results = await asyncio.gather(*triggers, return_exceptions=True) removes: list[Callable[[], None]] = []