Esempio n. 1
0
    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
Esempio n. 2
0
async def test_get_events(protect_client: ProtectApiClient, raw_events):
    expected_events = []
    for event in raw_events:
        if event["score"] >= 50 and event["type"] in EventType.device_events():
            expected_events.append(event)

    protect_client._minimum_score = 50

    events = await protect_client.get_events()

    assert len(events) == len(expected_events)
    for index, event in enumerate(events):
        compare_objs(event.model.value, expected_events[index], event.unifi_dict())

        if event.type.value in EventType.motion_events():
            await check_motion_event(event)
Esempio n. 3
0
    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
Esempio n. 4
0
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()
Esempio n. 5
0
async def check_bootstrap(bootstrap: Bootstrap):
    assert bootstrap.auth_user
    assert bootstrap.nvr.protect_url == f"https://127.0.0.1:0/protect/devices/{bootstrap.nvr.id}"

    for light in bootstrap.lights.values():
        if light.camera is not None:
            await check_camera(light.camera)
        light.last_motion_event
        check_device(light)

    for camera in bootstrap.cameras.values():
        await check_camera(camera)

    for viewer in bootstrap.viewers.values():
        assert viewer.liveview
        check_device(viewer)

    for sensor in bootstrap.sensors.values():
        check_device(sensor)

    for liveview in bootstrap.liveviews.values():
        liveview.owner
        assert liveview.protect_url == f"https://127.0.0.1:0/protect/liveview/{liveview.id}"

        for slot in liveview.slots:
            expected_ids = set(slot.camera_ids).intersection(
                set(bootstrap.cameras.keys()))
            assert len(expected_ids) == len(slot.cameras)

    for user in bootstrap.users.values():
        user.groups

        if user.cloud_account is not None:
            assert user.cloud_account.user == user

    for event in bootstrap.events.values():
        event.smart_detect_events

        if event.type.value in EventType.motion_events(
        ) and event.camera is not None:
            await check_motion_event(event)