This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+9 -13
View File
@@ -2,8 +2,7 @@
import io
from homeassistant.components.stream import Stream
from homeassistant.components.stream.const import (
DOMAIN, ATTR_STREAMS)
from homeassistant.components.stream.const import DOMAIN, ATTR_STREAMS
def generate_h264_video():
@@ -20,28 +19,25 @@ def generate_h264_video():
total_frames = duration * fps
output = io.BytesIO()
output.name = 'test.ts'
container = av.open(output, mode='w')
output.name = "test.ts"
container = av.open(output, mode="w")
stream = container.add_stream('libx264', rate=fps)
stream = container.add_stream("libx264", rate=fps)
stream.width = 480
stream.height = 320
stream.pix_fmt = 'yuv420p'
stream.pix_fmt = "yuv420p"
for frame_i in range(total_frames):
img = np.empty((480, 320, 3))
img[:, :, 0] = 0.5 + 0.5 * np.sin(
2 * np.pi * (0 / 3 + frame_i / total_frames))
img[:, :, 1] = 0.5 + 0.5 * np.sin(
2 * np.pi * (1 / 3 + frame_i / total_frames))
img[:, :, 2] = 0.5 + 0.5 * np.sin(
2 * np.pi * (2 / 3 + frame_i / total_frames))
img[:, :, 0] = 0.5 + 0.5 * np.sin(2 * np.pi * (0 / 3 + frame_i / total_frames))
img[:, :, 1] = 0.5 + 0.5 * np.sin(2 * np.pi * (1 / 3 + frame_i / total_frames))
img[:, :, 2] = 0.5 + 0.5 * np.sin(2 * np.pi * (2 / 3 + frame_i / total_frames))
img = np.round(255 * img).astype(np.uint8)
img = np.clip(img, 0, 255)
frame = av.VideoFrame.from_ndarray(img, format='rgb24')
frame = av.VideoFrame.from_ndarray(img, format="rgb24")
for packet in stream.encode(frame):
container.mux(packet)
+8 -15
View File
@@ -9,8 +9,7 @@ from homeassistant.components.stream import request_stream
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed
from tests.components.stream.common import (
generate_h264_video, preload_stream)
from tests.components.stream.common import generate_h264_video, preload_stream
@pytest.mark.skip("Flaky in CI")
@@ -21,14 +20,12 @@ async def test_hls_stream(hass, hass_client):
Purposefully not mocking anything here to test full
integration with the stream component.
"""
await async_setup_component(hass, 'stream', {
'stream': {}
})
await async_setup_component(hass, "stream", {"stream": {}})
# Setup demo HLS track
source = generate_h264_video()
stream = preload_stream(hass, source)
stream.add_provider('hls')
stream.add_provider("hls")
# Request stream
url = request_stream(hass, source)
@@ -42,7 +39,7 @@ async def test_hls_stream(hass, hass_client):
# Fetch segment
playlist = await playlist_response.text()
playlist_url = '/'.join(parsed_url.path.split('/')[:-1])
playlist_url = "/".join(parsed_url.path.split("/")[:-1])
segment_url = playlist_url + playlist.splitlines()[-1][1:]
segment_response = await http_client.get(segment_url)
assert segment_response.status == 200
@@ -58,14 +55,12 @@ async def test_hls_stream(hass, hass_client):
@pytest.mark.skip("Flaky in CI")
async def test_stream_timeout(hass, hass_client):
"""Test hls stream timeout."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
await async_setup_component(hass, "stream", {"stream": {}})
# Setup demo HLS track
source = generate_h264_video()
stream = preload_stream(hass, source)
stream.add_provider('hls')
stream.add_provider("hls")
# Request stream
url = request_stream(hass, source)
@@ -97,14 +92,12 @@ async def test_stream_timeout(hass, hass_client):
@pytest.mark.skip("Flaky in CI")
async def test_stream_ended(hass):
"""Test hls stream packets ended."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
await async_setup_component(hass, "stream", {"stream": {}})
# Setup demo HLS track
source = generate_h264_video()
stream = preload_stream(hass, source)
track = stream.add_provider('hls')
track = stream.add_provider("hls")
track.num_segments = 2
# Request stream
+33 -49
View File
@@ -5,7 +5,12 @@ import pytest
from homeassistant.const import CONF_FILENAME
from homeassistant.components.stream.const import (
DOMAIN, SERVICE_RECORD, CONF_STREAM_SOURCE, CONF_LOOKBACK, ATTR_STREAMS)
DOMAIN,
SERVICE_RECORD,
CONF_STREAM_SOURCE,
CONF_LOOKBACK,
ATTR_STREAMS,
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
@@ -14,35 +19,24 @@ from tests.common import mock_coro
async def test_record_service_invalid_file(hass):
"""Test record service call with invalid file."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
data = {
CONF_STREAM_SOURCE: 'rtsp://my.video',
CONF_FILENAME: '/my/invalid/path'
}
await async_setup_component(hass, "stream", {"stream": {}})
data = {CONF_STREAM_SOURCE: "rtsp://my.video", CONF_FILENAME: "/my/invalid/path"}
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN, SERVICE_RECORD, data, blocking=True)
await hass.services.async_call(DOMAIN, SERVICE_RECORD, data, blocking=True)
async def test_record_service_init_stream(hass):
"""Test record service call with invalid file."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
data = {
CONF_STREAM_SOURCE: 'rtsp://my.video',
CONF_FILENAME: '/my/invalid/path'
}
with patch('homeassistant.components.stream.Stream') as stream_mock, \
patch.object(hass.config, 'is_allowed_path', return_value=True):
await async_setup_component(hass, "stream", {"stream": {}})
data = {CONF_STREAM_SOURCE: "rtsp://my.video", CONF_FILENAME: "/my/invalid/path"}
with patch("homeassistant.components.stream.Stream") as stream_mock, patch.object(
hass.config, "is_allowed_path", return_value=True
):
# Setup stubs
stream_mock.return_value.outputs = {}
# Call Service
await hass.services.async_call(
DOMAIN, SERVICE_RECORD, data, blocking=True)
await hass.services.async_call(DOMAIN, SERVICE_RECORD, data, blocking=True)
# Assert
assert stream_mock.called
@@ -50,54 +44,44 @@ async def test_record_service_init_stream(hass):
async def test_record_service_existing_record_session(hass):
"""Test record service call with invalid file."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
source = 'rtsp://my.video'
data = {
CONF_STREAM_SOURCE: source,
CONF_FILENAME: '/my/invalid/path'
}
await async_setup_component(hass, "stream", {"stream": {}})
source = "rtsp://my.video"
data = {CONF_STREAM_SOURCE: source, CONF_FILENAME: "/my/invalid/path"}
# Setup stubs
stream_mock = MagicMock()
stream_mock.return_value.outputs = {'recorder': MagicMock()}
stream_mock.return_value.outputs = {"recorder": MagicMock()}
hass.data[DOMAIN][ATTR_STREAMS][source] = stream_mock
with patch.object(hass.config, 'is_allowed_path', return_value=True), \
pytest.raises(HomeAssistantError):
with patch.object(hass.config, "is_allowed_path", return_value=True), pytest.raises(
HomeAssistantError
):
# Call Service
await hass.services.async_call(
DOMAIN, SERVICE_RECORD, data, blocking=True)
await hass.services.async_call(DOMAIN, SERVICE_RECORD, data, blocking=True)
async def test_record_service_lookback(hass):
"""Test record service call with invalid file."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
await async_setup_component(hass, "stream", {"stream": {}})
data = {
CONF_STREAM_SOURCE: 'rtsp://my.video',
CONF_FILENAME: '/my/invalid/path',
CONF_LOOKBACK: 4
CONF_STREAM_SOURCE: "rtsp://my.video",
CONF_FILENAME: "/my/invalid/path",
CONF_LOOKBACK: 4,
}
with patch('homeassistant.components.stream.Stream') as stream_mock, \
patch.object(hass.config, 'is_allowed_path', return_value=True):
with patch("homeassistant.components.stream.Stream") as stream_mock, patch.object(
hass.config, "is_allowed_path", return_value=True
):
# Setup stubs
hls_mock = MagicMock()
hls_mock.num_segments = 3
hls_mock.target_duration = 2
hls_mock.recv.return_value = mock_coro()
stream_mock.return_value.outputs = {
'hls': hls_mock
}
stream_mock.return_value.outputs = {"hls": hls_mock}
# Call Service
await hass.services.async_call(
DOMAIN, SERVICE_RECORD, data, blocking=True)
await hass.services.async_call(DOMAIN, SERVICE_RECORD, data, blocking=True)
assert stream_mock.called
stream_mock.return_value.add_provider.assert_called_once_with(
'recorder')
stream_mock.return_value.add_provider.assert_called_once_with("recorder")
assert hls_mock.recv.called
+9 -15
View File
@@ -10,8 +10,7 @@ from homeassistant.components.stream.recorder import recorder_save_worker
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed
from tests.components.stream.common import (
generate_h264_video, preload_stream)
from tests.components.stream.common import generate_h264_video, preload_stream
@pytest.mark.skip("Flaky in CI")
@@ -22,16 +21,13 @@ async def test_record_stream(hass, hass_client):
Purposefully not mocking anything here to test full
integration with the stream component.
"""
await async_setup_component(hass, 'stream', {
'stream': {}
})
await async_setup_component(hass, "stream", {"stream": {}})
with patch(
'homeassistant.components.stream.recorder.recorder_save_worker'):
with patch("homeassistant.components.stream.recorder.recorder_save_worker"):
# Setup demo track
source = generate_h264_video()
stream = preload_stream(hass, source)
recorder = stream.add_provider('recorder')
recorder = stream.add_provider("recorder")
stream.start()
segments = 0
@@ -49,17 +45,15 @@ async def test_record_stream(hass, hass_client):
@pytest.mark.skip("Flaky in CI")
async def test_recorder_timeout(hass, hass_client):
"""Test recorder timeout."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
await async_setup_component(hass, "stream", {"stream": {}})
with patch(
'homeassistant.components.stream.recorder.RecorderOutput.cleanup'
) as mock_cleanup:
"homeassistant.components.stream.recorder.RecorderOutput.cleanup"
) as mock_cleanup:
# Setup demo track
source = generate_h264_video()
stream = preload_stream(hass, source)
recorder = stream.add_provider('recorder')
recorder = stream.add_provider("recorder")
stream.start()
await recorder.recv()
@@ -78,7 +72,7 @@ async def test_recorder_save():
# Setup
source = generate_h264_video()
output = BytesIO()
output.name = 'test.mp4'
output.name = "test.mp4"
# Run
recorder_save_worker(output, [Segment(1, source, 4)])