Add support for zones to Home Assistant
parent
68c2b539ee
commit
5ad27d8cdb
|
@ -30,7 +30,7 @@ import os
|
|||
import threading
|
||||
|
||||
from homeassistant.bootstrap import prepare_setup_platform
|
||||
from homeassistant.components import discovery, group
|
||||
from homeassistant.components import discovery, group, zone
|
||||
from homeassistant.config import load_yaml_config_file
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_per_platform
|
||||
|
@ -40,10 +40,11 @@ import homeassistant.util.dt as dt_util
|
|||
|
||||
from homeassistant.helpers.event import track_utc_time_change
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_PICTURE, DEVICE_DEFAULT_NAME, STATE_HOME, STATE_NOT_HOME)
|
||||
ATTR_ENTITY_PICTURE, ATTR_LATITUDE, ATTR_LONGITUDE, DEVICE_DEFAULT_NAME,
|
||||
STATE_HOME, STATE_NOT_HOME)
|
||||
|
||||
DOMAIN = "device_tracker"
|
||||
DEPENDENCIES = []
|
||||
DEPENDENCIES = ['zone']
|
||||
|
||||
GROUP_NAME_ALL_DEVICES = 'all devices'
|
||||
ENTITY_ID_ALL_DEVICES = group.ENTITY_ID_FORMAT.format('all_devices')
|
||||
|
@ -70,8 +71,6 @@ DEFAULT_HOME_RANGE = 100
|
|||
|
||||
SERVICE_SEE = 'see'
|
||||
|
||||
ATTR_LATITUDE = 'latitude'
|
||||
ATTR_LONGITUDE = 'longitude'
|
||||
ATTR_MAC = 'mac'
|
||||
ATTR_DEV_ID = 'dev_id'
|
||||
ATTR_HOST_NAME = 'host_name'
|
||||
|
@ -366,7 +365,14 @@ class Device(Entity):
|
|||
elif self.location_name:
|
||||
self._state = self.location_name
|
||||
elif self.gps is not None:
|
||||
self._state = STATE_HOME if self.gps_home else STATE_NOT_HOME
|
||||
zone_state = zone.in_zone(self.hass, self.gps[0], self.gps[1])
|
||||
if zone_state is None:
|
||||
self._state = STATE_NOT_HOME
|
||||
elif zone_state.entity_id == zone.ENTITY_ID_HOME:
|
||||
self._state = STATE_HOME
|
||||
else:
|
||||
self._state = zone_state.name
|
||||
|
||||
elif self.stale():
|
||||
self._state = STATE_NOT_HOME
|
||||
self.last_update_home = False
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
""" DO NOT MODIFY. Auto-generated by build_frontend script """
|
||||
VERSION = "e06fe517c5a089673b3f4842100c24a6"
|
||||
VERSION = "8d460d7298e41397b4f94ef103b2a067"
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
|||
Subproject commit d9cbdb428b8eef2e68cb1913f6bd9c013c4302f6
|
||||
Subproject commit b3fc452598f13d3508f4c7329a27b71ffb19d53b
|
|
@ -0,0 +1,128 @@
|
|||
"""
|
||||
homeassistant.components.zone
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Allows defintion of zones in Home Assistant.
|
||||
|
||||
zone School:
|
||||
latitude: 32.8773367
|
||||
longitude: -117.2494053
|
||||
# Optional radius in meters (default: 100)
|
||||
radius: 250
|
||||
# Optional icon to show instead of name
|
||||
# See https://www.google.com/design/icons/
|
||||
# Example: home, work, group-work, shopping-cart
|
||||
icon: group-work
|
||||
|
||||
zone Work:
|
||||
latitude: 32.8753367
|
||||
longitude: -117.2474053
|
||||
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.const import ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE
|
||||
from homeassistant.helpers import extract_domain_configs, generate_entity_id
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util.location import distance
|
||||
|
||||
DOMAIN = "zone"
|
||||
DEPENDENCIES = []
|
||||
ENTITY_ID_FORMAT = 'zone.{}'
|
||||
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
|
||||
STATE = 'zoning'
|
||||
|
||||
ATTR_RADIUS = 'radius'
|
||||
DEFAULT_RADIUS = 100
|
||||
|
||||
ATTR_ICON = 'icon'
|
||||
ICON_HOME = 'home'
|
||||
|
||||
|
||||
def in_zone(hass, latitude, longitude):
|
||||
""" 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
|
||||
in sorted(hass.states.entity_ids(DOMAIN)))
|
||||
|
||||
min_dist = None
|
||||
closest = None
|
||||
|
||||
for zone in zones:
|
||||
zone_dist = distance(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):
|
||||
min_dist = zone_dist
|
||||
closest = zone
|
||||
|
||||
return closest
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
""" Setup zone. """
|
||||
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)
|
||||
|
||||
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,
|
||||
hass.config.longitude, DEFAULT_RADIUS, ICON_HOME)
|
||||
zone.entity_id = ENTITY_ID_HOME
|
||||
zone.update_ha_state()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class Zone(Entity):
|
||||
""" Represents a Zone in Home Assistant. """
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(self, hass, name, latitude, longitude, radius, icon):
|
||||
self.hass = hass
|
||||
self._name = name
|
||||
self.latitude = latitude
|
||||
self.longitude = longitude
|
||||
self.radius = radius
|
||||
self.icon = icon
|
||||
|
||||
def should_poll(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" The state property really does nothing for a zone. """
|
||||
return STATE
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
attr = {
|
||||
ATTR_HIDDEN: True,
|
||||
ATTR_LATITUDE: self.latitude,
|
||||
ATTR_LONGITUDE: self.longitude,
|
||||
ATTR_RADIUS: self.radius,
|
||||
}
|
||||
if self.icon:
|
||||
attr[ATTR_ICON] = self.icon
|
||||
return attr
|
|
@ -101,6 +101,10 @@ ATTR_LAST_TRIP_TIME = "last_tripped_time"
|
|||
# For all entity's, this hold whether or not it should be hidden
|
||||
ATTR_HIDDEN = "hidden"
|
||||
|
||||
# Location of the entity
|
||||
ATTR_LATITUDE = "latitude"
|
||||
ATTR_LONGITUDE = "longitude"
|
||||
|
||||
# #### SERVICES ####
|
||||
SERVICE_HOMEASSISTANT_STOP = "stop"
|
||||
|
||||
|
|
|
@ -446,9 +446,8 @@ class StateMachine(object):
|
|||
|
||||
domain_filter = domain_filter.lower()
|
||||
|
||||
return [state.entity_id for key, state
|
||||
in self._states.items()
|
||||
if util.split_entity_id(key)[0] == domain_filter]
|
||||
return [state.entity_id for state in self._states.values()
|
||||
if state.domain == domain_filter]
|
||||
|
||||
def all(self):
|
||||
""" Returns a list of all states. """
|
||||
|
|
Loading…
Reference in New Issue