2019-02-13 20:21:14 +00:00
|
|
|
"""Offer zone automation rules."""
|
2016-04-04 19:18:58 +00:00
|
|
|
import voluptuous as vol
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2020-08-28 15:02:12 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_FRIENDLY_NAME,
|
|
|
|
CONF_ENTITY_ID,
|
|
|
|
CONF_EVENT,
|
|
|
|
CONF_PLATFORM,
|
|
|
|
CONF_ZONE,
|
|
|
|
)
|
2021-12-02 18:28:21 +00:00
|
|
|
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
|
|
|
from homeassistant.helpers import (
|
|
|
|
condition,
|
|
|
|
config_validation as cv,
|
|
|
|
entity_registry as er,
|
|
|
|
location,
|
|
|
|
)
|
2020-07-05 22:31:33 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change_event
|
2021-12-02 18:28:21 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-08-12 03:38:18 +00:00
|
|
|
|
2020-11-09 11:04:16 +00:00
|
|
|
# mypy: allow-incomplete-defs, allow-untyped-defs
|
|
|
|
# mypy: no-check-untyped-defs
|
2019-08-12 03:38:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_ENTER = "enter"
|
|
|
|
EVENT_LEAVE = "leave"
|
2015-09-29 07:18:52 +00:00
|
|
|
DEFAULT_EVENT = EVENT_ENTER
|
|
|
|
|
2020-08-28 15:02:12 +00:00
|
|
|
_EVENT_DESCRIPTION = {EVENT_ENTER: "entering", EVENT_LEAVE: "leaving"}
|
|
|
|
|
2021-12-02 18:28:21 +00:00
|
|
|
_TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_PLATFORM): "zone",
|
2021-12-02 18:28:21 +00:00
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_ids_or_uuids,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Required(CONF_ZONE): cv.entity_id,
|
|
|
|
vol.Required(CONF_EVENT, default=DEFAULT_EVENT): vol.Any(
|
|
|
|
EVENT_ENTER, EVENT_LEAVE
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-04-04 19:18:58 +00:00
|
|
|
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2021-12-02 18:28:21 +00:00
|
|
|
async def async_validate_trigger_config(
|
|
|
|
hass: HomeAssistant, config: ConfigType
|
|
|
|
) -> ConfigType:
|
|
|
|
"""Validate trigger config."""
|
|
|
|
config = _TRIGGER_SCHEMA(config)
|
|
|
|
registry = er.async_get(hass)
|
|
|
|
config[CONF_ENTITY_ID] = er.async_resolve_entity_ids(
|
|
|
|
registry, config[CONF_ENTITY_ID]
|
|
|
|
)
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2020-11-09 11:04:16 +00:00
|
|
|
async def async_attach_trigger(
|
|
|
|
hass, config, action, automation_info, *, platform_type: str = "zone"
|
|
|
|
) -> CALLBACK_TYPE:
|
2016-03-07 16:14:55 +00:00
|
|
|
"""Listen for state changes based on configuration."""
|
2021-09-04 00:25:51 +00:00
|
|
|
trigger_data = automation_info["trigger_data"]
|
2015-09-29 07:18:52 +00:00
|
|
|
entity_id = config.get(CONF_ENTITY_ID)
|
|
|
|
zone_entity_id = config.get(CONF_ZONE)
|
2016-04-04 19:18:58 +00:00
|
|
|
event = config.get(CONF_EVENT)
|
2020-10-08 07:44:34 +00:00
|
|
|
job = HassJob(action)
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2020-07-05 22:31:33 +00:00
|
|
|
def zone_automation_listener(zone_event):
|
2016-03-07 19:20:07 +00:00
|
|
|
"""Listen for state changes and calls action."""
|
2020-07-05 22:31:33 +00:00
|
|
|
entity = zone_event.data.get("entity_id")
|
|
|
|
from_s = zone_event.data.get("old_state")
|
|
|
|
to_s = zone_event.data.get("new_state")
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
from_s
|
|
|
|
and not location.has_location(from_s)
|
|
|
|
or not location.has_location(to_s)
|
|
|
|
):
|
2015-09-29 07:18:52 +00:00
|
|
|
return
|
|
|
|
|
2016-04-21 20:59:42 +00:00
|
|
|
zone_state = hass.states.get(zone_entity_id)
|
2020-05-01 14:37:25 +00:00
|
|
|
from_match = condition.zone(hass, zone_state, from_s) if from_s else False
|
2021-02-19 12:14:47 +00:00
|
|
|
to_match = condition.zone(hass, zone_state, to_s) if to_s else False
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
event == EVENT_ENTER
|
|
|
|
and not from_match
|
|
|
|
and to_match
|
|
|
|
or event == EVENT_LEAVE
|
|
|
|
and from_match
|
|
|
|
and not to_match
|
|
|
|
):
|
2020-08-28 15:02:12 +00:00
|
|
|
description = f"{entity} {_EVENT_DESCRIPTION[event]} {zone_state.attributes[ATTR_FRIENDLY_NAME]}"
|
2020-10-08 07:44:34 +00:00
|
|
|
hass.async_run_hass_job(
|
|
|
|
job,
|
2020-08-24 21:01:57 +00:00
|
|
|
{
|
|
|
|
"trigger": {
|
2021-06-14 15:09:20 +00:00
|
|
|
**trigger_data,
|
2020-11-09 11:04:16 +00:00
|
|
|
"platform": platform_type,
|
2020-08-24 21:01:57 +00:00
|
|
|
"entity_id": entity,
|
|
|
|
"from_state": from_s,
|
|
|
|
"to_state": to_s,
|
|
|
|
"zone": zone_state,
|
|
|
|
"event": event,
|
2020-08-28 15:02:12 +00:00
|
|
|
"description": description,
|
2020-08-24 21:01:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
to_s.context,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
|
2020-07-05 22:31:33 +00:00
|
|
|
return async_track_state_change_event(hass, entity_id, zone_automation_listener)
|