Stream Record Service (#22456)
* Initial commit of record service for live streams * fix lint * update service descriptions * add tests * fix lint
This commit is contained in:
1 parent
9d21afa444
commit
26726af689
11 files changed
+466
-19
No files matched your search
@@ -0,0 +1,103 @@
|
||||
"""The tests for stream."""
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.const import CONF_FILENAME
|
||||
from homeassistant.components.stream.const import (
|
||||
DOMAIN, SERVICE_RECORD, CONF_STREAM_SOURCE, CONF_LOOKBACK, ATTR_STREAMS)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
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'
|
||||
}
|
||||
with pytest.raises(HomeAssistantError):
|
||||
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):
|
||||
# Setup stubs
|
||||
stream_mock.return_value.outputs = {}
|
||||
|
||||
# Call Service
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_RECORD, data, blocking=True)
|
||||
|
||||
# Assert
|
||||
assert stream_mock.called
|
||||
|
||||
|
||||
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'
|
||||
}
|
||||
|
||||
# Setup stubs
|
||||
stream_mock = 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):
|
||||
# Call Service
|
||||
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': {}
|
||||
})
|
||||
data = {
|
||||
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):
|
||||
# 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
|
||||
}
|
||||
|
||||
# Call Service
|
||||
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')
|
||||
assert hls_mock.recv.called
|
||||
@@ -0,0 +1,83 @@
|
||||
"""The tests for hls streams."""
|
||||
from datetime import timedelta
|
||||
from io import BytesIO
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.stream.core import Segment
|
||||
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)
|
||||
|
||||
|
||||
async def test_record_stream(hass, hass_client):
|
||||
"""
|
||||
Test record stream.
|
||||
|
||||
Purposefully not mocking anything here to test full
|
||||
integration with the stream component.
|
||||
"""
|
||||
await async_setup_component(hass, 'stream', {
|
||||
'stream': {}
|
||||
})
|
||||
|
||||
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')
|
||||
stream.start()
|
||||
|
||||
segments = 0
|
||||
while True:
|
||||
segment = await recorder.recv()
|
||||
if not segment:
|
||||
break
|
||||
segments += 1
|
||||
|
||||
stream.stop()
|
||||
|
||||
assert segments == 3
|
||||
|
||||
|
||||
async def test_recorder_timeout(hass, hass_client):
|
||||
"""Test recorder timeout."""
|
||||
await async_setup_component(hass, 'stream', {
|
||||
'stream': {}
|
||||
})
|
||||
|
||||
with patch(
|
||||
'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')
|
||||
stream.start()
|
||||
|
||||
await recorder.recv()
|
||||
|
||||
# Wait a minute
|
||||
future = dt_util.utcnow() + timedelta(minutes=1)
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_cleanup.called
|
||||
|
||||
|
||||
async def test_recorder_save():
|
||||
"""Test recorder save."""
|
||||
# Setup
|
||||
source = generate_h264_video()
|
||||
output = BytesIO()
|
||||
output.name = 'test.mp4'
|
||||
|
||||
# Run
|
||||
recorder_save_worker(output, [Segment(1, source, 4)])
|
||||
|
||||
# Assert
|
||||
assert output.getvalue()
|
||||
Reference in new issue
Block a user