core/homeassistant/components/zone/zone.py

72 lines
1.8 KiB
Python
Raw Normal View History

"""Zone entity and functionality."""
from typing import cast
from homeassistant.const import ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE
from homeassistant.core import State
from homeassistant.helpers.entity import Entity
from homeassistant.util.location import distance
from .const import ATTR_PASSIVE, ATTR_RADIUS
2019-07-31 19:25:30 +00:00
STATE = "zoning"
2016-08-25 16:05:04 +00:00
# mypy: allow-untyped-defs
def in_zone(zone: State, latitude: float, longitude: float, radius: float = 0) -> bool:
"""Test if given latitude, longitude is in given zone.
Async friendly.
"""
2015-10-02 15:16:53 +00:00
zone_dist = distance(
2019-07-31 19:25:30 +00:00
latitude,
longitude,
zone.attributes[ATTR_LATITUDE],
zone.attributes[ATTR_LONGITUDE],
)
2015-10-02 15:16:53 +00:00
if zone_dist is None or zone.attributes[ATTR_RADIUS] is None:
return False
return zone_dist - radius < cast(float, zone.attributes[ATTR_RADIUS])
2015-10-02 15:16:53 +00:00
class Zone(Entity):
2016-03-08 16:55:57 +00:00
"""Representation of a Zone."""
name = None
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
self.latitude = latitude
self.longitude = longitude
2016-01-22 07:53:57 +00:00
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 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,
ATTR_LATITUDE: self.latitude,
ATTR_LONGITUDE: self.longitude,
2016-01-22 07:53:57 +00:00
ATTR_RADIUS: self._radius,
}
2016-01-22 07:53:57 +00:00
if self._passive:
data[ATTR_PASSIVE] = self._passive
return data