Use create_stream in generic camera config flow (#73237)

* Use create_stream in generic camera config flow
This commit is contained in:
uvjustin 2022-06-11 15:38:43 +10:00 committed by GitHub
parent 21cfbe875e
commit b1f2e5f897
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 220 additions and 291 deletions

View file

@ -17,26 +17,25 @@ from tests.common import AsyncMock, Mock
@respx.mock
async def test_fetching_url(hass, hass_client, fakeimgbytes_png, mock_av_open):
async def test_fetching_url(hass, hass_client, fakeimgbytes_png):
"""Test that it fetches the given url."""
respx.get("http://example.com").respond(stream=fakeimgbytes_png)
with mock_av_open:
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"authentication": "basic",
}
},
)
await hass.async_block_till_done()
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"authentication": "basic",
}
},
)
await hass.async_block_till_done()
client = await hass_client()
@ -179,30 +178,27 @@ async def test_limit_refetch(hass, hass_client, fakeimgbytes_png, fakeimgbytes_j
@respx.mock
async def test_stream_source(
hass, hass_client, hass_ws_client, fakeimgbytes_png, mock_av_open
):
async def test_stream_source(hass, hass_client, hass_ws_client, fakeimgbytes_png):
"""Test that the stream source is rendered."""
respx.get("http://example.com").respond(stream=fakeimgbytes_png)
respx.get("http://example.com/0a").respond(stream=fakeimgbytes_png)
hass.states.async_set("sensor.temp", "0")
with mock_av_open:
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"stream_source": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
},
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"stream_source": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
},
)
assert await async_setup_component(hass, "stream", {})
await hass.async_block_till_done()
},
)
assert await async_setup_component(hass, "stream", {})
await hass.async_block_till_done()
hass.states.async_set("sensor.temp", "5")
@ -227,29 +223,26 @@ async def test_stream_source(
@respx.mock
async def test_stream_source_error(
hass, hass_client, hass_ws_client, fakeimgbytes_png, mock_av_open
):
async def test_stream_source_error(hass, hass_client, hass_ws_client, fakeimgbytes_png):
"""Test that the stream source has an error."""
respx.get("http://example.com").respond(stream=fakeimgbytes_png)
with mock_av_open:
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
# Does not exist
"stream_source": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
},
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
# Does not exist
"stream_source": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
},
)
assert await async_setup_component(hass, "stream", {})
await hass.async_block_till_done()
},
)
assert await async_setup_component(hass, "stream", {})
await hass.async_block_till_done()
with patch(
"homeassistant.components.camera.Stream.endpoint_url",
@ -275,30 +268,27 @@ async def test_stream_source_error(
@respx.mock
async def test_setup_alternative_options(
hass, hass_ws_client, fakeimgbytes_png, mock_av_open
):
async def test_setup_alternative_options(hass, hass_ws_client, fakeimgbytes_png):
"""Test that the stream source is setup with different config options."""
respx.get("https://example.com").respond(stream=fakeimgbytes_png)
with mock_av_open:
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"authentication": "digest",
"username": "user",
"password": "pass",
"stream_source": "rtsp://example.com:554/rtsp/",
"rtsp_transport": "udp",
},
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"authentication": "digest",
"username": "user",
"password": "pass",
"stream_source": "rtsp://example.com:554/rtsp/",
"rtsp_transport": "udp",
},
)
await hass.async_block_till_done()
},
)
await hass.async_block_till_done()
assert hass.states.get("camera.config_test")
@ -346,7 +336,7 @@ async def test_no_stream_source(hass, hass_client, hass_ws_client, fakeimgbytes_
@respx.mock
async def test_camera_content_type(
hass, hass_client, fakeimgbytes_svg, fakeimgbytes_jpg, mock_av_open
hass, hass_client, fakeimgbytes_svg, fakeimgbytes_jpg
):
"""Test generic camera with custom content_type."""
urlsvg = "https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg"
@ -372,20 +362,18 @@ async def test_camera_content_type(
"verify_ssl": True,
}
with mock_av_open:
result1 = await hass.config_entries.flow.async_init(
"generic",
data=cam_config_jpg,
context={"source": SOURCE_IMPORT, "unique_id": 12345},
)
await hass.async_block_till_done()
with mock_av_open:
result2 = await hass.config_entries.flow.async_init(
"generic",
data=cam_config_svg,
context={"source": SOURCE_IMPORT, "unique_id": 54321},
)
await hass.async_block_till_done()
result1 = await hass.config_entries.flow.async_init(
"generic",
data=cam_config_jpg,
context={"source": SOURCE_IMPORT, "unique_id": 12345},
)
await hass.async_block_till_done()
result2 = await hass.config_entries.flow.async_init(
"generic",
data=cam_config_svg,
context={"source": SOURCE_IMPORT, "unique_id": 54321},
)
await hass.async_block_till_done()
assert result1["type"] == "create_entry"
assert result2["type"] == "create_entry"
@ -457,21 +445,20 @@ async def test_timeout_cancelled(hass, hass_client, fakeimgbytes_png, fakeimgbyt
assert await resp.read() == fakeimgbytes_png
async def test_no_still_image_url(hass, hass_client, mock_av_open):
async def test_no_still_image_url(hass, hass_client):
"""Test that the component can grab images from stream with no still_image_url."""
with mock_av_open:
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"stream_source": "rtsp://example.com:554/rtsp/",
},
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"stream_source": "rtsp://example.com:554/rtsp/",
},
)
await hass.async_block_till_done()
},
)
await hass.async_block_till_done()
client = await hass_client()
@ -503,23 +490,22 @@ async def test_no_still_image_url(hass, hass_client, mock_av_open):
assert await resp.read() == b"stream_keyframe_image"
async def test_frame_interval_property(hass, mock_av_open):
async def test_frame_interval_property(hass):
"""Test that the frame interval is calculated and returned correctly."""
with mock_av_open:
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"stream_source": "rtsp://example.com:554/rtsp/",
"framerate": 5,
},
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"stream_source": "rtsp://example.com:554/rtsp/",
"framerate": 5,
},
)
await hass.async_block_till_done()
},
)
await hass.async_block_till_done()
request = Mock()
with patch(