Skip to content

Commit 0582a09

Browse files
tomer-wCopilot
andcommitted
Add GPS extra state attributes to device_tracker
Expose altitude, course, and speed as extra_state_attributes on the device_tracker entity. Refactor to use _update_from_location for consistent attribute updates on init and on_update. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 029a508 commit 0582a09

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

custom_components/victron_mqtt/device_tracker.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
from homeassistant.core import HomeAssistant, callback
1616
from homeassistant.helpers.device_registry import DeviceInfo
1717
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
18+
from homeassistant.helpers.typing import StateType
1819

1920
from .entity import VictronBaseEntity
2021
from .hub import VictronGxConfigEntry
2122

2223
PARALLEL_UPDATES = 0
2324

25+
ATTR_ALTITUDE = "altitude"
26+
ATTR_COURSE = "course"
27+
ATTR_SPEED = "speed"
28+
2429

2530
async def async_setup_entry(
2631
hass: HomeAssistant,
@@ -58,14 +63,31 @@ def __init__(
5863
installation_id: str,
5964
) -> None:
6065
"""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)
6568

6669
@callback
6770
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."""
6876
if isinstance(value, GpsLocation):
6977
self._attr_latitude = value.latitude
7078
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

Comments
 (0)