Remove deprecated YAML import from generic camera (#107992)

This commit is contained in:
Jan Bouwhuis 2024-01-14 09:36:00 +01:00 committed by GitHub
parent f48d057307
commit 4b8d8baa69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 132 additions and 301 deletions

View file

@ -2,6 +2,7 @@
import asyncio
from datetime import timedelta
from http import HTTPStatus
from typing import Any
from unittest.mock import patch
import aiohttp
@ -11,6 +12,7 @@ import pytest
import respx
from homeassistant.components.camera import (
DEFAULT_CONTENT_TYPE,
async_get_mjpeg_stream,
async_get_stream_source,
)
@ -24,8 +26,13 @@ from homeassistant.components.generic.const import (
)
from homeassistant.components.stream.const import CONF_RTSP_TRANSPORT
from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, CONF_VERIFY_SSL
from homeassistant.const import (
CONF_AUTHENTICATION,
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
CONF_VERIFY_SSL,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -33,6 +40,34 @@ from tests.common import Mock, MockConfigEntry
from tests.typing import ClientSessionGenerator, WebSocketGenerator
async def help_setup_mock_config_entry(
hass: HomeAssistant, options: dict[str, Any], unique_id: Any | None = None
) -> MockConfigEntry:
"""Help setting up a generic camera config entry."""
entry_options = {
CONF_STILL_IMAGE_URL: options.get(CONF_STILL_IMAGE_URL),
CONF_STREAM_SOURCE: options.get(CONF_STREAM_SOURCE),
CONF_AUTHENTICATION: options.get(CONF_AUTHENTICATION),
CONF_USERNAME: options.get(CONF_USERNAME),
CONF_PASSWORD: options.get(CONF_PASSWORD),
CONF_LIMIT_REFETCH_TO_URL_CHANGE: options.get(
CONF_LIMIT_REFETCH_TO_URL_CHANGE, False
),
CONF_CONTENT_TYPE: options.get(CONF_CONTENT_TYPE, DEFAULT_CONTENT_TYPE),
CONF_FRAMERATE: options.get(CONF_FRAMERATE, 2),
CONF_VERIFY_SSL: options.get(CONF_VERIFY_SSL),
}
entry = MockConfigEntry(
domain="generic",
title=options[CONF_NAME],
options=entry_options,
unique_id=unique_id,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
return entry
@respx.mock
async def test_fetching_url(
hass: HomeAssistant, hass_client: ClientSessionGenerator, fakeimgbytes_png
@ -40,22 +75,16 @@ async def test_fetching_url(
"""Test that it fetches the given url."""
respx.get("http://example.com").respond(stream=fakeimgbytes_png)
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"authentication": "basic",
"framerate": 20,
}
},
)
await hass.async_block_till_done()
options = {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"authentication": "basic",
"framerate": 20,
}
await help_setup_mock_config_entry(hass, options)
client = await hass_client()
@ -84,22 +113,16 @@ async def test_image_caching(
respx.get("http://example.com").respond(stream=fakeimgbytes_png)
framerate = 5
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"authentication": "basic",
"framerate": framerate,
}
},
)
await hass.async_block_till_done()
options = {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"authentication": "basic",
"framerate": framerate,
}
await help_setup_mock_config_entry(hass, options)
client = await hass_client()
@ -154,21 +177,15 @@ async def test_fetching_without_verify_ssl(
"""Test that it fetches the given url when ssl verify is off."""
respx.get("https://example.com").respond(stream=fakeimgbytes_png)
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"username": "user",
"password": "pass",
"verify_ssl": "false",
}
},
)
await hass.async_block_till_done()
options = {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"username": "user",
"password": "pass",
"verify_ssl": "false",
}
await help_setup_mock_config_entry(hass, options)
client = await hass_client()
@ -184,21 +201,15 @@ async def test_fetching_url_with_verify_ssl(
"""Test that it fetches the given url when ssl verify is explicitly on."""
respx.get("https://example.com").respond(stream=fakeimgbytes_png)
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"username": "user",
"password": "pass",
"verify_ssl": "true",
}
},
)
await hass.async_block_till_done()
options = {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"username": "user",
"password": "pass",
"verify_ssl": True,
}
await help_setup_mock_config_entry(hass, options)
client = await hass_client()
@ -223,19 +234,13 @@ async def test_limit_refetch(
hass.states.async_set("sensor.temp", "0")
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
}
},
)
await hass.async_block_till_done()
options = {
"name": "config_test",
"platform": "generic",
"still_image_url": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
}
await help_setup_mock_config_entry(hass, options)
client = await hass_client()
@ -350,20 +355,15 @@ async def test_stream_source_error(
"""Test that the stream source has an error."""
respx.get("http://example.com").respond(stream=fakeimgbytes_png)
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,
},
},
)
options = {
"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,
}
await help_setup_mock_config_entry(hass, options)
assert await async_setup_component(hass, "stream", {})
await hass.async_block_till_done()
@ -397,23 +397,17 @@ async def test_setup_alternative_options(
"""Test that the stream source is setup with different config options."""
respx.get("https://example.com").respond(stream=fakeimgbytes_png)
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()
options = {
"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 help_setup_mock_config_entry(hass, options)
assert hass.states.get("camera.config_test")
@ -427,19 +421,13 @@ async def test_no_stream_source(
"""Test a stream request without stream source option set."""
respx.get("https://example.com").respond(stream=fakeimgbytes_png)
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"limit_refetch_to_url_change": True,
}
},
)
await hass.async_block_till_done()
options = {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"limit_refetch_to_url_change": True,
}
await help_setup_mock_config_entry(hass, options)
with patch(
"homeassistant.components.camera.Stream.endpoint_url",
@ -494,22 +482,9 @@ async def test_camera_content_type(
"framerate": 2,
"verify_ssl": True,
}
await help_setup_mock_config_entry(hass, cam_config_jpg, unique_id=12345)
await help_setup_mock_config_entry(hass, cam_config_svg, unique_id=54321)
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"
client = await hass_client()
resp_1 = await client.get("/api/camera_proxy/camera.config_test_svg")
@ -538,21 +513,15 @@ async def test_timeout_cancelled(
respx.get("http://example.com").respond(stream=fakeimgbytes_png)
await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"framerate": 20,
}
},
)
await hass.async_block_till_done()
options = {
"name": "config_test",
"platform": "generic",
"still_image_url": "http://example.com",
"username": "user",
"password": "pass",
"framerate": 20,
}
await help_setup_mock_config_entry(hass, options)
client = await hass_client()
@ -589,19 +558,13 @@ async def test_timeout_cancelled(
async def test_frame_interval_property(hass: HomeAssistant) -> None:
"""Test that the frame interval is calculated and returned correctly."""
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()
options = {
"name": "config_test",
"platform": "generic",
"stream_source": "rtsp://example.com:554/rtsp/",
"framerate": 5,
}
await help_setup_mock_config_entry(hass, options)
request = Mock()
with patch(