From 9dc84bfdca1352cc8290b218f4439536271bc828 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:21:33 +0200 Subject: [PATCH] Add shorthand attributes to device_tracker entities (#126599) * Add shorthand attributes to device_tracker entities * Simplify * Update config_entry.py * Update config_entry.py * Update device_tracker.py * Update device_tracker.py --- .../components/device_tracker/config_entry.py | 49 +++++++++++++++---- .../devolo_home_network/device_tracker.py | 16 ++---- .../components/tractive/device_tracker.py | 27 +++------- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/device_tracker/config_entry.py b/homeassistant/components/device_tracker/config_entry.py index 0e8a9d940da..505014b3def 100644 --- a/homeassistant/components/device_tracker/config_entry.py +++ b/homeassistant/components/device_tracker/config_entry.py @@ -170,6 +170,7 @@ class BaseTrackerEntity(Entity): _attr_device_info: None = None _attr_entity_category = EntityCategory.DIAGNOSTIC + _attr_source_type: SourceType @cached_property def battery_level(self) -> int | None: @@ -182,6 +183,8 @@ class BaseTrackerEntity(Entity): @property def source_type(self) -> SourceType | str: """Return the source type, eg gps or router, of the device.""" + if hasattr(self, "_attr_source_type"): + return self._attr_source_type raise NotImplementedError @property @@ -195,9 +198,24 @@ class BaseTrackerEntity(Entity): return attr -class TrackerEntity(BaseTrackerEntity): +CACHED_TRACKER_PROPERTIES_WITH_ATTR_ = { + "latitude", + "location_accuracy", + "location_name", + "longitude", +} + + +class TrackerEntity( + BaseTrackerEntity, cached_properties=CACHED_TRACKER_PROPERTIES_WITH_ATTR_ +): """Base class for a tracked device.""" + _attr_latitude: float | None = None + _attr_location_accuracy: int = 0 + _attr_location_name: str | None = None + _attr_longitude: float | None = None + @cached_property def should_poll(self) -> bool: """No polling for entities that have location pushed.""" @@ -214,22 +232,22 @@ class TrackerEntity(BaseTrackerEntity): Value in meters. """ - return 0 + return self._attr_location_accuracy @cached_property def location_name(self) -> str | None: """Return a location name for the current location of the device.""" - return None + return self._attr_location_name @cached_property def latitude(self) -> float | None: """Return latitude value of the device.""" - return None + return self._attr_latitude @cached_property def longitude(self) -> float | None: """Return longitude value of the device.""" - return None + return self._attr_longitude @property def state(self) -> str | None: @@ -266,23 +284,36 @@ class TrackerEntity(BaseTrackerEntity): return attr -class ScannerEntity(BaseTrackerEntity): +CACHED_SCANNER_PROPERTIES_WITH_ATTR_ = { + "ip_address", + "mac_address", + "hostname", +} + + +class ScannerEntity( + BaseTrackerEntity, cached_properties=CACHED_SCANNER_PROPERTIES_WITH_ATTR_ +): """Base class for a tracked device that is on a scanned network.""" + _attr_hostname: str | None = None + _attr_ip_address: str | None = None + _attr_mac_address: str | None = None + @cached_property def ip_address(self) -> str | None: """Return the primary ip address of the device.""" - return None + return self._attr_ip_address @cached_property def mac_address(self) -> str | None: """Return the mac address of the device.""" - return None + return self._attr_mac_address @cached_property def hostname(self) -> str | None: """Return hostname of the device.""" - return None + return self._attr_hostname @property def state(self) -> str: diff --git a/homeassistant/components/devolo_home_network/device_tracker.py b/homeassistant/components/devolo_home_network/device_tracker.py index 960069191ee..ce644da4e1d 100644 --- a/homeassistant/components/devolo_home_network/device_tracker.py +++ b/homeassistant/components/devolo_home_network/device_tracker.py @@ -89,6 +89,8 @@ class DevoloScannerEntity( ): """Representation of a devolo device tracker.""" + _attr_source_type = SourceType.ROUTER + def __init__( self, coordinator: DataUpdateCoordinator[list[ConnectedStationInfo]], @@ -98,7 +100,7 @@ class DevoloScannerEntity( """Initialize entity.""" super().__init__(coordinator) self._device = device - self._mac = mac + self._attr_mac_address = mac @property def extra_state_attributes(self) -> dict[str, str]: @@ -140,17 +142,7 @@ class DevoloScannerEntity( if station.mac_address == self.mac_address ) - @property - def mac_address(self) -> str: - """Return mac_address.""" - return self._mac - - @property - def source_type(self) -> SourceType: - """Return tracker source type.""" - return SourceType.ROUTER - @property def unique_id(self) -> str: """Return unique ID of the entity.""" - return f"{self._device.serial_number}_{self._mac}" + return f"{self._device.serial_number}_{self.mac_address}" diff --git a/homeassistant/components/tractive/device_tracker.py b/homeassistant/components/tractive/device_tracker.py index d5d6f5f541c..f31afaf92f6 100644 --- a/homeassistant/components/tractive/device_tracker.py +++ b/homeassistant/components/tractive/device_tracker.py @@ -47,9 +47,9 @@ class TractiveDeviceTracker(TractiveEntity, TrackerEntity): ) self._battery_level: int | None = item.hw_info.get("battery_level") - self._latitude: float = item.pos_report["latlong"][0] - self._longitude: float = item.pos_report["latlong"][1] - self._accuracy: int = item.pos_report["pos_uncertainty"] + self._attr_latitude = item.pos_report["latlong"][0] + self._attr_longitude = item.pos_report["latlong"][1] + self._attr_location_accuracy: int = item.pos_report["pos_uncertainty"] self._source_type: str = item.pos_report["sensor_used"] self._attr_unique_id = item.trackable["_id"] @@ -62,21 +62,6 @@ class TractiveDeviceTracker(TractiveEntity, TrackerEntity): return SourceType.ROUTER return SourceType.GPS - @property - def latitude(self) -> float: - """Return latitude value of the device.""" - return self._latitude - - @property - def longitude(self) -> float: - """Return longitude value of the device.""" - return self._longitude - - @property - def location_accuracy(self) -> int: - """Return the gps accuracy of the device.""" - return self._accuracy - @property def battery_level(self) -> int | None: """Return the battery level of the device.""" @@ -90,9 +75,9 @@ class TractiveDeviceTracker(TractiveEntity, TrackerEntity): @callback def _handle_position_update(self, event: dict[str, Any]) -> None: - self._latitude = event["latitude"] - self._longitude = event["longitude"] - self._accuracy = event["accuracy"] + self._attr_latitude = event["latitude"] + self._attr_longitude = event["longitude"] + self._attr_location_accuracy = event["accuracy"] self._source_type = event["sensor_used"] self._attr_available = True self.async_write_ha_state()