2020-11-09 11:04:16 +00:00
|
|
|
"""Provides device automations for Device Tracker."""
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
2020-11-09 11:04:16 +00:00
|
|
|
|
2022-05-23 14:03:21 +00:00
|
|
|
from typing import Final
|
2021-05-22 08:15:15 +00:00
|
|
|
|
2020-11-09 11:04:16 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-06-10 17:11:38 +00:00
|
|
|
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
|
2020-11-09 11:04:16 +00:00
|
|
|
from homeassistant.components.zone import DOMAIN as DOMAIN_ZONE, trigger as zone
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_DEVICE_ID,
|
|
|
|
CONF_DOMAIN,
|
|
|
|
CONF_ENTITY_ID,
|
|
|
|
CONF_EVENT,
|
|
|
|
CONF_PLATFORM,
|
|
|
|
CONF_TYPE,
|
|
|
|
CONF_ZONE,
|
|
|
|
)
|
|
|
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
|
2023-03-01 07:02:16 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, entity_registry as er
|
2022-08-15 15:44:12 +00:00
|
|
|
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
2020-11-09 11:04:16 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2022-01-05 16:28:12 +00:00
|
|
|
from .const import DOMAIN
|
2020-11-09 11:04:16 +00:00
|
|
|
|
2021-05-22 08:15:15 +00:00
|
|
|
TRIGGER_TYPES: Final[set[str]] = {"enters", "leaves"}
|
2020-11-09 11:04:16 +00:00
|
|
|
|
2021-06-10 17:11:38 +00:00
|
|
|
TRIGGER_SCHEMA: Final = DEVICE_TRIGGER_BASE_SCHEMA.extend(
|
2020-11-09 11:04:16 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
|
|
|
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
|
|
|
|
vol.Required(CONF_ZONE): cv.entity_domain(DOMAIN_ZONE),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-22 18:32:50 +00:00
|
|
|
async def async_get_triggers(
|
|
|
|
hass: HomeAssistant, device_id: str
|
2022-05-23 14:03:21 +00:00
|
|
|
) -> list[dict[str, str]]:
|
2020-11-09 11:04:16 +00:00
|
|
|
"""List device triggers for Device Tracker devices."""
|
2023-03-01 07:02:16 +00:00
|
|
|
registry = er.async_get(hass)
|
2020-11-09 11:04:16 +00:00
|
|
|
triggers = []
|
|
|
|
|
|
|
|
# Get all the integrations entities for this device
|
2023-03-01 07:02:16 +00:00
|
|
|
for entry in er.async_entries_for_device(registry, device_id):
|
2020-11-09 11:04:16 +00:00
|
|
|
if entry.domain != DOMAIN:
|
|
|
|
continue
|
|
|
|
|
|
|
|
triggers.append(
|
|
|
|
{
|
|
|
|
CONF_PLATFORM: "device",
|
|
|
|
CONF_DEVICE_ID: device_id,
|
|
|
|
CONF_DOMAIN: DOMAIN,
|
|
|
|
CONF_ENTITY_ID: entry.entity_id,
|
|
|
|
CONF_TYPE: "enters",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
triggers.append(
|
|
|
|
{
|
|
|
|
CONF_PLATFORM: "device",
|
|
|
|
CONF_DEVICE_ID: device_id,
|
|
|
|
CONF_DOMAIN: DOMAIN,
|
|
|
|
CONF_ENTITY_ID: entry.entity_id,
|
|
|
|
CONF_TYPE: "leaves",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return triggers
|
|
|
|
|
|
|
|
|
|
|
|
async def async_attach_trigger(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
2022-08-15 15:44:12 +00:00
|
|
|
action: TriggerActionType,
|
|
|
|
trigger_info: TriggerInfo,
|
2020-11-09 11:04:16 +00:00
|
|
|
) -> CALLBACK_TYPE:
|
|
|
|
"""Attach a trigger."""
|
|
|
|
if config[CONF_TYPE] == "enters":
|
|
|
|
event = zone.EVENT_ENTER
|
|
|
|
else:
|
|
|
|
event = zone.EVENT_LEAVE
|
|
|
|
|
|
|
|
zone_config = {
|
|
|
|
CONF_PLATFORM: DOMAIN_ZONE,
|
|
|
|
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
|
|
|
CONF_ZONE: config[CONF_ZONE],
|
|
|
|
CONF_EVENT: event,
|
|
|
|
}
|
2021-12-02 18:28:21 +00:00
|
|
|
zone_config = await zone.async_validate_trigger_config(hass, zone_config)
|
2020-11-09 11:04:16 +00:00
|
|
|
return await zone.async_attach_trigger(
|
2022-08-15 15:44:12 +00:00
|
|
|
hass, zone_config, action, trigger_info, platform_type="device"
|
2020-11-09 11:04:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-05-22 08:15:15 +00:00
|
|
|
async def async_get_trigger_capabilities(
|
|
|
|
hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> dict[str, vol.Schema]:
|
2020-11-09 11:04:16 +00:00
|
|
|
"""List trigger capabilities."""
|
|
|
|
zones = {
|
|
|
|
ent.entity_id: ent.name
|
|
|
|
for ent in sorted(hass.states.async_all(DOMAIN_ZONE), key=lambda ent: ent.name)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
"extra_fields": vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ZONE): vol.In(zones),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|