From 73962c484052f6813ddc65de054667255e171827 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 1 Feb 2024 03:34:14 +0000 Subject: [PATCH] Add device tracker --- custom_components/teslemetry/__init__.py | 1 + .../teslemetry/device_tracker.py | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 custom_components/teslemetry/device_tracker.py diff --git a/custom_components/teslemetry/__init__.py b/custom_components/teslemetry/__init__.py index fc3d3b2..c57fc46 100644 --- a/custom_components/teslemetry/__init__.py +++ b/custom_components/teslemetry/__init__.py @@ -23,6 +23,7 @@ Platform.BUTTON, Platform.COVER, Platform.CLIMATE, + Platform.DEVICE_TRACKER, Platform.SENSOR, ] diff --git a/custom_components/teslemetry/device_tracker.py b/custom_components/teslemetry/device_tracker.py new file mode 100644 index 0000000..fb85a7a --- /dev/null +++ b/custom_components/teslemetry/device_tracker.py @@ -0,0 +1,90 @@ +"""Device Tracker platform for Teslemetry integration.""" +from __future__ import annotations + +from homeassistant.components.device_tracker import SourceType +from homeassistant.components.device_tracker.config_entry import TrackerEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import StateType + +from .const import DOMAIN +from .entity import ( + TeslemetryEnergyEntity, + TeslemetryVehicleEntity, + TeslemetryWallConnectorEntity, +) +from .coordinator import ( + TeslemetryEnergyDataCoordinator, + TeslemetryVehicleDataCoordinator, +) +from .models import TeslemetryEnergyData, TeslemetryVehicleData + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up the Teslemetry device tracker platform from a config entry.""" + data = hass.data[DOMAIN][entry.entry_id] + + async_add_entities( + klass(vehicle) + for klass in ( + TeslemetryDeviceTrackerLocationEntity, + TeslemetryDeviceTrackerRouteEntity, + ) + for vehicle in data.vehicles + ) + + +class TeslemetryDeviceTrackerEntity(TeslemetryVehicleEntity, TrackerEntity): + """Base class for Teslemetry Tracker Entities.""" + + def __init__( + self, + vehicle: TeslemetryVehicleData, + ) -> None: + """Initialize the device tracker.""" + super().__init__(vehicle, self.key) + + @property + def source_type(self) -> SourceType | str: + """Return the source type of the device tracker.""" + return SourceType.GPS + + @property + def available(self) -> bool: + """Return if sensor is available.""" + return super().available and self.longitude is not None and self.latitude is not None + + +class TeslemetryDeviceTrackerLocationEntity(TeslemetryDeviceTrackerEntity): + """Vehicle Location Device Tracker Class.""" + + key = "location" + + @property + def longitude(self) -> float | None: + """Return the longitude of the device tracker.""" + return self.get("drive_state_longitude") + + @property + def latitude(self) -> float | None: + """Return the latitude of the device tracker.""" + return self.get("drive_state_latitude") + + +class TeslemetryDeviceTrackerRouteEntity(TeslemetryDeviceTrackerEntity): + """Vehicle Navigation Device Tracker Class.""" + + key = "route" + + @property + def longitude(self) -> float | None: + """Return the longitude of the device tracker.""" + return self.get("drive_state_active_route_longitude") + + @property + def latitude(self) -> float | None: + """Return the latitude of the device tracker.""" + return self.get("drive_state_active_route_latitude")