2020-04-01 18:42:22 +00:00
|
|
|
"""Provides device automations for Philips Hue events."""
|
2022-01-11 20:28:13 +00:00
|
|
|
from __future__ import annotations
|
2020-04-01 18:42:22 +00:00
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2020-04-01 18:42:22 +00:00
|
|
|
from homeassistant.components.device_automation.exceptions import (
|
|
|
|
InvalidDeviceAutomationConfig,
|
|
|
|
)
|
2021-11-16 19:59:17 +00:00
|
|
|
from homeassistant.const import CONF_DEVICE_ID
|
|
|
|
from homeassistant.core import CALLBACK_TYPE
|
|
|
|
from homeassistant.helpers import device_registry as dr
|
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .v1.device_trigger import (
|
|
|
|
async_attach_trigger as async_attach_trigger_v1,
|
|
|
|
async_get_triggers as async_get_triggers_v1,
|
|
|
|
async_validate_trigger_config as async_validate_trigger_config_v1,
|
2020-04-01 18:42:22 +00:00
|
|
|
)
|
2021-11-16 19:59:17 +00:00
|
|
|
from .v2.device_trigger import (
|
|
|
|
async_attach_trigger as async_attach_trigger_v2,
|
|
|
|
async_get_triggers as async_get_triggers_v2,
|
|
|
|
async_validate_trigger_config as async_validate_trigger_config_v2,
|
2020-04-01 18:42:22 +00:00
|
|
|
)
|
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from homeassistant.components.automation import (
|
|
|
|
AutomationActionType,
|
|
|
|
AutomationTriggerInfo,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
2020-04-01 18:42:22 +00:00
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
from .bridge import HueBridge
|
2020-04-01 18:42:22 +00:00
|
|
|
|
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
async def async_validate_trigger_config(hass: "HomeAssistant", config: ConfigType):
|
2020-04-01 18:42:22 +00:00
|
|
|
"""Validate config."""
|
2021-11-16 19:59:17 +00:00
|
|
|
if DOMAIN not in hass.data:
|
|
|
|
# happens at startup
|
|
|
|
return config
|
|
|
|
device_id = config[CONF_DEVICE_ID]
|
|
|
|
# lookup device in HASS DeviceRegistry
|
|
|
|
dev_reg: dr.DeviceRegistry = dr.async_get(hass)
|
2022-02-19 16:21:26 +00:00
|
|
|
if (device_entry := dev_reg.async_get(device_id)) is None:
|
2021-11-16 19:59:17 +00:00
|
|
|
raise InvalidDeviceAutomationConfig(f"Device ID {device_id} is not valid")
|
|
|
|
|
|
|
|
for conf_entry_id in device_entry.config_entries:
|
|
|
|
if conf_entry_id not in hass.data[DOMAIN]:
|
|
|
|
continue
|
2022-01-11 20:28:13 +00:00
|
|
|
bridge: HueBridge = hass.data[DOMAIN][conf_entry_id]
|
2021-11-16 19:59:17 +00:00
|
|
|
if bridge.api_version == 1:
|
|
|
|
return await async_validate_trigger_config_v1(bridge, device_entry, config)
|
|
|
|
return await async_validate_trigger_config_v2(bridge, device_entry, config)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_attach_trigger(
|
|
|
|
hass: "HomeAssistant",
|
|
|
|
config: ConfigType,
|
|
|
|
action: "AutomationActionType",
|
|
|
|
automation_info: "AutomationTriggerInfo",
|
|
|
|
) -> CALLBACK_TYPE:
|
2020-04-01 18:42:22 +00:00
|
|
|
"""Listen for state changes based on configuration."""
|
2021-11-16 19:59:17 +00:00
|
|
|
device_id = config[CONF_DEVICE_ID]
|
|
|
|
# lookup device in HASS DeviceRegistry
|
|
|
|
dev_reg: dr.DeviceRegistry = dr.async_get(hass)
|
2022-02-19 16:21:26 +00:00
|
|
|
if (device_entry := dev_reg.async_get(device_id)) is None:
|
2021-11-16 19:59:17 +00:00
|
|
|
raise InvalidDeviceAutomationConfig(f"Device ID {device_id} is not valid")
|
|
|
|
|
|
|
|
for conf_entry_id in device_entry.config_entries:
|
|
|
|
if conf_entry_id not in hass.data[DOMAIN]:
|
|
|
|
continue
|
2022-01-11 20:28:13 +00:00
|
|
|
bridge: HueBridge = hass.data[DOMAIN][conf_entry_id]
|
2021-11-16 19:59:17 +00:00
|
|
|
if bridge.api_version == 1:
|
|
|
|
return await async_attach_trigger_v1(
|
|
|
|
bridge, device_entry, config, action, automation_info
|
|
|
|
)
|
|
|
|
return await async_attach_trigger_v2(
|
|
|
|
bridge, device_entry, config, action, automation_info
|
|
|
|
)
|
|
|
|
raise InvalidDeviceAutomationConfig(
|
|
|
|
f"Device ID {device_id} is not found on any Hue bridge"
|
2020-04-01 18:42:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-16 19:59:17 +00:00
|
|
|
async def async_get_triggers(hass: "HomeAssistant", device_id: str):
|
|
|
|
"""Get device triggers for given (hass) device id."""
|
|
|
|
if DOMAIN not in hass.data:
|
|
|
|
return []
|
|
|
|
# lookup device in HASS DeviceRegistry
|
|
|
|
dev_reg: dr.DeviceRegistry = dr.async_get(hass)
|
2022-02-19 16:21:26 +00:00
|
|
|
if (device_entry := dev_reg.async_get(device_id)) is None:
|
2021-11-16 19:59:17 +00:00
|
|
|
raise ValueError(f"Device ID {device_id} is not valid")
|
|
|
|
|
|
|
|
# Iterate all config entries for this device
|
|
|
|
# and work out the bridge version
|
|
|
|
for conf_entry_id in device_entry.config_entries:
|
|
|
|
if conf_entry_id not in hass.data[DOMAIN]:
|
|
|
|
continue
|
2022-01-11 20:28:13 +00:00
|
|
|
bridge: HueBridge = hass.data[DOMAIN][conf_entry_id]
|
2021-11-16 19:59:17 +00:00
|
|
|
|
|
|
|
if bridge.api_version == 1:
|
|
|
|
return await async_get_triggers_v1(bridge, device_entry)
|
|
|
|
return await async_get_triggers_v2(bridge, device_entry)
|