Camera Preferences + Preload Stream (#22339)
* initial commit for camera preferences and preload stream * cleanup and add tests * respect camera preferences on each request stream call * return the new prefs after update
This commit is contained in:
1 parent
baa4945944
commit
bad0a8b342
11 files changed
+252
-22
No files matched your search
@@ -1,13 +1,17 @@
|
||||
"""The tests for the camera component."""
|
||||
import asyncio
|
||||
import base64
|
||||
import io
|
||||
from unittest.mock import patch, mock_open, PropertyMock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.const import (ATTR_ENTITY_ID, ATTR_ENTITY_PICTURE)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, ATTR_ENTITY_PICTURE, EVENT_HOMEASSISTANT_START)
|
||||
from homeassistant.components import camera, http
|
||||
from homeassistant.components.camera.const import DOMAIN, PREF_PRELOAD_STREAM
|
||||
from homeassistant.components.camera.prefs import CameraEntityPreferences
|
||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
@@ -16,7 +20,6 @@ from tests.common import (
|
||||
get_test_home_assistant, get_test_instance_port, assert_setup_component,
|
||||
mock_coro)
|
||||
from tests.components.camera import common
|
||||
from tests.components.stream.common import generate_h264_video
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -41,6 +44,12 @@ def mock_stream(hass):
|
||||
}))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup_camera_prefs(hass):
|
||||
"""Initialize HTTP API."""
|
||||
return common.mock_camera_prefs(hass, 'camera.demo_camera')
|
||||
|
||||
|
||||
class TestSetupCamera:
|
||||
"""Test class for setup camera."""
|
||||
|
||||
@@ -146,7 +155,7 @@ def test_snapshot_service(hass, mock_camera):
|
||||
assert mock_write.mock_calls[0][1][0] == b'Test'
|
||||
|
||||
|
||||
async def test_webocket_camera_thumbnail(hass, hass_ws_client, mock_camera):
|
||||
async def test_websocket_camera_thumbnail(hass, hass_ws_client, mock_camera):
|
||||
"""Test camera_thumbnail websocket command."""
|
||||
await async_setup_component(hass, 'camera')
|
||||
|
||||
@@ -167,8 +176,8 @@ async def test_webocket_camera_thumbnail(hass, hass_ws_client, mock_camera):
|
||||
base64.b64encode(b'Test').decode('utf-8')
|
||||
|
||||
|
||||
async def test_webocket_stream_no_source(hass, hass_ws_client,
|
||||
mock_camera, mock_stream):
|
||||
async def test_websocket_stream_no_source(hass, hass_ws_client,
|
||||
mock_camera, mock_stream):
|
||||
"""Test camera/stream websocket command."""
|
||||
await async_setup_component(hass, 'camera')
|
||||
|
||||
@@ -191,8 +200,8 @@ async def test_webocket_stream_no_source(hass, hass_ws_client,
|
||||
assert not msg['success']
|
||||
|
||||
|
||||
async def test_webocket_camera_stream(hass, hass_ws_client, hass_client,
|
||||
mock_camera, mock_stream):
|
||||
async def test_websocket_camera_stream(hass, hass_ws_client,
|
||||
mock_camera, mock_stream):
|
||||
"""Test camera/stream websocket command."""
|
||||
await async_setup_component(hass, 'camera')
|
||||
|
||||
@@ -201,7 +210,7 @@ async def test_webocket_camera_stream(hass, hass_ws_client, hass_client,
|
||||
) as mock_request_stream, \
|
||||
patch('homeassistant.components.demo.camera.DemoCamera.stream_source',
|
||||
new_callable=PropertyMock) as mock_stream_source:
|
||||
mock_stream_source.return_value = generate_h264_video()
|
||||
mock_stream_source.return_value = io.BytesIO()
|
||||
# Request playlist through WebSocket
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({
|
||||
@@ -219,6 +228,44 @@ async def test_webocket_camera_stream(hass, hass_ws_client, hass_client,
|
||||
assert msg['result']['url'][-13:] == 'playlist.m3u8'
|
||||
|
||||
|
||||
async def test_websocket_get_prefs(hass, hass_ws_client,
|
||||
mock_camera):
|
||||
"""Test get camera preferences websocket command."""
|
||||
await async_setup_component(hass, 'camera')
|
||||
|
||||
# Request preferences through websocket
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({
|
||||
'id': 7,
|
||||
'type': 'camera/get_prefs',
|
||||
'entity_id': 'camera.demo_camera',
|
||||
})
|
||||
msg = await client.receive_json()
|
||||
|
||||
# Assert WebSocket response
|
||||
assert msg['success']
|
||||
|
||||
|
||||
async def test_websocket_update_prefs(hass, hass_ws_client,
|
||||
mock_camera, setup_camera_prefs):
|
||||
"""Test updating preference."""
|
||||
await async_setup_component(hass, 'camera')
|
||||
assert setup_camera_prefs[PREF_PRELOAD_STREAM]
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({
|
||||
'id': 8,
|
||||
'type': 'camera/update_prefs',
|
||||
'entity_id': 'camera.demo_camera',
|
||||
'preload_stream': False,
|
||||
})
|
||||
response = await client.receive_json()
|
||||
|
||||
assert response['success']
|
||||
assert not setup_camera_prefs[PREF_PRELOAD_STREAM]
|
||||
assert response['result'][PREF_PRELOAD_STREAM] == \
|
||||
setup_camera_prefs[PREF_PRELOAD_STREAM]
|
||||
|
||||
|
||||
async def test_play_stream_service_no_source(hass, mock_camera, mock_stream):
|
||||
"""Test camera play_stream service."""
|
||||
data = {
|
||||
@@ -243,10 +290,54 @@ async def test_handle_play_stream_service(hass, mock_camera, mock_stream):
|
||||
) as mock_request_stream, \
|
||||
patch('homeassistant.components.demo.camera.DemoCamera.stream_source',
|
||||
new_callable=PropertyMock) as mock_stream_source:
|
||||
mock_stream_source.return_value = generate_h264_video()
|
||||
mock_stream_source.return_value = io.BytesIO()
|
||||
# Call service
|
||||
await hass.services.async_call(
|
||||
camera.DOMAIN, camera.SERVICE_PLAY_STREAM, data, blocking=True)
|
||||
# So long as we request the stream, the rest should be covered
|
||||
# by the play_media service tests.
|
||||
assert mock_request_stream.called
|
||||
|
||||
|
||||
async def test_no_preload_stream(hass, mock_stream):
|
||||
"""Test camera preload preference."""
|
||||
demo_prefs = CameraEntityPreferences({
|
||||
PREF_PRELOAD_STREAM: False,
|
||||
})
|
||||
with patch('homeassistant.components.camera.request_stream'
|
||||
) as mock_request_stream, \
|
||||
patch('homeassistant.components.camera.prefs.CameraPreferences.get',
|
||||
return_value=demo_prefs), \
|
||||
patch('homeassistant.components.demo.camera.DemoCamera.stream_source',
|
||||
new_callable=PropertyMock) as mock_stream_source:
|
||||
mock_stream_source.return_value = io.BytesIO()
|
||||
await async_setup_component(hass, 'camera', {
|
||||
DOMAIN: {
|
||||
'platform': 'demo'
|
||||
}
|
||||
})
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
assert not mock_request_stream.called
|
||||
|
||||
|
||||
async def test_preload_stream(hass, mock_stream):
|
||||
"""Test camera preload preference."""
|
||||
demo_prefs = CameraEntityPreferences({
|
||||
PREF_PRELOAD_STREAM: True,
|
||||
})
|
||||
with patch('homeassistant.components.camera.request_stream'
|
||||
) as mock_request_stream, \
|
||||
patch('homeassistant.components.camera.prefs.CameraPreferences.get',
|
||||
return_value=demo_prefs), \
|
||||
patch('homeassistant.components.demo.camera.DemoCamera.stream_source',
|
||||
new_callable=PropertyMock) as mock_stream_source:
|
||||
mock_stream_source.return_value = io.BytesIO()
|
||||
await async_setup_component(hass, 'camera', {
|
||||
DOMAIN: {
|
||||
'platform': 'demo'
|
||||
}
|
||||
})
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_request_stream.called
|
||||
Reference in new issue
Block a user