diff --git a/homeassistant/components/stream/recorder.py b/homeassistant/components/stream/recorder.py index 0344e220647..f61211340ef 100644 --- a/homeassistant/components/stream/recorder.py +++ b/homeassistant/components/stream/recorder.py @@ -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) diff --git a/tests/components/stream/test_recorder.py b/tests/components/stream/test_recorder.py index 199020097bd..48fe48d3337 100644 --- a/tests/components/stream/test_recorder.py +++ b/tests/components/stream/test_recorder.py @@ -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 ):