This commit is contained in:
Erik 2025-05-14 12:49:46 +02:00
parent c2a011b16f
commit 17b3d16a26
3 changed files with 27 additions and 21 deletions

View File

@ -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
)

View File

@ -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
)

View File

@ -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]] = []