diff --git a/framework/core/audioAmplifier/base.py b/framework/core/audioAmplifier/base.py index 2dec4d2..de24db9 100644 --- a/framework/core/audioAmplifier/base.py +++ b/framework/core/audioAmplifier/base.py @@ -105,3 +105,12 @@ def get_status(self) -> dict: :return: Dictionary of current amplifier state. """ pass + + @abstractmethod + def get_audio_format(self) -> str: + """ + Get a string of audio format information. + + :return: String of audio format like Dolby Atmos or PCM + """ + pass diff --git a/framework/core/audioAmplifier/denon_controller.py b/framework/core/audioAmplifier/denon_controller.py index a41690c..5e976fc 100644 --- a/framework/core/audioAmplifier/denon_controller.py +++ b/framework/core/audioAmplifier/denon_controller.py @@ -1,11 +1,16 @@ import asyncio + +import requests +from defusedxml import DefusedXmlException +from defusedxml.ElementTree import fromstring from denonavr import DenonAVR from .base import AudioAmplifier class DenonAVRController(AudioAmplifier): - def __init__(self, host: str): + def __init__(self, host: str, port: int = 10443): self.receiver = DenonAVR(host) + self.url = f"https://{host}:{port}/" self.setup() def setup(self): @@ -76,3 +81,28 @@ def get_status(self): "input": self.get_input(), "sound_mode": self.get_sound_mode(), } + + def get_audio_format(self): + """ + Web interface was showing input audio format. Could not find an equivalent method from + denonavr package. We need to get the inputSignal details, Searched whole package to find + any reference of inputSignal, but could not find. So using the web api itself here. + + Returns: + str: 'Dolby Atmos', 'PCM' + Raises: + ValueError: if could not find or parse the data + """ + try: + # The 'type=12' query parameter requests the configuration from the Denon AVR. + # the certificates in denon avr showed expired even after firmware update. so added verify=False + response = requests.get(f'{self.url}ajax/general/get_config?type=12', verify=False, timeout=15) + if response.status_code == 200: + xml_data = fromstring(response.content) + element = xml_data.find(".//InputSignal") + return element.text if element is not None else None + raise ValueError(f"Failed to fetch Audio format. Status code: {response.status_code}") + except DefusedXmlException: + raise ValueError("Failed to parse AVR response.") + except requests.exceptions.RequestException: + raise ValueError("Can't reach AVR. Please check configuration") \ No newline at end of file diff --git a/framework/core/audioAmplifierController.py b/framework/core/audioAmplifierController.py index e05a1dd..72764c8 100644 --- a/framework/core/audioAmplifierController.py +++ b/framework/core/audioAmplifierController.py @@ -96,3 +96,7 @@ def get_sound_mode(self) -> str: def get_status(self): self._log.info("Getting audio amplifier status") return self.audioAmplifier.get_status() + + def get_audio_format(self) -> str: + self._log.info("Getting audio format") + return self.audioAmplifier.get_audio_format() diff --git a/tests/audioAmp_test.py b/tests/audioAmp_test.py index a0a6813..4828fde 100644 --- a/tests/audioAmp_test.py +++ b/tests/audioAmp_test.py @@ -84,6 +84,14 @@ except: print(f"FAILED: Sound mode not set correctly. Expected: {sound_mode}, actual: {updated_sound_mode}") + # Audio format test + try: + audio_format = controller.get_audio_format() + assert audio_format == "Unknown" # when nothing plays + print(f"PASSED: Audio format: {audio_format}") + except Exception as e: + print(f"FAILED: Audio format - {audio_format} - {e}") + # Power OFF test try: controller.power_off()