From 702a2280047ead581ea8d84e61162b387416b798 Mon Sep 17 00:00:00 2001
From: Tucker Kern <tuckkern@gmail.com>
Date: Wed, 18 Dec 2024 23:27:06 +0000
Subject: [PATCH] Address review comments regarding metadata access

---
 .../components/snapcast/media_player.py       | 28 ++++++++++---------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py
index 3f9dce1b486ba8..39b2cfd569d568 100644
--- a/homeassistant/components/snapcast/media_player.py
+++ b/homeassistant/components/snapcast/media_player.py
@@ -4,7 +4,7 @@
 
 from collections.abc import Mapping
 import logging
-from typing import Any
+from typing import Any, cast
 
 from snapcast.control.client import Snapclient
 from snapcast.control.group import Snapgroup
@@ -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
 
     def __init__(
         self,
@@ -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:
@@ -279,44 +279,46 @@ 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]]:
         """Get metadata from the current stream."""
         if metadata := self.coordinator.server.stream(
             self._current_group.stream
         ).metadata:
-            return metadata.get(key, default)
+            return metadata
 
-        return default
+        # Fallback to an empty dict
+        return {}
 
     @property
     def media_title(self) -> str | None:
         """Title of current playing media."""
-        return self._get_metadata("title")
+        return cast(str, self.metadata.get("title"))
 
     @property
     def media_image_url(self) -> str | None:
         """Image url of current playing media."""
-        return self._get_metadata("artUrl")
+        return cast(str, self.metadata.get("artUrl"))
 
     @property
     def media_artist(self) -> str | None:
         """Artist of current playing media, music track only."""
-        return self._get_metadata("artist", [None])[0]
+        return self.metadata.get("artist", [None])[0]
 
     @property
     def media_album_name(self) -> str | None:
         """Album name of current playing media, music track only."""
-        return self._get_metadata("album")
+        return cast(str, self.metadata.get("album"))
 
     @property
     def media_album_artist(self) -> str | None:
         """Album artist of current playing media, music track only."""
-        return self._get_metadata("albumArtist", [None])[0]
+        return self.metadata.get("albumArtist", [None])[0]
 
     @property
     def media_track(self) -> int | None:
         """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)
 
         return None
@@ -324,7 +326,7 @@ def media_track(self) -> int | None:
     @property
     def media_duration(self) -> int | None:
         """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)
 
         return None