From ebeff31bf69a6980099bfa61a9b8e5b105855ba7 Mon Sep 17 00:00:00 2001 From: Mike Degatano Date: Wed, 27 Jul 2022 11:15:54 -0400 Subject: [PATCH] Pass supervisor debug value to audio (#3752) --- supervisor/plugins/audio.py | 30 +++++++++++++++++++++- tests/plugins/test_audio.py | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/plugins/test_audio.py diff --git a/supervisor/plugins/audio.py b/supervisor/plugins/audio.py index 8527e527f..fb0338af7 100644 --- a/supervisor/plugins/audio.py +++ b/supervisor/plugins/audio.py @@ -12,10 +12,17 @@ from typing import Optional from awesomeversion import AwesomeVersion import jinja2 +from ..const import LogLevel from ..coresys import CoreSys from ..docker.audio import DockerAudio from ..docker.stats import DockerStats -from ..exceptions import AudioError, AudioUpdateError, DockerError +from ..exceptions import ( + AudioError, + AudioUpdateError, + ConfigurationFileError, + DockerError, +) +from ..utils.json import write_json_file from .base import PluginBase from .const import FILE_HASSIO_AUDIO from .validate import SCHEMA_AUDIO_CONFIG @@ -47,6 +54,11 @@ class PluginAudio(PluginBase): """Return path of default asound config file.""" return self.sys_config.path_extern_audio.joinpath("asound") + @property + def pulse_audio_config(self) -> Path: + """Return Path to pulse audio config file.""" + return Path(self.sys_config.path_audio, "pulse_audio.json") + @property def latest_version(self) -> Optional[AwesomeVersion]: """Return latest version of Audio.""" @@ -120,6 +132,7 @@ class PluginAudio(PluginBase): async def restart(self) -> None: """Restart Audio plugin.""" _LOGGER.info("Restarting Audio plugin") + self._write_config() try: await self.instance.restart() except DockerError as err: @@ -128,6 +141,7 @@ class PluginAudio(PluginBase): async def start(self) -> None: """Run Audio plugin.""" _LOGGER.info("Starting Audio plugin") + self._write_config() try: await self.instance.run() except DockerError as err: @@ -171,3 +185,17 @@ class PluginAudio(PluginBase): default_source=input_profile, default_sink=output_profile, ) + + def _write_config(self): + """Write pulse audio config.""" + try: + write_json_file( + self.pulse_audio_config, + { + "debug": self.sys_config.logging == LogLevel.DEBUG, + }, + ) + except ConfigurationFileError as err: + raise AudioError( + f"Can't update pulse audio config: {err}", _LOGGER.error + ) from err diff --git a/tests/plugins/test_audio.py b/tests/plugins/test_audio.py new file mode 100644 index 000000000..21a41ab2d --- /dev/null +++ b/tests/plugins/test_audio.py @@ -0,0 +1,50 @@ +"""Test audio plugin.""" +from pathlib import Path +from unittest.mock import AsyncMock, Mock, patch + +import pytest + +from supervisor.const import LogLevel +from supervisor.coresys import CoreSys + +from tests.plugins.test_dns import fixture_docker_interface # noqa: F401 + + +@pytest.fixture(name="write_json") +async def fixture_write_json() -> Mock: + """Mock json file writer.""" + with patch("supervisor.plugins.audio.write_json_file") as write_json_file: + yield write_json_file + + +async def test_config_write( + coresys: CoreSys, + docker_interface: tuple[AsyncMock, AsyncMock], + write_json: Mock, +): + """Test config write on audio start and restart.""" + await coresys.plugins.audio.start() + docker_interface[0].assert_called_once() + docker_interface[1].assert_not_called() + + write_json.assert_called_once_with( + Path("/data/audio/pulse_audio.json"), + { + "debug": False, + }, + ) + + docker_interface[0].reset_mock() + write_json.reset_mock() + coresys.config.logging = LogLevel.DEBUG + + await coresys.plugins.audio.restart() + docker_interface[0].assert_not_called() + docker_interface[1].assert_called_once() + + write_json.assert_called_once_with( + Path("/data/audio/pulse_audio.json"), + { + "debug": True, + }, + )