forked from benleb/sureha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_sensor.py
257 lines (184 loc) · 7.81 KB
/
binary_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
"""Support for Sure PetCare Flaps/Pets binary sensors."""
from __future__ import annotations
from typing import Any
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY,
DEVICE_CLASS_PRESENCE,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from surepy.entities import SurepyEntity
from surepy.entities.devices import Hub as SureHub, SurepyDevice
from surepy.entities.pet import Pet as SurePet
from surepy.enums import EntityType, Location
# pylint: disable=relative-beyond-top-level
from . import SurePetcareAPI
from .const import DOMAIN, SPC, 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 binary-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[SurePetcareBinarySensor] = []
spc: SurePetcareAPI = hass.data[DOMAIN][SPC]
for surepy_entity in spc.coordinator.data.values():
if surepy_entity.type == EntityType.PET:
entities.append(Pet(spc.coordinator, surepy_entity.id, spc))
elif surepy_entity.type == EntityType.HUB:
entities.append(Hub(spc.coordinator, surepy_entity.id, spc))
# connectivity
elif surepy_entity.type in [
EntityType.CAT_FLAP,
EntityType.PET_FLAP,
EntityType.FEEDER,
EntityType.FELAQUA,
]:
entities.append(DeviceConnectivity(spc.coordinator, surepy_entity.id, spc))
async_add_entities(entities, True)
class SurePetcareBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""A binary sensor implementation for Sure Petcare Entities."""
_attr_should_poll = False
def __init__(
self,
coordinator,
_id: int,
spc: SurePetcareAPI,
device_class: str,
):
"""Initialize a Sure Petcare binary sensor."""
super().__init__(coordinator)
self._id: int = _id
self._spc: SurePetcareAPI = spc
self._coordinator = coordinator
self._surepy_entity: SurepyEntity = self._coordinator.data[self._id]
self._state: Any = self._surepy_entity.raw_data().get("status", {})
type_name = self._surepy_entity.type.name.replace("_", " ").title()
self._name: str = (
# cover edge case where a device has no name set
# (dont know how to do this but people have managed to do it ¯\_(ツ)_/¯)
self._surepy_entity.name
if self._surepy_entity.name
else f"Unnamed {type_name}"
)
self._attr_available = bool(self._state)
self._attr_device_class = None if not device_class else device_class
self._attr_name: str = f"{type_name} {self._name}"
self._attr_unique_id = f"{self._surepy_entity.household_id}-{self._id}"
if self._state:
self._attr_extra_state_attributes = {**self._surepy_entity.raw_data()}
@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 Hub(SurePetcareBinarySensor):
"""Sure Petcare Pet."""
def __init__(self, coordinator, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare Hub."""
super().__init__(coordinator, _id, spc, DEVICE_CLASS_CONNECTIVITY)
if self._attr_device_info:
self._attr_device_info["identifiers"] = {(DOMAIN, str(self._id))}
self._attr_available = self.is_on
@property
def is_on(self) -> bool:
"""Return True if the hub is on."""
hub: SureHub
online: bool = False
if hub := self._coordinator.data[self._id]:
self._attr_extra_state_attributes = {
"led_mode": int(hub.raw_data()["status"]["led_mode"]),
"pairing_mode": bool(hub.raw_data()["status"]["pairing_mode"]),
}
online = hub.online
return online
class Pet(SurePetcareBinarySensor):
"""Sure Petcare Pet."""
def __init__(self, coordinator, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare Pet."""
super().__init__(coordinator, _id, spc, DEVICE_CLASS_PRESENCE)
# explicit typing
self._surepy_entity: SurePet
# picture of the pet that can be added via the sure app/website
self._attr_entity_picture = self._surepy_entity.photo_url
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the additional attrs."""
pet: SurePet
attrs: dict[str, Any] = {}
if pet := self._coordinator.data[self._id]:
attrs = {
"since": pet.location.since,
"where": pet.location.where,
**pet.raw_data(),
}
return attrs
@property
def is_on(self) -> bool:
"""Return True if the pet is at home."""
pet: SurePet
inside: bool = False
if pet := self._coordinator.data[self._id]:
inside = bool(pet.location.where == Location.INSIDE)
return inside
class DeviceConnectivity(SurePetcareBinarySensor):
"""Sure Petcare Connectivity Sensor."""
def __init__(self, coordinator, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare device connectivity sensor."""
super().__init__(coordinator, _id, spc, DEVICE_CLASS_CONNECTIVITY)
self._attr_name = f"{self._name} Connectivity"
self._attr_unique_id = (
f"{self._surepy_entity.household_id}-{self._id}-connectivity"
)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the additional attrs."""
device: SurepyDevice
attrs: dict[str, Any] = {}
if (device := self._coordinator.data[self._id]) and (
state := device.raw_data().get("status")
):
attrs = {
"device_rssi": f'{state["signal"]["device_rssi"]:.2f}',
"hub_rssi": f'{state["signal"]["hub_rssi"]:.2f}',
}
return attrs
@property
def is_on(self) -> bool:
"""Return True if the pet is at home."""
return bool(self.extra_state_attributes)