Home Assistant Unofficial Reference 2024.12.1
device_trigger.py
Go to the documentation of this file.
1 """Provides device automations for control of Samsung 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 .helpers import (
19  async_get_client_by_device_entry,
20  async_get_device_entry_by_device_id,
21 )
22 from .triggers.turn_on import (
23  PLATFORM_TYPE as TURN_ON_PLATFORM_TYPE,
24  async_get_turn_on_trigger,
25 )
26 
27 TRIGGER_TYPES = {TURN_ON_PLATFORM_TYPE}
28 TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
29  {
30  vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
31  }
32 )
33 
34 
36  hass: HomeAssistant, config: ConfigType
37 ) -> ConfigType:
38  """Validate config."""
39  config = TRIGGER_SCHEMA(config)
40 
41  if config[CONF_TYPE] == TURN_ON_PLATFORM_TYPE:
42  device_id = config[CONF_DEVICE_ID]
43  try:
44  device = async_get_device_entry_by_device_id(hass, device_id)
46  except ValueError as err:
47  raise InvalidDeviceAutomationConfig(err) from err
48 
49  return config
50 
51 
53  _hass: HomeAssistant, device_id: str
54 ) -> list[dict[str, str]]:
55  """List device triggers for device."""
56  return [async_get_turn_on_trigger(device_id)]
57 
58 
60  hass: HomeAssistant,
61  config: ConfigType,
62  action: TriggerActionType,
63  trigger_info: TriggerInfo,
64 ) -> CALLBACK_TYPE:
65  """Attach a trigger."""
66  if (trigger_type := config[CONF_TYPE]) == TURN_ON_PLATFORM_TYPE:
67  trigger_config = {
68  CONF_PLATFORM: trigger_type,
69  CONF_DEVICE_ID: config[CONF_DEVICE_ID],
70  }
71  trigger_config = await trigger.async_validate_trigger_config(
72  hass, trigger_config
73  )
74  return await trigger.async_attach_trigger(
75  hass, trigger_config, action, trigger_info
76  )
77 
78  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
ConfigType async_validate_trigger_config(HomeAssistant hass, ConfigType config)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
list[dict[str, str]] async_get_triggers(HomeAssistant _hass, str device_id)
SamsungTVBridge async_get_client_by_device_entry(HomeAssistant hass, DeviceEntry device)
Definition: helpers.py:52