2015-09-29 07:18:52 +00:00
|
|
|
"""
|
2016-03-07 19:20:07 +00:00
|
|
|
Offer zone automation rules.
|
2015-10-13 19:09:11 +00:00
|
|
|
|
|
|
|
For more details about this automation rule, please refer to the documentation
|
2017-04-06 06:23:02 +00:00
|
|
|
at https://home-assistant.io/docs/automation/trigger/#zone-trigger
|
2015-09-29 07:18:52 +00:00
|
|
|
"""
|
2016-04-04 19:18:58 +00:00
|
|
|
import voluptuous as vol
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
from homeassistant.core import callback
|
2016-05-03 05:05:09 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_EVENT, CONF_ENTITY_ID, CONF_ZONE, MATCH_ALL, CONF_PLATFORM)
|
2016-10-01 08:22:13 +00:00
|
|
|
from homeassistant.helpers.event import async_track_state_change
|
2016-04-28 10:03:57 +00:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
condition, config_validation as cv, location)
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
EVENT_ENTER = 'enter'
|
|
|
|
EVENT_LEAVE = 'leave'
|
2015-09-29 07:18:52 +00:00
|
|
|
DEFAULT_EVENT = EVENT_ENTER
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
TRIGGER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'zone',
|
2016-05-03 05:05:09 +00:00
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_ids,
|
2016-04-04 19:18:58 +00:00
|
|
|
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
|
|
|
|
2018-11-05 08:23:58 +00:00
|
|
|
async def async_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)
|
2016-04-04 19:18:58 +00:00
|
|
|
event = config.get(CONF_EVENT)
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2015-09-29 07:18:52 +00:00
|
|
|
def zone_automation_listener(entity, from_s, to_s):
|
2016-03-07 19:20:07 +00:00
|
|
|
"""Listen for state changes and calls action."""
|
2016-04-28 10:03:57 +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)
|
2016-04-28 10:03:57 +00:00
|
|
|
if from_s:
|
|
|
|
from_match = condition.zone(hass, zone_state, from_s)
|
|
|
|
else:
|
|
|
|
from_match = False
|
|
|
|
to_match = condition.zone(hass, zone_state, to_s)
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2015-11-29 21:49:05 +00:00
|
|
|
# pylint: disable=too-many-boolean-expressions
|
2015-09-29 07:18:52 +00:00
|
|
|
if event == EVENT_ENTER and not from_match and to_match or \
|
|
|
|
event == EVENT_LEAVE and from_match and not to_match:
|
2018-09-04 19:16:24 +00:00
|
|
|
hass.async_run_job(action({
|
2016-04-21 20:59:42 +00:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'zone',
|
|
|
|
'entity_id': entity,
|
|
|
|
'from_state': from_s,
|
|
|
|
'to_state': to_s,
|
|
|
|
'zone': zone_state,
|
2016-05-03 05:05:09 +00:00
|
|
|
'event': event,
|
2016-04-21 20:59:42 +00:00
|
|
|
},
|
2018-09-04 19:16:24 +00:00
|
|
|
}, context=to_s.context))
|
2015-09-29 07:18:52 +00:00
|
|
|
|
2016-10-01 08:22:13 +00:00
|
|
|
return async_track_state_change(hass, entity_id, zone_automation_listener,
|
|
|
|
MATCH_ALL, MATCH_ALL)
|