Add binary sensor to SABnzbd (#131651)

This commit is contained in:
Jan-Philipp Benecke 2024-11-26 19:01:19 +01:00 committed by GitHub
parent 192ffc09ee
commit e31d398811
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 139 additions and 2 deletions

View File

@ -27,7 +27,7 @@ from .const import (
from .coordinator import SabnzbdUpdateCoordinator from .coordinator import SabnzbdUpdateCoordinator
from .helpers import get_client from .helpers import get_client
PLATFORMS = [Platform.BUTTON, Platform.NUMBER, Platform.SENSOR] PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.NUMBER, Platform.SENSOR]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SERVICES = ( SERVICES = (

View File

@ -0,0 +1,61 @@
"""Binary sensor platform for SABnzbd."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SabnzbdConfigEntry
from .entity import SabnzbdEntity
@dataclass(frozen=True, kw_only=True)
class SabnzbdBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Sabnzbd binary sensor entity."""
is_on_fn: Callable[[dict[str, Any]], bool]
BINARY_SENSORS: tuple[SabnzbdBinarySensorEntityDescription, ...] = (
SabnzbdBinarySensorEntityDescription(
key="warnings",
translation_key="warnings",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
is_on_fn=lambda data: data["have_warnings"] != "0",
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: SabnzbdConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Sabnzbd sensor entry."""
coordinator = config_entry.runtime_data
async_add_entities(
[SabnzbdBinarySensor(coordinator, sensor) for sensor in BINARY_SENSORS]
)
class SabnzbdBinarySensor(SabnzbdEntity, BinarySensorEntity):
"""Representation of an SABnzbd binary sensor."""
entity_description: SabnzbdBinarySensorEntityDescription
@property
def is_on(self) -> bool:
"""Return latest sensor data."""
return self.entity_description.is_on_fn(self.coordinator.data)

View File

@ -22,6 +22,11 @@
} }
}, },
"entity": { "entity": {
"binary_sensor": {
"warnings": {
"name": "Warnings"
}
},
"button": { "button": {
"pause": { "pause": {
"name": "[%key:common::action::pause%]" "name": "[%key:common::action::pause%]"

View File

@ -15,7 +15,7 @@
"diskspacetotal2": "7448.42", "diskspacetotal2": "7448.42",
"speedlimit": "85", "speedlimit": "85",
"speedlimit_abs": "22282240", "speedlimit_abs": "22282240",
"have_warnings": "0", "have_warnings": "1",
"finishaction": null, "finishaction": null,
"quota": "0 ", "quota": "0 ",
"have_quota": false, "have_quota": false,

View File

@ -0,0 +1,48 @@
# serializer version: 1
# name: test_sensor[binary_sensor.sabnzbd_warnings-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.sabnzbd_warnings',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
'original_icon': None,
'original_name': 'Warnings',
'platform': 'sabnzbd',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'warnings',
'unique_id': '01JD2YVVPBC62D620DGYNG2R8H_warnings',
'unit_of_measurement': None,
})
# ---
# name: test_sensor[binary_sensor.sabnzbd_warnings-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Sabnzbd Warnings',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.sabnzbd_warnings',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View File

@ -0,0 +1,23 @@
"""Binary sensor tests for the Sabnzbd component."""
from unittest.mock import patch
from syrupy import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
@patch("homeassistant.components.sabnzbd.PLATFORMS", [Platform.BINARY_SENSOR])
async def test_sensor(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test binary sensor setup."""
await hass.config_entries.async_setup(config_entry.entry_id)
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)