Esempio n. 1
0
async def test_async_browse_media_async_get_path_error(
        caplog: Any, frigate_client: AsyncMock, hass: HomeAssistant) -> None:
    """Test API error behavior."""
    frigate_client.async_get_path = AsyncMock(
        side_effect=FrigateApiClientError)

    await setup_mock_frigate_config_entry(hass, client=frigate_client)

    with pytest.raises(MediaSourceError):
        await media_source.async_browse_media(
            hass,
            f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}/recordings"
        )

    with pytest.raises(MediaSourceError):
        await media_source.async_browse_media(
            hass,
            (f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}"
             "/recordings/2021-06/04/15/front_door"),
        )
Esempio n. 2
0
async def test_async_browse_media_recordings_root(caplog: Any,
                                                  frigate_client: AsyncMock,
                                                  hass: HomeAssistant) -> None:
    """Test recordings root."""

    await setup_mock_frigate_config_entry(hass, client=frigate_client)

    frigate_client.async_get_path = AsyncMock(return_value=[
        {
            "name": "2021-06",
            "type": "directory",
            "mtime": "Sun, 30 June 2021 22:47:14 GMT",
        },
        {
            "name": "49.06.mp4",
            "type": "file",
            "mtime": "Sun, 30 June 2021 22:50:06 GMT",
            "size": 5168517,
        },
    ])

    media = await media_source.async_browse_media(
        hass,
        f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}/recordings",
    )

    assert media.as_dict() == {
        "title":
        "Recordings",
        "media_class":
        "directory",
        "media_content_type":
        "video",
        "media_content_id":
        (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}/recordings////"),
        "can_play":
        False,
        "can_expand":
        True,
        "children_media_class":
        "directory",
        "thumbnail":
        None,
        "children": [{
            "can_expand":
            True,
            "can_play":
            False,
            "children_media_class":
            "directory",
            "media_class":
            "directory",
            "media_content_id":
            (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
             "/recordings/2021-06///"),
            "media_content_type":
            "video",
            "thumbnail":
            None,
            "title":
            "June 2021",
        }],
    }

    frigate_client.async_get_path = AsyncMock(return_value=[
        {
            "name": "04",
            "type": "directory",
            "mtime": "Mon, 07 Jun 2021 02:33:16 GMT",
        },
        {
            "name": "NOT_AN_HOUR",
            "type": "directory",
            "mtime": "Mon, 07 Jun 2021 02:33:17 GMT",
        },
    ])

    media = await media_source.async_browse_media(
        hass,
        f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}/recordings/2021-06///",
    )

    assert media.as_dict() == {
        "title":
        "June 2021",
        "media_class":
        "directory",
        "media_content_type":
        "video",
        "media_content_id":
        (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}/recordings/2021-06///"
         ),
        "can_play":
        False,
        "can_expand":
        True,
        "children_media_class":
        "directory",
        "thumbnail":
        None,
        "children": [{
            "can_expand":
            True,
            "can_play":
            False,
            "children_media_class":
            "directory",
            "media_class":
            "directory",
            "media_content_id":
            (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
             "/recordings/2021-06/04//"),
            "media_content_type":
            "video",
            "thumbnail":
            None,
            "title":
            "June 04",
        }],
    }
    # There's a bogus value for an hour, that should be skipped.
    assert "Skipping non-standard folder" in caplog.text

    frigate_client.async_get_path = AsyncMock(return_value=[
        {
            "name": "15",
            "type": "directory",
            "mtime": "Sun, 04 June 2021 22:47:14 GMT",
        },
    ])

    media = await media_source.async_browse_media(
        hass,
        f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}/recordings/2021-06/04//",
    )

    assert media.as_dict() == {
        "title":
        "June 04",
        "media_class":
        "directory",
        "media_content_type":
        "video",
        "media_content_id":
        (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
         "/recordings/2021-06/04//"),
        "can_play":
        False,
        "can_expand":
        True,
        "children_media_class":
        "directory",
        "thumbnail":
        None,
        "children": [{
            "can_expand":
            True,
            "can_play":
            False,
            "children_media_class":
            "directory",
            "media_class":
            "directory",
            "media_content_id":
            (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
             "/recordings/2021-06/04/15/"),
            "media_content_type":
            "video",
            "thumbnail":
            None,
            "title":
            "15:00:00",
        }],
    }

    frigate_client.async_get_path = AsyncMock(return_value=[
        {
            "name": "front_door",
            "type": "directory",
            "mtime": "Sun, 30 June 2021 23:00:50 GMT",
        },
        {
            "name": "sitting_room",
            "type": "directory",
            "mtime": "Sun, 04 June 2021 23:00:40 GMT",
        },
    ])

    media = await media_source.async_browse_media(
        hass,
        (f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}"
         "/recordings/2021-06/04/15/"),
    )

    assert media.as_dict() == {
        "title":
        "15:00:00",
        "media_class":
        "directory",
        "media_content_type":
        "video",
        "media_content_id":
        (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
         "/recordings/2021-06/04/15/"),
        "can_play":
        False,
        "can_expand":
        True,
        "children_media_class":
        "directory",
        "thumbnail":
        None,
        "children": [
            {
                "can_expand":
                False,
                "can_play":
                True,
                "children_media_class":
                None,
                "media_class":
                "movie",
                "media_content_id":
                (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
                 "/recordings/2021-06/04/15/front_door"),
                "media_content_type":
                "video",
                "thumbnail":
                None,
                "title":
                "Front Door",
            },
            {
                "can_expand":
                False,
                "can_play":
                True,
                "children_media_class":
                None,
                "media_class":
                "movie",
                "media_content_id":
                (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
                 "/recordings/2021-06/04/15/sitting_room"),
                "media_content_type":
                "video",
                "thumbnail":
                None,
                "title":
                "Sitting Room",
            },
        ],
    }

    # Verify an inappropriate identifier will result in a MediaSourceError.
    with pytest.raises(MediaSourceError):
        await media_source.async_browse_media(
            hass,
            (f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}"
             "/recordings/2021-06/04/NOT_AN_HOUR/"),
        )

    # Ensure a syntactically correct, but semantically incorrect path will
    # result in a MediaSourceError (there is no 29th February in 2021).
    with pytest.raises(MediaSourceError):
        await media_source.async_browse_media(
            hass,
            f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}"
            "/recordings/2021-02/29",
        )

    # Fetch a recording on the zeroth hour:
    # https://github.com/blakeblackshear/frigate-hass-integration/issues/126
    frigate_client.async_get_path = AsyncMock(return_value=[
        {
            "name": "front_door",
            "type": "directory",
            "mtime": "Sun, 30 June 2021 23:00:50 GMT",
        },
    ])
    media = await media_source.async_browse_media(
        hass,
        (f"{const.URI_SCHEME}{DOMAIN}/{TEST_FRIGATE_INSTANCE_ID}"
         "/recordings/2021-06/04/00/"),
    )
    assert media.as_dict() == {
        "title":
        "00:00:00",
        "media_class":
        "directory",
        "media_content_type":
        "video",
        "media_content_id":
        "media-source://frigate/frigate_client_id/recordings/2021-06/04/00/",
        "can_play":
        False,
        "can_expand":
        True,
        "children_media_class":
        "directory",
        "thumbnail":
        None,
        "children": [
            {
                "can_expand":
                False,
                "can_play":
                True,
                "children_media_class":
                None,
                "media_class":
                "movie",
                "media_content_id":
                (f"media-source://frigate/{TEST_FRIGATE_INSTANCE_ID}"
                 "/recordings/2021-06/04/00/front_door"),
                "media_content_type":
                "video",
                "thumbnail":
                None,
                "title":
                "Front Door",
            },
        ],
    }