1 """Component providing support for Reolink sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import date, datetime
8 from decimal
import Decimal
10 from reolink_aio.api
import Host
11 from reolink_aio.enums
import BatteryEnum
16 SensorEntityDescription,
25 ReolinkChannelCoordinatorEntity,
26 ReolinkChannelEntityDescription,
27 ReolinkHostCoordinatorEntity,
28 ReolinkHostEntityDescription,
30 from .util
import ReolinkConfigEntry, ReolinkData
35 @dataclass(frozen=True, kw_only=True)
37 SensorEntityDescription,
38 ReolinkChannelEntityDescription,
40 """A class that describes sensor entities for a camera channel."""
42 value: Callable[[Host, int], StateType]
45 @dataclass(frozen=True, kw_only=True)
47 SensorEntityDescription,
48 ReolinkHostEntityDescription,
50 """A class that describes host sensor entities."""
52 value: Callable[[Host], StateType]
57 key=
"ptz_pan_position",
58 cmd_key=
"GetPtzCurPos",
59 translation_key=
"ptz_pan_position",
60 state_class=SensorStateClass.MEASUREMENT,
61 entity_category=EntityCategory.DIAGNOSTIC,
62 value=
lambda api, ch: api.ptz_pan_position(ch),
63 supported=
lambda api, ch: api.supported(ch,
"ptz_pan_position"),
66 key=
"ptz_tilt_position",
67 cmd_key=
"GetPtzCurPos",
68 translation_key=
"ptz_tilt_position",
69 state_class=SensorStateClass.MEASUREMENT,
70 entity_category=EntityCategory.DIAGNOSTIC,
71 value=
lambda api, ch: api.ptz_tilt_position(ch),
72 supported=
lambda api, ch: api.supported(ch,
"ptz_tilt_position"),
75 key=
"battery_percent",
77 cmd_key=
"GetBatteryInfo",
78 native_unit_of_measurement=PERCENTAGE,
79 device_class=SensorDeviceClass.BATTERY,
80 state_class=SensorStateClass.MEASUREMENT,
81 entity_category=EntityCategory.DIAGNOSTIC,
82 value=
lambda api, ch: api.battery_percentage(ch),
83 supported=
lambda api, ch: api.supported(ch,
"battery"),
86 key=
"battery_temperature",
88 cmd_key=
"GetBatteryInfo",
89 translation_key=
"battery_temperature",
90 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
91 device_class=SensorDeviceClass.TEMPERATURE,
92 state_class=SensorStateClass.MEASUREMENT,
93 entity_category=EntityCategory.DIAGNOSTIC,
94 entity_registry_enabled_default=
False,
95 value=
lambda api, ch: api.battery_temperature(ch),
96 supported=
lambda api, ch: api.supported(ch,
"battery"),
101 cmd_key=
"GetBatteryInfo",
102 translation_key=
"battery_state",
103 device_class=SensorDeviceClass.ENUM,
104 entity_category=EntityCategory.DIAGNOSTIC,
105 entity_registry_enabled_default=
False,
106 options=[state.name
for state
in BatteryEnum],
107 value=
lambda api, ch: BatteryEnum(api.battery_status(ch)).name,
108 supported=
lambda api, ch: api.supported(ch,
"battery"),
115 cmd_key=
"GetWifiSignal",
116 translation_key=
"wifi_signal",
117 state_class=SensorStateClass.MEASUREMENT,
118 entity_category=EntityCategory.DIAGNOSTIC,
119 entity_registry_enabled_default=
False,
120 value=
lambda api: api.wifi_signal,
121 supported=
lambda api: api.supported(
None,
"wifi")
and api.wifi_connection,
125 cmd_key=
"GetPerformance",
126 translation_key=
"cpu_usage",
127 native_unit_of_measurement=PERCENTAGE,
128 state_class=SensorStateClass.MEASUREMENT,
129 entity_category=EntityCategory.DIAGNOSTIC,
130 entity_registry_enabled_default=
False,
131 value=
lambda api: api.cpu_usage,
132 supported=
lambda api: api.supported(
None,
"performance"),
139 cmd_key=
"GetHddInfo",
140 native_unit_of_measurement=PERCENTAGE,
141 state_class=SensorStateClass.MEASUREMENT,
142 entity_category=EntityCategory.DIAGNOSTIC,
143 entity_registry_enabled_default=
False,
144 value=
lambda api, idx: api.hdd_storage(idx),
145 supported=
lambda api, idx: api.supported(
None,
"hdd"),
152 config_entry: ReolinkConfigEntry,
153 async_add_entities: AddEntitiesCallback,
155 """Set up a Reolink IP Camera."""
156 reolink_data: ReolinkData = config_entry.runtime_data
159 ReolinkSensorEntity | ReolinkHostSensorEntity | ReolinkHddSensorEntity
162 for entity_description
in SENSORS
163 for channel
in reolink_data.host.api.channels
164 if entity_description.supported(reolink_data.host.api, channel)
168 for entity_description
in HOST_SENSORS
169 if entity_description.supported(reolink_data.host.api)
173 for entity_description
in HDD_SENSORS
174 for hdd_index
in reolink_data.host.api.hdd_list
175 if entity_description.supported(reolink_data.host.api, hdd_index)
181 """Base sensor class for Reolink IP camera sensors."""
183 entity_description: ReolinkSensorEntityDescription
187 reolink_data: ReolinkData,
189 entity_description: ReolinkSensorEntityDescription,
191 """Initialize Reolink sensor."""
193 super().
__init__(reolink_data, channel)
197 """Return the value reported by the sensor."""
202 """Base sensor class for Reolink host sensors."""
204 entity_description: ReolinkHostSensorEntityDescription
208 reolink_data: ReolinkData,
209 entity_description: ReolinkHostSensorEntityDescription,
211 """Initialize Reolink host sensor."""
217 """Return the value reported by the sensor."""
222 """Base sensor class for Reolink host sensors."""
224 entity_description: ReolinkSensorEntityDescription
228 reolink_data: ReolinkData,
230 entity_description: ReolinkSensorEntityDescription,
232 """Initialize Reolink host sensor."""
238 f
"{self._host.unique_id}_{hdd_index}_{entity_description.key}"
240 if self.
_host_host.api.hdd_type(hdd_index) ==
"HDD":
247 """Return the value reported by the sensor."""
252 """Return True if entity is available."""
253 return self.
_host_host.api.hdd_available(self.
_hdd_index_hdd_index)
and super().available
_attr_translation_placeholders
None __init__(self, ReolinkData reolink_data, int hdd_index, ReolinkSensorEntityDescription entity_description)
StateType|date|datetime|Decimal native_value(self)
None __init__(self, ReolinkData reolink_data, ReolinkHostSensorEntityDescription entity_description)
StateType|date|datetime|Decimal native_value(self)
None __init__(self, ReolinkData reolink_data, int channel, ReolinkSensorEntityDescription entity_description)
StateType|date|datetime|Decimal native_value(self)
None async_setup_entry(HomeAssistant hass, ReolinkConfigEntry config_entry, AddEntitiesCallback async_add_entities)