Handle stream failures in recorder (#47151)

* Handle stream failures in recorder

Fail gracefully with an error message when the recorder is invoked with no segments due to a stream failure.

* Update homeassistant/components/stream/recorder.py

Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>

Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>
This commit is contained in:
Allen Porter 2021-02-28 18:01:28 -08:00 committed by GitHub
parent 44ed6cda40
commit 715a254913
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -21,6 +21,11 @@ def async_setup_recorder(hass):
def recorder_save_worker(file_out: str, segments: Deque[Segment]):
"""Handle saving stream."""
if not segments:
_LOGGER.error("Recording failed to capture anything")
return
if not os.path.exists(os.path.dirname(file_out)):
os.makedirs(os.path.dirname(file_out), exist_ok=True)

View File

@ -193,6 +193,18 @@ async def test_recorder_discontinuity(tmpdir):
assert os.path.exists(filename)
async def test_recorder_no_segements(tmpdir):
"""Test recorder behavior with a stream failure which causes no segments."""
# Setup
filename = f"{tmpdir}/test.mp4"
# Run
recorder_save_worker("unused-file", [])
# Assert
assert not os.path.exists(filename)
async def test_record_stream_audio(
hass, hass_client, stream_worker_sync, record_worker_sync
):