|
15 | 15 | from homeassistant.core import HomeAssistant, callback |
16 | 16 | from homeassistant.helpers.device_registry import DeviceInfo |
17 | 17 | from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 18 | +from homeassistant.helpers.typing import StateType |
18 | 19 |
|
19 | 20 | from .entity import VictronBaseEntity |
20 | 21 | from .hub import VictronGxConfigEntry |
21 | 22 |
|
22 | 23 | PARALLEL_UPDATES = 0 |
23 | 24 |
|
| 25 | +ATTR_ALTITUDE = "altitude" |
| 26 | +ATTR_COURSE = "course" |
| 27 | +ATTR_SPEED = "speed" |
| 28 | + |
24 | 29 |
|
25 | 30 | async def async_setup_entry( |
26 | 31 | hass: HomeAssistant, |
@@ -58,14 +63,31 @@ def __init__( |
58 | 63 | installation_id: str, |
59 | 64 | ) -> None: |
60 | 65 | """Initialize the device tracker.""" |
61 | | - super().__init__(device, metric, device_info, "device_tracker", simple_naming, installation_id) |
62 | | - if isinstance(metric.value, GpsLocation): |
63 | | - self._attr_latitude = metric.value.latitude |
64 | | - self._attr_longitude = metric.value.longitude |
| 66 | + super().__init__(device, metric, device_info, "device_tracker", simple_naming, installation_id) |
| 67 | + self._update_from_location(metric.value) |
65 | 68 |
|
66 | 69 | @callback |
67 | 70 | def _on_update_cb(self, value: Any) -> None: |
| 71 | + self._update_from_location(value) |
| 72 | + self.async_write_ha_state() |
| 73 | + |
| 74 | + def _update_from_location(self, value: Any) -> None: |
| 75 | + """Update entity attributes from a GpsLocation value.""" |
68 | 76 | if isinstance(value, GpsLocation): |
69 | 77 | self._attr_latitude = value.latitude |
70 | 78 | self._attr_longitude = value.longitude |
71 | | - self.async_write_ha_state() |
| 79 | + self._altitude = value.altitude |
| 80 | + self._course = value.course |
| 81 | + self._speed = value.speed |
| 82 | + |
| 83 | + @property |
| 84 | + def extra_state_attributes(self) -> dict[str, StateType]: |
| 85 | + """Return extra state attributes for altitude, course, and speed.""" |
| 86 | + attrs: dict[str, StateType] = {} |
| 87 | + if self._altitude is not None: |
| 88 | + attrs[ATTR_ALTITUDE] = self._altitude |
| 89 | + if self._course is not None: |
| 90 | + attrs[ATTR_COURSE] = self._course |
| 91 | + if self._speed is not None: |
| 92 | + attrs[ATTR_SPEED] = self._speed |
| 93 | + return attrs |
0 commit comments