Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for monitoring an SABnzbd NZB client."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
8  SensorDeviceClass,
9  SensorEntity,
10  SensorEntityDescription,
11  SensorStateClass,
12 )
13 from homeassistant.const import UnitOfDataRate, UnitOfInformation
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.typing import StateType
17 
18 from . import SabnzbdConfigEntry
19 from .entity import SabnzbdEntity
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Describes Sabnzbd sensor entity."""
25 
26  key: str
27 
28 
29 SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = (
31  key="status",
32  translation_key="status",
33  ),
35  key="kbpersec",
36  translation_key="speed",
37  device_class=SensorDeviceClass.DATA_RATE,
38  native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
39  suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
40  suggested_display_precision=1,
41  state_class=SensorStateClass.MEASUREMENT,
42  ),
44  key="mb",
45  translation_key="queue",
46  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
47  device_class=SensorDeviceClass.DATA_SIZE,
48  state_class=SensorStateClass.MEASUREMENT,
49  ),
51  key="mbleft",
52  translation_key="left",
53  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
54  device_class=SensorDeviceClass.DATA_SIZE,
55  state_class=SensorStateClass.MEASUREMENT,
56  ),
58  key="diskspacetotal1",
59  translation_key="total_disk_space",
60  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
61  device_class=SensorDeviceClass.DATA_SIZE,
62  state_class=SensorStateClass.MEASUREMENT,
63  ),
65  key="diskspace1",
66  translation_key="free_disk_space",
67  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
68  device_class=SensorDeviceClass.DATA_SIZE,
69  state_class=SensorStateClass.MEASUREMENT,
70  ),
72  key="noofslots_total",
73  translation_key="queue_count",
74  state_class=SensorStateClass.TOTAL,
75  suggested_display_precision=2,
76  ),
78  key="day_size",
79  translation_key="daily_total",
80  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
81  device_class=SensorDeviceClass.DATA_SIZE,
82  entity_registry_enabled_default=False,
83  state_class=SensorStateClass.TOTAL_INCREASING,
84  suggested_display_precision=2,
85  ),
87  key="week_size",
88  translation_key="weekly_total",
89  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
90  device_class=SensorDeviceClass.DATA_SIZE,
91  entity_registry_enabled_default=False,
92  state_class=SensorStateClass.TOTAL_INCREASING,
93  suggested_display_precision=2,
94  ),
96  key="month_size",
97  translation_key="monthly_total",
98  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
99  device_class=SensorDeviceClass.DATA_SIZE,
100  entity_registry_enabled_default=False,
101  state_class=SensorStateClass.TOTAL_INCREASING,
102  suggested_display_precision=2,
103  ),
105  key="total_size",
106  translation_key="overall_total",
107  native_unit_of_measurement=UnitOfInformation.GIGABYTES,
108  device_class=SensorDeviceClass.DATA_SIZE,
109  state_class=SensorStateClass.TOTAL_INCREASING,
110  suggested_display_precision=2,
111  ),
112 )
113 
114 
116  hass: HomeAssistant,
117  config_entry: SabnzbdConfigEntry,
118  async_add_entities: AddEntitiesCallback,
119 ) -> None:
120  """Set up a Sabnzbd sensor entry."""
121  coordinator = config_entry.runtime_data
122 
123  async_add_entities([SabnzbdSensor(coordinator, sensor) for sensor in SENSOR_TYPES])
124 
125 
127  """Representation of an SABnzbd sensor."""
128 
129  entity_description: SabnzbdSensorEntityDescription
130 
131  @property
132  def native_value(self) -> StateType:
133  """Return latest sensor data."""
134  return self.coordinator.data.get(self.entity_descriptionentity_description.key)
None async_setup_entry(HomeAssistant hass, SabnzbdConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:119