Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make generic camera stream_source a template #36123

Merged
merged 9 commits into from
Jun 22, 2020
Merged
Prev Previous commit
Next Next commit
Add test for stream source template
  • Loading branch information
MartinHjelmare committed Jun 17, 2020
commit 06d2e022df92ac1054793205321f702f82a3b253
65 changes: 62 additions & 3 deletions tests/components/generic/test_camera.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""The tests for generic camera component."""
import asyncio
from unittest import mock

from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.const import HTTP_INTERNAL_SERVER_ERROR, HTTP_NOT_FOUND
from homeassistant.setup import async_setup_component

from tests.async_mock import patch


async def test_fetching_url(aioclient_mock, hass, hass_client):
"""Test that it fetches the given url."""
Expand Down Expand Up @@ -104,7 +106,6 @@ async def test_limit_refetch(aioclient_mock, hass, hass_client):
"name": "config_test",
"platform": "generic",
"still_image_url": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"stream_source": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
}
},
Expand All @@ -116,7 +117,7 @@ async def test_limit_refetch(aioclient_mock, hass, hass_client):

hass.states.async_set("sensor.temp", "5")

with mock.patch("async_timeout.timeout", side_effect=asyncio.TimeoutError()):
with patch("async_timeout.timeout", side_effect=asyncio.TimeoutError()):
resp = await client.get("/api/camera_proxy/camera.config_test")
assert aioclient_mock.call_count == 0
assert resp.status == HTTP_INTERNAL_SERVER_ERROR
Expand Down Expand Up @@ -153,6 +154,64 @@ async def test_limit_refetch(aioclient_mock, hass, hass_client):
assert body == "hello planet"


async def test_stream_source(aioclient_mock, hass, hass_client, hass_ws_client):
"""Test that the stream source is rendered."""
assert await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "generic",
"still_image_url": "https://example.com",
"stream_source": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
}
},
)

hass.states.async_set("sensor.temp", "5")

with patch(
"homeassistant.components.camera.request_stream",
return_value="http://home.assistant/playlist.m3u8",
) as mock_request_stream:
# Request playlist through WebSocket
client = await hass_ws_client(hass)

await client.send_json(
{"id": 1, "type": "camera/stream", "entity_id": "camera.config_test"}
)
msg = await client.receive_json()

# Assert WebSocket response
assert mock_request_stream.call_count == 1
assert mock_request_stream.call_args[0][1] == "http://example.com/5a"
assert msg["id"] == 1
assert msg["type"] == TYPE_RESULT
assert msg["success"]
assert msg["result"]["url"][-13:] == "playlist.m3u8"

# Cause a template render error
hass.states.async_remove("sensor.temp")

await client.send_json(
{"id": 2, "type": "camera/stream", "entity_id": "camera.config_test"}
)
msg = await client.receive_json()

# Assert that no new call to the stream request should have been made
assert mock_request_stream.call_count == 1
# Assert the websocket error message
assert msg["id"] == 2
assert msg["type"] == TYPE_RESULT
assert msg["success"] is False
assert msg["error"] == {
"code": "start_stream_failed",
"message": "camera.config_test does not support play stream service",
}


async def test_camera_content_type(aioclient_mock, hass, hass_client):
"""Test generic camera with custom content_type."""
svg_image = "<some image>"
Expand Down