Skip to content

Commit

Permalink
Add device tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 1, 2024
1 parent d802e98 commit 73962c4
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/teslemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Platform.BUTTON,
Platform.COVER,
Platform.CLIMATE,
Platform.DEVICE_TRACKER,
Platform.SENSOR,
]

Expand Down
90 changes: 90 additions & 0 deletions custom_components/teslemetry/device_tracker.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 73962c4

Please sign in to comment.