core/homeassistant/components/zone/zone.py

110 lines
3.1 KiB
Python
Raw Normal View History

"""Zone entity and functionality."""
from homeassistant.const import ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE
from homeassistant.helpers.entity import Entity
from homeassistant.loader import bind_hass
from homeassistant.util.async_ import run_callback_threadsafe
from homeassistant.util.location import distance
from .const import DOMAIN
2015-09-30 06:08:37 +00:00
ATTR_PASSIVE = 'passive'
ATTR_RADIUS = 'radius'
STATE = 'zoning'
2016-08-25 16:05:04 +00:00
@bind_hass
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."""
return run_callback_threadsafe(
hass.loop, async_active_zone, hass, latitude, longitude, radius
).result()
@bind_hass
def async_active_zone(hass, latitude, longitude, radius=0):
"""Find the active zone for given latitude, longitude.
This method must be run in the event loop.
"""
# 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.async_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
def in_zone(zone, latitude, longitude, radius=0) -> bool:
"""Test if given latitude, longitude is in given zone.
Async friendly.
"""
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]
class Zone(Entity):
2016-03-08 16:55:57 +00:00
"""Representation of a Zone."""
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."""
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
@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