async def get_events( self, start: Optional[datetime] = None, end: Optional[datetime] = None, limit: Optional[int] = None, types: Optional[List[EventType]] = None, ) -> List[Event]: """ Same as `get_events_raw`, except * returns actual `Event` objects instead of raw Python dictionaries * filers out non-device events * filters out events with too low of a score Args: * `start`: start time for events * `end`: end time for events * `limit`: max number of events to return * `types`: list of EventTypes to get events for If `limit`, `start` and `end` are not provided, it will default to all events in the last 24 hours. If `start` is provided, then `end` or `limit` must be provided. If `end` is provided, then `start` or `limit` must be provided. Otherwise, you will get a 400 error from Unifi Protect """ response = await self.get_events_raw(start=start, end=end, limit=limit, types=types) events = [] for event_dict in response: # ignore unknown events if "type" not in event_dict or event_dict[ "type"] not in EventType.values(): _LOGGER.debug("Unknown event type: %s", event_dict) continue event = create_from_unifi_dict(event_dict, api=self) # should never happen if not isinstance(event, Event): continue if event.type.value in EventType.device_events( ) and event.score >= self._minimum_score: events.append(event) return events
async def test_get_events_raw_default(protect_client: ProtectApiClient, now: datetime): events = await protect_client.get_events_raw() end = now + timedelta(seconds=10) protect_client.api_request.assert_called_with( # type: ignore url="events", method="get", require_auth=True, raise_exception=True, params={ "start": to_js_time(end - timedelta(hours=24)), "end": to_js_time(end), }, ) assert len(events) == CONSTANTS["event_count"] for event in events: assert event["type"] in EventType.values() assert event["modelKey"] in ModelType.values()
async def get_events( self, start: Optional[datetime] = None, end: Optional[datetime] = None, limit: Optional[int] = None, camera_ids: Optional[List[str]] = None, ) -> List[Event]: """ Same as `get_events_raw`, except * returns actual `Event` objects instead of raw Python dictionaries * filers out non-device events * filters out events with too low of a score """ response = await self.get_events_raw(start=start, end=end, limit=limit, camera_ids=camera_ids) events = [] for event_dict in response: # ignore unknown events if "type" not in event_dict or event_dict[ "type"] not in EventType.values(): _LOGGER.debug("Unknown event type: %s", event_dict) continue event = create_from_unifi_dict(event_dict, api=self) # should never happen if not isinstance(event, Event): continue if event.type.value in EventType.device_events( ) and event.score >= self._minimum_score: events.append(event) return events