Skip to content

Commit

Permalink
Use enum for HeosNowPlayingMedia.type
Browse files Browse the repository at this point in the history
- Make now_playing_media.type MediaType
  • Loading branch information
andrewsayre authored Jan 26, 2025
2 parents e4cd44d + a5184c7 commit dece8a6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
23 changes: 16 additions & 7 deletions pyheos/command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,31 @@ def optional_int(value: str | None) -> int | None:
return None


def parse_enum(
key: str, data: dict[str, Any], enum_type: type[TEnum], default: TEnum
) -> TEnum:
"""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."""
def parse_optional_enum(
key: str, data: dict[str, Any], enum_type: type[TEnum]
) -> TEnum | None:
"""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."""
value = data.get(key)
if value is None:
return default
return None
try:
return enum_type(value)
except ValueError:
_LOGGER.warning(
"Unrecognized '%s' value: '%s', using default value: '%s'. Full data: %s. %s",
"Unrecognized '%s' value: '%s'. Full data: %s. %s",
key,
value,
default,
data,
REPORT_ISSUE_TEXT,
)
return None


def parse_enum(
key: str, data: dict[str, Any], enum_type: type[TEnum], default: TEnum
) -> TEnum:
"""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."""
value = parse_optional_enum(key, data, enum_type)
if value is None:
return default
return value
6 changes: 3 additions & 3 deletions pyheos/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import TYPE_CHECKING, Any, Final, Optional, cast

from pyheos.abc import RemoveHeosFieldABC
from pyheos.command import optional_int, parse_enum
from pyheos.command import optional_int, parse_enum, parse_optional_enum
from pyheos.dispatch import DisconnectType, EventCallbackType, callback_wrapper
from pyheos.media import MediaItem, QueueItem, ServiceOption
from pyheos.message import HeosMessage
Expand Down Expand Up @@ -101,7 +101,7 @@ class PlayerUpdateResult:
class HeosNowPlayingMedia:
"""Define now playing media information."""

type: str | None = None
type: MediaType | None = None
song: str | None = None
station: str | None = None
album: str | None = None
Expand All @@ -128,7 +128,7 @@ def __post_init__(self, *args: Any, **kwargs: Any) -> None:
def _update_from_message(self, message: HeosMessage) -> None:
"""Update the current instance from another instance."""
data = cast(dict[str, Any], message.payload)
self.type = data.get(c.ATTR_TYPE)
self.type = parse_optional_enum(c.ATTR_TYPE, data, MediaType)
self.song = data.get(c.ATTR_SONG)
self.station = data.get(c.ATTR_STATION)
self.album = data.get(c.ATTR_ALBUM)
Expand Down
10 changes: 5 additions & 5 deletions tests/snapshots/test_heos.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@
<ControlType.PLAY_NEXT: 'play_next'>,
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
]),
'type': 'station',
'type': <MediaType.STATION: 'station'>,
})
# ---
# name: test_get_players
Expand Down Expand Up @@ -782,7 +782,7 @@
<ControlType.PLAY_NEXT: 'play_next'>,
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
]),
'type': 'station',
'type': <MediaType.STATION: 'station'>,
}),
'playback_error': None,
'player_id': 1,
Expand Down Expand Up @@ -840,7 +840,7 @@
<ControlType.PLAY_NEXT: 'play_next'>,
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
]),
'type': 'station',
'type': <MediaType.STATION: 'station'>,
}),
'playback_error': None,
'player_id': 2,
Expand Down Expand Up @@ -907,7 +907,7 @@
<ControlType.STOP: 'stop'>,
<ControlType.PLAY_NEXT: 'play_next'>,
]),
'type': 'station',
'type': <MediaType.STATION: 'station'>,
})
# ---
# name: test_player_now_playing_changed_event[current_state]
Expand Down Expand Up @@ -948,7 +948,7 @@
<ControlType.PLAY_NEXT: 'play_next'>,
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
]),
'type': 'station',
'type': <MediaType.STATION: 'station'>,
})
# ---
# name: test_validate_connection
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/test_player.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@
<ControlType.PLAY_NEXT: 'play_next'>,
<ControlType.PLAY_PREVIOUS: 'play_previous'>,
]),
'type': 'station',
'type': <MediaType.STATION: 'station'>,
}),
'playback_error': None,
'player_id': -263109739,
Expand Down

0 comments on commit dece8a6

Please sign in to comment.