mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Refactor Syncthru binary sensor (#142696)
This commit is contained in:
parent
b01eac3ba5
commit
ffcc2254ce
@ -2,11 +2,15 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pysyncthru import SyncthruState
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from pysyncthru import SyncThru, SyncthruState
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
@ -29,6 +33,27 @@ SYNCTHRU_STATE_PROBLEM = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class SyncThruBinarySensorDescription(BinarySensorEntityDescription):
|
||||||
|
"""Describes Syncthru binary sensor entities."""
|
||||||
|
|
||||||
|
value_fn: Callable[[SyncThru], bool | None]
|
||||||
|
|
||||||
|
|
||||||
|
BINARY_SENSORS: tuple[SyncThruBinarySensorDescription, ...] = (
|
||||||
|
SyncThruBinarySensorDescription(
|
||||||
|
key="online",
|
||||||
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||||
|
value_fn=lambda printer: printer.is_online(),
|
||||||
|
),
|
||||||
|
SyncThruBinarySensorDescription(
|
||||||
|
key="problem",
|
||||||
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||||
|
value_fn=lambda printer: SYNCTHRU_STATE_PROBLEM[printer.device_status()],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
@ -39,67 +64,42 @@ 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]
|
name: str = config_entry.data[CONF_NAME]
|
||||||
entities = [
|
|
||||||
SyncThruOnlineSensor(coordinator, name),
|
|
||||||
SyncThruProblemSensor(coordinator, name),
|
|
||||||
]
|
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(
|
||||||
|
SyncThruBinarySensor(coordinator, name, description)
|
||||||
|
for description in BINARY_SENSORS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class SyncThruBinarySensor(CoordinatorEntity[SyncthruCoordinator], BinarySensorEntity):
|
class SyncThruBinarySensor(CoordinatorEntity[SyncthruCoordinator], BinarySensorEntity):
|
||||||
"""Implementation of an abstract Samsung Printer binary sensor platform."""
|
"""Implementation of an abstract Samsung Printer binary sensor platform."""
|
||||||
|
|
||||||
def __init__(self, coordinator: SyncthruCoordinator, name: str) -> None:
|
entity_description: SyncThruBinarySensorDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: SyncthruCoordinator,
|
||||||
|
name: str,
|
||||||
|
entity_description: SyncThruBinarySensorDescription,
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.syncthru = coordinator.data
|
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}"
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
self._id_suffix = ""
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return unique ID for the sensor."""
|
|
||||||
serial = self.syncthru.serial_number()
|
|
||||||
return f"{serial}{self._id_suffix}" if serial else None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self) -> DeviceInfo | None:
|
def device_info(self) -> DeviceInfo | None:
|
||||||
"""Return device information."""
|
"""Return device information."""
|
||||||
if (identifiers := device_identifiers(self.syncthru)) is None:
|
if (identifiers := device_identifiers(self.coordinator.data)) is None:
|
||||||
return None
|
return None
|
||||||
return DeviceInfo(
|
return DeviceInfo(
|
||||||
identifiers=identifiers,
|
identifiers=identifiers,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SyncThruOnlineSensor(SyncThruBinarySensor):
|
|
||||||
"""Implementation of a sensor that checks whether is turned on/online."""
|
|
||||||
|
|
||||||
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
|
||||||
|
|
||||||
def __init__(self, coordinator: SyncthruCoordinator, name: str) -> None:
|
|
||||||
"""Initialize the sensor."""
|
|
||||||
super().__init__(coordinator, name)
|
|
||||||
self._id_suffix = "_online"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self) -> bool | None:
|
||||||
"""Set the state to whether the printer is online."""
|
"""Return true if the binary sensor is on."""
|
||||||
return self.syncthru.is_online()
|
return self.entity_description.value_fn(self.coordinator.data)
|
||||||
|
|
||||||
|
|
||||||
class SyncThruProblemSensor(SyncThruBinarySensor):
|
|
||||||
"""Implementation of a sensor that checks whether the printer works correctly."""
|
|
||||||
|
|
||||||
_attr_device_class = BinarySensorDeviceClass.PROBLEM
|
|
||||||
|
|
||||||
def __init__(self, coordinator: SyncthruCoordinator, name: str) -> None:
|
|
||||||
"""Initialize the sensor."""
|
|
||||||
super().__init__(coordinator, name)
|
|
||||||
self._id_suffix = "_problem"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Set the state to whether there is a problem with the printer."""
|
|
||||||
return SYNCTHRU_STATE_PROBLEM[self.syncthru.device_status()]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user