Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Device automation helpers for entity."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.components.homeassistant.triggers import state as state_trigger
8 from homeassistant.const import CONF_ENTITY_ID, CONF_FOR, CONF_PLATFORM, CONF_TYPE
9 from homeassistant.core import CALLBACK_TYPE, HomeAssistant
10 from homeassistant.helpers import config_validation as cv, entity_registry as er
11 from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
12 from homeassistant.helpers.typing import ConfigType
13 
14 from . import DEVICE_TRIGGER_BASE_SCHEMA
15 from .const import CONF_CHANGED_STATES
16 
17 ENTITY_TRIGGERS = [
18  {
19  # Trigger when entity is turned on or off
20  CONF_PLATFORM: "device",
21  CONF_TYPE: CONF_CHANGED_STATES,
22  },
23 ]
24 
25 TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
26  {
27  vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
28  vol.Required(CONF_TYPE): vol.In([CONF_CHANGED_STATES]),
29  vol.Optional(CONF_FOR): cv.positive_time_period_dict,
30  }
31 )
32 
33 
35  hass: HomeAssistant,
36  config: ConfigType,
37  action: TriggerActionType,
38  trigger_info: TriggerInfo,
39 ) -> CALLBACK_TYPE:
40  """Listen for state changes based on configuration."""
41  to_state = None
42  state_config = {
43  CONF_PLATFORM: "state",
44  state_trigger.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
45  state_trigger.CONF_TO: to_state,
46  }
47  if CONF_FOR in config:
48  state_config[CONF_FOR] = config[CONF_FOR]
49 
50  state_config = await state_trigger.async_validate_trigger_config(hass, state_config)
51  return await state_trigger.async_attach_trigger(
52  hass, state_config, action, trigger_info, platform_type="device"
53  )
54 
55 
57  hass: HomeAssistant,
58  device_id: str,
59  automation_templates: list[dict[str, str]],
60  domain: str,
61 ) -> list[dict[str, str]]:
62  """List device automations."""
63  automations: list[dict[str, str]] = []
64  entity_registry = er.async_get(hass)
65 
66  entries = [
67  entry
68  for entry in er.async_entries_for_device(entity_registry, device_id)
69  if entry.domain == domain
70  ]
71 
72  for entry in entries:
73  automations.extend(
74  {
75  **template,
76  "device_id": device_id,
77  "entity_id": entry.id,
78  "domain": domain,
79  }
80  for template in automation_templates
81  )
82 
83  return automations
84 
85 
87  hass: HomeAssistant, device_id: str, domain: str
88 ) -> list[dict[str, str]]:
89  """List device triggers."""
90  return await _async_get_automations(hass, device_id, ENTITY_TRIGGERS, domain)
91 
92 
94  hass: HomeAssistant, config: ConfigType
95 ) -> dict[str, vol.Schema]:
96  """List trigger capabilities."""
97  return {
98  "extra_fields": vol.Schema(
99  {vol.Optional(CONF_FOR): cv.positive_time_period_dict}
100  )
101  }
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
Definition: entity.py:39
dict[str, vol.Schema] async_get_trigger_capabilities(HomeAssistant hass, ConfigType config)
Definition: entity.py:95
list[dict[str, str]] async_get_triggers(HomeAssistant hass, str device_id, str domain)
Definition: entity.py:88
list[dict[str, str]] _async_get_automations(HomeAssistant hass, str device_id, list[dict[str, str]] automation_templates, str domain)
Definition: entity.py:61