Home Assistant Unofficial Reference 2024.12.1
device_trigger.py
Go to the documentation of this file.
1 """Provides device automations for control of LG webOS Smart TV."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
8  DEVICE_TRIGGER_BASE_SCHEMA,
9  InvalidDeviceAutomationConfig,
10 )
11 from homeassistant.const import CONF_DEVICE_ID, CONF_PLATFORM, CONF_TYPE
12 from homeassistant.core import CALLBACK_TYPE, HomeAssistant
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
15 from homeassistant.helpers.typing import ConfigType
16 
17 from . import trigger
18 from .const import DOMAIN
19 from .helpers import (
20  async_get_client_by_device_entry,
21  async_get_device_entry_by_device_id,
22 )
23 from .triggers.turn_on import (
24  PLATFORM_TYPE as TURN_ON_PLATFORM_TYPE,
25  async_get_turn_on_trigger,
26 )
27 
28 TRIGGER_TYPES = {TURN_ON_PLATFORM_TYPE}
29 TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
30  {
31  vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
32  }
33 )
34 
35 
37  hass: HomeAssistant, config: ConfigType
38 ) -> ConfigType:
39  """Validate config."""
40  config = TRIGGER_SCHEMA(config)
41 
42  if config[CONF_TYPE] == TURN_ON_PLATFORM_TYPE:
43  device_id = config[CONF_DEVICE_ID]
44  try:
45  device = async_get_device_entry_by_device_id(hass, device_id)
46  if DOMAIN in hass.data:
48  except ValueError as err:
49  raise InvalidDeviceAutomationConfig(err) from err
50 
51  return config
52 
53 
55  _hass: HomeAssistant, device_id: str
56 ) -> list[dict[str, str]]:
57  """List device triggers for device."""
58  return [async_get_turn_on_trigger(device_id)]
59 
60 
62  hass: HomeAssistant,
63  config: ConfigType,
64  action: TriggerActionType,
65  trigger_info: TriggerInfo,
66 ) -> CALLBACK_TYPE:
67  """Attach a trigger."""
68  if (trigger_type := config[CONF_TYPE]) == TURN_ON_PLATFORM_TYPE:
69  trigger_config = {
70  CONF_PLATFORM: trigger_type,
71  CONF_DEVICE_ID: config[CONF_DEVICE_ID],
72  }
73  trigger_config = await trigger.async_validate_trigger_config(
74  hass, trigger_config
75  )
76  return await trigger.async_attach_trigger(
77  hass, trigger_config, action, trigger_info
78  )
79 
80  raise HomeAssistantError(f"Unhandled trigger type {trigger_type}")
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
SamsungTVBridge async_get_client_by_device_entry(HomeAssistant hass, DeviceEntry device)
Definition: helpers.py:52
ConfigType async_validate_trigger_config(HomeAssistant hass, ConfigType config)
list[dict[str, str]] async_get_triggers(HomeAssistant _hass, str device_id)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)