2019-02-13 20:21:14 +00:00
|
|
|
"""Support for tracking Tesla cars."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
2017-08-31 04:13:02 +00:00
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
from homeassistant.components.device_tracker import SOURCE_TYPE_GPS
|
|
|
|
from homeassistant.components.device_tracker.config_entry import TrackerEntity
|
2017-08-31 04:13:02 +00:00
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
from . import DOMAIN as TESLA_DOMAIN, TeslaDevice
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-08-31 04:13:02 +00:00
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Tesla binary_sensors by config_entry."""
|
|
|
|
entities = [
|
|
|
|
TeslaDeviceEntity(
|
2020-08-27 11:56:20 +00:00
|
|
|
device,
|
|
|
|
hass.data[TESLA_DOMAIN][config_entry.entry_id]["coordinator"],
|
2019-12-23 20:54:25 +00:00
|
|
|
)
|
|
|
|
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"][
|
|
|
|
"devices_tracker"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
async_add_entities(entities, True)
|
2017-08-31 04:13:02 +00:00
|
|
|
|
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
class TeslaDeviceEntity(TeslaDevice, TrackerEntity):
|
2017-08-31 04:13:02 +00:00
|
|
|
"""A class representing a Tesla device."""
|
|
|
|
|
2019-12-23 20:54:25 +00:00
|
|
|
@property
|
2021-03-18 13:43:52 +00:00
|
|
|
def latitude(self) -> float | None:
|
2019-12-23 20:54:25 +00:00
|
|
|
"""Return latitude value of the device."""
|
2020-08-08 03:16:28 +00:00
|
|
|
location = self.tesla_device.get_location()
|
|
|
|
return self.tesla_device.get_location().get("latitude") if location else None
|
2019-12-23 20:54:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-18 13:43:52 +00:00
|
|
|
def longitude(self) -> float | None:
|
2019-12-23 20:54:25 +00:00
|
|
|
"""Return longitude value of the device."""
|
2020-08-08 03:16:28 +00:00
|
|
|
location = self.tesla_device.get_location()
|
|
|
|
return self.tesla_device.get_location().get("longitude") if location else None
|
2019-12-23 20:54:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source_type(self):
|
|
|
|
"""Return the source type, eg gps or router, of the device."""
|
|
|
|
return SOURCE_TYPE_GPS
|
2020-08-08 03:16:28 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-08-08 03:16:28 +00:00
|
|
|
"""Return the state attributes of the device."""
|
2021-03-11 19:16:26 +00:00
|
|
|
attr = super().extra_state_attributes.copy()
|
2020-08-08 03:16:28 +00:00
|
|
|
location = self.tesla_device.get_location()
|
|
|
|
if location:
|
2020-08-09 14:16:39 +00:00
|
|
|
attr.update(
|
|
|
|
{
|
|
|
|
"trackr_id": self.unique_id,
|
|
|
|
"heading": location["heading"],
|
|
|
|
"speed": location["speed"],
|
|
|
|
}
|
|
|
|
)
|
2020-08-08 03:16:28 +00:00
|
|
|
return attr
|