Config entry device tracker (#24040)

* Move zone helpers to zone root

* Add config entry support to device tracker

* Convert Geofency

* Convert GPSLogger

* Track unsub per entry

* Convert locative

* Migrate OwnTracks

* Lint

* location -> latitude, longitude props

* Lint

* lint

* Fix test
This commit is contained in:
Paulus Schoutsen 2019-05-25 13:34:53 -07:00 committed by GitHub
parent 144b530045
commit e6d7f6ed71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 946 additions and 517 deletions

View file

@ -1,35 +1,90 @@
"""Support for the Locative platform."""
import logging
from homeassistant.components.device_tracker import (
DOMAIN as DEVICE_TRACKER_DOMAIN)
from homeassistant.core import callback
from homeassistant.components.device_tracker import SOURCE_TYPE_GPS
from homeassistant.components.device_tracker.config_entry import (
DeviceTrackerEntity
)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util import slugify
from . import DOMAIN as LOCATIVE_DOMAIN, TRACKER_UPDATE
from . import DOMAIN as LT_DOMAIN, TRACKER_UPDATE
_LOGGER = logging.getLogger(__name__)
DATA_KEY = '{}.{}'.format(LOCATIVE_DOMAIN, DEVICE_TRACKER_DOMAIN)
async def async_setup_entry(hass, entry, async_see):
async def async_setup_entry(hass, entry, async_add_entities):
"""Configure a dispatcher connection based on a config entry."""
async def _set_location(device, gps_location, location_name):
"""Fire HA event to set location."""
await async_see(
dev_id=slugify(device),
gps=gps_location,
location_name=location_name
)
@callback
def _receive_data(device, location, location_name):
"""Receive set location."""
if device in hass.data[LT_DOMAIN]['devices']:
return
hass.data[LT_DOMAIN]['devices'].add(device)
async_add_entities([LocativeEntity(
device, location, location_name
)])
hass.data[LT_DOMAIN]['unsub_device_tracker'][entry.entry_id] = \
async_dispatcher_connect(hass, TRACKER_UPDATE, _receive_data)
hass.data[DATA_KEY] = async_dispatcher_connect(
hass, TRACKER_UPDATE, _set_location
)
return True
async def async_unload_entry(hass, entry):
"""Unload the config entry and remove the dispatcher connection."""
hass.data[DATA_KEY]()
return True
class LocativeEntity(DeviceTrackerEntity):
"""Represent a tracked device."""
def __init__(self, device, location, location_name):
"""Set up Locative entity."""
self._name = device
self._location = location
self._location_name = location_name
self._unsub_dispatcher = None
@property
def latitude(self):
"""Return latitude value of the device."""
return self._location[0]
@property
def longitude(self):
"""Return longitude value of the device."""
return self._location[1]
@property
def location_name(self):
"""Return a location name for the current location of the device."""
return self._location_name
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def source_type(self):
"""Return the source type, eg gps or router, of the device."""
return SOURCE_TYPE_GPS
async def async_added_to_hass(self):
"""Register state update callback."""
self._unsub_dispatcher = async_dispatcher_connect(
self.hass, TRACKER_UPDATE, self._async_receive_data)
async def async_will_remove_from_hass(self):
"""Clean up after entity before removal."""
self._unsub_dispatcher()
@callback
def _async_receive_data(self, device, location, location_name):
"""Update device data."""
self._location_name = location_name
self._location = location
self.async_write_ha_state()