-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sensor.py
355 lines (268 loc) 路 10.8 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""Support for Sure PetCare Flaps/Pets sensors."""
from __future__ import annotations
from typing import Any, cast
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_VOLTAGE,
DEVICE_CLASS_BATTERY,
MASS_GRAMS,
PERCENTAGE,
VOLUME_MILLILITERS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from surepy.entities import SurepyEntity
from surepy.entities.devices import (
Feeder as SureFeeder,
FeederBowl as SureFeederBowl,
Felaqua as SureFelaqua,
Flap as SureFlap,
SurepyDevice,
)
from surepy.enums import EntityType, LockState
# pylint: disable=relative-beyond-top-level
from . import SurePetcareAPI
from .const import (
ATTR_VOLTAGE_FULL,
ATTR_VOLTAGE_LOW,
DOMAIN,
SPC,
SURE_BATT_VOLTAGE_FULL,
SURE_BATT_VOLTAGE_LOW,
SURE_MANUFACTURER,
)
PARALLEL_UPDATES = 2
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: Any,
discovery_info: Any = None,
) -> None:
"""Set up Sure PetCare sensor platform."""
await async_setup_entry(hass, config, async_add_entities)
async def async_setup_entry(
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: Any
) -> None:
"""Set up config entry Sure PetCare Flaps sensors."""
entities: list[Flap | Felaqua | Feeder | FeederBowl | Battery] = []
spc: SurePetcareAPI = hass.data[DOMAIN][SPC]
for surepy_entity in spc.coordinator.data.values():
if surepy_entity.type in [
EntityType.CAT_FLAP,
EntityType.PET_FLAP,
]:
entities.append(Flap(spc.coordinator, surepy_entity.id, spc))
elif surepy_entity.type == EntityType.FELAQUA:
entities.append(Felaqua(spc.coordinator, surepy_entity.id, spc))
elif surepy_entity.type == EntityType.FEEDER:
for bowl in surepy_entity.bowls.values():
entities.append(
FeederBowl(spc.coordinator, surepy_entity.id, spc, bowl.raw_data())
)
entities.append(Feeder(spc.coordinator, surepy_entity.id, spc))
if surepy_entity.type in [
EntityType.CAT_FLAP,
EntityType.PET_FLAP,
EntityType.FEEDER,
EntityType.FELAQUA,
]:
voltage_batteries_full = cast(
float,
config_entry.options.get(ATTR_VOLTAGE_FULL, SURE_BATT_VOLTAGE_FULL),
)
voltage_batteries_low = cast(
float, config_entry.options.get(ATTR_VOLTAGE_LOW, SURE_BATT_VOLTAGE_LOW)
)
entities.append(
Battery(
spc.coordinator,
surepy_entity.id,
spc,
voltage_full=voltage_batteries_full,
voltage_low=voltage_batteries_low,
)
)
async_add_entities(entities)
class SurePetcareSensor(CoordinatorEntity, SensorEntity):
"""A binary sensor implementation for Sure Petcare Entities."""
_attr_should_poll = False
def __init__(self, coordinator, _id: int, spc: SurePetcareAPI):
"""Initialize a Sure Petcare sensor."""
super().__init__(coordinator)
self._id = _id
self._spc: SurePetcareAPI = spc
self._coordinator = coordinator
self._surepy_entity: SurepyEntity = self._coordinator.data[_id]
self._state: dict[str, Any] = self._surepy_entity.raw_data()["status"]
self._attr_available = bool(self._state)
self._attr_unique_id = f"{self._surepy_entity.household_id}-{self._id}"
self._attr_extra_state_attributes = (
{**self._surepy_entity.raw_data()} if self._state else {}
)
self._attr_name: str = (
f"{self._surepy_entity.type.name.replace('_', ' ').title()} "
f"{self._surepy_entity.name.capitalize()}"
)
@property
def device_info(self):
device = {}
try:
model = f"{self._surepy_entity.type.name.replace('_', ' ').title()}"
if serial := self._surepy_entity.raw_data().get("serial_number"):
model = f"{model} ({serial})"
elif mac_address := self._surepy_entity.raw_data().get("mac_address"):
model = f"{model} ({mac_address})"
elif tag_id := self._surepy_entity.raw_data().get("tag_id"):
model = f"{model} ({tag_id})"
device = {
"identifiers": {(DOMAIN, self._id)},
"name": self._surepy_entity.name.capitalize(),
"manufacturer": SURE_MANUFACTURER,
"model": model,
}
if self._state:
versions = self._state.get("version", {})
if dev_fw_version := versions.get("device", {}).get("firmware"):
device["sw_version"] = dev_fw_version
if (lcd_version := versions.get("lcd", {})) and (
rf_version := versions.get("rf", {})
):
device["sw_version"] = (
f"lcd: {lcd_version.get('version', lcd_version)['firmware']} | "
f"fw: {rf_version.get('version', rf_version)['firmware']}"
)
except AttributeError:
pass
return device
class Flap(SurePetcareSensor):
"""Sure Petcare Flap."""
def __init__(self, coordinator, _id: int, spc: SurePetcareAPI) -> None:
super().__init__(coordinator, _id, spc)
self._surepy_entity: SureFlap
self._attr_entity_picture = self._surepy_entity.icon
self._attr_unit_of_measurement = None
if self._state:
self._attr_extra_state_attributes = {
"learn_mode": bool(self._state["learn_mode"]),
**self._surepy_entity.raw_data(),
}
if locking := self._state.get("locking"):
self._attr_state = LockState(locking["mode"]).name.casefold()
@property
def state(self) -> str | None:
"""Return battery level in percent."""
if (
state := cast(SureFlap, self._coordinator.data[self._id])
.raw_data()
.get("status")
):
return LockState(state["locking"]["mode"]).name.casefold()
class Felaqua(SurePetcareSensor):
"""Sure Petcare Felaqua."""
def __init__(self, coordinator, _id: int, spc: SurePetcareAPI):
super().__init__(coordinator, _id, spc)
self._surepy_entity: SureFelaqua
self._attr_entity_picture = self._surepy_entity.icon
self._attr_unit_of_measurement = VOLUME_MILLILITERS
@property
def state(self) -> float | None:
"""Return the remaining water."""
if felaqua := cast(SureFelaqua, self._coordinator.data[self._id]):
return int(felaqua.water_remaining) if felaqua.water_remaining else None
class FeederBowl(SurePetcareSensor):
"""Sure Petcare Feeder Bowl."""
def __init__(
self,
coordinator,
_id: int,
spc: SurePetcareAPI,
bowl_data: dict[str, int | str],
):
"""Initialize a Bowl sensor."""
super().__init__(coordinator, _id, spc)
self.feeder_id = _id
self.bowl_id = int(bowl_data["index"])
self._id = int(f"{_id}{str(self.bowl_id)}")
self._spc: SurePetcareAPI = spc
self._surepy_feeder_entity: SurepyEntity = self._coordinator.data[_id]
self._surepy_entity: SureFeederBowl = self._coordinator.data[_id].bowls[
self.bowl_id
]
self._state: dict[str, Any] = bowl_data
# https://github.com/PyCQA/pylint/issues/2062
# pylint: disable=no-member
self._attr_name = (
f"{EntityType.FEEDER.name.replace('_', ' ').title()} "
f"{self._surepy_entity.name.capitalize()}"
)
self._attr_icon = "mdi:bowl"
self._attr_state = int(self._surepy_entity.weight)
self._attr_unique_id = (
f"{self._surepy_feeder_entity.household_id}-{self.feeder_id}-{self.bowl_id}"
)
self._attr_unit_of_measurement = MASS_GRAMS
@property
def state(self) -> float | None:
"""Return the remaining water."""
if (feeder := cast(SureFeeder, self._coordinator.data[self.feeder_id])) and (
weight := feeder.bowls[self.bowl_id].weight
):
return int(weight) if weight and weight > 0 else None
class Feeder(SurePetcareSensor):
"""Sure Petcare Feeder."""
def __init__(self, coordinator, _id: int, spc: SurePetcareAPI):
super().__init__(coordinator, _id, spc)
self._surepy_entity: SureFeeder
self._attr_entity_picture = self._surepy_entity.icon
self._attr_unit_of_measurement = MASS_GRAMS
@property
def state(self) -> float | None:
"""Return the total remaining food."""
if feeder := cast(SureFeeder, self._coordinator.data[self._id]):
return int(feeder.total_weight) if feeder.total_weight else None
class Battery(SurePetcareSensor):
"""Sure Petcare Flap."""
def __init__(
self,
coordinator,
_id: int,
spc: SurePetcareAPI,
voltage_full: float,
voltage_low: float,
):
super().__init__(coordinator, _id, spc)
self._surepy_entity: SurepyDevice
self._attr_name = f"{self._attr_name} Battery Level"
self.voltage_low = voltage_low
self.voltage_full = voltage_full
self._attr_unit_of_measurement = PERCENTAGE
self._attr_device_class = DEVICE_CLASS_BATTERY
self._attr_unique_id = (
f"{self._surepy_entity.household_id}-{self._surepy_entity.id}-battery"
)
@property
def state(self) -> int | None:
"""Return battery level in percent."""
if battery := cast(SurepyDevice, self._coordinator.data[self._id]):
self._surepy_entity = battery
battery_level = battery.calculate_battery_level(
voltage_full=self.voltage_full, voltage_low=self.voltage_low
)
# return batterie level between 0 and 100
return battery_level
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the additional attrs."""
attrs = {}
if (device := cast(SurepyDevice, self._coordinator.data[self._id])) and (
state := device.raw_data().get("status")
):
self._surepy_entity = device
voltage = float(state["battery"])
attrs = {
"battery_level": device.battery_level,
ATTR_VOLTAGE: f"{voltage:.2f}",
f"{ATTR_VOLTAGE}_per_battery": f"{voltage / 4:.2f}",
}
return attrs