2019-04-03 17:40:03 +02:00
|
|
|
"""Support for the Locative platform."""
|
2022-10-28 17:40:37 +02:00
|
|
|
from homeassistant.components.device_tracker import SourceType, TrackerEntity
|
2022-01-04 11:30:13 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-01-11 15:14:11 -08:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2022-01-04 11:30:13 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2015-10-12 00:14:05 +02:00
|
|
|
|
2019-05-25 13:34:53 -07:00
|
|
|
from . import DOMAIN as LT_DOMAIN, TRACKER_UPDATE
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2015-10-12 00:14:05 +02:00
|
|
|
|
2022-01-04 11:30:13 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2019-01-27 14:43:16 -08:00
|
|
|
"""Configure a dispatcher connection based on a config entry."""
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2019-05-25 13:34:53 -07:00
|
|
|
@callback
|
|
|
|
def _receive_data(device, location, location_name):
|
|
|
|
"""Receive set location."""
|
2019-07-31 12:25:30 -07:00
|
|
|
if device in hass.data[LT_DOMAIN]["devices"]:
|
2019-05-25 13:34:53 -07:00
|
|
|
return
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.data[LT_DOMAIN]["devices"].add(device)
|
2019-05-25 13:34:53 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async_add_entities([LocativeEntity(device, location, location_name)])
|
2019-01-27 14:43:16 -08:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
hass.data[LT_DOMAIN]["unsub_device_tracker"][
|
|
|
|
entry.entry_id
|
|
|
|
] = async_dispatcher_connect(hass, TRACKER_UPDATE, _receive_data)
|
2019-01-27 14:43:16 -08:00
|
|
|
|
2019-05-25 13:34:53 -07:00
|
|
|
|
2019-07-04 06:44:40 -04:00
|
|
|
class LocativeEntity(TrackerEntity):
|
2019-05-25 13:34:53 -07:00
|
|
|
"""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
|
2022-07-31 15:49:57 +02:00
|
|
|
def source_type(self) -> SourceType:
|
2019-05-25 13:34:53 -07:00
|
|
|
"""Return the source type, eg gps or router, of the device."""
|
2022-07-31 15:49:57 +02:00
|
|
|
return SourceType.GPS
|
2019-05-25 13:34:53 -07:00
|
|
|
|
2022-09-01 14:14:31 +02:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-05-25 13:34:53 -07:00
|
|
|
"""Register state update callback."""
|
|
|
|
self._unsub_dispatcher = async_dispatcher_connect(
|
2019-07-31 12:25:30 -07:00
|
|
|
self.hass, TRACKER_UPDATE, self._async_receive_data
|
|
|
|
)
|
2019-05-25 13:34:53 -07:00
|
|
|
|
2022-09-01 14:14:31 +02:00
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
2019-05-25 13:34:53 -07:00
|
|
|
"""Clean up after entity before removal."""
|
|
|
|
self._unsub_dispatcher()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _async_receive_data(self, device, location, location_name):
|
|
|
|
"""Update device data."""
|
2019-06-25 05:00:28 +02:00
|
|
|
if device != self._name:
|
|
|
|
return
|
2019-05-25 13:34:53 -07:00
|
|
|
self._location_name = location_name
|
|
|
|
self._location = location
|
|
|
|
self.async_write_ha_state()
|