diff --git a/homeassistant/components/stream/__init__.py b/homeassistant/components/stream/__init__.py index 5c858324c2ee..2d96a659856f 100644 --- a/homeassistant/components/stream/__init__.py +++ b/homeassistant/components/stream/__init__.py @@ -30,7 +30,7 @@ from yarl import URL from homeassistant.const import EVENT_HOMEASSISTANT_STOP, EVENT_LOGGING_CHANGED from homeassistant.core import Event, HomeAssistant, callback -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType from homeassistant.setup import SetupPhases, async_pause_setup @@ -567,13 +567,19 @@ class Stream: # Check for file access if not self.hass.config.is_allowed_path(video_path): - raise HomeAssistantError(f"Can't write {video_path}, no access to path!") + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="path_not_allowed", + translation_placeholders={"filename": video_path}, + ) # Add recorder if recorder := self.outputs().get(RECORDER_PROVIDER): assert isinstance(recorder, RecorderOutput) - raise HomeAssistantError( - f"Stream already recording to {recorder.video_path}!" + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="already_recording", + translation_placeholders={"filename": recorder.video_path}, ) recorder = cast( RecorderOutput, self.add_provider(RECORDER_PROVIDER, timeout=duration) diff --git a/homeassistant/components/stream/strings.json b/homeassistant/components/stream/strings.json new file mode 100644 index 000000000000..185ea4bdce55 --- /dev/null +++ b/homeassistant/components/stream/strings.json @@ -0,0 +1,10 @@ +{ + "exceptions": { + "already_recording": { + "message": "The stream is already recording to {filename}." + }, + "path_not_allowed": { + "message": "Cannot write to {filename} because access to this path is not allowed. `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`." + } + } +} diff --git a/tests/components/stream/test_recorder.py b/tests/components/stream/test_recorder.py index dc3373de2e5c..e74b2e40030d 100644 --- a/tests/components/stream/test_recorder.py +++ b/tests/components/stream/test_recorder.py @@ -20,7 +20,7 @@ from homeassistant.components.stream.const import ( from homeassistant.components.stream.core import Orientation, Part from homeassistant.components.stream.fmp4utils import find_box from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import ServiceValidationError from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util @@ -104,11 +104,31 @@ async def test_record_path_not_allowed(hass: HomeAssistant, h264_video) -> None: stream = create_stream(hass, h264_video, {}, dynamic_stream_settings()) with ( patch.object(hass.config, "is_allowed_path", return_value=False), - pytest.raises(HomeAssistantError), + pytest.raises( + ServiceValidationError, + match="Cannot write to /example/path because access to this path is not allowed", + ), ): await stream.async_record("/example/path") +async def test_record_already_recording(hass: HomeAssistant, h264_video) -> None: + """Test recording a stream that is already recording.""" + + stream = create_stream(hass, h264_video, {}, dynamic_stream_settings()) + recorder = stream.add_provider(RECORDER_PROVIDER) + recorder.video_path = "/example/first.mp4" + + with ( + patch.object(hass.config, "is_allowed_path", return_value=True), + pytest.raises( + ServiceValidationError, + match="The stream is already recording to /example/first.mp4", + ), + ): + await stream.async_record("/example/second.mp4") + + def add_parts_to_segment(segment, source): """Add relevant part data to segment for testing recorder.""" moof_locs = [*find_box(source.getbuffer(), b"moof"), len(source.getbuffer())]