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