2019-06-10 22:36:11 +00:00
|
|
|
"""Offer device oriented automation."""
|
2022-05-19 16:23:40 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-05-23 10:18:17 +00:00
|
|
|
from typing import Any, Protocol, cast
|
2022-02-17 21:08:43 +00:00
|
|
|
|
2019-06-10 22:36:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-10-02 20:14:52 +00:00
|
|
|
from homeassistant.const import CONF_DOMAIN
|
2022-02-17 21:08:43 +00:00
|
|
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
|
2022-08-15 15:39:14 +00:00
|
|
|
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
2022-02-17 21:08:43 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-06-10 22:36:11 +00:00
|
|
|
|
2021-12-20 18:16:30 +00:00
|
|
|
from . import (
|
|
|
|
DEVICE_TRIGGER_BASE_SCHEMA,
|
|
|
|
DeviceAutomationType,
|
|
|
|
async_get_device_automation_platform,
|
|
|
|
)
|
2021-09-03 17:15:57 +00:00
|
|
|
from .exceptions import InvalidDeviceAutomationConfig
|
|
|
|
|
2021-06-10 17:11:38 +00:00
|
|
|
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
|
2019-09-27 15:48:48 +00:00
|
|
|
|
|
|
|
|
2022-02-22 21:15:16 +00:00
|
|
|
class DeviceAutomationTriggerProtocol(Protocol):
|
|
|
|
"""Define the format of device_trigger modules.
|
|
|
|
|
|
|
|
Each module must define either TRIGGER_SCHEMA or async_validate_trigger_config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
TRIGGER_SCHEMA: vol.Schema
|
|
|
|
|
|
|
|
async def async_validate_trigger_config(
|
|
|
|
self, hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> ConfigType:
|
|
|
|
"""Validate config."""
|
|
|
|
|
|
|
|
async def async_attach_trigger(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
2022-08-15 15:39:14 +00:00
|
|
|
action: TriggerActionType,
|
|
|
|
trigger_info: TriggerInfo,
|
2022-02-22 21:15:16 +00:00
|
|
|
) -> CALLBACK_TYPE:
|
|
|
|
"""Attach a trigger."""
|
2022-05-19 16:23:40 +00:00
|
|
|
|
2022-06-03 06:00:47 +00:00
|
|
|
async def async_get_trigger_capabilities(
|
2022-05-19 16:23:40 +00:00
|
|
|
self, hass: HomeAssistant, config: ConfigType
|
2022-06-03 06:00:47 +00:00
|
|
|
) -> dict[str, vol.Schema]:
|
2022-05-19 16:23:40 +00:00
|
|
|
"""List trigger capabilities."""
|
|
|
|
|
2022-06-03 06:00:47 +00:00
|
|
|
async def async_get_triggers(
|
2022-05-19 16:23:40 +00:00
|
|
|
self, hass: HomeAssistant, device_id: str
|
2022-06-03 06:00:47 +00:00
|
|
|
) -> list[dict[str, Any]]:
|
2022-05-19 16:23:40 +00:00
|
|
|
"""List triggers."""
|
2022-02-22 21:15:16 +00:00
|
|
|
|
|
|
|
|
2022-02-17 21:08:43 +00:00
|
|
|
async def async_validate_trigger_config(
|
|
|
|
hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> ConfigType:
|
2019-09-27 15:48:48 +00:00
|
|
|
"""Validate config."""
|
2021-09-03 17:15:57 +00:00
|
|
|
try:
|
2022-02-22 21:15:16 +00:00
|
|
|
platform = await async_get_device_automation_platform(
|
|
|
|
hass, config[CONF_DOMAIN], DeviceAutomationType.TRIGGER
|
|
|
|
)
|
|
|
|
if not hasattr(platform, "async_validate_trigger_config"):
|
|
|
|
return cast(ConfigType, platform.TRIGGER_SCHEMA(config))
|
2022-02-17 21:08:43 +00:00
|
|
|
return await platform.async_validate_trigger_config(hass, config)
|
2022-10-07 12:23:53 +00:00
|
|
|
except InvalidDeviceAutomationConfig as err:
|
|
|
|
raise vol.Invalid(str(err) or "Invalid trigger configuration") from err
|
2019-06-10 22:36:11 +00:00
|
|
|
|
|
|
|
|
2022-02-17 21:08:43 +00:00
|
|
|
async def async_attach_trigger(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
2022-08-15 15:39:14 +00:00
|
|
|
action: TriggerActionType,
|
|
|
|
trigger_info: TriggerInfo,
|
2022-02-17 21:08:43 +00:00
|
|
|
) -> CALLBACK_TYPE:
|
2019-06-10 22:36:11 +00:00
|
|
|
"""Listen for trigger."""
|
2019-10-02 20:14:52 +00:00
|
|
|
platform = await async_get_device_automation_platform(
|
2021-12-20 18:16:30 +00:00
|
|
|
hass, config[CONF_DOMAIN], DeviceAutomationType.TRIGGER
|
2019-10-02 20:14:52 +00:00
|
|
|
)
|
2022-08-15 15:39:14 +00:00
|
|
|
return await platform.async_attach_trigger(hass, config, action, trigger_info)
|