Skip to content

Commit c78c69e

Browse files
feat(#72): add motion and sound detection sensors for GranarySmartCameraFeeder (#255)
Co-authored-by: Jamie Jones <29973406+jjjonesjr33@users.noreply.github.com>
1 parent c4df5e0 commit c78c69e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

custom_components/petlibro/binary_sensor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,22 @@ def extra_state_attributes(self):
382382
value_fn=lambda device: device.feeding_plan_state,
383383
name="Feeding Schedule"
384384
),
385+
PetLibroBinarySensorEntityDescription[GranarySmartCameraFeeder](
386+
key="motion_detected",
387+
translation_key="motion_detected",
388+
icon="mdi:motion-sensor",
389+
device_class=BinarySensorDeviceClass.MOTION,
390+
should_report=lambda device: device.motion_detected is not None,
391+
name="Motion Detected"
392+
),
393+
PetLibroBinarySensorEntityDescription[GranarySmartCameraFeeder](
394+
key="sound_detected",
395+
translation_key="sound_detected",
396+
icon="mdi:ear-hearing",
397+
device_class=BinarySensorDeviceClass.SOUND,
398+
should_report=lambda device: device.sound_detected is not None,
399+
name="Sound Detected"
400+
),
385401
],
386402
OneRFIDSmartFeeder: [
387403
PetLibroBinarySensorEntityDescription[OneRFIDSmartFeeder](

custom_components/petlibro/devices/feeders/granary_smart_camera_feeder.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ async def refresh(self):
3131
get_work_record = await self.api.get_device_work_record(self.serial)
3232
feeding_plan_list = (await self.api.device_feeding_plan_list(self.serial)
3333
if self._data.get("enableFeedingPlan") else [])
34+
get_device_events = await self.api.device_events(self.serial)
3435
# Update internal data with fetched API data
3536
self.update_data({
3637
"grainStatus": grain_status or {},
@@ -39,7 +40,8 @@ async def refresh(self):
3940
"getUpgrade": get_upgrade or {},
4041
"getfeedingplantoday": get_feeding_plan_today or {},
4142
"feedingPlan": feeding_plan_list or [],
42-
"workRecord": get_work_record or [],
43+
"workRecord": get_work_record or [],
44+
"getDeviceEvents": get_device_events or {},
4345
})
4446
except PetLibroAPIError as err:
4547
_LOGGER.error(f"Error refreshing data for GranarySmartCameraFeeder: {err}")
@@ -216,6 +218,16 @@ def video_record_switch(self) -> bool:
216218
def video_record_mode(self) -> str:
217219
"""Return the current video recording mode."""
218220
return self._data.get("realInfo", {}).get("videoRecordMode", "unknown")
221+
222+
@property
223+
def motion_detected(self) -> bool:
224+
events = self._data.get("getDeviceEvents", {}).get("data", {}).get("eventInfos", [])
225+
return any(event.get("eventKey") == "MOTION_DETECTED" for event in events)
226+
227+
@property
228+
def sound_detected(self) -> bool:
229+
events = self._data.get("getDeviceEvents", {}).get("data", {}).get("eventInfos", [])
230+
return any(event.get("eventKey") == "SOUND_DETECTED" for event in events)
219231

220232
@property
221233
def remaining_desiccant(self) -> float | None:

0 commit comments

Comments
 (0)