Skip to content

Commit 3d1fe26

Browse files
committed
Safeguards for device number properties
1 parent 42ec3c2 commit 3d1fe26

10 files changed

Lines changed: 232 additions & 134 deletions

custom_components/petlibro/devices/feeders/air_smart_feeder.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ def today_feeding_quantities(self) -> list[int]:
5858
return self._data.get("grainStatus", {}).get("todayFeedingQuantities", [])
5959

6060
@property
61-
def today_feeding_quantity(self) -> int:
62-
return self._data.get("grainStatus", {}).get("todayFeedingQuantity", 0)
61+
def today_feeding_quantity(self) -> float:
62+
quantity = self._data.get("grainStatus", {}).get("todayFeedingQuantity")
63+
return quantity if isinstance(quantity, (int, float)) else 0
6364

6465
@property
6566
def today_feeding_times(self) -> int:
66-
return self._data.get("grainStatus", {}).get("todayFeedingTimes", 0)
67+
times = self._data.get("grainStatus", {}).get("todayFeedingTimes")
68+
return times if isinstance(times, int) else 0
6769

6870
@property
6971
def feeding_plan_state(self) -> bool:
@@ -135,11 +137,13 @@ def wifi_ssid(self) -> str:
135137

136138
@property
137139
def wifi_rssi(self) -> int:
138-
return self._data.get("realInfo", {}).get("wifiRssi", -100)
140+
wifi_rssi = self._data.get("realInfo", {}).get("wifiRssi")
141+
return wifi_rssi if isinstance(wifi_rssi, int) else -100
139142

140143
@property
141-
def electric_quantity(self) -> int:
142-
return self._data.get("realInfo", {}).get("electricQuantity", 0)
144+
def electric_quantity(self) -> float:
145+
quantity = self._data.get("realInfo", {}).get("electricQuantity")
146+
return quantity if isinstance(quantity, (float, int)) else 0
143147

144148
@property
145149
def enable_feeding_plan(self) -> bool:
@@ -180,7 +184,8 @@ def child_lock_switch(self) -> bool:
180184

181185
@property
182186
def close_door_time_sec(self) -> int:
183-
return self._data.get("realInfo", {}).get("closeDoorTimeSec", 0)
187+
time_sec = self._data.get("realInfo", {}).get("closeDoorTimeSec")
188+
return time_sec if isinstance(time_sec, int) else 0
184189

185190
@property
186191
def screen_display_switch(self) -> bool:
@@ -217,15 +222,16 @@ def last_feed_time(self) -> datetime | None:
217222
return None
218223

219224
@property
220-
def last_feed_quantity(self) -> int | None:
225+
def last_feed_quantity(self) -> int:
221226
"""Return the last feed amount."""
222227
raw = self._data.get("workRecord", [])
223228
if raw and isinstance(raw, list):
224229
for day_entry in raw:
225230
for record in day_entry.get("workRecords", []):
226231
_LOGGER.debug("Evaluating record type: %s", record.get("type"))
227232
if record.get("type") == "GRAIN_OUTPUT_SUCCESS":
228-
return record.get("actualGrainNum") or 0
233+
actualGrainNum = record.get("actualGrainNum")
234+
return actualGrainNum if isinstance(actualGrainNum, int) else 0
229235
return 0
230236

231237
@property
@@ -303,13 +309,14 @@ def next_feed_time(self) -> datetime | None:
303309
return None
304310

305311
@property
306-
def next_feed_quantity(self) -> int | None:
312+
def next_feed_quantity(self) -> int:
307313
"""Return the next scheduled feed amount."""
308314
next_feed = self.get_next_feed.copy()
309315
if next_feed and (plan_id := next_feed.get("id")):
310316
feeding_plan = self.feeding_plan_data.get(str(plan_id), {})
311317
if feeding_plan:
312-
return feeding_plan.get("grainNum", 0)
318+
grainNum = feeding_plan.get("grainNum")
319+
return grainNum if isinstance(grainNum, int) else 0
313320
return 0
314321

315322
@property

custom_components/petlibro/devices/feeders/granary_smart_camera_feeder.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ def today_feeding_quantities(self) -> list[int]:
5555
return self._data.get("grainStatus", {}).get("todayFeedingQuantities", [])
5656

5757
@property
58-
def today_feeding_quantity(self) -> int:
59-
return self._data.get("grainStatus", {}).get("todayFeedingQuantity", 0)
58+
def today_feeding_quantity(self) -> float:
59+
quantity = self._data.get("grainStatus", {}).get("todayFeedingQuantity")
60+
return quantity if isinstance(quantity, (int, float)) else 0
6061

6162
@property
6263
def today_feeding_times(self) -> int:
63-
return self._data.get("grainStatus", {}).get("todayFeedingTimes", 0)
64+
times = self._data.get("grainStatus", {}).get("todayFeedingTimes")
65+
return times if isinstance(times, int) else 0
6466

6567
@property
6668
def feeding_plan_state(self) -> bool:
@@ -132,11 +134,13 @@ def wifi_ssid(self) -> str:
132134

133135
@property
134136
def wifi_rssi(self) -> int:
135-
return self._data.get("realInfo", {}).get("wifiRssi", -100)
137+
wifi_rssi = self._data.get("realInfo", {}).get("wifiRssi")
138+
return wifi_rssi if isinstance(wifi_rssi, int) else -100
136139

137140
@property
138-
def electric_quantity(self) -> int:
139-
return self._data.get("realInfo", {}).get("electricQuantity", 0)
141+
def electric_quantity(self) -> float:
142+
quantity = self._data.get("realInfo", {}).get("electricQuantity")
143+
return quantity if isinstance(quantity, (float, int)) else 0
140144

141145
@property
142146
def enable_feeding_plan(self) -> bool:
@@ -177,7 +181,8 @@ def child_lock_switch(self) -> bool:
177181

178182
@property
179183
def close_door_time_sec(self) -> int:
180-
return self._data.get("realInfo", {}).get("closeDoorTimeSec", 0)
184+
time_sec = self._data.get("realInfo", {}).get("closeDoorTimeSec")
185+
return time_sec if isinstance(time_sec, int) else 0
181186

182187
@property
183188
def screen_display_switch(self) -> bool:
@@ -239,15 +244,16 @@ def last_feed_time(self) -> datetime | None:
239244
return None
240245

241246
@property
242-
def last_feed_quantity(self) -> int | None:
247+
def last_feed_quantity(self) -> int:
243248
"""Return the last feed amount."""
244249
raw = self._data.get("workRecord", [])
245250
if raw and isinstance(raw, list):
246251
for day_entry in raw:
247252
for record in day_entry.get("workRecords", []):
248253
_LOGGER.debug("Evaluating record type: %s", record.get("type"))
249254
if record.get("type") == "GRAIN_OUTPUT_SUCCESS":
250-
return record.get("actualGrainNum") or 0
255+
actualGrainNum = record.get("actualGrainNum")
256+
return actualGrainNum if isinstance(actualGrainNum, int) else 0
251257
return 0
252258

253259
@property
@@ -325,13 +331,14 @@ def next_feed_time(self) -> datetime | None:
325331
return None
326332

327333
@property
328-
def next_feed_quantity(self) -> int | None:
334+
def next_feed_quantity(self) -> int:
329335
"""Return the next scheduled feed amount."""
330336
next_feed = self.get_next_feed.copy()
331337
if next_feed and (plan_id := next_feed.get("id")):
332338
feeding_plan = self.feeding_plan_data.get(str(plan_id), {})
333339
if feeding_plan:
334-
return feeding_plan.get("grainNum", 0)
340+
grainNum = feeding_plan.get("grainNum")
341+
return grainNum if isinstance(grainNum, int) else 0
335342
return 0
336343

337344
@property

custom_components/petlibro/devices/feeders/granary_smart_feeder.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ def today_feeding_quantities(self) -> list[int]:
5757
return self._data.get("grainStatus", {}).get("todayFeedingQuantities", [])
5858

5959
@property
60-
def today_feeding_quantity(self) -> int:
61-
return self._data.get("grainStatus", {}).get("todayFeedingQuantity", 0)
60+
def today_feeding_quantity(self) -> float:
61+
quantity = self._data.get("grainStatus", {}).get("todayFeedingQuantity")
62+
return quantity if isinstance(quantity, (int, float)) else 0
6263

6364
@property
6465
def today_feeding_times(self) -> int:
65-
return self._data.get("grainStatus", {}).get("todayFeedingTimes", 0)
66+
times = self._data.get("grainStatus", {}).get("todayFeedingTimes")
67+
return times if isinstance(times, (int)) else 0
6668

6769
@property
6870
def feeding_plan_state(self) -> bool:
@@ -134,11 +136,13 @@ def wifi_ssid(self) -> str:
134136

135137
@property
136138
def wifi_rssi(self) -> int:
137-
return self._data.get("realInfo", {}).get("wifiRssi", -100)
139+
wifi_rssi = self._data.get("realInfo", {}).get("wifiRssi")
140+
return wifi_rssi if isinstance(wifi_rssi, int) else -100
138141

139142
@property
140-
def electric_quantity(self) -> int:
141-
return self._data.get("realInfo", {}).get("electricQuantity", 0)
143+
def electric_quantity(self) -> float:
144+
quantity = self._data.get("realInfo", {}).get("electricQuantity")
145+
return quantity if isinstance(quantity, (float, int)) else 0
142146

143147
@property
144148
def enable_feeding_plan(self) -> bool:
@@ -179,7 +183,8 @@ def child_lock_switch(self) -> bool:
179183

180184
@property
181185
def close_door_time_sec(self) -> int:
182-
return self._data.get("realInfo", {}).get("closeDoorTimeSec", 0)
186+
time_sec = self._data.get("realInfo", {}).get("closeDoorTimeSec")
187+
return time_sec if isinstance(time_sec, int) else 0
183188

184189
@property
185190
def screen_display_switch(self) -> bool:
@@ -215,15 +220,16 @@ def last_feed_time(self) -> datetime | None:
215220
return None
216221

217222
@property
218-
def last_feed_quantity(self) -> int | None:
223+
def last_feed_quantity(self) -> int:
219224
"""Return the last feed amount."""
220225
raw = self._data.get("workRecord", [])
221226
if raw and isinstance(raw, list):
222227
for day_entry in raw:
223228
for record in day_entry.get("workRecords", []):
224229
_LOGGER.debug("Evaluating record type: %s", record.get("type"))
225230
if record.get("type") == "GRAIN_OUTPUT_SUCCESS":
226-
return record.get("actualGrainNum") or 0
231+
actualGrainNum = record.get("actualGrainNum")
232+
return actualGrainNum if isinstance(actualGrainNum, int) else 0
227233
return 0
228234

229235
@property
@@ -301,13 +307,14 @@ def next_feed_time(self) -> datetime | None:
301307
return None
302308

303309
@property
304-
def next_feed_quantity(self) -> int | None:
310+
def next_feed_quantity(self) -> int:
305311
"""Return the next scheduled feed amount."""
306312
next_feed = self.get_next_feed.copy()
307313
if next_feed and (plan_id := next_feed.get("id")):
308314
feeding_plan = self.feeding_plan_data.get(str(plan_id), {})
309315
if feeding_plan:
310-
return feeding_plan.get("grainNum", 0)
316+
grainNum = feeding_plan.get("grainNum")
317+
return grainNum if isinstance(grainNum, int) else 0
311318
return 0
312319

313320
@property
@@ -319,7 +326,8 @@ def manual_feed_quantity(self):
319326

320327
@property
321328
def desiccant_frequency(self) -> float:
322-
return self._data.get("realInfo", {}).get("changeDesiccantFrequency", 0)
329+
frequency = self._data.get("realInfo", {}).get("changeDesiccantFrequency")
330+
return frequency if isinstance(frequency, (float, int)) else 0
323331

324332
# Error-handling updated for set_feeding_plan
325333
async def set_feeding_plan(self, value: bool) -> None:

custom_components/petlibro/devices/feeders/one_rfid_smart_feeder.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ def today_feeding_quantities(self) -> list[int]:
5959
return self._data.get("grainStatus", {}).get("todayFeedingQuantities", [])
6060

6161
@property
62-
def today_feeding_quantity(self) -> int:
63-
return self._data.get("grainStatus", {}).get("todayFeedingQuantity", 0)
62+
def today_feeding_quantity(self) -> float:
63+
quantity = self._data.get("grainStatus", {}).get("todayFeedingQuantity")
64+
return quantity if isinstance(quantity, (int, float)) else 0
6465

6566
@property
6667
def today_feeding_times(self) -> int:
67-
return self._data.get("grainStatus", {}).get("todayFeedingTimes", 0)
68+
times = self._data.get("grainStatus", {}).get("todayFeedingTimes")
69+
return times if isinstance(times, int) else 0
6870

6971
@property
7072
def today_eating_times(self) -> int:
@@ -152,11 +154,13 @@ def wifi_ssid(self) -> str:
152154

153155
@property
154156
def wifi_rssi(self) -> int:
155-
return self._data.get("realInfo", {}).get("wifiRssi", -100)
157+
wifi_rssi = self._data.get("realInfo", {}).get("wifiRssi")
158+
return wifi_rssi if isinstance(wifi_rssi, int) else -100
156159

157160
@property
158-
def electric_quantity(self) -> int:
159-
return self._data.get("realInfo", {}).get("electricQuantity", 0)
161+
def electric_quantity(self) -> float:
162+
quantity = self._data.get("realInfo", {}).get("electricQuantity")
163+
return quantity if isinstance(quantity, (float, int)) else 0
160164

161165
@property
162166
def enable_feeding_plan(self) -> bool:
@@ -197,7 +201,8 @@ def child_lock_switch(self) -> bool:
197201

198202
@property
199203
def close_door_time_sec(self) -> int:
200-
return self._data.get("realInfo", {}).get("closeDoorTimeSec", 0)
204+
time_sec = self._data.get("realInfo", {}).get("closeDoorTimeSec")
205+
return time_sec if isinstance(time_sec, int) else 0
201206

202207
@property
203208
def display_switch(self) -> bool:
@@ -242,15 +247,16 @@ def last_feed_time(self) -> datetime | None:
242247
return None
243248

244249
@property
245-
def last_feed_quantity(self) -> int | None:
250+
def last_feed_quantity(self) -> int:
246251
"""Return the last feed amount."""
247252
raw = self._data.get("workRecord", [])
248253
if raw and isinstance(raw, list):
249254
for day_entry in raw:
250255
for record in day_entry.get("workRecords", []):
251256
_LOGGER.debug("Evaluating record type: %s", record.get("type"))
252257
if record.get("type") == "GRAIN_OUTPUT_SUCCESS":
253-
return record.get("actualGrainNum") or 0
258+
actualGrainNum = record.get("actualGrainNum")
259+
return actualGrainNum if isinstance(actualGrainNum, int) else 0
254260
return 0
255261

256262
@property
@@ -327,13 +333,14 @@ def next_feed_time(self) -> datetime | None:
327333
return None
328334

329335
@property
330-
def next_feed_quantity(self) -> int | None:
336+
def next_feed_quantity(self) -> int:
331337
"""Return the next scheduled feed amount."""
332338
next_feed = self.get_next_feed.copy()
333339
if next_feed and (plan_id := next_feed.get("id")):
334340
feeding_plan = self.feeding_plan_data.get(str(plan_id), {})
335341
if feeding_plan:
336-
return feeding_plan.get("grainNum", 0)
342+
grainNum = feeding_plan.get("grainNum")
343+
return grainNum if isinstance(grainNum, int) else 0
337344
return 0
338345

339346
@property
@@ -670,4 +677,4 @@ def update_progress(self) -> float:
670677
return 0.0
671678

672679
progress = upgrade_data.get("progress")
673-
return float(progress) if progress is not None else 0.0
680+
return float(progress) if progress is not None else 0.0

custom_components/petlibro/devices/feeders/polar_wet_food_feeder.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ def door_blocked(self) -> bool:
6767
return bool(self._data.get("realInfo", {}).get("barnDoorError", False))
6868

6969
@property
70-
def electric_quantity(self) -> int:
70+
def electric_quantity(self) -> float:
7171
"""Electric quantity (battery percentage or power state)."""
72-
return self._data.get("electricQuantity", 0)
72+
quantity = self._data.get("electricQuantity")
73+
return quantity if isinstance(quantity, (float, int)) else 0
7374

7475
@property
7576
def feeding_plan_state(self) -> bool:
@@ -162,7 +163,8 @@ def enable_low_battery_notice(self) -> bool:
162163

163164
@property
164165
def wifi_rssi(self) -> int:
165-
return self._data.get("wifiRssi", -100) # WiFi signal strength
166+
wifi_rssi = self._data.get("wifiRssi")
167+
return wifi_rssi if isinstance(wifi_rssi, int) else -100 # WiFi signal strength
166168

167169
@property
168170
def wifi_ssid(self) -> str:

0 commit comments

Comments
 (0)