mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00:00
Introduce base entity in Syncthru (#142694)
This commit is contained in:
parent
b20f46e8b9
commit
3efb009e82
@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pysyncthru import SyncThru, SyncThruAPINotSupported
|
from pysyncthru import SyncThruAPINotSupported
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import SyncthruCoordinator
|
from .coordinator import SyncthruCoordinator
|
||||||
@ -26,17 +25,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
# and the config should simply be discarded
|
# and the config should simply be discarded
|
||||||
return False
|
return False
|
||||||
|
|
||||||
device_registry = dr.async_get(hass)
|
|
||||||
device_registry.async_get_or_create(
|
|
||||||
config_entry_id=entry.entry_id,
|
|
||||||
configuration_url=coordinator.syncthru.url,
|
|
||||||
connections=device_connections(coordinator.syncthru),
|
|
||||||
manufacturer="Samsung",
|
|
||||||
identifiers=device_identifiers(coordinator.syncthru),
|
|
||||||
model=coordinator.syncthru.model(),
|
|
||||||
name=coordinator.syncthru.hostname(),
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -46,18 +34,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id, None)
|
hass.data[DOMAIN].pop(entry.entry_id, None)
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
def device_identifiers(printer: SyncThru) -> set[tuple[str, str]] | None:
|
|
||||||
"""Get device identifiers for device registry."""
|
|
||||||
serial = printer.serial_number()
|
|
||||||
if serial is None:
|
|
||||||
return None
|
|
||||||
return {(DOMAIN, serial)}
|
|
||||||
|
|
||||||
|
|
||||||
def device_connections(printer: SyncThru) -> set[tuple[str, str]]:
|
|
||||||
"""Get device connections for device registry."""
|
|
||||||
if mac := printer.raw().get("identity", {}).get("mac_addr"):
|
|
||||||
return {(dr.CONNECTION_NETWORK_MAC, mac)}
|
|
||||||
return set()
|
|
||||||
|
@ -15,12 +15,11 @@ from homeassistant.components.binary_sensor import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from . import SyncthruCoordinator, device_identifiers
|
from . import SyncthruCoordinator
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .entity import SyncthruEntity
|
||||||
|
|
||||||
SYNCTHRU_STATE_PROBLEM = {
|
SYNCTHRU_STATE_PROBLEM = {
|
||||||
SyncthruState.INVALID: True,
|
SyncthruState.INVALID: True,
|
||||||
@ -71,7 +70,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SyncThruBinarySensor(CoordinatorEntity[SyncthruCoordinator], BinarySensorEntity):
|
class SyncThruBinarySensor(SyncthruEntity, BinarySensorEntity):
|
||||||
"""Implementation of an abstract Samsung Printer binary sensor platform."""
|
"""Implementation of an abstract Samsung Printer binary sensor platform."""
|
||||||
|
|
||||||
entity_description: SyncThruBinarySensorDescription
|
entity_description: SyncThruBinarySensorDescription
|
||||||
@ -90,15 +89,6 @@ class SyncThruBinarySensor(CoordinatorEntity[SyncthruCoordinator], BinarySensorE
|
|||||||
self._attr_unique_id = f"{serial_number}_{entity_description.key}"
|
self._attr_unique_id = f"{serial_number}_{entity_description.key}"
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo | None:
|
|
||||||
"""Return device information."""
|
|
||||||
if (identifiers := device_identifiers(self.coordinator.data)) is None:
|
|
||||||
return None
|
|
||||||
return DeviceInfo(
|
|
||||||
identifiers=identifiers,
|
|
||||||
)
|
|
||||||
|
|
||||||
@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."""
|
||||||
|
28
homeassistant/components/syncthru/entity.py
Normal file
28
homeassistant/components/syncthru/entity.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""Base class for Syncthru entities."""
|
||||||
|
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from . import DOMAIN, SyncthruCoordinator
|
||||||
|
|
||||||
|
|
||||||
|
class SyncthruEntity(CoordinatorEntity[SyncthruCoordinator]):
|
||||||
|
"""Base class for Syncthru entities."""
|
||||||
|
|
||||||
|
def __init__(self, coordinator: SyncthruCoordinator) -> None:
|
||||||
|
"""Initialize the Syncthru entity."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
serial_number = coordinator.syncthru.serial_number()
|
||||||
|
assert serial_number is not None
|
||||||
|
connections = set()
|
||||||
|
if mac := coordinator.syncthru.raw().get("identity", {}).get("mac_addr"):
|
||||||
|
connections.add((dr.CONNECTION_NETWORK_MAC, mac))
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, serial_number)},
|
||||||
|
connections=connections,
|
||||||
|
configuration_url=coordinator.syncthru.url,
|
||||||
|
manufacturer="Samsung",
|
||||||
|
model=coordinator.syncthru.model(),
|
||||||
|
name=coordinator.syncthru.hostname(),
|
||||||
|
)
|
@ -12,12 +12,11 @@ from homeassistant.components.sensor import SensorEntity, SensorEntityDescriptio
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME, PERCENTAGE
|
from homeassistant.const import CONF_NAME, PERCENTAGE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from . import SyncthruCoordinator, device_identifiers
|
from . import SyncthruCoordinator
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .entity import SyncthruEntity
|
||||||
|
|
||||||
SYNCTHRU_STATE_HUMAN = {
|
SYNCTHRU_STATE_HUMAN = {
|
||||||
SyncthruState.INVALID: "invalid",
|
SyncthruState.INVALID: "invalid",
|
||||||
@ -138,7 +137,7 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SyncThruSensor(CoordinatorEntity[SyncthruCoordinator], SensorEntity):
|
class SyncThruSensor(SyncthruEntity, SensorEntity):
|
||||||
"""Implementation of an abstract Samsung Printer sensor platform."""
|
"""Implementation of an abstract Samsung Printer sensor platform."""
|
||||||
|
|
||||||
_attr_icon = "mdi:printer"
|
_attr_icon = "mdi:printer"
|
||||||
@ -159,15 +158,6 @@ class SyncThruSensor(CoordinatorEntity[SyncthruCoordinator], SensorEntity):
|
|||||||
assert serial_number is not None
|
assert serial_number is not None
|
||||||
self._attr_unique_id = f"{serial_number}_{entity_description.key}"
|
self._attr_unique_id = f"{serial_number}_{entity_description.key}"
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo | None:
|
|
||||||
"""Return device information."""
|
|
||||||
if (identifiers := device_identifiers(self.syncthru)) is None:
|
|
||||||
return None
|
|
||||||
return DeviceInfo(
|
|
||||||
identifiers=identifiers,
|
|
||||||
)
|
|
||||||
|
|
||||||
@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."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user