This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
device_tracker.py
68 lines (50 loc) · 2.09 KB
/
device_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Device Tracker entities for MySkoda."""
from homeassistant.components.device_tracker.config_entry import TrackerEntity
from homeassistant.components.device_tracker.const import SourceType
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DATA_COODINATOR, DOMAIN
from .entity import MySkodaDataEntity
from .myskoda import Vehicle
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
coordinator = hass.data[DOMAIN][config.entry_id][DATA_COODINATOR]
vehicles = coordinator.data.get("vehicles")
entities = [DeviceTracker(coordinator, vehicle) for vehicle in vehicles]
async_add_entities(entities, update_before_add=True)
class DeviceTracker(MySkodaDataEntity, TrackerEntity):
"""GPS device tracker for MySkoda."""
vehicle: Vehicle
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
EntityDescription(
name=vehicle.info.title,
key=f"{vehicle.info.vin}_device_tracker",
),
)
@property
def source_type(self) -> SourceType: # noqa: D102
return SourceType.GPS
@property
def latitude(self) -> float | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.position.lat
@property
def longitude(self) -> float | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.position.lng