mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
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:
committed by
Paulus Schoutsen
parent
9d21afa444
commit
26726af689
103
tests/components/stream/test_init.py
Normal file
103
tests/components/stream/test_init.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user