From b95701779988befa7bd69e8769bdd5d0b8e26c3e Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sat, 12 Apr 2025 20:50:37 +0200 Subject: [PATCH] Clean up Syncthru unique id (#142778) --- .../components/syncthru/binary_sensor.py | 19 +------------- homeassistant/components/syncthru/entity.py | 7 +++++- homeassistant/components/syncthru/sensor.py | 25 +++++-------------- 3 files changed, 13 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/syncthru/binary_sensor.py b/homeassistant/components/syncthru/binary_sensor.py index f68f33043ee..45a3e263465 100644 --- a/homeassistant/components/syncthru/binary_sensor.py +++ b/homeassistant/components/syncthru/binary_sensor.py @@ -13,7 +13,6 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback @@ -62,11 +61,8 @@ async def async_setup_entry( coordinator: SyncthruCoordinator = hass.data[DOMAIN][config_entry.entry_id] - name: str = config_entry.data[CONF_NAME] - async_add_entities( - SyncThruBinarySensor(coordinator, name, description) - for description in BINARY_SENSORS + SyncThruBinarySensor(coordinator, description) for description in BINARY_SENSORS ) @@ -75,19 +71,6 @@ class SyncThruBinarySensor(SyncthruEntity, BinarySensorEntity): entity_description: SyncThruBinarySensorDescription - def __init__( - self, - coordinator: SyncthruCoordinator, - name: str, - entity_description: SyncThruBinarySensorDescription, - ) -> None: - """Initialize the sensor.""" - super().__init__(coordinator) - self.entity_description = entity_description - serial_number = coordinator.data.serial_number() - assert serial_number is not None - self._attr_unique_id = f"{serial_number}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" diff --git a/homeassistant/components/syncthru/entity.py b/homeassistant/components/syncthru/entity.py index 0b5e6324953..a2feafbc495 100644 --- a/homeassistant/components/syncthru/entity.py +++ b/homeassistant/components/syncthru/entity.py @@ -2,6 +2,7 @@ from homeassistant.helpers import device_registry as dr from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import DOMAIN, SyncthruCoordinator @@ -12,11 +13,15 @@ class SyncthruEntity(CoordinatorEntity[SyncthruCoordinator]): _attr_has_entity_name = True - def __init__(self, coordinator: SyncthruCoordinator) -> None: + def __init__( + self, coordinator: SyncthruCoordinator, entity_description: EntityDescription + ) -> None: """Initialize the Syncthru entity.""" super().__init__(coordinator) + self.entity_description = entity_description serial_number = coordinator.syncthru.serial_number() assert serial_number is not None + self._attr_unique_id = f"{serial_number}_{entity_description.key}" connections = set() if mac := coordinator.syncthru.raw().get("identity", {}).get("mac_addr"): connections.add((dr.CONNECTION_NETWORK_MAC, mac)) diff --git a/homeassistant/components/syncthru/sensor.py b/homeassistant/components/syncthru/sensor.py index 77d1123e773..f3fb9d6689e 100644 --- a/homeassistant/components/syncthru/sensor.py +++ b/homeassistant/components/syncthru/sensor.py @@ -10,7 +10,7 @@ from pysyncthru import SyncThru, SyncthruState from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_NAME, PERCENTAGE +from homeassistant.const import PERCENTAGE from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback @@ -131,7 +131,6 @@ async def async_setup_entry( supp_tray = printer.input_tray_status(filter_supported=True) supp_output_tray = printer.output_tray_status() - name: str = config_entry.data[CONF_NAME] entities: list[SyncThruSensorDescription] = [ get_toner_entity_description(color) for color in supp_toner ] @@ -140,7 +139,7 @@ async def async_setup_entry( entities.extend(get_output_tray_entity_description(key) for key in supp_output_tray) async_add_entities( - SyncThruSensor(coordinator, name, description) + SyncThruSensor(coordinator, description) for description in SENSOR_TYPES + tuple(entities) ) @@ -151,28 +150,16 @@ class SyncThruSensor(SyncthruEntity, SensorEntity): _attr_icon = "mdi:printer" entity_description: SyncThruSensorDescription - def __init__( - self, - coordinator: SyncthruCoordinator, - name: str, - entity_description: SyncThruSensorDescription, - ) -> None: - """Initialize the sensor.""" - super().__init__(coordinator) - self.entity_description = entity_description - self.syncthru = coordinator.data - serial_number = coordinator.data.serial_number() - assert serial_number is not None - self._attr_unique_id = f"{serial_number}_{entity_description.key}" - @property def native_value(self) -> str | int | None: """Return the state of the sensor.""" - return self.entity_description.value_fn(self.syncthru) + return self.entity_description.value_fn(self.coordinator.data) @property def extra_state_attributes(self) -> dict[str, Any] | None: """Return the state attributes.""" if self.entity_description.extra_state_attributes_fn: - return self.entity_description.extra_state_attributes_fn(self.syncthru) + return self.entity_description.extra_state_attributes_fn( + self.coordinator.data + ) return None