mirror of
https://github.com/home-assistant/core.git
synced 2025-08-02 18:18:21 +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
|
from .triggers import event, value_updated
|
||||||
|
|
||||||
TRIGGERS = {
|
TRIGGERS = {
|
||||||
event.PLATFORM_TYPE: Trigger(
|
event.PLATFORM_TYPE: event.EventTrigger,
|
||||||
event.async_validate_trigger_config,
|
value_updated.PLATFORM_TYPE: value_updated.ValueUpdatedTrigger,
|
||||||
event.async_attach_trigger,
|
|
||||||
),
|
|
||||||
value_updated.PLATFORM_TYPE: Trigger(
|
|
||||||
value_updated.async_validate_trigger_config,
|
|
||||||
value_updated.async_attach_trigger,
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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 the triggers for Z-Wave JS."""
|
||||||
return TRIGGERS
|
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.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
||||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
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 homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from ..const import (
|
from ..const import (
|
||||||
@ -251,3 +251,25 @@ async def async_attach_trigger(
|
|||||||
_create_zwave_listeners()
|
_create_zwave_listeners()
|
||||||
|
|
||||||
return async_remove
|
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.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
||||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
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 homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from ..config_validation import VALUE_SCHEMA
|
from ..config_validation import VALUE_SCHEMA
|
||||||
@ -202,3 +202,25 @@ async def async_attach_trigger(
|
|||||||
_create_zwave_listeners()
|
_create_zwave_listeners()
|
||||||
|
|
||||||
return async_remove
|
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
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import abc
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
@ -49,18 +50,26 @@ DATA_PLUGGABLE_ACTIONS: HassKey[defaultdict[tuple, PluggableActionsEntry]] = Has
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class Trigger(abc.ABC):
|
||||||
class Trigger:
|
"""Trigger class."""
|
||||||
"""Trigger descriptor data class."""
|
|
||||||
|
|
||||||
async_validate_trigger_config: Callable[
|
@classmethod
|
||||||
[HomeAssistant, ConfigType],
|
@abc.abstractmethod
|
||||||
Coroutine[Any, Any, ConfigType],
|
async def async_validate_trigger_config(
|
||||||
]
|
cls, hass: HomeAssistant, config: ConfigType
|
||||||
async_attach_trigger: Callable[
|
) -> ConfigType:
|
||||||
[HomeAssistant, ConfigType, TriggerActionType, TriggerInfo],
|
"""Validate config."""
|
||||||
Coroutine[Any, Any, CALLBACK_TYPE],
|
|
||||||
]
|
@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):
|
class TriggerProtocol(Protocol):
|
||||||
@ -69,7 +78,7 @@ class TriggerProtocol(Protocol):
|
|||||||
New implementations should only implement async_get_triggers.
|
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."""
|
"""Return the triggers provided by this integration."""
|
||||||
|
|
||||||
TRIGGER_SCHEMA: vol.Schema
|
TRIGGER_SCHEMA: vol.Schema
|
||||||
|
Loading…
x
Reference in New Issue
Block a user