Add fetching camera thumbnails over websocket (#14231)
* Add fetching camera thumbnails over websocket * Lint
This commit is contained in:
parent
4ecce2598a
commit
58257af289
8 changed files with 135 additions and 74 deletions
|
@ -1,18 +1,19 @@
|
|||
"""The tests for the camera component."""
|
||||
import asyncio
|
||||
import base64
|
||||
from unittest.mock import patch, mock_open
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
from homeassistant.const import ATTR_ENTITY_PICTURE
|
||||
import homeassistant.components.camera as camera
|
||||
import homeassistant.components.http as http
|
||||
from homeassistant.components import camera, http, websocket_api
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, get_test_instance_port, assert_setup_component)
|
||||
get_test_home_assistant, get_test_instance_port, assert_setup_component,
|
||||
mock_coro)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -90,36 +91,32 @@ class TestGetImage(object):
|
|||
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
||||
|
||||
assert mock_camera.called
|
||||
assert image == b'Test'
|
||||
assert image.content == b'Test'
|
||||
|
||||
def test_get_image_without_exists_camera(self):
|
||||
"""Try to get image without exists camera."""
|
||||
self.hass.states.remove('camera.demo_camera')
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
with patch('homeassistant.helpers.entity_component.EntityComponent.'
|
||||
'get_entity', return_value=None), \
|
||||
pytest.raises(HomeAssistantError):
|
||||
run_coroutine_threadsafe(camera.async_get_image(
|
||||
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
||||
|
||||
def test_get_image_with_timeout(self, aioclient_mock):
|
||||
def test_get_image_with_timeout(self):
|
||||
"""Try to get image with timeout."""
|
||||
aioclient_mock.get(self.url, exc=asyncio.TimeoutError())
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
with patch('homeassistant.components.camera.Camera.async_camera_image',
|
||||
side_effect=asyncio.TimeoutError), \
|
||||
pytest.raises(HomeAssistantError):
|
||||
run_coroutine_threadsafe(camera.async_get_image(
|
||||
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
|
||||
def test_get_image_with_bad_http_state(self, aioclient_mock):
|
||||
"""Try to get image with bad http status."""
|
||||
aioclient_mock.get(self.url, status=400)
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
def test_get_image_fails(self):
|
||||
"""Try to get image with timeout."""
|
||||
with patch('homeassistant.components.camera.Camera.async_camera_image',
|
||||
return_value=mock_coro(None)), \
|
||||
pytest.raises(HomeAssistantError):
|
||||
run_coroutine_threadsafe(camera.async_get_image(
|
||||
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
||||
|
||||
assert len(aioclient_mock.mock_calls) == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_snapshot_service(hass, mock_camera):
|
||||
|
@ -136,3 +133,24 @@ def test_snapshot_service(hass, mock_camera):
|
|||
|
||||
assert len(mock_write.mock_calls) == 1
|
||||
assert mock_write.mock_calls[0][1][0] == b'Test'
|
||||
|
||||
|
||||
async def test_webocket_camera_thumbnail(hass, hass_ws_client, mock_camera):
|
||||
"""Test camera_thumbnail websocket command."""
|
||||
await async_setup_component(hass, 'camera')
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({
|
||||
'id': 5,
|
||||
'type': 'camera_thumbnail',
|
||||
'entity_id': 'camera.demo_camera',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['id'] == 5
|
||||
assert msg['type'] == websocket_api.TYPE_RESULT
|
||||
assert msg['success']
|
||||
assert msg['result']['content_type'] == 'image/jpeg'
|
||||
assert msg['result']['content'] == \
|
||||
base64.b64encode(b'Test').decode('utf-8')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue