2015-09-29 00:18:52 -07:00
|
|
|
"""
|
2016-03-07 20:20:07 +01:00
|
|
|
Offer zone automation rules.
|
2015-10-13 21:09:11 +02:00
|
|
|
|
|
|
|
For more details about this automation rule, please refer to the documentation
|
2015-11-09 13:12:18 +01:00
|
|
|
at https://home-assistant.io/components/automation/#zone-trigger
|
2015-09-29 00:18:52 -07:00
|
|
|
"""
|
2016-04-04 12:18:58 -07:00
|
|
|
import voluptuous as vol
|
2015-09-29 00:18:52 -07:00
|
|
|
|
|
|
|
from homeassistant.components import zone
|
2015-09-29 23:08:37 -07:00
|
|
|
from homeassistant.const import (
|
2016-04-04 12:18:58 -07:00
|
|
|
ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE, MATCH_ALL, CONF_PLATFORM)
|
2016-02-18 21:27:50 -08:00
|
|
|
from homeassistant.helpers.event import track_state_change
|
2016-04-04 12:18:58 -07:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-29 00:18:52 -07:00
|
|
|
|
|
|
|
CONF_ENTITY_ID = "entity_id"
|
|
|
|
CONF_ZONE = "zone"
|
|
|
|
CONF_EVENT = "event"
|
|
|
|
EVENT_ENTER = "enter"
|
|
|
|
EVENT_LEAVE = "leave"
|
|
|
|
DEFAULT_EVENT = EVENT_ENTER
|
|
|
|
|
2016-04-04 12:18:58 -07:00
|
|
|
TRIGGER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'zone',
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
|
|
|
vol.Required(CONF_ZONE): cv.entity_id,
|
|
|
|
vol.Required(CONF_EVENT, default=DEFAULT_EVENT):
|
|
|
|
vol.Any(EVENT_ENTER, EVENT_LEAVE),
|
|
|
|
})
|
|
|
|
|
|
|
|
IF_ACTION_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'zone',
|
|
|
|
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
|
|
|
vol.Required(CONF_ZONE): cv.entity_id,
|
|
|
|
})
|
|
|
|
|
2015-09-29 00:18:52 -07:00
|
|
|
|
|
|
|
def trigger(hass, config, action):
|
2016-03-07 17:14:55 +01:00
|
|
|
"""Listen for state changes based on configuration."""
|
2015-09-29 00:18:52 -07:00
|
|
|
entity_id = config.get(CONF_ENTITY_ID)
|
|
|
|
zone_entity_id = config.get(CONF_ZONE)
|
2016-04-04 12:18:58 -07:00
|
|
|
event = config.get(CONF_EVENT)
|
2015-09-29 00:18:52 -07:00
|
|
|
|
|
|
|
def zone_automation_listener(entity, from_s, to_s):
|
2016-03-07 20:20:07 +01:00
|
|
|
"""Listen for state changes and calls action."""
|
2015-09-29 00:18:52 -07:00
|
|
|
if from_s and None in (from_s.attributes.get(ATTR_LATITUDE),
|
2015-10-02 08:16:53 -07:00
|
|
|
from_s.attributes.get(ATTR_LONGITUDE)) or \
|
|
|
|
None in (to_s.attributes.get(ATTR_LATITUDE),
|
|
|
|
to_s.attributes.get(ATTR_LONGITUDE)):
|
2015-09-29 00:18:52 -07:00
|
|
|
return
|
|
|
|
|
2016-04-21 13:59:42 -07:00
|
|
|
zone_state = hass.states.get(zone_entity_id)
|
|
|
|
from_match = _in_zone(hass, zone_state, from_s) if from_s else None
|
|
|
|
to_match = _in_zone(hass, zone_state, to_s)
|
2015-09-29 00:18:52 -07:00
|
|
|
|
2015-11-29 13:49:05 -08:00
|
|
|
# pylint: disable=too-many-boolean-expressions
|
2015-09-29 00:18:52 -07:00
|
|
|
if event == EVENT_ENTER and not from_match and to_match or \
|
|
|
|
event == EVENT_LEAVE and from_match and not to_match:
|
2016-04-21 13:59:42 -07:00
|
|
|
action({
|
|
|
|
'trigger': {
|
|
|
|
'platform': 'zone',
|
|
|
|
'entity_id': entity,
|
|
|
|
'from_state': from_s,
|
|
|
|
'to_state': to_s,
|
|
|
|
'zone': zone_state,
|
|
|
|
},
|
|
|
|
})
|
2015-09-29 00:18:52 -07:00
|
|
|
|
|
|
|
track_state_change(
|
|
|
|
hass, entity_id, zone_automation_listener, MATCH_ALL, MATCH_ALL)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def if_action(hass, config):
|
2016-03-07 20:20:07 +01:00
|
|
|
"""Wrap action method with zone based condition."""
|
2015-09-29 00:18:52 -07:00
|
|
|
entity_id = config.get(CONF_ENTITY_ID)
|
|
|
|
zone_entity_id = config.get(CONF_ZONE)
|
|
|
|
|
2016-04-21 13:59:42 -07:00
|
|
|
def if_in_zone(variables):
|
2016-03-07 17:14:55 +01:00
|
|
|
"""Test if condition."""
|
2016-04-21 13:59:42 -07:00
|
|
|
zone_state = hass.states.get(zone_entity_id)
|
|
|
|
return _in_zone(hass, zone_state, hass.states.get(entity_id))
|
2015-09-29 00:18:52 -07:00
|
|
|
|
|
|
|
return if_in_zone
|
2015-09-29 23:08:37 -07:00
|
|
|
|
|
|
|
|
2016-04-21 13:59:42 -07:00
|
|
|
def _in_zone(hass, zone_state, state):
|
2016-03-07 17:14:55 +01:00
|
|
|
"""Check if state is in zone."""
|
2015-10-02 08:16:53 -07:00
|
|
|
if not state or None in (state.attributes.get(ATTR_LATITUDE),
|
|
|
|
state.attributes.get(ATTR_LONGITUDE)):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return zone_state and zone.in_zone(
|
|
|
|
zone_state, state.attributes.get(ATTR_LATITUDE),
|
2015-09-29 23:08:37 -07:00
|
|
|
state.attributes.get(ATTR_LONGITUDE),
|
|
|
|
state.attributes.get(ATTR_GPS_ACCURACY, 0))
|