Add backward compatibility with older versions of Traccar server (#146639)

Co-authored-by: Joakim Sørensen <joasoe@proton.me>
This commit is contained in:
Robin Thoni
2025-07-04 14:36:25 +02:00
committed by GitHub
co-authored by Joakim Sørensen
parent 1fc624c7a7
commit 4be2e84ce6
2 changed files with 18 additions and 4 deletions
@@ -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)
@@ -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 []