Skip to content

Commit

Permalink
Parse decoder messages
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Johnson <ben@binarylogic.com>
  • Loading branch information
binarylogic committed Apr 12, 2024
1 parent 2df5c05 commit e148ef9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/test_trinnov_altitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion trinnov_altitude/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 18 additions & 0 deletions trinnov_altitude/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions trinnov_altitude/trinnov_altitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e148ef9

Please sign in to comment.