Skip to content

Commit

Permalink
Don't return image hashes when the mpd instance doesn't support it
Browse files Browse the repository at this point in the history
  • Loading branch information
mweinelt committed Jan 8, 2021
1 parent 58195c6 commit 1000d59
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions homeassistant/components/mpd/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def __init__(self, server, port, password, name):
self._media_position_updated_at = None
self._media_position = None
self._commands = None
self._can_albumart = False
self._can_readpicture = False

# set up MPD client
self._client = MPDClient()
Expand Down Expand Up @@ -167,6 +169,10 @@ async def async_update(self):
await self._connect()
self._commands = list(await self._client.commands())

# not all MPD implementations and versions support the `albumart` and `fetchpicture` commands
self._can_albumart = "albumart" in self._commands
self._can_readpicture = "readpicture" in self._commands

await self._fetch_status()
except (mpd.ConnectionError, OSError, BrokenPipeError, ValueError) as error:
# Cleanly disconnect in case connection is not in valid state
Expand Down Expand Up @@ -258,6 +264,9 @@ def media_album_name(self):
@property
def media_image_hash(self):
"""Hash value for media image."""
if not self._can_albumart and not self._can_readpicture:
return None

file = self._currentsong.get("file")
if file:
return hashlib.sha256(file.encode("utf-8")).hexdigest()[:16]
Expand All @@ -270,14 +279,10 @@ async def async_get_media_image(self):
if not file:
return None, None

# not all MPD implementations and versions support the `albumart` and `fetchpicture` commands
can_albumart = "albumart" in self._commands
can_readpicture = "readpicture" in self._commands

response = None

# read artwork embedded into the media file
if can_readpicture:
if self._can_readpicture:
try:
response = await self._client.readpicture(file)
except mpd.CommandError as error:
Expand All @@ -287,7 +292,7 @@ async def async_get_media_image(self):
)

# read artwork contained in the media directory (cover.{jpg,png,tiff,bmp}) if none is embedded
if can_albumart and not response:
if self._can_albumart and not response:
try:
response = await self._client.albumart(file)
except mpd.CommandError as error:
Expand Down

0 comments on commit 1000d59

Please sign in to comment.