core/homeassistant/components/mpd
Thijs Putman 360ef894a7
Use non-persistent connection for MPD (#94507)
* Do not use a persistent connection to MPD

In other words, don't rely on the connection management provided by
"python-mpd2". Instead of keeping the connection to MPD open, we
explicitly connect before and disconnect after each command.

There is probably a bit of overhead to this, but as the integration
uses a local-polling approach to begin with, no functionality is lost or
degraded.

This change greatly hardens the MPD integration against both network
issues and problems with the daemon itself. All connection-related
failure modes have effectively been removed.

* Update state retrieval methods

Only "async_get_media_image" attempts to connect, all others are either
called from there, or from the main "async_update" method (see previous
commit) which also attempts to connect.

So, this commit mainly revolves around gracefully handling situations
where no connection is available when trying to retrieve MPD state.

Finally, note the removal of "self._commands". This property is only
used at the start of "_async_get_file_image_response" and was thus
changed into a local variable.

* Update media-player control methods

These all need to explicitly connect to MPD as part of their flow.

* Correct ruff failure (auto-fixed)

* Use "async_timeout.timeout" context manager

* Minor changes

* Replace "async_timeout" with "asyncio.timeout"

* Initialise "self._status" to empty dictionary

Used to be initialised as None, which caused "NoneType is not iterable"
type of issues in case of an unexpected disconnect (at which point
status gets set to None again). Instead of guarding against None
everywhere, using an empty dictionary seemed more prudent...

Furthermore, more cautiously access its members to prevent potential
KeyError-s in similar cases.

* Fix livelock in "async_mute_volume()"

This method doesn't need a connection; it calls into two other methods
that actually connect to MPD – attempting to connect from here resulted
in a livelock.
2023-11-27 18:35:46 +01:00
..
__init__.py
manifest.json
media_player.py