mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +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,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
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]
|
coordinator: SyncthruCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
name: str = config_entry.data[CONF_NAME]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SyncThruBinarySensor(coordinator, name, description)
|
SyncThruBinarySensor(coordinator, description) for description in BINARY_SENSORS
|
||||||
for description in BINARY_SENSORS
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -75,19 +71,6 @@ class SyncThruBinarySensor(SyncthruEntity, BinarySensorEntity):
|
|||||||
|
|
||||||
entity_description: SyncThruBinarySensorDescription
|
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
|
@property
|
||||||
def is_on(self) -> bool | None:
|
def is_on(self) -> bool | None:
|
||||||
"""Return true if the binary sensor is on."""
|
"""Return true if the binary sensor is on."""
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import DOMAIN, SyncthruCoordinator
|
from . import DOMAIN, SyncthruCoordinator
|
||||||
@ -12,11 +13,15 @@ class SyncthruEntity(CoordinatorEntity[SyncthruCoordinator]):
|
|||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(self, coordinator: SyncthruCoordinator) -> None:
|
def __init__(
|
||||||
|
self, coordinator: SyncthruCoordinator, entity_description: EntityDescription
|
||||||
|
) -> None:
|
||||||
"""Initialize the Syncthru entity."""
|
"""Initialize the Syncthru entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
self.entity_description = entity_description
|
||||||
serial_number = coordinator.syncthru.serial_number()
|
serial_number = coordinator.syncthru.serial_number()
|
||||||
assert serial_number is not None
|
assert serial_number is not None
|
||||||
|
self._attr_unique_id = f"{serial_number}_{entity_description.key}"
|
||||||
connections = set()
|
connections = set()
|
||||||
if mac := coordinator.syncthru.raw().get("identity", {}).get("mac_addr"):
|
if mac := coordinator.syncthru.raw().get("identity", {}).get("mac_addr"):
|
||||||
connections.add((dr.CONNECTION_NETWORK_MAC, mac))
|
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.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
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_tray = printer.input_tray_status(filter_supported=True)
|
||||||
supp_output_tray = printer.output_tray_status()
|
supp_output_tray = printer.output_tray_status()
|
||||||
|
|
||||||
name: str = config_entry.data[CONF_NAME]
|
|
||||||
entities: list[SyncThruSensorDescription] = [
|
entities: list[SyncThruSensorDescription] = [
|
||||||
get_toner_entity_description(color) for color in supp_toner
|
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)
|
entities.extend(get_output_tray_entity_description(key) for key in supp_output_tray)
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SyncThruSensor(coordinator, name, description)
|
SyncThruSensor(coordinator, description)
|
||||||
for description in SENSOR_TYPES + tuple(entities)
|
for description in SENSOR_TYPES + tuple(entities)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -151,28 +150,16 @@ class SyncThruSensor(SyncthruEntity, SensorEntity):
|
|||||||
_attr_icon = "mdi:printer"
|
_attr_icon = "mdi:printer"
|
||||||
entity_description: SyncThruSensorDescription
|
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
|
@property
|
||||||
def native_value(self) -> str | int | None:
|
def native_value(self) -> str | int | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.entity_description.value_fn(self.syncthru)
|
return self.entity_description.value_fn(self.coordinator.data)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
if self.entity_description.extra_state_attributes_fn:
|
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
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user