Home Assistant Unofficial Reference 2024.12.1
trigger.py
Go to the documentation of this file.
1 """Offer device oriented automation."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, Protocol
6 
7 import voluptuous as vol
8 
9 from homeassistant.const import CONF_DOMAIN
10 from homeassistant.core import CALLBACK_TYPE, HomeAssistant
12  TriggerActionType,
13  TriggerInfo,
14  TriggerProtocol,
15 )
16 from homeassistant.helpers.typing import ConfigType
17 
18 from . import (
19  DEVICE_TRIGGER_BASE_SCHEMA,
20  DeviceAutomationType,
21  async_get_device_automation_platform,
22 )
23 from .helpers import async_validate_device_automation_config
24 
25 TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA)
26 
27 
29  """Define the format of device_trigger modules.
30 
31  Each module must define either TRIGGER_SCHEMA or async_validate_trigger_config
32  from TriggerProtocol.
33  """
34 
36  self, hass: HomeAssistant, config: ConfigType
37  ) -> dict[str, vol.Schema]:
38  """List trigger capabilities."""
39 
40  async def async_get_triggers(
41  self, hass: HomeAssistant, device_id: str
42  ) -> list[dict[str, Any]]:
43  """List triggers."""
44 
45 
47  hass: HomeAssistant, config: ConfigType
48 ) -> ConfigType:
49  """Validate config."""
51  hass, config, TRIGGER_SCHEMA, DeviceAutomationType.TRIGGER
52  )
53 
54 
56  hass: HomeAssistant,
57  config: ConfigType,
58  action: TriggerActionType,
59  trigger_info: TriggerInfo,
60 ) -> CALLBACK_TYPE:
61  """Listen for trigger."""
62  platform = await async_get_device_automation_platform(
63  hass, config[CONF_DOMAIN], DeviceAutomationType.TRIGGER
64  )
65  return await platform.async_attach_trigger(hass, config, action, trigger_info)
dict[str, vol.Schema] async_get_trigger_capabilities(self, HomeAssistant hass, ConfigType config)
Definition: trigger.py:37
list[dict[str, str]] async_get_triggers(HomeAssistant hass, str device_id)
ConfigType async_validate_device_automation_config(HomeAssistant hass, ConfigType config, vol.Schema automation_schema, DeviceAutomationType automation_type)
Definition: helpers.py:53
ConfigType async_validate_trigger_config(HomeAssistant hass, ConfigType config)
Definition: trigger.py:48
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
Definition: trigger.py:60
DeviceAutomationTriggerProtocol async_get_device_automation_platform(HomeAssistant hass, str domain, Literal[DeviceAutomationType.TRIGGER] automation_type)
Definition: __init__.py:137