mirror of
https://github.com/home-assistant/core.git
synced 2025-08-02 10:08:23 +00:00
Refactor the Trigger class
This commit is contained in:
parent
a441e2ddca
commit
56506f259a
@ -8,17 +8,11 @@ from homeassistant.helpers.trigger import Trigger
|
||||
from .triggers import event, value_updated
|
||||
|
||||
TRIGGERS = {
|
||||
event.PLATFORM_TYPE: Trigger(
|
||||
event.async_validate_trigger_config,
|
||||
event.async_attach_trigger,
|
||||
),
|
||||
value_updated.PLATFORM_TYPE: Trigger(
|
||||
value_updated.async_validate_trigger_config,
|
||||
value_updated.async_attach_trigger,
|
||||
),
|
||||
event.PLATFORM_TYPE: event.EventTrigger,
|
||||
value_updated.PLATFORM_TYPE: value_updated.ValueUpdatedTrigger,
|
||||
}
|
||||
|
||||
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, Trigger]:
|
||||
async def async_get_triggers(hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
"""Return the triggers for Z-Wave JS."""
|
||||
return TRIGGERS
|
||||
|
@ -16,7 +16,7 @@ from homeassistant.const import ATTR_DEVICE_ID, ATTR_ENTITY_ID, CONF_PLATFORM
|
||||
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||
from homeassistant.helpers.trigger import Trigger, TriggerActionType, TriggerInfo
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from ..const import (
|
||||
@ -251,3 +251,25 @@ async def async_attach_trigger(
|
||||
_create_zwave_listeners()
|
||||
|
||||
return async_remove
|
||||
|
||||
|
||||
class EventTrigger(Trigger):
|
||||
"""Z-Wave JS event trigger."""
|
||||
|
||||
@classmethod
|
||||
async def async_validate_trigger_config(
|
||||
cls, hass: HomeAssistant, config: ConfigType
|
||||
) -> ConfigType:
|
||||
"""Validate config."""
|
||||
return await async_validate_trigger_config(hass, config)
|
||||
|
||||
@classmethod
|
||||
async def async_attach_trigger(
|
||||
cls,
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
action: TriggerActionType,
|
||||
trigger_info: TriggerInfo,
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Attach a trigger."""
|
||||
return await async_attach_trigger(hass, config, action, trigger_info)
|
||||
|
@ -14,7 +14,7 @@ from homeassistant.const import ATTR_DEVICE_ID, ATTR_ENTITY_ID, CONF_PLATFORM, M
|
||||
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||
from homeassistant.helpers.trigger import Trigger, TriggerActionType, TriggerInfo
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from ..config_validation import VALUE_SCHEMA
|
||||
@ -202,3 +202,25 @@ async def async_attach_trigger(
|
||||
_create_zwave_listeners()
|
||||
|
||||
return async_remove
|
||||
|
||||
|
||||
class ValueUpdatedTrigger(Trigger):
|
||||
"""Z-Wave JS value updated trigger."""
|
||||
|
||||
@classmethod
|
||||
async def async_validate_trigger_config(
|
||||
cls, hass: HomeAssistant, config: ConfigType
|
||||
) -> ConfigType:
|
||||
"""Validate config."""
|
||||
return await async_validate_trigger_config(hass, config)
|
||||
|
||||
@classmethod
|
||||
async def async_attach_trigger(
|
||||
cls,
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
action: TriggerActionType,
|
||||
trigger_info: TriggerInfo,
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Attach a trigger."""
|
||||
return await async_attach_trigger(hass, config, action, trigger_info)
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
import asyncio
|
||||
from collections import defaultdict
|
||||
from collections.abc import Callable, Coroutine
|
||||
@ -49,18 +50,26 @@ DATA_PLUGGABLE_ACTIONS: HassKey[defaultdict[tuple, PluggableActionsEntry]] = Has
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Trigger:
|
||||
"""Trigger descriptor data class."""
|
||||
class Trigger(abc.ABC):
|
||||
"""Trigger class."""
|
||||
|
||||
async_validate_trigger_config: Callable[
|
||||
[HomeAssistant, ConfigType],
|
||||
Coroutine[Any, Any, ConfigType],
|
||||
]
|
||||
async_attach_trigger: Callable[
|
||||
[HomeAssistant, ConfigType, TriggerActionType, TriggerInfo],
|
||||
Coroutine[Any, Any, CALLBACK_TYPE],
|
||||
]
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
async def async_validate_trigger_config(
|
||||
cls, hass: HomeAssistant, config: ConfigType
|
||||
) -> ConfigType:
|
||||
"""Validate config."""
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
async def async_attach_trigger(
|
||||
cls,
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
action: TriggerActionType,
|
||||
trigger_info: TriggerInfo,
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Attach a trigger."""
|
||||
|
||||
|
||||
class TriggerProtocol(Protocol):
|
||||
@ -69,7 +78,7 @@ class TriggerProtocol(Protocol):
|
||||
New implementations should only implement async_get_triggers.
|
||||
"""
|
||||
|
||||
async def async_get_triggers(self, hass: HomeAssistant) -> dict[str, Trigger]:
|
||||
async def async_get_triggers(self, hass: HomeAssistant) -> dict[str, type[Trigger]]:
|
||||
"""Return the triggers provided by this integration."""
|
||||
|
||||
TRIGGER_SCHEMA: vol.Schema
|
||||
|
Loading…
x
Reference in New Issue
Block a user