core/homeassistant/components/automation/zone.py

86 lines
2.8 KiB
Python
Raw Normal View History

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
2015-11-09 12:12:18 +00:00
at https://home-assistant.io/components/automation/#zone-trigger
2015-09-29 07:18:52 +00:00
"""
import logging
from homeassistant.components import zone
2015-09-30 06:08:37 +00:00
from homeassistant.const import (
ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE, MATCH_ALL)
2016-02-19 05:27:50 +00:00
from homeassistant.helpers.event import track_state_change
2015-09-29 07:18:52 +00:00
CONF_ENTITY_ID = "entity_id"
CONF_ZONE = "zone"
CONF_EVENT = "event"
EVENT_ENTER = "enter"
EVENT_LEAVE = "leave"
DEFAULT_EVENT = EVENT_ENTER
def trigger(hass, config, action):
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)
if entity_id is None or zone_entity_id is None:
logging.getLogger(__name__).error(
"Missing trigger configuration key %s or %s", CONF_ENTITY_ID,
CONF_ZONE)
return False
event = config.get(CONF_EVENT, DEFAULT_EVENT)
def zone_automation_listener(entity, from_s, to_s):
2016-03-07 19:20:07 +00:00
"""Listen for state changes and calls action."""
2015-09-29 07:18:52 +00:00
if from_s and None in (from_s.attributes.get(ATTR_LATITUDE),
2015-10-02 15:16:53 +00: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 07:18:52 +00:00
return
2015-10-02 15:16:53 +00:00
from_match = _in_zone(hass, zone_entity_id, from_s) if from_s else None
to_match = _in_zone(hass, zone_entity_id, 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:
action()
track_state_change(
hass, entity_id, zone_automation_listener, MATCH_ALL, MATCH_ALL)
return True
def if_action(hass, config):
2016-03-07 19:20:07 +00:00
"""Wrap action method with zone based condition."""
2015-09-29 07:18:52 +00:00
entity_id = config.get(CONF_ENTITY_ID)
zone_entity_id = config.get(CONF_ZONE)
if entity_id is None or zone_entity_id is None:
logging.getLogger(__name__).error(
"Missing condition configuration key %s or %s", CONF_ENTITY_ID,
CONF_ZONE)
return False
def if_in_zone():
2016-03-07 16:14:55 +00:00
"""Test if condition."""
2015-10-02 15:16:53 +00:00
return _in_zone(hass, zone_entity_id, hass.states.get(entity_id))
2015-09-29 07:18:52 +00:00
return if_in_zone
2015-09-30 06:08:37 +00:00
2015-10-02 15:16:53 +00:00
def _in_zone(hass, zone_entity_id, state):
2016-03-07 16:14:55 +00:00
"""Check if state is in zone."""
2015-10-02 15:16:53 +00:00
if not state or None in (state.attributes.get(ATTR_LATITUDE),
state.attributes.get(ATTR_LONGITUDE)):
return False
zone_state = hass.states.get(zone_entity_id)
return zone_state and zone.in_zone(
zone_state, state.attributes.get(ATTR_LATITUDE),
2015-09-30 06:08:37 +00:00
state.attributes.get(ATTR_LONGITUDE),
state.attributes.get(ATTR_GPS_ACCURACY, 0))