2019-04-03 15:40:03 +00:00
|
|
|
"""Device tracker platform that adds support for OwnTracks over MQTT."""
|
2022-09-13 00:50:44 +00:00
|
|
|
from homeassistant.components.device_tracker import ATTR_SOURCE_TYPE, DOMAIN, SourceType
|
2019-12-09 11:11:27 +00:00
|
|
|
from homeassistant.components.device_tracker.config_entry import TrackerEntity
|
2022-01-04 10:30:13 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-06-02 20:57:21 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-09 11:11:27 +00:00
|
|
|
ATTR_BATTERY_LEVEL,
|
2019-06-02 20:57:21 +00:00
|
|
|
ATTR_GPS_ACCURACY,
|
|
|
|
ATTR_LATITUDE,
|
|
|
|
ATTR_LONGITUDE,
|
|
|
|
)
|
2022-01-04 10:30:13 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-06-02 20:57:21 +00:00
|
|
|
from homeassistant.helpers import device_registry
|
2021-10-24 09:34:45 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-04 10:30:13 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-12-09 11:11:27 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DOMAIN as OT_DOMAIN
|
2018-11-28 21:20:13 +00:00
|
|
|
|
2016-08-26 14:22:08 +00:00
|
|
|
|
2022-01-04 10:30:13 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2018-11-28 21:20:13 +00:00
|
|
|
"""Set up OwnTracks based off an entry."""
|
2020-06-26 21:25:50 +00:00
|
|
|
# Restore previously loaded devices
|
2022-05-17 11:40:19 +00:00
|
|
|
dev_reg = device_registry.async_get(hass)
|
2020-06-26 21:25:50 +00:00
|
|
|
dev_ids = {
|
|
|
|
identifier[1]
|
|
|
|
for device in dev_reg.devices.values()
|
|
|
|
for identifier in device.identifiers
|
|
|
|
if identifier[0] == OT_DOMAIN
|
|
|
|
}
|
|
|
|
|
|
|
|
entities = []
|
|
|
|
for dev_id in dev_ids:
|
|
|
|
entity = hass.data[OT_DOMAIN]["devices"][dev_id] = OwnTracksEntity(dev_id)
|
|
|
|
entities.append(entity)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-05-25 20:34:53 +00:00
|
|
|
@callback
|
2019-06-02 20:57:21 +00:00
|
|
|
def _receive_data(dev_id, **data):
|
2019-05-25 20:34:53 +00:00
|
|
|
"""Receive set location."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity = hass.data[OT_DOMAIN]["devices"].get(dev_id)
|
2019-06-02 20:57:21 +00:00
|
|
|
|
|
|
|
if entity is not None:
|
|
|
|
entity.update_data(data)
|
2019-05-25 20:34:53 +00:00
|
|
|
return
|
2017-03-03 08:23:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entity = hass.data[OT_DOMAIN]["devices"][dev_id] = OwnTracksEntity(dev_id, data)
|
2019-06-02 20:57:21 +00:00
|
|
|
async_add_entities([entity])
|
2019-05-25 20:34:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[OT_DOMAIN]["context"].set_async_see(_receive_data)
|
2019-06-02 20:57:21 +00:00
|
|
|
|
2022-10-17 19:11:58 +00:00
|
|
|
async_add_entities(entities)
|
2019-06-02 20:57:21 +00:00
|
|
|
|
2017-09-25 16:05:09 +00:00
|
|
|
|
2019-07-04 10:44:40 +00:00
|
|
|
class OwnTracksEntity(TrackerEntity, RestoreEntity):
|
2019-05-25 20:34:53 +00:00
|
|
|
"""Represent a tracked device."""
|
|
|
|
|
2019-06-02 20:57:21 +00:00
|
|
|
def __init__(self, dev_id, data=None):
|
2019-05-25 20:34:53 +00:00
|
|
|
"""Set up OwnTracks entity."""
|
|
|
|
self._dev_id = dev_id
|
2019-06-02 20:57:21 +00:00
|
|
|
self._data = data or {}
|
2020-02-24 16:47:52 +00:00
|
|
|
self.entity_id = f"{DOMAIN}.{dev_id}"
|
2019-05-25 20:34:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique ID."""
|
|
|
|
return self._dev_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def battery_level(self):
|
|
|
|
"""Return the battery level of the device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._data.get("battery")
|
2019-05-25 20:34:53 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-05-25 20:34:53 +00:00
|
|
|
"""Return device specific attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._data.get("attributes")
|
2019-05-25 20:34:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def location_accuracy(self):
|
|
|
|
"""Return the gps accuracy of the device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._data.get("gps_accuracy")
|
2019-05-25 20:34:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def latitude(self):
|
|
|
|
"""Return latitude value of the device."""
|
2019-06-02 20:57:21 +00:00
|
|
|
# Check with "get" instead of "in" because value can be None
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._data.get("gps"):
|
|
|
|
return self._data["gps"][0]
|
2017-09-25 16:05:09 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-05-25 20:34:53 +00:00
|
|
|
@property
|
|
|
|
def longitude(self):
|
|
|
|
"""Return longitude value of the device."""
|
2019-06-02 20:57:21 +00:00
|
|
|
# Check with "get" instead of "in" because value can be None
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._data.get("gps"):
|
|
|
|
return self._data["gps"][1]
|
2017-09-25 16:05:09 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-05-25 20:34:53 +00:00
|
|
|
@property
|
|
|
|
def location_name(self):
|
|
|
|
"""Return a location name for the current location of the device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._data.get("location_name")
|
2019-05-25 20:34:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._data.get("host_name")
|
2019-05-25 20:34:53 +00:00
|
|
|
|
|
|
|
@property
|
2022-07-31 13:48:43 +00:00
|
|
|
def source_type(self) -> SourceType:
|
2019-05-25 20:34:53 +00:00
|
|
|
"""Return the source type, eg gps or router, of the device."""
|
2022-07-31 13:48:43 +00:00
|
|
|
return self._data.get("source_type", SourceType.GPS)
|
2019-05-25 20:34:53 +00:00
|
|
|
|
|
|
|
@property
|
2021-10-24 09:34:45 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2019-05-25 20:34:53 +00:00
|
|
|
"""Return the device info."""
|
2021-10-24 09:34:45 +00:00
|
|
|
return DeviceInfo(identifiers={(OT_DOMAIN, self._dev_id)}, name=self.name)
|
2019-05-25 20:34:53 +00:00
|
|
|
|
2022-09-06 08:25:35 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-06-02 20:57:21 +00:00
|
|
|
"""Call when entity about to be added to Home Assistant."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
# Don't restore if we got set up with data.
|
|
|
|
if self._data:
|
|
|
|
return
|
|
|
|
|
2021-10-30 14:29:07 +00:00
|
|
|
if (state := await self.async_get_last_state()) is None:
|
2019-06-02 20:57:21 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
attr = state.attributes
|
|
|
|
self._data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"host_name": state.name,
|
|
|
|
"gps": (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE)),
|
|
|
|
"gps_accuracy": attr.get(ATTR_GPS_ACCURACY),
|
|
|
|
"battery": attr.get(ATTR_BATTERY_LEVEL),
|
|
|
|
"source_type": attr.get(ATTR_SOURCE_TYPE),
|
2019-06-02 20:57:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 20:34:53 +00:00
|
|
|
@callback
|
2019-06-02 20:57:21 +00:00
|
|
|
def update_data(self, data):
|
2019-05-25 20:34:53 +00:00
|
|
|
"""Mark the device as seen."""
|
2019-06-02 20:57:21 +00:00
|
|
|
self._data = data
|
2020-06-12 16:27:51 +00:00
|
|
|
if self.hass:
|
|
|
|
self.async_write_ha_state()
|