From e148ef9ff495c112cc2f8f97e7714eb466a2fb61 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Fri, 12 Apr 2024 12:07:53 -0700 Subject: [PATCH] Parse decoder messages Signed-off-by: Ben Johnson --- tests/test_trinnov_altitude.py | 2 ++ trinnov_altitude/__init__.py | 2 +- trinnov_altitude/messages.py | 18 ++++++++++++++++++ trinnov_altitude/trinnov_altitude.py | 9 +++++++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/tests/test_trinnov_altitude.py b/tests/test_trinnov_altitude.py index 3a9e6b3..7939d44 100644 --- a/tests/test_trinnov_altitude.py +++ b/tests/test_trinnov_altitude.py @@ -88,11 +88,13 @@ async def test_start_listening_syncs(mock_server, connected_client): await asyncio.sleep(0.5) assert connected_client.audiosync == "Slave" assert connected_client.bypass is False + assert connected_client.decoder == "none" assert connected_client.dim is False assert connected_client.id == "10485761" assert connected_client.mute is False assert connected_client.preset == "Builtin" assert connected_client.source == "Apple TV" + assert connected_client.upmixer == "none" assert connected_client.version == "4.3.2rc1" assert connected_client.volume == -40 diff --git a/trinnov_altitude/__init__.py b/trinnov_altitude/__init__.py index 5f3e5a0..81acfe9 100644 --- a/trinnov_altitude/__init__.py +++ b/trinnov_altitude/__init__.py @@ -6,4 +6,4 @@ log_format = "[L %(lineno)s - %(funcName)5s() ] %(message)s" logging.basicConfig(level=level, format=log_format) -__version__ = "0.1.24" +__version__ = "0.1.25" diff --git a/trinnov_altitude/messages.py b/trinnov_altitude/messages.py index fe6eb2f..30de110 100644 --- a/trinnov_altitude/messages.py +++ b/trinnov_altitude/messages.py @@ -17,6 +17,14 @@ def message_factory(message) -> Message: # noqa: C901 elif match := re.match(r"^CURRENT_PROFILE\s(-?\d+)", message): state = int(match.group(1)) return CurrentSourceMessage(state) + elif match := re.match( + r"DECODER NONAUDIO (\d+) PLAYABLE (\d+) DECODER (\w+) UPMIXER (\w+)", message + ): + nonaudio = bool(int(match.group(1))) + playable = bool(int(match.group(2))) + decoder = match.group(3) + upmixer = match.group(4) + return DecoderMessage(nonaudio, playable, decoder, upmixer) elif match := re.match(r"^DIM\s(-?\d+)", message): state = bool(int(match.group(1))) return DimMessage(state) @@ -81,6 +89,16 @@ def __init__(self, index: int) -> None: self.index = index +class DecoderMessage(Message): + def __init__( + self, nonaudio: bool, playable: bool, decoder: str, upmixer: str + ) -> None: + self.nonaudio = nonaudio + self.playable = playable + self.decoder = decoder + self.upmixer = upmixer + + class DimMessage(Message): def __init__(self, state: bool) -> None: self.state = state diff --git a/trinnov_altitude/trinnov_altitude.py b/trinnov_altitude/trinnov_altitude.py index 80c95b0..72ad552 100644 --- a/trinnov_altitude/trinnov_altitude.py +++ b/trinnov_altitude/trinnov_altitude.py @@ -123,8 +123,8 @@ async def connect(self, timeout: int | float | None = USE_DEFAULT_TIMEOUT): except (OSError, ValueError) as e: raise exceptions.ConnectionFailedError(e) else: - # Trinnov will only send the state of - # these attributes if they are true. + # Default these values since the Trinnov Altitude will only + # send them upon connect if they are active. self.bypass = False self.dim = False self.mute = False @@ -164,10 +164,12 @@ async def disconnect(self, timeout: int | float | None = USE_DEFAULT_TIMEOUT): # Reset state self.audiosync = None self.bypass = None + self.decoder = None self.dim = None self.mute = None self.preset = None self.source = None + self.upmixer = None self.volume = None # Fire the callback to signal a connection state change @@ -648,6 +650,9 @@ def _process_message(self, raw_message: str) -> messages.Message: # noqa: C901 self.preset = self.presets.get(message.index) elif isinstance(message, messages.CurrentSourceMessage): self.source = self.sources.get(message.index) + elif isinstance(message, messages.DecoderMessage): + self.decoder = message.decoder + self.upmixer = message.upmixer elif isinstance(message, messages.DimMessage): self.dim = message.state elif isinstance(message, messages.ErrorMessage):