Update config zones

This commit is contained in:
Paulus Schoutsen committed 2015-09-29 23:08:37 -07:00
1 parent 2eb36c18bd
commit 1dc9bfdf73
6 files changed
+53 -40

No files matched your search

+30 -20
View File
@@ -4,7 +4,8 @@ homeassistant.components.zone
Allows defintion of zones in Home Assistant.
zone School:
zone:
name: School
latitude: 32.8773367
longitude: -117.2494053
# Optional radius in meters (default: 100)
@@ -14,14 +15,16 @@ zone School:
# Example: home, work, group-work, shopping-cart
icon: group-work
zone Work:
zone:
name: Work
latitude: 32.8753367
longitude: -117.2474053
"""
import logging
from homeassistant.const import ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE
from homeassistant.const import (
ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME)
from homeassistant.helpers import extract_domain_configs, generate_entity_id
from homeassistant.helpers.entity import Entity
from homeassistant.util.location import distance
@@ -32,6 +35,8 @@ ENTITY_ID_FORMAT = 'zone.{}'
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
STATE = 'zoning'
DEFAULT_NAME = 'Unnamed zone'
ATTR_RADIUS = 'radius'
DEFAULT_RADIUS = 100
@@ -39,7 +44,7 @@ ATTR_ICON = 'icon'
ICON_HOME = 'home'
def in_zone(hass, latitude, longitude):
def in_zone(hass, latitude, longitude, radius=0):
""" Find the 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
@@ -53,8 +58,8 @@ def in_zone(hass, latitude, longitude):
zone.attributes[ATTR_LATITUDE],
zone.attributes[ATTR_LONGITUDE])
if zone_dist < zone.attributes[ATTR_RADIUS] and (closest is None or
zone_dist < min_dist):
if zone_dist - radius < zone.attributes[ATTR_RADIUS] and \
(closest is None or zone_dist < min_dist):
min_dist = zone_dist
closest = zone
@@ -66,22 +71,27 @@ def setup(hass, config):
entities = set()
for key in extract_domain_configs(config, DOMAIN):
conf = config[key]
name = key.split(' ')[1]
latitude = conf.get(ATTR_LATITUDE)
longitude = conf.get(ATTR_LONGITUDE)
radius = conf.get(ATTR_RADIUS, DEFAULT_RADIUS)
icon = conf.get(ATTR_ICON)
entries = config[key]
if not isinstance(entries, list):
entries = entries,
if None in (latitude, longitude):
logging.getLogger(__name__).error(
'Each zone needs a latitude and longitude.')
continue
for entry in entries:
name = entry.get(CONF_NAME, DEFAULT_NAME)
latitude = entry.get(ATTR_LATITUDE)
longitude = entry.get(ATTR_LONGITUDE)
radius = entry.get(ATTR_RADIUS, DEFAULT_RADIUS)
icon = entry.get(ATTR_ICON)
zone = Zone(hass, name, latitude, longitude, radius, icon)
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, entities)
zone.update_ha_state()
entities.add(zone.entity_id)
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)
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,