core/homeassistant/components/zone/trigger.py

86 lines
2.6 KiB
Python
Raw Normal View History

"""Offer zone automation rules."""
import voluptuous as vol
2015-09-29 07:18:52 +00:00
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
CONF_ENTITY_ID,
CONF_EVENT,
CONF_PLATFORM,
CONF_ZONE,
)
from homeassistant.core import HassJob, callback
2019-07-31 19:25:30 +00:00
from homeassistant.helpers import condition, config_validation as cv, location
from homeassistant.helpers.event import async_track_state_change_event
# mypy: allow-untyped-defs, no-check-untyped-defs
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
_EVENT_DESCRIPTION = {EVENT_ENTER: "entering", EVENT_LEAVE: "leaving"}
2019-07-31 19:25:30 +00:00
TRIGGER_SCHEMA = vol.Schema(
{
vol.Required(CONF_PLATFORM): "zone",
vol.Required(CONF_ENTITY_ID): cv.entity_ids,
vol.Required(CONF_ZONE): cv.entity_id,
vol.Required(CONF_EVENT, default=DEFAULT_EVENT): vol.Any(
EVENT_ENTER, EVENT_LEAVE
),
}
)
2015-09-29 07:18:52 +00:00
async def async_attach_trigger(hass, config, action, automation_info):
2016-03-07 16:14:55 +00:00
"""Listen for state changes based on configuration."""
2015-09-29 07:18:52 +00:00
entity_id = config.get(CONF_ENTITY_ID)
zone_entity_id = config.get(CONF_ZONE)
event = config.get(CONF_EVENT)
job = HassJob(action)
2015-09-29 07:18:52 +00:00
@callback
def zone_automation_listener(zone_event):
2016-03-07 19:20:07 +00:00
"""Listen for state changes and calls action."""
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
zone_state = hass.states.get(zone_entity_id)
from_match = condition.zone(hass, zone_state, from_s) if from_s else False
to_match = condition.zone(hass, zone_state, to_s)
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
):
description = f"{entity} {_EVENT_DESCRIPTION[event]} {zone_state.attributes[ATTR_FRIENDLY_NAME]}"
hass.async_run_hass_job(
job,
{
"trigger": {
"platform": "zone",
"entity_id": entity,
"from_state": from_s,
"to_state": to_s,
"zone": zone_state,
"event": event,
"description": description,
}
},
to_s.context,
2019-07-31 19:25:30 +00:00
)
return async_track_state_change_event(hass, entity_id, zone_automation_listener)