Async migration device_tracker (#4406)
* Async migration device_tracker * change location stuff to async * address paulus comments * fix lint & add async discovery listener * address paulus comments v2 * fix tests * fix test_mqtt * fix test_init * fix gps_acc * fix lint * change async_update_stale to callback
This commit is contained in:
1 parent
265232af98
commit
c56f99baaf
7 files changed
+233
-120
No files matched your search
@@ -14,6 +14,7 @@ from homeassistant.const import (
|
||||
CONF_LONGITUDE, CONF_ICON)
|
||||
from homeassistant.helpers import config_per_platform
|
||||
from homeassistant.helpers.entity import Entity, async_generate_entity_id
|
||||
from homeassistant.util.async import run_callback_threadsafe
|
||||
from homeassistant.util.location import distance
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
@@ -51,9 +52,19 @@ PLATFORM_SCHEMA = vol.Schema({
|
||||
|
||||
def active_zone(hass, latitude, longitude, radius=0):
|
||||
"""Find the active zone for given latitude, longitude."""
|
||||
return run_callback_threadsafe(
|
||||
hass.loop, async_active_zone, hass, latitude, longitude, radius
|
||||
).result()
|
||||
|
||||
|
||||
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.entity_ids(DOMAIN)))
|
||||
in sorted(hass.states.async_entity_ids(DOMAIN)))
|
||||
|
||||
min_dist = None
|
||||
closest = None
|
||||
@@ -80,7 +91,10 @@ def active_zone(hass, latitude, longitude, radius=0):
|
||||
|
||||
|
||||
def in_zone(zone, latitude, longitude, radius=0):
|
||||
"""Test if given latitude, longitude is in given zone."""
|
||||
"""Test if given latitude, longitude is in given zone.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
zone_dist = distance(
|
||||
latitude, longitude,
|
||||
zone.attributes[ATTR_LATITUDE], zone.attributes[ATTR_LONGITUDE])
|
||||
|
||||
Reference in new issue
Block a user