Home Assistant Unofficial Reference 2024.12.1
turn_on.py
Go to the documentation of this file.
1 """LG Netcast TV device turn on trigger."""
2 
3 import voluptuous as vol
4 
5 from homeassistant.const import (
6  ATTR_DEVICE_ID,
7  ATTR_ENTITY_ID,
8  CONF_DEVICE_ID,
9  CONF_DOMAIN,
10  CONF_PLATFORM,
11  CONF_TYPE,
12 )
13 from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
14 from homeassistant.helpers import config_validation as cv, entity_registry as er
16  PluggableAction,
17  TriggerActionType,
18  TriggerInfo,
19 )
20 from homeassistant.helpers.typing import ConfigType
21 
22 from ..const import DOMAIN
23 from ..helpers import async_get_device_entry_by_device_id
24 
25 PLATFORM_TYPE = f"{DOMAIN}.{__name__.rsplit('.', maxsplit=1)[-1]}"
26 
27 TRIGGER_SCHEMA = vol.All(
28  cv.TRIGGER_BASE_SCHEMA.extend(
29  {
30  vol.Required(CONF_PLATFORM): PLATFORM_TYPE,
31  vol.Optional(ATTR_DEVICE_ID): vol.All(cv.ensure_list, [cv.string]),
32  vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
33  },
34  ),
35  cv.has_at_least_one_key(ATTR_ENTITY_ID, ATTR_DEVICE_ID),
36 )
37 
38 
39 def async_get_turn_on_trigger(device_id: str) -> dict[str, str]:
40  """Return data for a turn on trigger."""
41 
42  return {
43  CONF_PLATFORM: "device",
44  CONF_DEVICE_ID: device_id,
45  CONF_DOMAIN: DOMAIN,
46  CONF_TYPE: PLATFORM_TYPE,
47  }
48 
49 
51  hass: HomeAssistant,
52  config: ConfigType,
53  action: TriggerActionType,
54  trigger_info: TriggerInfo,
55  *,
56  platform_type: str = PLATFORM_TYPE,
57 ) -> CALLBACK_TYPE | None:
58  """Attach a trigger."""
59  device_ids = set()
60  if ATTR_DEVICE_ID in config:
61  device_ids.update(config.get(ATTR_DEVICE_ID, []))
62 
63  if ATTR_ENTITY_ID in config:
64  ent_reg = er.async_get(hass)
65 
66  def _get_device_id_from_entity_id(entity_id):
67  entity_entry = ent_reg.async_get(entity_id)
68 
69  if (
70  entity_entry is None
71  or entity_entry.device_id is None
72  or entity_entry.platform != DOMAIN
73  ):
74  raise ValueError(f"Entity {entity_id} is not a valid {DOMAIN} entity.")
75 
76  return entity_entry.device_id
77 
78  device_ids.update(
79  {
80  _get_device_id_from_entity_id(entity_id)
81  for entity_id in config.get(ATTR_ENTITY_ID, [])
82  }
83  )
84 
85  trigger_data = trigger_info["trigger_data"]
86 
87  unsubs = []
88 
89  for device_id in device_ids:
90  device = async_get_device_entry_by_device_id(hass, device_id)
91  device_name = device.name_by_user or device.name
92 
93  variables = {
94  **trigger_data,
95  CONF_PLATFORM: platform_type,
96  ATTR_DEVICE_ID: device_id,
97  "description": f"lg netcast turn on trigger for {device_name}",
98  }
99 
100  turn_on_trigger = async_get_turn_on_trigger(device_id)
101 
102  unsubs.append(
103  PluggableAction.async_attach_trigger(
104  hass, turn_on_trigger, action, {"trigger": variables}
105  )
106  )
107 
108  @callback
109  def async_remove() -> None:
110  """Remove state listeners async."""
111  for unsub in unsubs:
112  unsub()
113  unsubs.clear()
114 
115  return async_remove
DeviceEntry async_get_device_entry_by_device_id(HomeAssistant hass, str device_id)
Definition: helpers.py:50
dict[str, str] async_get_turn_on_trigger(str device_id)
Definition: turn_on.py:39
CALLBACK_TYPE|None async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info, *str platform_type=PLATFORM_TYPE)
Definition: turn_on.py:57
None async_remove(HomeAssistant hass, str intent_type)
Definition: intent.py:90