Skip to content

Commit a5184c7

Browse files
committed
Make now_playing_media.type MediaType
1 parent e4cd44d commit a5184c7

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

pyheos/command/__init__.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,31 @@ def optional_int(value: str | None) -> int | None:
166166
return None
167167

168168

169-
def parse_enum(
170-
key: str, data: dict[str, Any], enum_type: type[TEnum], default: TEnum
171-
) -> TEnum:
172-
"""Parse an enum value from the provided data. This is a safe operation that will return the default value if the key is missing or the value is not recognized."""
169+
def parse_optional_enum(
170+
key: str, data: dict[str, Any], enum_type: type[TEnum]
171+
) -> TEnum | None:
172+
"""Parse an enum value from the provided data. This is a safe operation that will return None if the key is missing or the value is not recognized."""
173173
value = data.get(key)
174174
if value is None:
175-
return default
175+
return None
176176
try:
177177
return enum_type(value)
178178
except ValueError:
179179
_LOGGER.warning(
180-
"Unrecognized '%s' value: '%s', using default value: '%s'. Full data: %s. %s",
180+
"Unrecognized '%s' value: '%s'. Full data: %s. %s",
181181
key,
182182
value,
183-
default,
184183
data,
185184
REPORT_ISSUE_TEXT,
186185
)
186+
return None
187+
188+
189+
def parse_enum(
190+
key: str, data: dict[str, Any], enum_type: type[TEnum], default: TEnum
191+
) -> TEnum:
192+
"""Parse an enum value from the provided data. This is a safe operation that will return the default value if the key is missing or the value is not recognized."""
193+
value = parse_optional_enum(key, data, enum_type)
194+
if value is None:
187195
return default
196+
return value

pyheos/player.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import TYPE_CHECKING, Any, Final, Optional, cast
88

99
from pyheos.abc import RemoveHeosFieldABC
10-
from pyheos.command import optional_int, parse_enum
10+
from pyheos.command import optional_int, parse_enum, parse_optional_enum
1111
from pyheos.dispatch import DisconnectType, EventCallbackType, callback_wrapper
1212
from pyheos.media import MediaItem, QueueItem, ServiceOption
1313
from pyheos.message import HeosMessage
@@ -101,7 +101,7 @@ class PlayerUpdateResult:
101101
class HeosNowPlayingMedia:
102102
"""Define now playing media information."""
103103

104-
type: str | None = None
104+
type: MediaType | None = None
105105
song: str | None = None
106106
station: str | None = None
107107
album: str | None = None
@@ -128,7 +128,7 @@ def __post_init__(self, *args: Any, **kwargs: Any) -> None:
128128
def _update_from_message(self, message: HeosMessage) -> None:
129129
"""Update the current instance from another instance."""
130130
data = cast(dict[str, Any], message.payload)
131-
self.type = data.get(c.ATTR_TYPE)
131+
self.type = parse_optional_enum(c.ATTR_TYPE, data, MediaType)
132132
self.song = data.get(c.ATTR_SONG)
133133
self.station = data.get(c.ATTR_STATION)
134134
self.album = data.get(c.ATTR_ALBUM)

tests/snapshots/test_heos.ambr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@
730730
<ControlType.PLAY_NEXT: 'play_next'>,
731731
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
732732
]),
733-
'type': 'station',
733+
'type': <MediaType.STATION: 'station'>,
734734
})
735735
# ---
736736
# name: test_get_players
@@ -782,7 +782,7 @@
782782
<ControlType.PLAY_NEXT: 'play_next'>,
783783
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
784784
]),
785-
'type': 'station',
785+
'type': <MediaType.STATION: 'station'>,
786786
}),
787787
'playback_error': None,
788788
'player_id': 1,
@@ -840,7 +840,7 @@
840840
<ControlType.PLAY_NEXT: 'play_next'>,
841841
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
842842
]),
843-
'type': 'station',
843+
'type': <MediaType.STATION: 'station'>,
844844
}),
845845
'playback_error': None,
846846
'player_id': 2,
@@ -907,7 +907,7 @@
907907
<ControlType.STOP: 'stop'>,
908908
<ControlType.PLAY_NEXT: 'play_next'>,
909909
]),
910-
'type': 'station',
910+
'type': <MediaType.STATION: 'station'>,
911911
})
912912
# ---
913913
# name: test_player_now_playing_changed_event[current_state]
@@ -948,7 +948,7 @@
948948
<ControlType.PLAY_NEXT: 'play_next'>,
949949
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
950950
]),
951-
'type': 'station',
951+
'type': <MediaType.STATION: 'station'>,
952952
})
953953
# ---
954954
# name: test_validate_connection

tests/snapshots/test_player.ambr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@
404404
<ControlType.PLAY_NEXT: 'play_next'>,
405405
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
406406
]),
407-
'type': 'station',
407+
'type': <MediaType.STATION: 'station'>,
408408
}),
409409
'playback_error': None,
410410
'player_id': -263109739,

0 commit comments

Comments
 (0)