Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensor platform for SABnzbd."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12  BinarySensorEntityDescription,
13 )
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import SabnzbdConfigEntry
19 from .entity import SabnzbdEntity
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Describes Sabnzbd binary sensor entity."""
25 
26  is_on_fn: Callable[[dict[str, Any]], bool]
27 
28 
29 BINARY_SENSORS: tuple[SabnzbdBinarySensorEntityDescription, ...] = (
31  key="warnings",
32  translation_key="warnings",
33  device_class=BinarySensorDeviceClass.PROBLEM,
34  entity_category=EntityCategory.DIAGNOSTIC,
35  is_on_fn=lambda data: data["have_warnings"] != "0",
36  ),
37 )
38 
39 
41  hass: HomeAssistant,
42  config_entry: SabnzbdConfigEntry,
43  async_add_entities: AddEntitiesCallback,
44 ) -> None:
45  """Set up a Sabnzbd sensor entry."""
46  coordinator = config_entry.runtime_data
47 
49  [SabnzbdBinarySensor(coordinator, sensor) for sensor in BINARY_SENSORS]
50  )
51 
52 
54  """Representation of an SABnzbd binary sensor."""
55 
56  entity_description: SabnzbdBinarySensorEntityDescription
57 
58  @property
59  def is_on(self) -> bool:
60  """Return latest sensor data."""
61  return self.entity_descriptionentity_description.is_on_fn(self.coordinator.data)
None async_setup_entry(HomeAssistant hass, SabnzbdConfigEntry config_entry, AddEntitiesCallback async_add_entities)