Skip to content

Commit

Permalink
Fix custom attribute lookup in Traccar Server (home-assistant#109331)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Feb 1, 2024
1 parent c1f8835 commit 3511f35
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 8 additions & 6 deletions homeassistant/components/traccar_server/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ async def _async_update_data(self) -> TraccarServerCoordinatorData:
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
Expand Down Expand Up @@ -151,13 +150,16 @@ async def import_events(self, devices: list[DeviceModel]) -> None:
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"],
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/traccar_server/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ def battery_level(self) -> int:
@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"],
Expand Down

0 comments on commit 3511f35

Please sign in to comment.