Home Assistant Unofficial Reference 2024.12.1
device_trigger.py
Go to the documentation of this file.
1 """Provides device automations for control of device."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
7 from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
8 from homeassistant.const import CONF_DEVICE_ID, CONF_TYPE
9 from homeassistant.core import CALLBACK_TYPE, HomeAssistant
10 from homeassistant.exceptions import HomeAssistantError
12  PluggableAction,
13  TriggerActionType,
14  TriggerInfo,
15 )
16 from homeassistant.helpers.typing import ConfigType
17 
18 from .const import DOMAIN, TRIGGER_TYPE_TURN_ON
19 from .helpers import async_get_turn_on_trigger
20 
21 TRIGGER_TYPES = {TRIGGER_TYPE_TURN_ON}
22 TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
23  {
24  vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
25  }
26 )
27 
28 
30  hass: HomeAssistant, device_id: str
31 ) -> list[dict[str, str]]:
32  """List device triggers for device."""
33  triggers = []
34  triggers.append(async_get_turn_on_trigger(device_id))
35 
36  return triggers
37 
38 
40  hass: HomeAssistant,
41  config: ConfigType,
42  action: TriggerActionType,
43  trigger_info: TriggerInfo,
44 ) -> CALLBACK_TYPE:
45  """Attach a trigger."""
46  trigger_data = trigger_info["trigger_data"]
47  if (trigger_type := config[CONF_TYPE]) == TRIGGER_TYPE_TURN_ON:
48  variables = {
49  "trigger": {
50  **trigger_data,
51  "platform": "device",
52  "domain": DOMAIN,
53  "device_id": config[CONF_DEVICE_ID],
54  "description": f"philips_js '{trigger_type}' event",
55  }
56  }
57 
58  turn_on_trigger = async_get_turn_on_trigger(config[CONF_DEVICE_ID])
59  return PluggableAction.async_attach_trigger(
60  hass, turn_on_trigger, action, variables
61  )
62 
63  raise HomeAssistantError(f"Unhandled trigger type {trigger_type}")
dict[str, str] async_get_turn_on_trigger(str device_id)
Definition: turn_on.py:39
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)