From 4be2e84ce65de68883f16770818cf2e354cd6cc7 Mon Sep 17 00:00:00 2001 From: Robin Thoni Date: Fri, 4 Jul 2025 14:36:25 +0200 Subject: [PATCH] Add backward compatibility with older versions of Traccar server (#146639) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Joakim Sørensen --- .../components/traccar_server/coordinator.py | 6 +++--- .../components/traccar_server/helpers.py | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/traccar_server/coordinator.py b/homeassistant/components/traccar_server/coordinator.py index 2c878856cc2..3a0bfe47e5f 100644 --- a/homeassistant/components/traccar_server/coordinator.py +++ b/homeassistant/components/traccar_server/coordinator.py @@ -31,7 +31,7 @@ from .const import ( EVENTS, LOGGER, ) -from .helpers import get_device, get_first_geofence +from .helpers import get_device, get_first_geofence, get_geofence_ids class TraccarServerCoordinatorDataDevice(TypedDict): @@ -131,7 +131,7 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat "device": device, "geofence": get_first_geofence( geofences, - position["geofenceIds"] or [], + get_geofence_ids(device, position), ), "position": position, "attributes": attr, @@ -187,7 +187,7 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat self.data[device_id]["attributes"] = attr self.data[device_id]["geofence"] = get_first_geofence( self._geofences, - position["geofenceIds"] or [], + get_geofence_ids(self.data[device_id]["device"], position), ) update_devices.add(device_id) diff --git a/homeassistant/components/traccar_server/helpers.py b/homeassistant/components/traccar_server/helpers.py index 971f51376b8..9a22f2784bc 100644 --- a/homeassistant/components/traccar_server/helpers.py +++ b/homeassistant/components/traccar_server/helpers.py @@ -2,7 +2,7 @@ from __future__ import annotations -from pytraccar import DeviceModel, GeofenceModel +from pytraccar import DeviceModel, GeofenceModel, PositionModel def get_device(device_id: int, devices: list[DeviceModel]) -> DeviceModel | None: @@ -22,3 +22,17 @@ def get_first_geofence( (geofence for geofence in geofences if geofence["id"] in target), None, ) + + +def get_geofence_ids( + device: DeviceModel, + position: PositionModel, +) -> list[int]: + """Compatibility helper to return a list of geofence IDs.""" + # For Traccar >=5.8 https://github.com/traccar/traccar/commit/30bafaed42e74863c5ca68a33c87f39d1e2de93d + if "geofenceIds" in position: + return position["geofenceIds"] or [] + # For Traccar <5.8 + if "geofenceIds" in device: + return device["geofenceIds"] or [] + return []