mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Clean up Syncthru unique id (#142778)
This commit is contained in:
parent
ebe71a1a38
commit
b957017799
@ -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."""
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user