From 65edb1cfb2b68bb06cbcdd9daa279370eef1a200 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 7 Jul 2026 13:37:46 +0200 Subject: [PATCH] Use state attribute enums in legacy device_tracker (#175854) Co-authored-by: Claude Opus 4.8 (1M context) --- .../components/device_tracker/legacy.py | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/device_tracker/legacy.py b/homeassistant/components/device_tracker/legacy.py index 5fedb6686515..8abb4a44f3d3 100644 --- a/homeassistant/components/device_tracker/legacy.py +++ b/homeassistant/components/device_tracker/legacy.py @@ -77,7 +77,9 @@ from .const import ( LOGGER, PLATFORM_TYPE_LEGACY, SCAN_INTERVAL, + DeviceTrackerEntityStateAttribute, SourceType, + TrackerEntityStateAttribute, ) _LOGGER = logging.getLogger(__name__) @@ -840,12 +842,14 @@ class Device(RestoreEntity): @override def state_attributes(self) -> dict[str, StateType]: """Return the device state attributes.""" - attributes: dict[str, StateType] = {ATTR_SOURCE_TYPE: self.source_type} + attributes: dict[str, StateType] = { + DeviceTrackerEntityStateAttribute.SOURCE_TYPE: self.source_type + } if self.gps is not None: - attributes[ATTR_LATITUDE] = self.gps[0] - attributes[ATTR_LONGITUDE] = self.gps[1] - attributes[ATTR_GPS_ACCURACY] = self.gps_accuracy + attributes[TrackerEntityStateAttribute.LATITUDE] = self.gps[0] + attributes[TrackerEntityStateAttribute.LONGITUDE] = self.gps[1] + attributes[TrackerEntityStateAttribute.GPS_ACCURACY] = self.gps_accuracy if self.battery is not None: attributes[ATTR_BATTERY] = self.battery @@ -952,17 +956,17 @@ class Device(RestoreEntity): self.last_seen = dt_util.utcnow() for attribute, var in ( - (ATTR_SOURCE_TYPE, "source_type"), - (ATTR_GPS_ACCURACY, "gps_accuracy"), + (DeviceTrackerEntityStateAttribute.SOURCE_TYPE, "source_type"), + (TrackerEntityStateAttribute.GPS_ACCURACY, "gps_accuracy"), (ATTR_BATTERY, "battery"), ): if attribute in state.attributes: setattr(self, var, state.attributes[attribute]) - if ATTR_LONGITUDE in state.attributes: + if TrackerEntityStateAttribute.LONGITUDE in state.attributes: self.gps = ( - state.attributes[ATTR_LATITUDE], - state.attributes[ATTR_LONGITUDE], + state.attributes[TrackerEntityStateAttribute.LATITUDE], + state.attributes[TrackerEntityStateAttribute.LONGITUDE], )