Home Assistant Unofficial Reference 2024.12.1
device_trigger.py
Go to the documentation of this file.
1 """Provides device triggers for LG Netcast."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  DEVICE_TRIGGER_BASE_SCHEMA,
11  InvalidDeviceAutomationConfig,
12 )
13 from homeassistant.const import CONF_DEVICE_ID, CONF_PLATFORM, CONF_TYPE
14 from homeassistant.core import CALLBACK_TYPE, HomeAssistant
15 from homeassistant.exceptions import HomeAssistantError
16 from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
17 from homeassistant.helpers.typing import ConfigType
18 
19 from . import trigger
20 from .const import DOMAIN
21 from .helpers import async_get_device_entry_by_device_id
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 
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 
45  try:
46  device = async_get_device_entry_by_device_id(hass, device_id)
47  except ValueError as err:
48  raise InvalidDeviceAutomationConfig(err) from err
49 
50  if DOMAIN in hass.data:
51  for config_entry_id in device.config_entries:
52  if hass.data[DOMAIN].get(config_entry_id):
53  break
54  else:
56  f"Device {device.id} is not from an existing {DOMAIN} config entry"
57  )
58 
59  return config
60 
61 
63  _hass: HomeAssistant, device_id: str
64 ) -> list[dict[str, Any]]:
65  """List device triggers for LG Netcast devices."""
66  return [async_get_turn_on_trigger(device_id)]
67 
68 
70  hass: HomeAssistant,
71  config: ConfigType,
72  action: TriggerActionType,
73  trigger_info: TriggerInfo,
74 ) -> CALLBACK_TYPE:
75  """Attach a trigger."""
76  if (trigger_type := config[CONF_TYPE]) == TURN_ON_PLATFORM_TYPE:
77  trigger_config = {
78  CONF_PLATFORM: trigger_type,
79  CONF_DEVICE_ID: config[CONF_DEVICE_ID],
80  }
81  trigger_config = await trigger.async_validate_trigger_config(
82  hass, trigger_config
83  )
84  return await trigger.async_attach_trigger(
85  hass, trigger_config, action, trigger_info
86  )
87 
88  raise HomeAssistantError(f"Unhandled trigger type {trigger_type}")
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
list[dict[str, Any]] async_get_triggers(HomeAssistant _hass, str device_id)
ConfigType async_validate_trigger_config(HomeAssistant hass, ConfigType config)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
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