Add exception translations to stream recording (#184438)

This commit is contained in:
Franck Nijhof
2026-10-06 12:40:23 +02:00
committed by GitHub
parent 5ad67e8f54
commit 59ccb76986
3 changed files with 42 additions and 6 deletions
+10 -4
View File
@@ -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)
@@ -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`."
}
}
}
+22 -2
View File
@@ -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())]