2017-01-14 07:18:03 +00:00
|
|
|
"""The tests for the camera component."""
|
|
|
|
import asyncio
|
2018-05-03 20:02:59 +00:00
|
|
|
import base64
|
2017-10-29 22:14:26 +00:00
|
|
|
from unittest.mock import patch, mock_open
|
2017-01-14 07:18:03 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2017-10-29 22:14:26 +00:00
|
|
|
from homeassistant.setup import setup_component, async_setup_component
|
2017-01-14 07:18:03 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_PICTURE
|
2018-05-03 20:02:59 +00:00
|
|
|
from homeassistant.components import camera, http, websocket_api
|
2017-01-14 07:18:03 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2018-03-11 17:01:12 +00:00
|
|
|
from homeassistant.util.async_ import run_coroutine_threadsafe
|
2017-01-14 07:18:03 +00:00
|
|
|
|
2017-01-22 02:05:15 +00:00
|
|
|
from tests.common import (
|
2018-05-03 20:02:59 +00:00
|
|
|
get_test_home_assistant, get_test_instance_port, assert_setup_component,
|
|
|
|
mock_coro)
|
2017-01-14 07:18:03 +00:00
|
|
|
|
|
|
|
|
2017-10-29 22:14:26 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_camera(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.camera.demo.DemoCamera.camera_image',
|
|
|
|
return_value=b'Test'):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class TestSetupCamera:
|
2017-01-14 07:18:03 +00:00
|
|
|
"""Test class for setup camera."""
|
|
|
|
|
|
|
|
def setup_method(self):
|
|
|
|
"""Setup things to be run when tests are started."""
|
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def teardown_method(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_setup_component(self):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Setup demo platform on camera component."""
|
2017-01-14 07:18:03 +00:00
|
|
|
config = {
|
|
|
|
camera.DOMAIN: {
|
|
|
|
'platform': 'demo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
with assert_setup_component(1, camera.DOMAIN):
|
|
|
|
setup_component(self.hass, camera.DOMAIN, config)
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class TestGetImage:
|
2017-01-14 07:18:03 +00:00
|
|
|
"""Test class for camera."""
|
|
|
|
|
|
|
|
def setup_method(self):
|
|
|
|
"""Setup things to be run when tests are started."""
|
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
2017-01-22 02:05:15 +00:00
|
|
|
setup_component(
|
|
|
|
self.hass, http.DOMAIN,
|
|
|
|
{http.DOMAIN: {http.CONF_SERVER_PORT: get_test_instance_port()}})
|
|
|
|
|
2017-01-14 07:18:03 +00:00
|
|
|
config = {
|
|
|
|
camera.DOMAIN: {
|
|
|
|
'platform': 'demo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_component(self.hass, camera.DOMAIN, config)
|
|
|
|
|
|
|
|
state = self.hass.states.get('camera.demo_camera')
|
|
|
|
self.url = "{0}{1}".format(
|
|
|
|
self.hass.config.api.base_url,
|
|
|
|
state.attributes.get(ATTR_ENTITY_PICTURE))
|
|
|
|
|
|
|
|
def teardown_method(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
@patch('homeassistant.components.camera.demo.DemoCamera.camera_image',
|
|
|
|
autospec=True, return_value=b'Test')
|
|
|
|
def test_get_image_from_camera(self, mock_camera):
|
2018-01-27 19:58:27 +00:00
|
|
|
"""Grab an image from camera entity."""
|
2017-01-14 07:18:03 +00:00
|
|
|
self.hass.start()
|
|
|
|
|
|
|
|
image = run_coroutine_threadsafe(camera.async_get_image(
|
|
|
|
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
|
|
|
|
|
|
|
assert mock_camera.called
|
2018-05-03 20:02:59 +00:00
|
|
|
assert image.content == b'Test'
|
2017-01-14 07:18:03 +00:00
|
|
|
|
|
|
|
def test_get_image_without_exists_camera(self):
|
|
|
|
"""Try to get image without exists camera."""
|
2018-05-03 20:02:59 +00:00
|
|
|
with patch('homeassistant.helpers.entity_component.EntityComponent.'
|
|
|
|
'get_entity', return_value=None), \
|
|
|
|
pytest.raises(HomeAssistantError):
|
2017-01-14 07:18:03 +00:00
|
|
|
run_coroutine_threadsafe(camera.async_get_image(
|
|
|
|
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
|
|
|
|
2018-05-03 20:02:59 +00:00
|
|
|
def test_get_image_with_timeout(self):
|
2017-01-14 07:18:03 +00:00
|
|
|
"""Try to get image with timeout."""
|
2018-05-03 20:02:59 +00:00
|
|
|
with patch('homeassistant.components.camera.Camera.async_camera_image',
|
|
|
|
side_effect=asyncio.TimeoutError), \
|
|
|
|
pytest.raises(HomeAssistantError):
|
2017-01-14 07:18:03 +00:00
|
|
|
run_coroutine_threadsafe(camera.async_get_image(
|
|
|
|
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
|
|
|
|
2018-05-03 20:02:59 +00:00
|
|
|
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):
|
2017-01-14 07:18:03 +00:00
|
|
|
run_coroutine_threadsafe(camera.async_get_image(
|
|
|
|
self.hass, 'camera.demo_camera'), self.hass.loop).result()
|
|
|
|
|
2017-10-29 22:14:26 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_snapshot_service(hass, mock_camera):
|
|
|
|
"""Test snapshot service."""
|
|
|
|
mopen = mock_open()
|
|
|
|
|
|
|
|
with patch('homeassistant.components.camera.open', mopen, create=True), \
|
|
|
|
patch.object(hass.config, 'is_allowed_path',
|
|
|
|
return_value=True):
|
|
|
|
hass.components.camera.async_snapshot('/tmp/bla')
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
mock_write = mopen().write
|
|
|
|
|
|
|
|
assert len(mock_write.mock_calls) == 1
|
|
|
|
assert mock_write.mock_calls[0][1][0] == b'Test'
|
2018-05-03 20:02:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
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')
|