core/homeassistant/components/zone.py

168 lines
5.3 KiB
Python
Raw Normal View History

"""
2016-03-07 17:49:31 +00:00
Support for the definition of zones.
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/
"""
import logging
2015-09-30 06:08:37 +00:00
from homeassistant.const import (
2015-11-03 08:20:48 +00:00
ATTR_HIDDEN, ATTR_ICON, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME)
from homeassistant.helpers import extract_domain_configs
from homeassistant.helpers.entity import Entity, generate_entity_id
from homeassistant.util.location import distance
2016-03-06 19:07:30 +00:00
from homeassistant.util import convert
DOMAIN = "zone"
ENTITY_ID_FORMAT = 'zone.{}'
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
STATE = 'zoning'
2015-09-30 06:08:37 +00:00
DEFAULT_NAME = 'Unnamed zone'
ATTR_RADIUS = 'radius'
DEFAULT_RADIUS = 100
2016-01-22 07:53:57 +00:00
ATTR_PASSIVE = 'passive'
DEFAULT_PASSIVE = False
2015-11-03 08:20:48 +00:00
ICON_HOME = 'mdi:home'
ICON_IMPORT = 'mdi:import'
entities = set()
_LOGGER = logging.getLogger(__name__)
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."""
# 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-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):
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]
def setup(hass, config):
2016-03-07 17:49:31 +00:00
"""Setup zone."""
entities = set()
for key in extract_domain_configs(config, DOMAIN):
2015-09-30 06:08:37 +00:00
entries = config[key]
if not isinstance(entries, list):
entries = entries,
for entry in entries:
name = entry.get(CONF_NAME, DEFAULT_NAME)
2016-03-06 19:07:30 +00:00
latitude = convert(entry.get(ATTR_LATITUDE), float)
longitude = convert(entry.get(ATTR_LONGITUDE), float)
radius = convert(entry.get(ATTR_RADIUS, DEFAULT_RADIUS), float)
2015-09-30 06:08:37 +00:00
icon = entry.get(ATTR_ICON)
2016-01-22 07:53:57 +00:00
passive = entry.get(ATTR_PASSIVE, DEFAULT_PASSIVE)
2015-09-30 06:08:37 +00:00
if None in (latitude, longitude):
logging.getLogger(__name__).error(
'Each zone needs a latitude and longitude.')
continue
zone = Zone(hass, name, latitude, longitude, radius, icon,
passive, False)
2015-09-30 06:08:37 +00:00
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name,
entities)
zone.update_ha_state()
entities.add(zone.entity_id)
if ENTITY_ID_HOME not in entities:
zone = Zone(hass, hass.config.location_name, hass.config.latitude,
hass.config.longitude, DEFAULT_RADIUS, ICON_HOME,
False, False)
zone.entity_id = ENTITY_ID_HOME
zone.update_ha_state()
return True
# Add a zone to the existing set
def add_zone(hass, name, latitude, longitude, radius):
"""Add a zone from other components"""
_LOGGER.info("Adding new zone %s", name)
if name not in entities:
zone = Zone(hass, name, latitude, longitude, radius, ICON_IMPORT,
False, True)
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name,
entities)
zone.update_ha_state()
entities.add(zone.entity_id)
else:
_LOGGER.info("Zone already exists")
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
def __init__(self, hass, name, latitude, longitude, radius, icon, passive,
imported):
2016-03-08 16:55:57 +00:00
"""Initialize the zone."""
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
self._imported = imported
@property
def name(self):
2016-03-08 16:55:57 +00:00
"""Return the name of the zone."""
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."""
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
@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 = {
ATTR_HIDDEN: True,
2016-01-22 07:53:57 +00:00
ATTR_LATITUDE: self._latitude,
ATTR_LONGITUDE: self._longitude,
ATTR_RADIUS: self._radius,
}
2016-01-22 07:53:57 +00:00
if self._passive:
data[ATTR_PASSIVE] = self._passive
return data