Clean up camera and demo camera (#34058)

* Clean up demo camera

* Complete test_motion_detection

* Clean up image reading

* Clean up camera integration async methods

* Fix and clean camera integration tests

* Fix image processing patch
This commit is contained in:
Martin Hjelmare 2020-04-12 14:56:19 +02:00 committed by GitHub
parent 4f519a2fe1
commit 20aa089243
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 63 deletions

View file

@ -2,8 +2,9 @@
import asyncio
import base64
import io
from unittest.mock import PropertyMock, mock_open, patch
from unittest.mock import PropertyMock, mock_open
from asynctest import patch
import pytest
from homeassistant.components import camera
@ -14,40 +15,38 @@ from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START
from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
from tests.common import mock_coro
from tests.components.camera import common
@pytest.fixture
def mock_camera(hass):
@pytest.fixture(name="mock_camera")
def mock_camera_fixture(hass):
"""Initialize a demo camera platform."""
assert hass.loop.run_until_complete(
async_setup_component(hass, "camera", {camera.DOMAIN: {"platform": "demo"}})
)
with patch(
"homeassistant.components.demo.camera.DemoCamera.camera_image",
return_value=b"Test",
"homeassistant.components.demo.camera.Path.read_bytes", return_value=b"Test",
):
yield
@pytest.fixture
def mock_stream(hass):
@pytest.fixture(name="mock_stream")
def mock_stream_fixture(hass):
"""Initialize a demo camera platform with streaming."""
assert hass.loop.run_until_complete(
async_setup_component(hass, "stream", {"stream": {}})
)
@pytest.fixture
def setup_camera_prefs(hass):
@pytest.fixture(name="setup_camera_prefs")
def setup_camera_prefs_fixture(hass):
"""Initialize HTTP API."""
return common.mock_camera_prefs(hass, "camera.demo_camera")
@pytest.fixture
async def image_mock_url(hass):
@pytest.fixture(name="image_mock_url")
async def image_mock_url_fixture(hass):
"""Fixture for get_image tests."""
await async_setup_component(
hass, camera.DOMAIN, {camera.DOMAIN: {"platform": "demo"}}
@ -58,7 +57,7 @@ async def test_get_image_from_camera(hass, image_mock_url):
"""Grab an image from camera entity."""
with patch(
"homeassistant.components.demo.camera.DemoCamera.camera_image",
"homeassistant.components.demo.camera.Path.read_bytes",
autospec=True,
return_value=b"Test",
) as mock_camera:
@ -80,7 +79,7 @@ async def test_get_image_without_exists_camera(hass, image_mock_url):
async def test_get_image_with_timeout(hass, image_mock_url):
"""Try to get image with timeout."""
with patch(
"homeassistant.components.camera.Camera.async_camera_image",
"homeassistant.components.demo.camera.DemoCamera.async_camera_image",
side_effect=asyncio.TimeoutError,
), pytest.raises(HomeAssistantError):
await camera.async_get_image(hass, "camera.demo_camera")
@ -89,8 +88,8 @@ async def test_get_image_with_timeout(hass, image_mock_url):
async def test_get_image_fails(hass, image_mock_url):
"""Try to get image with timeout."""
with patch(
"homeassistant.components.camera.Camera.async_camera_image",
return_value=mock_coro(None),
"homeassistant.components.demo.camera.DemoCamera.async_camera_image",
return_value=None,
), pytest.raises(HomeAssistantError):
await camera.async_get_image(hass, "camera.demo_camera")
@ -169,7 +168,7 @@ async def test_websocket_camera_stream(hass, hass_ws_client, mock_camera, mock_s
return_value="http://home.assistant/playlist.m3u8",
) as mock_request_stream, patch(
"homeassistant.components.demo.camera.DemoCamera.stream_source",
return_value=mock_coro("http://example.com"),
return_value="http://example.com",
):
# Request playlist through WebSocket
client = await hass_ws_client(hass)
@ -248,7 +247,7 @@ async def test_handle_play_stream_service(hass, mock_camera, mock_stream):
"homeassistant.components.camera.request_stream"
) as mock_request_stream, patch(
"homeassistant.components.demo.camera.DemoCamera.stream_source",
return_value=mock_coro("http://example.com"),
return_value="http://example.com",
):
# Call service
await hass.services.async_call(
@ -294,7 +293,7 @@ async def test_preload_stream(hass, mock_stream):
return_value=demo_prefs,
), patch(
"homeassistant.components.demo.camera.DemoCamera.stream_source",
return_value=mock_coro("http://example.com"),
return_value="http://example.com",
):
await async_setup_component(hass, "camera", {DOMAIN: {"platform": "demo"}})
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
@ -323,10 +322,9 @@ async def test_record_service(hass, mock_camera, mock_stream):
"""Test record service."""
with patch(
"homeassistant.components.demo.camera.DemoCamera.stream_source",
return_value=mock_coro("http://example.com"),
return_value="http://example.com",
), patch(
"homeassistant.components.stream.async_handle_record_service",
return_value=mock_coro(),
) as mock_record_service, patch.object(
hass.config, "is_allowed_path", return_value=True
):