1 """Provides device automations for Kodi."""
3 from __future__
import annotations
5 import voluptuous
as vol
21 from .const
import DOMAIN, EVENT_TURN_OFF, EVENT_TURN_ON
23 TRIGGER_TYPES = {
"turn_on",
"turn_off"}
25 TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
27 vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
28 vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
34 hass: HomeAssistant, device_id: str
35 ) -> list[dict[str, str]]:
36 """List device triggers for Kodi devices."""
37 registry = er.async_get(hass)
41 for entry
in er.async_entries_for_device(registry, device_id):
42 if entry.domain ==
"media_player":
45 CONF_PLATFORM:
"device",
46 CONF_DEVICE_ID: device_id,
48 CONF_ENTITY_ID: entry.id,
54 CONF_PLATFORM:
"device",
55 CONF_DEVICE_ID: device_id,
57 CONF_ENTITY_ID: entry.id,
58 CONF_TYPE:
"turn_off",
69 action: TriggerActionType,
71 trigger_info: TriggerInfo,
73 registry = er.async_get(hass)
74 entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID])
75 trigger_data = trigger_info[
"trigger_data"]
79 def _handle_event(event: Event):
80 if event.data[ATTR_ENTITY_ID] == entity_id:
81 hass.async_run_hass_job(
87 "description": event_type,
88 "entity_id": entity_id,
94 return hass.bus.async_listen(event_type, _handle_event)
100 action: TriggerActionType,
101 trigger_info: TriggerInfo,
103 """Attach a trigger."""
104 if config[CONF_TYPE] ==
"turn_on":
105 return _attach_trigger(hass, config, action, EVENT_TURN_ON, trigger_info)
107 if config[CONF_TYPE] ==
"turn_off":
108 return _attach_trigger(hass, config, action, EVENT_TURN_OFF, trigger_info)
list[dict[str, str]] async_get_triggers(HomeAssistant hass, str device_id)
def _attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, event_type, TriggerInfo trigger_info)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)