2015-09-29 06:13:13 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support for the definition of zones.
|
2015-09-29 06:13:13 +00:00
|
|
|
|
2015-11-09 12:12:18 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/zone/
|
2015-09-29 06:13:13 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-09-14 06:13:10 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-09-30 06:08:37 +00:00
|
|
|
from homeassistant.const import (
|
2016-09-14 06:13:10 +00:00
|
|
|
ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME, CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE, CONF_ICON)
|
2016-09-24 07:04:03 +00:00
|
|
|
from homeassistant.helpers import config_per_platform
|
2016-01-24 07:00:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity, generate_entity_id
|
2015-09-29 06:13:13 +00:00
|
|
|
from homeassistant.util.location import distance
|
2016-09-14 06:13:10 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-29 06:13:13 +00:00
|
|
|
|
2016-09-14 06:13:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-09-30 06:08:37 +00:00
|
|
|
|
2016-09-14 06:13:10 +00:00
|
|
|
ATTR_PASSIVE = 'passive'
|
2015-09-29 06:13:13 +00:00
|
|
|
ATTR_RADIUS = 'radius'
|
|
|
|
|
2016-09-14 06:13:10 +00:00
|
|
|
CONF_PASSIVE = 'passive'
|
|
|
|
CONF_RADIUS = 'radius'
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'Unnamed zone'
|
2016-01-22 07:53:57 +00:00
|
|
|
DEFAULT_PASSIVE = False
|
2016-09-14 06:13:10 +00:00
|
|
|
DEFAULT_RADIUS = 100
|
|
|
|
DOMAIN = 'zone'
|
|
|
|
|
|
|
|
ENTITY_ID_FORMAT = 'zone.{}'
|
|
|
|
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
|
2016-01-22 07:53:57 +00:00
|
|
|
|
2015-11-03 08:20:48 +00:00
|
|
|
ICON_HOME = 'mdi:home'
|
2016-08-12 09:18:28 +00:00
|
|
|
ICON_IMPORT = 'mdi:import'
|
2015-09-29 06:13:13 +00:00
|
|
|
|
2016-09-14 06:13:10 +00:00
|
|
|
STATE = 'zoning'
|
|
|
|
|
2016-09-16 02:40:18 +00:00
|
|
|
# The config that zone accepts is the same as if it has platforms.
|
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_LATITUDE): cv.latitude,
|
|
|
|
vol.Required(CONF_LONGITUDE): cv.longitude,
|
|
|
|
vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_PASSIVE, default=DEFAULT_PASSIVE): cv.boolean,
|
|
|
|
vol.Optional(CONF_ICON): cv.icon,
|
|
|
|
})
|
2015-09-29 06:13:13 +00:00
|
|
|
|
2016-08-25 16:05:04 +00:00
|
|
|
|
2015-10-02 15:16:53 +00:00
|
|
|
def active_zone(hass, latitude, longitude, radius=0):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Find the active zone for given latitude, longitude."""
|
2015-09-29 06:13:13 +00:00
|
|
|
# Sort entity IDs so that we are deterministic if equal distance to 2 zones
|
|
|
|
zones = (hass.states.get(entity_id) for entity_id
|
|
|
|
in sorted(hass.states.entity_ids(DOMAIN)))
|
|
|
|
|
|
|
|
min_dist = None
|
|
|
|
closest = None
|
|
|
|
|
|
|
|
for zone in zones:
|
2016-01-22 07:53:57 +00:00
|
|
|
if zone.attributes.get(ATTR_PASSIVE):
|
|
|
|
continue
|
|
|
|
|
2015-10-02 15:16:53 +00:00
|
|
|
zone_dist = distance(
|
|
|
|
latitude, longitude,
|
|
|
|
zone.attributes[ATTR_LATITUDE], zone.attributes[ATTR_LONGITUDE])
|
2015-09-29 06:13:13 +00:00
|
|
|
|
2015-10-02 15:16:53 +00:00
|
|
|
within_zone = zone_dist - radius < zone.attributes[ATTR_RADIUS]
|
|
|
|
closer_zone = closest is None or zone_dist < min_dist
|
|
|
|
smaller_zone = (zone_dist == min_dist and
|
|
|
|
zone.attributes[ATTR_RADIUS] <
|
|
|
|
closest.attributes[ATTR_RADIUS])
|
|
|
|
|
|
|
|
if within_zone and (closer_zone or smaller_zone):
|
2015-09-29 06:13:13 +00:00
|
|
|
min_dist = zone_dist
|
|
|
|
closest = zone
|
|
|
|
|
|
|
|
return closest
|
|
|
|
|
|
|
|
|
2015-10-02 15:16:53 +00:00
|
|
|
def in_zone(zone, latitude, longitude, radius=0):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Test if given latitude, longitude is in given zone."""
|
2015-10-02 15:16:53 +00:00
|
|
|
zone_dist = distance(
|
|
|
|
latitude, longitude,
|
|
|
|
zone.attributes[ATTR_LATITUDE], zone.attributes[ATTR_LONGITUDE])
|
|
|
|
|
|
|
|
return zone_dist - radius < zone.attributes[ATTR_RADIUS]
|
|
|
|
|
|
|
|
|
2015-09-29 06:13:13 +00:00
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Setup zone."""
|
2016-08-25 16:03:07 +00:00
|
|
|
entities = set()
|
2016-09-24 07:04:03 +00:00
|
|
|
for _, entry in config_per_platform(config, DOMAIN):
|
|
|
|
name = entry.get(CONF_NAME)
|
|
|
|
zone = Zone(hass, name, entry[CONF_LATITUDE], entry[CONF_LONGITUDE],
|
|
|
|
entry.get(CONF_RADIUS), entry.get(CONF_ICON),
|
|
|
|
entry.get(CONF_PASSIVE))
|
|
|
|
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, entities)
|
|
|
|
zone.update_ha_state()
|
|
|
|
entities.add(zone.entity_id)
|
2015-09-29 06:13:13 +00:00
|
|
|
|
|
|
|
if ENTITY_ID_HOME not in entities:
|
2016-08-26 14:22:08 +00:00
|
|
|
zone = Zone(hass, hass.config.location_name,
|
|
|
|
hass.config.latitude, hass.config.longitude,
|
2016-09-24 07:04:03 +00:00
|
|
|
DEFAULT_RADIUS, ICON_HOME, False)
|
2015-09-29 06:13:13 +00:00
|
|
|
zone.entity_id = ENTITY_ID_HOME
|
|
|
|
zone.update_ha_state()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2016-08-25 16:05:04 +00:00
|
|
|
|
2015-09-29 06:13:13 +00:00
|
|
|
class Zone(Entity):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Representation of a Zone."""
|
|
|
|
|
2016-01-22 07:53:57 +00:00
|
|
|
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
2016-09-24 07:04:03 +00:00
|
|
|
def __init__(self, hass, name, latitude, longitude, radius, icon, passive):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the zone."""
|
2015-09-29 06:13:13 +00:00
|
|
|
self.hass = hass
|
|
|
|
self._name = name
|
2016-01-22 07:53:57 +00:00
|
|
|
self._latitude = latitude
|
|
|
|
self._longitude = longitude
|
|
|
|
self._radius = radius
|
2015-11-03 08:20:48 +00:00
|
|
|
self._icon = icon
|
2016-01-22 07:53:57 +00:00
|
|
|
self._passive = passive
|
2015-09-29 06:13:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the name of the zone."""
|
2015-09-29 06:13:13 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Return the state property really does nothing for a zone."""
|
2015-09-29 06:13:13 +00:00
|
|
|
return STATE
|
|
|
|
|
2015-11-03 08:20:48 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Return the icon if any."""
|
2015-11-03 08:20:48 +00:00
|
|
|
return self._icon
|
|
|
|
|
2015-09-29 06:13:13 +00:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the state attributes of the zone."""
|
2016-01-22 07:53:57 +00:00
|
|
|
data = {
|
2015-09-29 06:13:13 +00:00
|
|
|
ATTR_HIDDEN: True,
|
2016-01-22 07:53:57 +00:00
|
|
|
ATTR_LATITUDE: self._latitude,
|
|
|
|
ATTR_LONGITUDE: self._longitude,
|
|
|
|
ATTR_RADIUS: self._radius,
|
2015-09-29 06:13:13 +00:00
|
|
|
}
|
2016-01-22 07:53:57 +00:00
|
|
|
if self._passive:
|
|
|
|
data[ATTR_PASSIVE] = self._passive
|
|
|
|
return data
|