Home Assistant Unofficial Reference 2024.12.1
device_trigger.py
Go to the documentation of this file.
1 """Provides device automations for Kodi."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
8 from homeassistant.const import (
9  ATTR_ENTITY_ID,
10  CONF_DEVICE_ID,
11  CONF_DOMAIN,
12  CONF_ENTITY_ID,
13  CONF_PLATFORM,
14  CONF_TYPE,
15 )
16 from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback
17 from homeassistant.helpers import config_validation as cv, entity_registry as er
18 from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
19 from homeassistant.helpers.typing import ConfigType
20 
21 from .const import DOMAIN, EVENT_TURN_OFF, EVENT_TURN_ON
22 
23 TRIGGER_TYPES = {"turn_on", "turn_off"}
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(TRIGGER_TYPES),
29  }
30 )
31 
32 
34  hass: HomeAssistant, device_id: str
35 ) -> list[dict[str, str]]:
36  """List device triggers for Kodi devices."""
37  registry = er.async_get(hass)
38  triggers = []
39 
40  # Get all the integrations entities for this device
41  for entry in er.async_entries_for_device(registry, device_id):
42  if entry.domain == "media_player":
43  triggers.append(
44  {
45  CONF_PLATFORM: "device",
46  CONF_DEVICE_ID: device_id,
47  CONF_DOMAIN: DOMAIN,
48  CONF_ENTITY_ID: entry.id,
49  CONF_TYPE: "turn_on",
50  }
51  )
52  triggers.append(
53  {
54  CONF_PLATFORM: "device",
55  CONF_DEVICE_ID: device_id,
56  CONF_DOMAIN: DOMAIN,
57  CONF_ENTITY_ID: entry.id,
58  CONF_TYPE: "turn_off",
59  }
60  )
61 
62  return triggers
63 
64 
65 @callback
67  hass: HomeAssistant,
68  config: ConfigType,
69  action: TriggerActionType,
70  event_type,
71  trigger_info: TriggerInfo,
72 ):
73  registry = er.async_get(hass)
74  entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID])
75  trigger_data = trigger_info["trigger_data"]
76  job = HassJob(action)
77 
78  @callback
79  def _handle_event(event: Event):
80  if event.data[ATTR_ENTITY_ID] == entity_id:
81  hass.async_run_hass_job(
82  job,
83  {
84  "trigger": {
85  **trigger_data,
86  **config,
87  "description": event_type,
88  "entity_id": entity_id,
89  }
90  },
91  event.context,
92  )
93 
94  return hass.bus.async_listen(event_type, _handle_event)
95 
96 
98  hass: HomeAssistant,
99  config: ConfigType,
100  action: TriggerActionType,
101  trigger_info: TriggerInfo,
102 ) -> CALLBACK_TYPE:
103  """Attach a trigger."""
104  if config[CONF_TYPE] == "turn_on":
105  return _attach_trigger(hass, config, action, EVENT_TURN_ON, trigger_info)
106 
107  if config[CONF_TYPE] == "turn_off":
108  return _attach_trigger(hass, config, action, EVENT_TURN_OFF, trigger_info)
109 
110  return lambda: None
list[dict[str, str]] async_get_triggers(HomeAssistant hass, str device_id)
def _attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, event_type, TriggerInfo trigger_info)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)