From 25f922d87b5d690702ecc5b6cd450ff2107942b9 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Tue, 19 Nov 2024 22:08:16 +0100 Subject: [PATCH] Add base entity to sabnzbd (#130995) --- .../components/sabnzbd/coordinator.py | 2 ++ homeassistant/components/sabnzbd/entity.py | 30 +++++++++++++++++ homeassistant/components/sabnzbd/sensor.py | 32 +++---------------- .../sabnzbd/snapshots/test_sensor.ambr | 22 ++++++------- 4 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 homeassistant/components/sabnzbd/entity.py diff --git a/homeassistant/components/sabnzbd/coordinator.py b/homeassistant/components/sabnzbd/coordinator.py index 0c69a81a7f5..14f7c18e38c 100644 --- a/homeassistant/components/sabnzbd/coordinator.py +++ b/homeassistant/components/sabnzbd/coordinator.py @@ -16,6 +16,8 @@ _LOGGER = logging.getLogger(__name__) class SabnzbdUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): """The SABnzbd update coordinator.""" + config_entry: ConfigEntry + def __init__( self, hass: HomeAssistant, diff --git a/homeassistant/components/sabnzbd/entity.py b/homeassistant/components/sabnzbd/entity.py new file mode 100644 index 00000000000..f7515c3d178 --- /dev/null +++ b/homeassistant/components/sabnzbd/entity.py @@ -0,0 +1,30 @@ +"""Base entity for Sabnzbd.""" + +from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo +from homeassistant.helpers.entity import EntityDescription +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import SabnzbdUpdateCoordinator + + +class SabnzbdEntity(CoordinatorEntity[SabnzbdUpdateCoordinator]): + """Defines a base Sabnzbd entity.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: SabnzbdUpdateCoordinator, + description: EntityDescription, + ) -> None: + """Initialize the base entity.""" + super().__init__(coordinator) + + entry_id = coordinator.config_entry.entry_id + self._attr_unique_id = f"{entry_id}_{description.key}" + self.entity_description = description + self._attr_device_info = DeviceInfo( + entry_type=DeviceEntryType.SERVICE, + identifiers={(DOMAIN, entry_id)}, + ) diff --git a/homeassistant/components/sabnzbd/sensor.py b/homeassistant/components/sabnzbd/sensor.py index d956d06f1ac..1d2bbdc55e7 100644 --- a/homeassistant/components/sabnzbd/sensor.py +++ b/homeassistant/components/sabnzbd/sensor.py @@ -13,13 +13,12 @@ from homeassistant.components.sensor import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfDataRate, UnitOfInformation from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from homeassistant.helpers.update_coordinator import CoordinatorEntity -from . import DOMAIN, SabnzbdUpdateCoordinator -from .const import DEFAULT_NAME +from .const import DOMAIN +from .coordinator import SabnzbdUpdateCoordinator +from .entity import SabnzbdEntity @dataclass(frozen=True, kw_only=True) @@ -139,34 +138,13 @@ async def async_setup_entry( entry_id = config_entry.entry_id coordinator: SabnzbdUpdateCoordinator = hass.data[DOMAIN][entry_id] - async_add_entities( - [SabnzbdSensor(coordinator, sensor, entry_id) for sensor in SENSOR_TYPES] - ) + async_add_entities([SabnzbdSensor(coordinator, sensor) for sensor in SENSOR_TYPES]) -class SabnzbdSensor(CoordinatorEntity[SabnzbdUpdateCoordinator], SensorEntity): +class SabnzbdSensor(SabnzbdEntity, SensorEntity): """Representation of an SABnzbd sensor.""" entity_description: SabnzbdSensorEntityDescription - _attr_should_poll = False - _attr_has_entity_name = True - - def __init__( - self, - coordinator: SabnzbdUpdateCoordinator, - description: SabnzbdSensorEntityDescription, - entry_id, - ) -> None: - """Initialize the sensor.""" - super().__init__(coordinator) - - self._attr_unique_id = f"{entry_id}_{description.key}" - self.entity_description = description - self._attr_device_info = DeviceInfo( - entry_type=DeviceEntryType.SERVICE, - identifiers={(DOMAIN, entry_id)}, - name=DEFAULT_NAME, - ) @property def native_value(self) -> StateType: diff --git a/tests/components/sabnzbd/snapshots/test_sensor.ambr b/tests/components/sabnzbd/snapshots/test_sensor.ambr index f339906d038..8b977e69aa6 100644 --- a/tests/components/sabnzbd/snapshots/test_sensor.ambr +++ b/tests/components/sabnzbd/snapshots/test_sensor.ambr @@ -41,7 +41,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Daily total', + 'friendly_name': 'Sabnzbd Daily total', 'state_class': , 'unit_of_measurement': , }), @@ -92,7 +92,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Free disk space', + 'friendly_name': 'Sabnzbd Free disk space', 'state_class': , 'unit_of_measurement': , }), @@ -143,7 +143,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Left to download', + 'friendly_name': 'Sabnzbd Left to download', 'state_class': , 'unit_of_measurement': , }), @@ -197,7 +197,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Monthly total', + 'friendly_name': 'Sabnzbd Monthly total', 'state_class': , 'unit_of_measurement': , }), @@ -251,7 +251,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Overall total', + 'friendly_name': 'Sabnzbd Overall total', 'state_class': , 'unit_of_measurement': , }), @@ -302,7 +302,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Queue', + 'friendly_name': 'Sabnzbd Queue', 'state_class': , 'unit_of_measurement': , }), @@ -355,7 +355,7 @@ # name: test_sensor[sensor.sabnzbd_queue_count-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'SABnzbd Queue count', + 'friendly_name': 'Sabnzbd Queue count', 'state_class': , }), 'context': , @@ -411,7 +411,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_rate', - 'friendly_name': 'SABnzbd Speed', + 'friendly_name': 'Sabnzbd Speed', 'state_class': , 'unit_of_measurement': , }), @@ -459,7 +459,7 @@ # name: test_sensor[sensor.sabnzbd_status-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'SABnzbd Status', + 'friendly_name': 'Sabnzbd Status', }), 'context': , 'entity_id': 'sensor.sabnzbd_status', @@ -508,7 +508,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Total disk space', + 'friendly_name': 'Sabnzbd Total disk space', 'state_class': , 'unit_of_measurement': , }), @@ -562,7 +562,7 @@ StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'data_size', - 'friendly_name': 'SABnzbd Weekly total', + 'friendly_name': 'Sabnzbd Weekly total', 'state_class': , 'unit_of_measurement': , }),