2019-06-10 22:36:11 +00:00
|
|
|
"""Offer device oriented automation."""
|
2022-05-19 16:23:40 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Awaitable
|
2022-02-22 21:15:16 +00:00
|
|
|
from typing import Protocol, cast
|
2022-02-17 21:08:43 +00:00
|
|
|
|
2019-06-10 22:36:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2022-02-17 21:08:43 +00:00
|
|
|
from homeassistant.components.automation import (
|
|
|
|
AutomationActionType,
|
|
|
|
AutomationTriggerInfo,
|
|
|
|
)
|
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
|
|
|
|
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,
|
2022-05-19 16:23:40 +00:00
|
|
|
GetAutomationCapabilitiesResult,
|
|
|
|
GetAutomationsResult,
|
2021-12-20 18:16:30 +00:00
|
|
|
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,
|
|
|
|
action: AutomationActionType,
|
|
|
|
automation_info: AutomationTriggerInfo,
|
|
|
|
) -> CALLBACK_TYPE:
|
|
|
|
"""Attach a trigger."""
|
2022-05-19 16:23:40 +00:00
|
|
|
|
|
|
|
def async_get_trigger_capabilities(
|
|
|
|
self, hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> GetAutomationCapabilitiesResult | Awaitable[GetAutomationCapabilitiesResult]:
|
|
|
|
"""List trigger capabilities."""
|
|
|
|
|
|
|
|
def async_get_triggers(
|
|
|
|
self, hass: HomeAssistant, device_id: str
|
|
|
|
) -> GetAutomationsResult | Awaitable[GetAutomationsResult]:
|
|
|
|
"""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)
|
2021-09-03 17:15:57 +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,
|
|
|
|
action: AutomationActionType,
|
|
|
|
automation_info: AutomationTriggerInfo,
|
|
|
|
) -> 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
|
|
|
)
|
2019-09-24 21:57:05 +00:00
|
|
|
return await platform.async_attach_trigger(hass, config, action, automation_info)
|