Guard currently playing for being a NoneType

This commit is contained in:
Franck Nijhof
2020-02-14 20:24:31 +01:00
parent 4b137989e7
commit f5f56b0db0

View File

@@ -152,11 +152,15 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
@property @property
def volume_level(self) -> Optional[float]: def volume_level(self) -> Optional[float]:
"""Return the device volume.""" """Return the device volume."""
if self._currently_playing is None:
return None
return self._currently_playing.get("device", {}).get("volume_percent", 0) / 100 return self._currently_playing.get("device", {}).get("volume_percent", 0) / 100
@property @property
def media_content_id(self) -> Optional[str]: def media_content_id(self) -> Optional[str]:
"""Return the media URL.""" """Return the media URL."""
if self._currently_playing is None:
return None
return self._currently_playing.get("item", {}).get("name") return self._currently_playing.get("item", {}).get("name")
@property @property
@@ -167,7 +171,10 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
@property @property
def media_duration(self) -> Optional[int]: def media_duration(self) -> Optional[int]:
"""Duration of current playing media in seconds.""" """Duration of current playing media in seconds."""
if self._currently_playing.get("item") is None: if (
self._currently_playing is None
or self._currently_playing.get("item") is None
):
return None return None
return self._currently_playing["item"]["duration_ms"] / 1000 return self._currently_playing["item"]["duration_ms"] / 1000
@@ -189,7 +196,8 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
def media_image_url(self) -> Optional[str]: def media_image_url(self) -> Optional[str]:
"""Return the media image URL.""" """Return the media image URL."""
if ( if (
self._currently_playing.get("item") is None self._currently_playing is None
or self._currently_playing.get("item") is None
or not self._currently_playing["item"]["album"]["images"] or not self._currently_playing["item"]["album"]["images"]
): ):
return None return None
@@ -203,12 +211,17 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
@property @property
def media_title(self) -> Optional[str]: def media_title(self) -> Optional[str]:
"""Return the media title.""" """Return the media title."""
if self._currently_playing is None:
return None
return self._currently_playing.get("item", {}).get("name") return self._currently_playing.get("item", {}).get("name")
@property @property
def media_artist(self) -> Optional[str]: def media_artist(self) -> Optional[str]:
"""Return the media artist.""" """Return the media artist."""
if self._currently_playing.get("item") is None: if (
self._currently_playing is None
or self._currently_playing.get("item") is None
):
return None return None
return ", ".join( return ", ".join(
[artist["name"] for artist in self._currently_playing["item"]["artists"]] [artist["name"] for artist in self._currently_playing["item"]["artists"]]
@@ -217,13 +230,18 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
@property @property
def media_album_name(self) -> Optional[str]: def media_album_name(self) -> Optional[str]:
"""Return the media album.""" """Return the media album."""
if self._currently_playing.get("item") is None: if (
self._currently_playing is None
or self._currently_playing.get("item") is None
):
return None return None
return self._currently_playing["item"]["album"]["name"] return self._currently_playing["item"]["album"]["name"]
@property @property
def media_track(self) -> Optional[int]: def media_track(self) -> Optional[int]:
"""Track number of current playing media, music track only.""" """Track number of current playing media, music track only."""
if self._currently_playing is None:
return None
return self._currently_playing.get("item", {}).get("track_number") return self._currently_playing.get("item", {}).get("track_number")
@property @property
@@ -236,6 +254,8 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
@property @property
def source(self) -> Optional[str]: def source(self) -> Optional[str]:
"""Return the current playback device.""" """Return the current playback device."""
if self._currently_playing is None:
return None
return self._currently_playing.get("device", {}).get("name") return self._currently_playing.get("device", {}).get("name")
@property @property
@@ -248,6 +268,8 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
@property @property
def shuffle(self) -> bool: def shuffle(self) -> bool:
"""Shuffling state.""" """Shuffling state."""
if self._currently_playing is None:
return False
return bool(self._currently_playing.get("shuffle_state")) return bool(self._currently_playing.get("shuffle_state"))
@property @property