Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mpd errors on startup when device is offline/no cover art is displayed #44931

Closed
Mariusthvdb opened this issue Jan 7, 2021 · 25 comments · Fixed by #45141
Closed

Mpd errors on startup when device is offline/no cover art is displayed #44931

Mariusthvdb opened this issue Jan 7, 2021 · 25 comments · Fixed by #45141
Assignees

Comments

@Mariusthvdb
Copy link
Contributor

Mariusthvdb commented Jan 7, 2021

The problem

After updating to 2021.1, MPD errors on startup, when device is offline. more importantly, when the device is online, it wont show up as media_player in HA.

Added to that, no coverart is displayed at all, which would be the new feature of this release

Environment

  • Home Assistant Core release with the issue: 2021.1
  • Last working Home Assistant Core release (if known):
  • Operating environment (OS/Container/Supervised/Core): OS
  • Integration causing this issue: MPD
  • Link to integration documentation on our website: https://www.home-assistant.io/integrations/mpd/

Problem-relevant configuration.yaml

media_player:

  platform: mpd
  host: 192.168.1.177
  name: Bryston Bdp-1

Traceback/Error logs

2021-01-07 10:38:08 ERROR (MainThread) [homeassistant.components.media_player] mpd: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 167, in async_update
    await self._connect()
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 121, in _connect
    await self._client.connect(self.server, self.port)
  File "/usr/local/lib/python3.8/site-packages/mpd/asyncio.py", line 167, in connect
    r, w = await asyncio.open_connection(host, port, loop=loop)
  File "/usr/local/lib/python3.8/asyncio/streams.py", line 52, in open_connection
    transport, _ = await loop.create_connection(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1025, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1010, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 924, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 496, in sock_connect
    return await fut
  File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 528, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
OSError: [Errno 113] Connect call failed ('192.168.1.177', 6600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 174, in async_update
    self._disconnect()
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 133, in _disconnect
    self._client.disconnect()
  File "/usr/local/lib/python3.8/site-packages/mpd/asyncio.py", line 188, in disconnect
    if self.__wfile is not None:
AttributeError: 'MPDClient' object has no attribute '_MPDClient__wfile'

Additional information

please see community post: https://community.home-assistant.io/t/how-to-get-the-new-m-usic-p-layer-d-aemon-image-to-appear/265301

I did try to enter a password (my devices system allows system settings only after entering login/pw) but that errored stating 'wrong password'
Since I can not enter a login name in the config, I take it is useless to try to add a password, so I took it out again.

Media player is setup correctly when device is on on HA startup, albeit without coverart.

This is hardly a desired situation, since most of the time the MPD device will be off, and HA should be able to handle that gracefully, and, check for the device to become online.

@probot-home-assistant
Copy link

mpd documentation
mpd source
(message by IssueLinks)

@probot-home-assistant
Copy link

Hey there @fabaff, mind taking a look at this issue as its been labeled with an integration (mpd) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

@winterscar
Copy link

My issue (#44948) may be a duplicate of this. I won't close it just yet as I'm not seeing the errors in the logs, just not getting album art.

@mweinelt
Copy link
Contributor

mweinelt commented Jan 8, 2021

OSError: [Errno 113] Connect call failed ('192.168.1.177', 6600)

This honestly looks like it cannot reach the MPD instance. Though if I had to guess where things go wrong it's here:

file = self._currentsong.get("file")

Would probably help to check if any of these two is available, else return 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

Can you test that?

@Mariusthvdb
Copy link
Contributor Author

it cant reach the MPD instance,because it isnt powered on, that my whole point. Before, we didn't see these errors, and the media_player simply came online after powering there MPD instance. That doesn't happen any longer.

About the test for album art: how can I test that? Using HA OS here.

@mweinelt
Copy link
Contributor

mweinelt commented Jan 8, 2021

I have no idea how HA OS works tbh. This would be my proposed change:
mweinelt@b1f4d45

I'll look into the failed connection noise later tonight.

@Mariusthvdb
Copy link
Contributor Author

Mariusthvdb commented Jan 8, 2021

Sorry, but I don't get it. We read in the release note that MPD now shows album art:

The Music Player Daemon integration now shows album art in the media player.

and yet you state here the integration doesn't support that? I am confused...

@mweinelt
Copy link
Contributor

mweinelt commented Jan 8, 2021

The MPD platform works with multiple backends that all support the MPD protocol, such as MPD (the original implementation) and Mopidy (a reimplmentation). Not all backends support all features. So the problem is that you are using Mopidy and that it does not supply that information via the MPD protocol.

@Mariusthvdb
Copy link
Contributor Author

Why would you say I use Mopidy? I know of no such thing tbh, only connected my MPD player via the media_player config and the core integration.
Sorry if this sound noobish, simply trying to get to the bottom of this to see where a config change might help getting the images in the player card.

@mweinelt
Copy link
Contributor

mweinelt commented Jan 9, 2021

Sorry, i mixed this up with #44948.

@Mariusthvdb
Copy link
Contributor Author

seems we need MPD 0.21 for album art: mopidy/mopidy-mpd#17 (comment) ?

@mweinelt
Copy link
Contributor

Yeah, sounds like it. The mpd changelog doesn't really say. Anyway, there are two commands involved:

  1. albumart, the older one, could be from 0.21 (release notes don't say), grabbing cover.{png,jpg,bmp,tiff} from the medias directory
  2. fetchpicture, released in 0.22 (https://raw.githubusercontent.com/MusicPlayerDaemon/MPD/v0.22/NEWS), to get artwork embedded in the media file

The mpd component can use both, either or none, which is dynamically determined from the list of commands given by the MPD implementation.

@deisi
Copy link
Contributor

deisi commented Jan 10, 2021

I have the same issue, as the OP but trouble following the discussion here. So do I understand it correctly, that mpd version 0.21.xx is currently broken with Homeassistant and I just need to update mpd?

And I'm currently using:

$ mpd -V
Music Player Daemon 0.21.20 (0.21.20)

@mweinelt
Copy link
Contributor

It shouldn't be. What errors are you getting and what is your setup? Is mpd always avaiable?

@deisi
Copy link
Contributor

deisi commented Jan 11, 2021

mpd is always on. I get the same error as @Mariusthvdb

2021-01-11 08:24:00 ERROR (MainThread) [homeassistant.components.media_player] mpd: Error on device update!
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 167, in async_update
    await self._connect()
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 121, in _connect
    await self._client.connect(self.server, self.port)
  File "/usr/local/lib/python3.8/site-packages/mpd/asyncio.py", line 167, in connect
    r, w = await asyncio.open_connection(host, port, loop=loop)
  File "/usr/local/lib/python3.8/asyncio/streams.py", line 52, in open_connection
    transport, _ = await loop.create_connection(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 986, in create_connection
    infos = await self._ensure_resolved(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1365, in _ensure_resolved
    return await loop.getaddrinfo(host, port, family=family, type=type,
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 825, in getaddrinfo
    return await self.run_in_executor(
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Try again

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 174, in async_update
    self._disconnect()
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 133, in _disconnect
    self._client.disconnect()
  File "/usr/local/lib/python3.8/site-packages/mpd/asyncio.py", line 188, in disconnect
    if self.__wfile is not None:
AttributeError: 'MPDClient' object has no attribute '_MPDClient__wfile'

As setup, I use hassio running in a dedicated VM with ubuntu 18.04 as base with Docker 20.10.2.
MPD runs on a ubuntu 20.04 based host server. Home assistant runs on 2021.1.1

The issue is, like @Mariusthvdb mentioned, that mpd doesn't show up as instance any more.

@mweinelt
Copy link
Contributor

mweinelt commented Jan 11, 2021

You seem to be using a DNS name and something in your resolver setup failed, hence the

socket.gaierror: [Errno -3] Try again

Regarding the second issue, it might just be that we need to catch AttributeError, as the MPDClient object seems to behave oddly at that point.

Can you apply the following patch and see if things are working better with it?

diff --git a/homeassistant/components/mpd/media_player.py b/homeassistant/components/mpd/media_player.py
index 1273b720dd..49207fa24c 100644
--- a/homeassistant/components/mpd/media_player.py
+++ b/homeassistant/components/mpd/media_player.py
@@ -131,7 +131,7 @@ class MpdDevice(MediaPlayerEntity):
         """Disconnect from MPD."""
         try:
             self._client.disconnect()
-        except mpd.ConnectionError:
+        except (mpd.ConnectionError, AttributeError):
             pass
         self._is_connected = False
         self._status = None

Meanwhile I opened up an issue over at python-mpd2. I don't think we should need to catch AttributeErrors of the sort. Mic92/python-mpd2#152

@rishatik92
Copy link
Contributor

rishatik92 commented Jan 11, 2021

i have the same problem:

2021-01-11 11:16:02 ERROR (MainThread) [homeassistant.components.media_player] mpd: Error on device update!
Traceback (most recent call last):
  File "/home/pi/homeassistant/lib/python3.9/site-packages/homeassistant/components/mpd/media_player.py", line 167, in async_update
    await self._connect()
  File "/home/pi/homeassistant/lib/python3.9/site-packages/homeassistant/components/mpd/media_player.py", line 121, in _connect
    await self._client.connect(self.server, self.port)
  File "/home/pi/homeassistant/lib/python3.9/site-packages/mpd/asyncio.py", line 167, in connect
    r, w = await asyncio.open_connection(host, port, loop=loop)
  File "/usr/local/lib/python3.9/asyncio/streams.py", line 52, in open_connection
    transport, _ = await loop.create_connection(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1056, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1041, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 955, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 502, in sock_connect
    return await fut
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 537, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('127.0.0.1', 6600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity_platform.py", line 359, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/home/pi/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/home/pi/homeassistant/lib/python3.9/site-packages/homeassistant/components/mpd/media_player.py", line 174, in async_update
    self._disconnect()
  File "/home/pi/homeassistant/lib/python3.9/site-packages/homeassistant/components/mpd/media_player.py", line 133, in _disconnect
    self._client.disconnect()
  File "/home/pi/homeassistant/lib/python3.9/site-packages/mpd/asyncio.py", line 188, in disconnect
    if self.__wfile is not None:
AttributeError: 'MPDClient' object has no attribute '_MPDClient__wfile'

@mweinelt
Copy link
Contributor

Okay, here is what is going on.

So my best guess is that this error is only happening on home-assistant instances, where the initial connect to the mpd instance did not pan out. As we've seen above it could be ConnectionRefusedError (mpd not listening on it's socket), socket.gaierror (flaky DNS setup) or even OSError (No clue what's wrong in that case).

And for the more technically inclined:
The python-mpd2's async MPDClient seems to only initialize self.__wfile after a successful connection setup. When we call disconnect through async_update, because that is where we check if the connection is still alive, and the connection setup failed, there will be no self._client.__wfile and that results in the given AttributeError. What needs to be done is initialize them to None in the class constructor, so the condition will stop erroring out.

@jjok
Copy link

jjok commented Jan 11, 2021

MPD has stopped working for me too. Generally, the server (Mopidy in this case) is switched off. Previously when I switched it on Home Assistant would pick it up, but it no longer does that.
Restarting Home Assistant while the server is on causes the integration to start working again.

@deisi
Copy link
Contributor

deisi commented Jan 11, 2021

What needs to be done is initialize them to None in the class constructor, so the condition will stop erroring out.

Meaning this is currently an dependency issue or? @fabaff might want to add the label if so.

You seem to be using a DNS name and something in your resolver setup failed, hence the

Hm, I have the server in the /etc/hosts but whatever I switched to IP instead of hostname and now it seems to be working again.

@mweinelt

Do you still want me to test the little patch? Seems to me you have figured it out and since the change will happen upstream is it worth it? If you still would like me to test, I would test, else I can live happy with what I have and move on?

@mweinelt
Copy link
Contributor

A fix has been merged upstream, and now I'm waiting for a new release. No further testing is required AFAICT.

@noisemaker00
Copy link

I have a similar issue with 'mopidy' installed (from this repo: https://github.com/bestlibre/hassio-addons/tree/master/mopidy) locally but I cannot understand if is something related (if not I will open a separate issue).
I have an HassOS image in RPi 3
These are the logs

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 167, in async_update
    await self._connect()
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 121, in _connect
    await self._client.connect(self.server, self.port)
  File "/usr/local/lib/python3.8/site-packages/mpd/asyncio.py", line 167, in connect
    r, w = await asyncio.open_connection(host, port, loop=loop)
  File "/usr/local/lib/python3.8/asyncio/streams.py", line 52, in open_connection
    transport, _ = await loop.create_connection(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1025, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1010, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 924, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 496, in sock_connect
    return await fut
  File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 528, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('127.0.0.1', 6600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_add_entity
    await entity.async_device_update(warning=False)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 466, in async_device_update
    await task
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 174, in async_update
    self._disconnect()
  File "/usr/src/homeassistant/homeassistant/components/mpd/media_player.py", line 133, in _disconnect
    self._client.disconnect()
  File "/usr/local/lib/python3.8/site-packages/mpd/asyncio.py", line 188, in disconnect
    if self.__wfile is not None:
AttributeError: 'MPDClient' object has no attribute '_MPDClient__wfile'

@mweinelt
Copy link
Contributor

Yes, related. Will be fixed when python-mpd2 releases an update. The fix is in their main branch.

@antipiot
Copy link

Hello! I guess have the same problem. After the read of this post, im still not sure if the upcoming fix will cover my case:

My MPD server is not always on when i update / reboot Homassistant, wich lead for the entity to not being added at all and thus not available when the MPD server gets powered on.

MPD was indeed added trough Homassistant configuration.yml.
It's working when i reboot homassistant once my MPD server is powered ON

I'd like homeassistant to add the entity even if the MPD server is not available at boot in order for the integration to be available once the server is booted. (wich was the case before if i'm not mistaken)

Does this fix correct this aswell?

@mweinelt
Copy link
Contributor

Yes.

mweinelt added a commit to mweinelt/home-assistant that referenced this issue Jan 16, 2021
frenck pushed a commit that referenced this issue Jan 16, 2021
balloob pushed a commit that referenced this issue Jan 23, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Feb 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants