Skip to content

Commit

Permalink
Address review comments regarding metadata access
Browse files Browse the repository at this point in the history
  • Loading branch information
mill1000 committed Dec 18, 2024
1 parent 83ef4fc commit 702a228
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions homeassistant/components/snapcast/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from collections.abc import Mapping
import logging
from typing import Any
from typing import Any, cast

Check warning on line 7 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L7

Added line #L7 was not covered by tests

from snapcast.control.client import Snapclient
from snapcast.control.group import Snapgroup
Expand Down Expand Up @@ -182,6 +182,8 @@ class SnapcastBaseDevice(SnapcastCoordinatorEntity, MediaPlayerEntity):
| MediaPlayerEntityFeature.VOLUME_SET
| MediaPlayerEntityFeature.SELECT_SOURCE
)
_attr_media_content_type = MediaType.MUSIC
_attr_device_class = MediaPlayerDeviceClass.SPEAKER

Check warning on line 186 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L185-L186

Added lines #L185 - L186 were not covered by tests

def __init__(
self,
Expand All @@ -194,8 +196,6 @@ def __init__(

self._device = device
self._attr_unique_id = self.get_unique_id(host_id, device.identifier)
self._attr_media_content_type = MediaType.MUSIC
self._attr_device_class = MediaPlayerDeviceClass.SPEAKER

@classmethod
def get_unique_id(cls, host, id) -> str:
Expand Down Expand Up @@ -279,52 +279,54 @@ async def async_unjoin(self) -> None:
"""Handle the unjoin service."""
raise NotImplementedError

def _get_metadata(self, key, default=None) -> Any:
@property
def metadata(self) -> Mapping[str, str | list[str | None]]:

Check warning on line 283 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L282-L283

Added lines #L282 - L283 were not covered by tests
"""Get metadata from the current stream."""
if metadata := self.coordinator.server.stream(

Check warning on line 285 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L285

Added line #L285 was not covered by tests
self._current_group.stream
).metadata:
return metadata.get(key, default)
return metadata

Check warning on line 288 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L288

Added line #L288 was not covered by tests

return default
# Fallback to an empty dict
return {}

Check warning on line 291 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L291

Added line #L291 was not covered by tests

@property
def media_title(self) -> str | None:

Check warning on line 294 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L293-L294

Added lines #L293 - L294 were not covered by tests
"""Title of current playing media."""
return self._get_metadata("title")
return cast(str, self.metadata.get("title"))

Check warning on line 296 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L296

Added line #L296 was not covered by tests

@property
def media_image_url(self) -> str | None:

Check warning on line 299 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L298-L299

Added lines #L298 - L299 were not covered by tests
"""Image url of current playing media."""
return self._get_metadata("artUrl")
return cast(str, self.metadata.get("artUrl"))

Check warning on line 301 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L301

Added line #L301 was not covered by tests

@property
def media_artist(self) -> str | None:

Check warning on line 304 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L303-L304

Added lines #L303 - L304 were not covered by tests
"""Artist of current playing media, music track only."""
return self._get_metadata("artist", [None])[0]
return self.metadata.get("artist", [None])[0]

Check warning on line 306 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L306

Added line #L306 was not covered by tests

@property
def media_album_name(self) -> str | None:

Check warning on line 309 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L308-L309

Added lines #L308 - L309 were not covered by tests
"""Album name of current playing media, music track only."""
return self._get_metadata("album")
return cast(str, self.metadata.get("album"))

Check warning on line 311 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L311

Added line #L311 was not covered by tests

@property
def media_album_artist(self) -> str | None:

Check warning on line 314 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L313-L314

Added lines #L313 - L314 were not covered by tests
"""Album artist of current playing media, music track only."""
return self._get_metadata("albumArtist", [None])[0]
return self.metadata.get("albumArtist", [None])[0]

Check warning on line 316 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L316

Added line #L316 was not covered by tests

@property
def media_track(self) -> int | None:

Check warning on line 319 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L318-L319

Added lines #L318 - L319 were not covered by tests
"""Track number of current playing media, music track only."""
if value := self._get_metadata("trackNumber") is not None:
if value := self.metadata.get("trackNumber") is not None:
return int(value)

Check warning on line 322 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L321-L322

Added lines #L321 - L322 were not covered by tests

return None

Check warning on line 324 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L324

Added line #L324 was not covered by tests

@property
def media_duration(self) -> int | None:

Check warning on line 327 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L326-L327

Added lines #L326 - L327 were not covered by tests
"""Duration of current playing media in seconds."""
if value := self._get_metadata("duration") is not None:
if value := self.metadata.get("duration") is not None:
return int(value)

Check warning on line 330 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L329-L330

Added lines #L329 - L330 were not covered by tests

return None

Check warning on line 332 in homeassistant/components/snapcast/media_player.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/snapcast/media_player.py#L332

Added line #L332 was not covered by tests
Expand Down

0 comments on commit 702a228

Please sign in to comment.