1 """Provides device automations for Philips Hue events."""
3 from __future__
import annotations
5 from typing
import TYPE_CHECKING, Any
13 from .const
import DOMAIN
14 from .v1.device_trigger
import (
15 async_attach_trigger
as async_attach_trigger_v1,
16 async_get_triggers
as async_get_triggers_v1,
17 async_validate_trigger_config
as async_validate_trigger_config_v1,
19 from .v2.device_trigger
import (
20 async_attach_trigger
as async_attach_trigger_v2,
21 async_get_triggers
as async_get_triggers_v2,
22 async_validate_trigger_config
as async_validate_trigger_config_v2,
29 from .bridge
import HueBridge
33 hass: HomeAssistant, config: ConfigType
35 """Validate config."""
36 if DOMAIN
not in hass.data:
39 device_id = config[CONF_DEVICE_ID]
41 dev_reg: dr.DeviceRegistry = dr.async_get(hass)
42 if (device_entry := dev_reg.async_get(device_id))
is None:
45 for conf_entry_id
in device_entry.config_entries:
46 if conf_entry_id
not in hass.data[DOMAIN]:
48 bridge: HueBridge = hass.data[DOMAIN][conf_entry_id]
49 if bridge.api_version == 1:
50 return await async_validate_trigger_config_v1(bridge, device_entry, config)
51 return await async_validate_trigger_config_v2(bridge, device_entry, config)
58 action: TriggerActionType,
59 trigger_info: TriggerInfo,
61 """Listen for state changes based on configuration."""
62 device_id = config[CONF_DEVICE_ID]
64 dev_reg: dr.DeviceRegistry = dr.async_get(hass)
65 if (device_entry := dev_reg.async_get(device_id))
is None:
68 for conf_entry_id
in device_entry.config_entries:
69 if conf_entry_id
not in hass.data[DOMAIN]:
71 bridge: HueBridge = hass.data[DOMAIN][conf_entry_id]
72 if bridge.api_version == 1:
73 return await async_attach_trigger_v1(
74 bridge, device_entry, config, action, trigger_info
76 return await async_attach_trigger_v2(
77 bridge, device_entry, config, action, trigger_info
80 f
"Device ID {device_id} is not found on any Hue bridge"
85 hass: HomeAssistant, device_id: str
86 ) -> list[dict[str, Any]]:
87 """Get device triggers for given (hass) device id."""
88 if DOMAIN
not in hass.data:
91 dev_reg: dr.DeviceRegistry = dr.async_get(hass)
92 if (device_entry := dev_reg.async_get(device_id))
is None:
93 raise ValueError(f
"Device ID {device_id} is not valid")
97 for conf_entry_id
in device_entry.config_entries:
98 if conf_entry_id
not in hass.data[DOMAIN]:
100 bridge: HueBridge = hass.data[DOMAIN][conf_entry_id]
102 if bridge.api_version == 1:
103 return async_get_triggers_v1(bridge, device_entry)
104 return async_get_triggers_v2(bridge, device_entry)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
list[dict[str, Any]] async_get_triggers(HomeAssistant hass, str device_id)
ConfigType async_validate_trigger_config(HomeAssistant hass, ConfigType config)