From 3511f3541835187988056a1ac9bee6eb3c1d6eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Thu, 1 Feb 2024 22:06:34 +0100 Subject: [PATCH] Fix custom attribute lookup in Traccar Server (#109331) --- .../components/traccar_server/coordinator.py | 14 ++++++++------ .../components/traccar_server/device_tracker.py | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/traccar_server/coordinator.py b/homeassistant/components/traccar_server/coordinator.py index 337d0dcafbb..90c910e6062 100644 --- a/homeassistant/components/traccar_server/coordinator.py +++ b/homeassistant/components/traccar_server/coordinator.py @@ -93,10 +93,9 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat skip_accuracy_filter = False for custom_attr in self.custom_attributes: - attr[custom_attr] = getattr( - device["attributes"], + attr[custom_attr] = device["attributes"].get( custom_attr, - getattr(position["attributes"], custom_attr, None), + position["attributes"].get(custom_attr, None), ) if custom_attr in self.skip_accuracy_filter_for: skip_accuracy_filter = True @@ -151,13 +150,16 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat device = get_device(event["deviceId"], devices) self.hass.bus.async_fire( # This goes against two of the HA core guidelines: - # 1. Event names should be prefixed with the domain name of the integration + # 1. Event names should be prefixed with the domain name of + # the integration # 2. This should be event entities - # However, to not break it for those who currently use the "old" integration, this is kept as is. + # + # However, to not break it for those who currently use + # the "old" integration, this is kept as is. f"traccar_{EVENTS[event['type']]}", { "device_traccar_id": event["deviceId"], - "device_name": getattr(device, "name", None), + "device_name": device["name"] if device else None, "type": event["type"], "serverTime": event["eventTime"], "attributes": event["attributes"], diff --git a/homeassistant/components/traccar_server/device_tracker.py b/homeassistant/components/traccar_server/device_tracker.py index 2abcc6398fb..226d942e465 100644 --- a/homeassistant/components/traccar_server/device_tracker.py +++ b/homeassistant/components/traccar_server/device_tracker.py @@ -51,12 +51,13 @@ class TraccarServerDeviceTracker(TraccarServerEntity, TrackerEntity): @property def extra_state_attributes(self) -> dict[str, Any]: """Return device specific attributes.""" + geofence_name = self.traccar_geofence["name"] if self.traccar_geofence else None return { **self.traccar_attributes, ATTR_ADDRESS: self.traccar_position["address"], ATTR_ALTITUDE: self.traccar_position["altitude"], ATTR_CATEGORY: self.traccar_device["category"], - ATTR_GEOFENCE: getattr(self.traccar_geofence, "name", None), + ATTR_GEOFENCE: geofence_name, ATTR_MOTION: self.traccar_position["attributes"].get("motion", False), ATTR_SPEED: self.traccar_position["speed"], ATTR_STATUS: self.traccar_device["status"],