Update config zones
This commit is contained in:
parent
2eb36c18bd
commit
1dc9bfdf73
6 changed files with 53 additions and 40 deletions
|
@ -8,7 +8,8 @@ import logging
|
||||||
|
|
||||||
from homeassistant.components import zone
|
from homeassistant.components import zone
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import track_state_change
|
||||||
from homeassistant.const import MATCH_ALL, ATTR_LATITUDE, ATTR_LONGITUDE
|
from homeassistant.const import (
|
||||||
|
ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE, MATCH_ALL)
|
||||||
|
|
||||||
|
|
||||||
CONF_ENTITY_ID = "entity_id"
|
CONF_ENTITY_ID = "entity_id"
|
||||||
|
@ -42,16 +43,8 @@ def trigger(hass, config, action):
|
||||||
to_s.attributes.get(ATTR_LONGITUDE)):
|
to_s.attributes.get(ATTR_LONGITUDE)):
|
||||||
return
|
return
|
||||||
|
|
||||||
if from_s:
|
from_zone = _in_zone(hass, from_s) if from_s else None
|
||||||
from_zone = zone.in_zone(
|
to_zone = _in_zone(hass, to_s)
|
||||||
hass, from_s.attributes.get(ATTR_LATITUDE),
|
|
||||||
from_s.attributes.get(ATTR_LONGITUDE))
|
|
||||||
else:
|
|
||||||
from_zone = None
|
|
||||||
|
|
||||||
to_zone = zone.in_zone(hass, to_s.attributes.get(ATTR_LATITUDE),
|
|
||||||
to_s.attributes.get(ATTR_LONGITUDE))
|
|
||||||
|
|
||||||
from_match = from_zone and from_zone.entity_id == zone_entity_id
|
from_match = from_zone and from_zone.entity_id == zone_entity_id
|
||||||
to_match = to_zone and to_zone.entity_id == zone_entity_id
|
to_match = to_zone and to_zone.entity_id == zone_entity_id
|
||||||
|
|
||||||
|
@ -80,13 +73,20 @@ def if_action(hass, config):
|
||||||
""" Test if condition. """
|
""" Test if condition. """
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
|
|
||||||
if None in (state.attributes.get(ATTR_LATITUDE),
|
if not state or None in (state.attributes.get(ATTR_LATITUDE),
|
||||||
state.attributes.get(ATTR_LONGITUDE)):
|
state.attributes.get(ATTR_LONGITUDE)):
|
||||||
return
|
return
|
||||||
|
|
||||||
cur_zone = zone.in_zone(hass, state.attributes.get(ATTR_LATITUDE),
|
cur_zone = _in_zone(hass, state)
|
||||||
state.attributes.get(ATTR_LONGITUDE))
|
|
||||||
|
|
||||||
return cur_zone and cur_zone.entity_id == zone_entity_id
|
return cur_zone and cur_zone.entity_id == zone_entity_id
|
||||||
|
|
||||||
return if_in_zone
|
return if_in_zone
|
||||||
|
|
||||||
|
|
||||||
|
def _in_zone(hass, state):
|
||||||
|
""" Check if state is in zone. """
|
||||||
|
return zone.in_zone(
|
||||||
|
hass, state.attributes.get(ATTR_LATITUDE),
|
||||||
|
state.attributes.get(ATTR_LONGITUDE),
|
||||||
|
state.attributes.get(ATTR_GPS_ACCURACY, 0))
|
||||||
|
|
|
@ -40,8 +40,8 @@ import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from homeassistant.helpers.event import track_utc_time_change
|
from homeassistant.helpers.event import track_utc_time_change
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_PICTURE, ATTR_LATITUDE, ATTR_LONGITUDE, DEVICE_DEFAULT_NAME,
|
ATTR_ENTITY_PICTURE, ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE,
|
||||||
STATE_HOME, STATE_NOT_HOME)
|
DEVICE_DEFAULT_NAME, STATE_HOME, STATE_NOT_HOME)
|
||||||
|
|
||||||
DOMAIN = "device_tracker"
|
DOMAIN = "device_tracker"
|
||||||
DEPENDENCIES = ['zone']
|
DEPENDENCIES = ['zone']
|
||||||
|
@ -76,7 +76,6 @@ ATTR_DEV_ID = 'dev_id'
|
||||||
ATTR_HOST_NAME = 'host_name'
|
ATTR_HOST_NAME = 'host_name'
|
||||||
ATTR_LOCATION_NAME = 'location_name'
|
ATTR_LOCATION_NAME = 'location_name'
|
||||||
ATTR_GPS = 'gps'
|
ATTR_GPS = 'gps'
|
||||||
ATTR_GPS_ACCURACY = 'gps_accuracy'
|
|
||||||
ATTR_BATTERY = 'battery'
|
ATTR_BATTERY = 'battery'
|
||||||
|
|
||||||
DISCOVERY_PLATFORMS = {
|
DISCOVERY_PLATFORMS = {
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit b3fc452598f13d3508f4c7329a27b71ffb19d53b
|
Subproject commit 4dba97292744a32146e35e36031dbca59eb62733
|
|
@ -4,7 +4,8 @@ homeassistant.components.zone
|
||||||
|
|
||||||
Allows defintion of zones in Home Assistant.
|
Allows defintion of zones in Home Assistant.
|
||||||
|
|
||||||
zone School:
|
zone:
|
||||||
|
name: School
|
||||||
latitude: 32.8773367
|
latitude: 32.8773367
|
||||||
longitude: -117.2494053
|
longitude: -117.2494053
|
||||||
# Optional radius in meters (default: 100)
|
# Optional radius in meters (default: 100)
|
||||||
|
@ -14,14 +15,16 @@ zone School:
|
||||||
# Example: home, work, group-work, shopping-cart
|
# Example: home, work, group-work, shopping-cart
|
||||||
icon: group-work
|
icon: group-work
|
||||||
|
|
||||||
zone Work:
|
zone:
|
||||||
|
name: Work
|
||||||
latitude: 32.8753367
|
latitude: 32.8753367
|
||||||
longitude: -117.2474053
|
longitude: -117.2474053
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import logging
|
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 import extract_domain_configs, generate_entity_id
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util.location import distance
|
from homeassistant.util.location import distance
|
||||||
|
@ -32,6 +35,8 @@ ENTITY_ID_FORMAT = 'zone.{}'
|
||||||
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
|
ENTITY_ID_HOME = ENTITY_ID_FORMAT.format('home')
|
||||||
STATE = 'zoning'
|
STATE = 'zoning'
|
||||||
|
|
||||||
|
DEFAULT_NAME = 'Unnamed zone'
|
||||||
|
|
||||||
ATTR_RADIUS = 'radius'
|
ATTR_RADIUS = 'radius'
|
||||||
DEFAULT_RADIUS = 100
|
DEFAULT_RADIUS = 100
|
||||||
|
|
||||||
|
@ -39,7 +44,7 @@ ATTR_ICON = 'icon'
|
||||||
ICON_HOME = 'home'
|
ICON_HOME = 'home'
|
||||||
|
|
||||||
|
|
||||||
def in_zone(hass, latitude, longitude):
|
def in_zone(hass, latitude, longitude, radius=0):
|
||||||
""" Find the zone for given latitude, longitude. """
|
""" Find the zone for given latitude, longitude. """
|
||||||
# Sort entity IDs so that we are deterministic if equal distance to 2 zones
|
# Sort entity IDs so that we are deterministic if equal distance to 2 zones
|
||||||
zones = (hass.states.get(entity_id) for entity_id
|
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_LATITUDE],
|
||||||
zone.attributes[ATTR_LONGITUDE])
|
zone.attributes[ATTR_LONGITUDE])
|
||||||
|
|
||||||
if zone_dist < zone.attributes[ATTR_RADIUS] and (closest is None or
|
if zone_dist - radius < zone.attributes[ATTR_RADIUS] and \
|
||||||
zone_dist < min_dist):
|
(closest is None or zone_dist < min_dist):
|
||||||
min_dist = zone_dist
|
min_dist = zone_dist
|
||||||
closest = zone
|
closest = zone
|
||||||
|
|
||||||
|
@ -66,12 +71,16 @@ def setup(hass, config):
|
||||||
entities = set()
|
entities = set()
|
||||||
|
|
||||||
for key in extract_domain_configs(config, DOMAIN):
|
for key in extract_domain_configs(config, DOMAIN):
|
||||||
conf = config[key]
|
entries = config[key]
|
||||||
name = key.split(' ')[1]
|
if not isinstance(entries, list):
|
||||||
latitude = conf.get(ATTR_LATITUDE)
|
entries = entries,
|
||||||
longitude = conf.get(ATTR_LONGITUDE)
|
|
||||||
radius = conf.get(ATTR_RADIUS, DEFAULT_RADIUS)
|
for entry in entries:
|
||||||
icon = conf.get(ATTR_ICON)
|
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)
|
||||||
|
|
||||||
if None in (latitude, longitude):
|
if None in (latitude, longitude):
|
||||||
logging.getLogger(__name__).error(
|
logging.getLogger(__name__).error(
|
||||||
|
@ -79,7 +88,8 @@ def setup(hass, config):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
zone = Zone(hass, name, latitude, longitude, radius, icon)
|
zone = Zone(hass, name, latitude, longitude, radius, icon)
|
||||||
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, entities)
|
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name,
|
||||||
|
entities)
|
||||||
zone.update_ha_state()
|
zone.update_ha_state()
|
||||||
entities.add(zone.entity_id)
|
entities.add(zone.entity_id)
|
||||||
|
|
||||||
|
|
|
@ -105,6 +105,9 @@ ATTR_HIDDEN = "hidden"
|
||||||
ATTR_LATITUDE = "latitude"
|
ATTR_LATITUDE = "latitude"
|
||||||
ATTR_LONGITUDE = "longitude"
|
ATTR_LONGITUDE = "longitude"
|
||||||
|
|
||||||
|
# Accuracy of location in meters
|
||||||
|
ATTR_GPS_ACCURACY = 'gps_accuracy'
|
||||||
|
|
||||||
# #### SERVICES ####
|
# #### SERVICES ####
|
||||||
SERVICE_HOMEASSISTANT_STOP = "stop"
|
SERVICE_HOMEASSISTANT_STOP = "stop"
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,8 @@ class TestAutomationEvent(unittest.TestCase):
|
||||||
def setUp(self): # pylint: disable=invalid-name
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
zone.setup(self.hass, {
|
zone.setup(self.hass, {
|
||||||
'zone test': {
|
'zone': {
|
||||||
|
'name': 'test',
|
||||||
'latitude': 32.880837,
|
'latitude': 32.880837,
|
||||||
'longitude': -117.237561,
|
'longitude': -117.237561,
|
||||||
'radius': 250,
|
'radius': 250,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue