-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
Platform.BUTTON, | ||
Platform.COVER, | ||
Platform.CLIMATE, | ||
Platform.DEVICE_TRACKER, | ||
Platform.SENSOR, | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |