Home Assistant Unofficial Reference 2024.12.1
device_trigger.py
Go to the documentation of this file.
1 """Provides device automations for homekit devices."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Generator
6 from typing import TYPE_CHECKING, Any
7 
8 from aiohomekit.model.characteristics import CharacteristicsTypes
9 from aiohomekit.model.characteristics.const import InputEventValues
10 from aiohomekit.model.services import Service, ServicesTypes
11 from aiohomekit.utils import clamp_enum_to_char
12 import voluptuous as vol
13 
14 from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE
17 from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
18 from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
19 from homeassistant.helpers.typing import ConfigType
20 
21 from .const import DOMAIN, KNOWN_DEVICES, TRIGGERS
22 
23 if TYPE_CHECKING:
24  from .connection import HKDevice
25 
26 TRIGGER_TYPES = {
27  "doorbell",
28  "button1",
29  "button2",
30  "button3",
31  "button4",
32  "button5",
33  "button6",
34  "button7",
35  "button8",
36  "button9",
37  "button10",
38 }
39 TRIGGER_SUBTYPES = {"single_press", "double_press", "long_press"}
40 
41 CONF_IID = "iid"
42 CONF_SUBTYPE = "subtype"
43 
44 TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
45  {
46  vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
47  vol.Required(CONF_SUBTYPE): vol.In(TRIGGER_SUBTYPES),
48  }
49 )
50 
51 HK_TO_HA_INPUT_EVENT_VALUES = {
52  InputEventValues.SINGLE_PRESS: "single_press",
53  InputEventValues.DOUBLE_PRESS: "double_press",
54  InputEventValues.LONG_PRESS: "long_press",
55 }
56 
57 
59  """Represents a stateless source of event data from HomeKit."""
60 
61  def __init__(self, hass: HomeAssistant) -> None:
62  """Initialize a set of triggers for a device."""
63  self._hass_hass = hass
64  self._triggers: dict[tuple[str, str], dict[str, Any]] = {}
65  self._callbacks: dict[tuple[str, str], list[Callable[[Any], None]]] = {}
66  self._iid_trigger_keys: dict[int, set[tuple[str, str]]] = {}
67 
68  @callback
70  self, connection: HKDevice, aid: int, triggers: list[dict[str, Any]]
71  ) -> None:
72  """Set up a set of triggers for a device.
73 
74  This function must be re-entrant since
75  it is called when the device is first added and
76  when the config entry is reloaded.
77  """
78  for trigger_data in triggers:
79  trigger_key = (trigger_data[CONF_TYPE], trigger_data[CONF_SUBTYPE])
80  self._triggers[trigger_key] = trigger_data
81  iid = trigger_data["characteristic"]
82  self._iid_trigger_keys.setdefault(iid, set()).add(trigger_key)
83  connection.add_watchable_characteristics([(aid, iid)])
84 
85  def fire(self, iid: int, ev: dict[str, Any]) -> None:
86  """Process events that have been received from a HomeKit accessory."""
87  for trigger_key in self._iid_trigger_keys.get(iid, set()):
88  for event_handler in self._callbacks.get(trigger_key, []):
89  event_handler(ev)
90 
91  def async_get_triggers(self) -> Generator[tuple[str, str]]:
92  """List device triggers for HomeKit devices."""
93  yield from self._triggers
94 
95  @callback
97  self,
98  config: ConfigType,
99  action: TriggerActionType,
100  trigger_info: TriggerInfo,
101  ) -> CALLBACK_TYPE:
102  """Attach a trigger."""
103  trigger_data = trigger_info["trigger_data"]
104  type_: str = config[CONF_TYPE]
105  sub_type: str = config[CONF_SUBTYPE]
106  trigger_key = (type_, sub_type)
107  job = HassJob(action)
108  trigger_callbacks = self._callbacks.setdefault(trigger_key, [])
109  hass = self._hass_hass
110 
111  @callback
112  def event_handler(ev: dict[str, Any]) -> None:
113  if sub_type != HK_TO_HA_INPUT_EVENT_VALUES[ev["value"]]:
114  return
115  hass.async_run_hass_job(job, {"trigger": {**trigger_data, **config}})
116 
117  trigger_callbacks.append(event_handler)
118 
119  def async_remove_handler() -> None:
120  trigger_callbacks.remove(event_handler)
121 
122  return async_remove_handler
123 
124 
125 def enumerate_stateless_switch(service: Service) -> list[dict[str, Any]]:
126  """Enumerate a stateless switch, like a single button."""
127 
128  # A stateless switch that has a SERVICE_LABEL_INDEX is part of a group
129  # And is handled separately
130  if (
131  service.has(CharacteristicsTypes.SERVICE_LABEL_INDEX)
132  and len(service.linked) > 0
133  ):
134  return []
135 
136  char = service[CharacteristicsTypes.INPUT_EVENT]
137 
138  # HomeKit itself supports single, double and long presses. But the
139  # manufacturer might not - clamp options to what they say.
140  all_values = clamp_enum_to_char(InputEventValues, char)
141 
142  return [
143  {
144  "characteristic": char.iid,
145  "value": event_type,
146  "type": "button1",
147  "subtype": HK_TO_HA_INPUT_EVENT_VALUES[event_type],
148  }
149  for event_type in all_values
150  ]
151 
152 
153 def enumerate_stateless_switch_group(service: Service) -> list[dict[str, Any]]:
154  """Enumerate a group of stateless switches, like a remote control."""
155  switches = list(
156  service.accessory.services.filter(
157  service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH,
158  child_service=service,
159  order_by=[CharacteristicsTypes.SERVICE_LABEL_INDEX],
160  )
161  )
162 
163  results: list[dict[str, Any]] = []
164  for idx, switch in enumerate(switches):
165  char = switch[CharacteristicsTypes.INPUT_EVENT]
166 
167  # HomeKit itself supports single, double and long presses. But the
168  # manufacturer might not - clamp options to what they say.
169  all_values = clamp_enum_to_char(InputEventValues, char)
170 
171  results.extend(
172  {
173  "characteristic": char.iid,
174  "value": event_type,
175  "type": f"button{idx + 1}",
176  "subtype": HK_TO_HA_INPUT_EVENT_VALUES[event_type],
177  }
178  for event_type in all_values
179  )
180  return results
181 
182 
183 def enumerate_doorbell(service: Service) -> list[dict[str, Any]]:
184  """Enumerate doorbell buttons."""
185  input_event = service[CharacteristicsTypes.INPUT_EVENT]
186 
187  # HomeKit itself supports single, double and long presses. But the
188  # manufacturer might not - clamp options to what they say.
189  all_values = clamp_enum_to_char(InputEventValues, input_event)
190 
191  return [
192  {
193  "characteristic": input_event.iid,
194  "value": event_type,
195  "type": "doorbell",
196  "subtype": HK_TO_HA_INPUT_EVENT_VALUES[event_type],
197  }
198  for event_type in all_values
199  ]
200 
201 
202 TRIGGER_FINDERS = {
203  ServicesTypes.SERVICE_LABEL: enumerate_stateless_switch_group,
204  ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH: enumerate_stateless_switch,
205  ServicesTypes.DOORBELL: enumerate_doorbell,
206 }
207 
208 
210  hass: HomeAssistant, config_entry: ConfigEntry
211 ) -> None:
212  """Triggers aren't entities as they have no state, but we still need to set them up for a config entry."""
213  hkid = config_entry.data["AccessoryPairingID"]
214  conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
215 
216  @callback
217  def async_add_characteristic(service: Service) -> bool:
218  aid = service.accessory.aid
219  service_type = service.type
220 
221  # If not a known service type then we can't handle any stateless events for it
222  if service_type not in TRIGGER_FINDERS:
223  return False
224 
225  # We can't have multiple trigger sources for the same device id
226  # Can't have a doorbell and a remote control in the same accessory
227  # They have to be different accessories (they can be on the same bridge)
228  # In practice, this is inline with what iOS actually supports AFAWCT.
229  device_id = conn.devices[aid]
230  if TRIGGERS in hass.data and device_id in hass.data[TRIGGERS]:
231  return False
232 
233  # Just because we recognize the service type doesn't mean we can actually
234  # extract any triggers - so only proceed if we can
235  triggers = TRIGGER_FINDERS[service_type](service)
236  if len(triggers) == 0:
237  return False
238 
239  trigger = async_get_or_create_trigger_source(conn.hass, device_id)
240  trigger.async_setup(conn, aid, triggers)
241 
242  return True
243 
244  conn.add_trigger_factory(async_add_characteristic)
245 
246 
247 @callback
249  hass: HomeAssistant, device_id: str
250 ) -> TriggerSource:
251  """Get or create a trigger source for a device id."""
252  trigger_sources: dict[str, TriggerSource] = hass.data.setdefault(TRIGGERS, {})
253  if not (source := trigger_sources.get(device_id)):
254  source = TriggerSource(hass)
255  trigger_sources[device_id] = source
256  return source
257 
258 
260  conn: HKDevice, events: dict[tuple[int, int], dict[str, Any]]
261 ) -> None:
262  """Process events generated by a HomeKit accessory into automation triggers."""
263  trigger_sources: dict[str, TriggerSource] = conn.hass.data.get(TRIGGERS, {})
264  if not trigger_sources:
265  return
266  for (aid, iid), ev in events.items():
267  if aid in conn.devices:
268  device_id = conn.devices[aid]
269  if source := trigger_sources.get(device_id):
270  # If the value is None, we received the event via polling
271  # and we don't want to trigger on that
272  if ev.get("value") is not None:
273  source.fire(iid, ev)
274 
275 
277  hass: HomeAssistant, device_id: str
278 ) -> list[dict[str, str]]:
279  """List device triggers for homekit devices."""
280 
281  if device_id not in hass.data.get(TRIGGERS, {}):
282  return []
283 
284  device: TriggerSource = hass.data[TRIGGERS][device_id]
285 
286  return [
287  {
288  CONF_PLATFORM: "device",
289  CONF_DEVICE_ID: device_id,
290  CONF_DOMAIN: DOMAIN,
291  CONF_TYPE: trigger,
292  CONF_SUBTYPE: subtype,
293  }
294  for trigger, subtype in device.async_get_triggers()
295  ]
296 
297 
299  hass: HomeAssistant,
300  config: ConfigType,
301  action: TriggerActionType,
302  trigger_info: TriggerInfo,
303 ) -> CALLBACK_TYPE:
304  """Attach a trigger."""
305  device_id = config[CONF_DEVICE_ID]
307  config, action, trigger_info
308  )
None async_setup(self, HKDevice connection, int aid, list[dict[str, Any]] triggers)
CALLBACK_TYPE async_attach_trigger(self, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
bool add(self, _T matcher)
Definition: match.py:185
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
list[dict[str, Any]] enumerate_doorbell(Service service)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
list[dict[str, Any]] enumerate_stateless_switch_group(Service service)
list[dict[str, Any]] enumerate_stateless_switch(Service service)
list[dict[str, str]] async_get_triggers(HomeAssistant hass, str device_id)
None async_setup_triggers_for_entry(HomeAssistant hass, ConfigEntry config_entry)
None async_fire_triggers(HKDevice conn, dict[tuple[int, int], dict[str, Any]] events)
TriggerSource async_get_or_create_trigger_source(HomeAssistant hass, str device_id)
bool async_add_characteristic(Characteristic char)
Definition: number.py:78