Skip to content

Commit fa69daf

Browse files
authored
Convert mpd component to use the async MPDClient (home-assistant#44384)
Upgrades python-mpd2 to 3.0.1.
1 parent adf4eb0 commit fa69daf

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed

homeassistant/components/mpd/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"domain": "mpd",
33
"name": "Music Player Daemon (MPD)",
44
"documentation": "https://www.home-assistant.io/integrations/mpd",
5-
"requirements": ["python-mpd2==1.0.0"],
5+
"requirements": ["python-mpd2==3.0.1"],
66
"codeowners": ["@fabaff"]
77
}

homeassistant/components/mpd/media_player.py

+62-61
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55

66
import mpd
7+
from mpd.asyncio import MPDClient
78
import voluptuous as vol
89

910
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
@@ -75,15 +76,15 @@
7576
)
7677

7778

78-
def setup_platform(hass, config, add_entities, discovery_info=None):
79+
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
7980
"""Set up the MPD platform."""
8081
host = config.get(CONF_HOST)
8182
port = config.get(CONF_PORT)
8283
name = config.get(CONF_NAME)
8384
password = config.get(CONF_PASSWORD)
8485

85-
device = MpdDevice(host, port, password, name)
86-
add_entities([device], True)
86+
entity = MpdDevice(host, port, password, name)
87+
async_add_entities([entity], True)
8788

8889

8990
class MpdDevice(MediaPlayerEntity):
@@ -108,17 +109,17 @@ def __init__(self, server, port, password, name):
108109
self._media_position = None
109110

110111
# set up MPD client
111-
self._client = mpd.MPDClient()
112+
self._client = MPDClient()
112113
self._client.timeout = 30
113114
self._client.idletimeout = None
114115

115-
def _connect(self):
116+
async def _connect(self):
116117
"""Connect to MPD."""
117118
try:
118-
self._client.connect(self.server, self.port)
119+
await self._client.connect(self.server, self.port)
119120

120121
if self.password is not None:
121-
self._client.password(self.password)
122+
await self._client.password(self.password)
122123
except mpd.ConnectionError:
123124
return
124125

@@ -133,10 +134,10 @@ def _disconnect(self):
133134
self._is_connected = False
134135
self._status = None
135136

136-
def _fetch_status(self):
137+
async def _fetch_status(self):
137138
"""Fetch status from MPD."""
138-
self._status = self._client.status()
139-
self._currentsong = self._client.currentsong()
139+
self._status = await self._client.status()
140+
self._currentsong = await self._client.currentsong()
140141

141142
position = self._status.get("elapsed")
142143

@@ -150,20 +151,20 @@ def _fetch_status(self):
150151
self._media_position_updated_at = dt_util.utcnow()
151152
self._media_position = int(float(position))
152153

153-
self._update_playlists()
154+
await self._update_playlists()
154155

155156
@property
156157
def available(self):
157158
"""Return true if MPD is available and connected."""
158159
return self._is_connected
159160

160-
def update(self):
161+
async def async_update(self):
161162
"""Get the latest data and update the state."""
162163
try:
163164
if not self._is_connected:
164-
self._connect()
165+
await self._connect()
165166

166-
self._fetch_status()
167+
await self._fetch_status()
167168
except (mpd.ConnectionError, OSError, BrokenPipeError, ValueError) as error:
168169
# Cleanly disconnect in case connection is not in valid state
169170
_LOGGER.debug("Error updating status: %s", error)
@@ -282,76 +283,76 @@ def source_list(self):
282283
"""Return the list of available input sources."""
283284
return self._playlists
284285

285-
def select_source(self, source):
286+
async def async_select_source(self, source):
286287
"""Choose a different available playlist and play it."""
287-
self.play_media(MEDIA_TYPE_PLAYLIST, source)
288+
await self.async_play_media(MEDIA_TYPE_PLAYLIST, source)
288289

289290
@Throttle(PLAYLIST_UPDATE_INTERVAL)
290-
def _update_playlists(self, **kwargs):
291+
async def _update_playlists(self, **kwargs):
291292
"""Update available MPD playlists."""
292293
try:
293294
self._playlists = []
294-
for playlist_data in self._client.listplaylists():
295+
for playlist_data in await self._client.listplaylists():
295296
self._playlists.append(playlist_data["playlist"])
296297
except mpd.CommandError as error:
297298
self._playlists = None
298299
_LOGGER.warning("Playlists could not be updated: %s:", error)
299300

300-
def set_volume_level(self, volume):
301+
async def async_set_volume_level(self, volume):
301302
"""Set volume of media player."""
302303
if "volume" in self._status:
303-
self._client.setvol(int(volume * 100))
304+
await self._client.setvol(int(volume * 100))
304305

305-
def volume_up(self):
306+
async def async_volume_up(self):
306307
"""Service to send the MPD the command for volume up."""
307308
if "volume" in self._status:
308309
current_volume = int(self._status["volume"])
309310

310311
if current_volume <= 100:
311312
self._client.setvol(current_volume + 5)
312313

313-
def volume_down(self):
314+
async def async_volume_down(self):
314315
"""Service to send the MPD the command for volume down."""
315316
if "volume" in self._status:
316317
current_volume = int(self._status["volume"])
317318

318319
if current_volume >= 0:
319-
self._client.setvol(current_volume - 5)
320+
await self._client.setvol(current_volume - 5)
320321

321-
def media_play(self):
322+
async def async_media_play(self):
322323
"""Service to send the MPD the command for play/pause."""
323324
if self._status["state"] == "pause":
324-
self._client.pause(0)
325+
await self._client.pause(0)
325326
else:
326-
self._client.play()
327+
await self._client.play()
327328

328-
def media_pause(self):
329+
async def async_media_pause(self):
329330
"""Service to send the MPD the command for play/pause."""
330-
self._client.pause(1)
331+
await self._client.pause(1)
331332

332-
def media_stop(self):
333+
async def async_media_stop(self):
333334
"""Service to send the MPD the command for stop."""
334-
self._client.stop()
335+
await self._client.stop()
335336

336-
def media_next_track(self):
337+
async def async_media_next_track(self):
337338
"""Service to send the MPD the command for next track."""
338-
self._client.next()
339+
await self._client.next()
339340

340-
def media_previous_track(self):
341+
async def async_media_previous_track(self):
341342
"""Service to send the MPD the command for previous track."""
342-
self._client.previous()
343+
await self._client.previous()
343344

344-
def mute_volume(self, mute):
345+
async def async_mute_volume(self, mute):
345346
"""Mute. Emulated with set_volume_level."""
346347
if "volume" in self._status:
347348
if mute:
348349
self._muted_volume = self.volume_level
349-
self.set_volume_level(0)
350+
await self.async_set_volume_level(0)
350351
else:
351-
self.set_volume_level(self._muted_volume)
352+
await self.async_set_volume_level(self._muted_volume)
352353
self._muted = mute
353354

354-
def play_media(self, media_type, media_id, **kwargs):
355+
async def async_play_media(self, media_type, media_id, **kwargs):
355356
"""Send the media player the command for playing a playlist."""
356357
_LOGGER.debug("Playing playlist: %s", media_id)
357358
if media_type == MEDIA_TYPE_PLAYLIST:
@@ -360,14 +361,14 @@ def play_media(self, media_type, media_id, **kwargs):
360361
else:
361362
self._currentplaylist = None
362363
_LOGGER.warning("Unknown playlist name %s", media_id)
363-
self._client.clear()
364-
self._client.load(media_id)
365-
self._client.play()
364+
await self._client.clear()
365+
await self._client.load(media_id)
366+
await self._client.play()
366367
else:
367-
self._client.clear()
368+
await self._client.clear()
368369
self._currentplaylist = None
369-
self._client.add(media_id)
370-
self._client.play()
370+
await self._client.add(media_id)
371+
await self._client.play()
371372

372373
@property
373374
def repeat(self):
@@ -378,40 +379,40 @@ def repeat(self):
378379
return REPEAT_MODE_ALL
379380
return REPEAT_MODE_OFF
380381

381-
def set_repeat(self, repeat):
382+
async def async_set_repeat(self, repeat):
382383
"""Set repeat mode."""
383384
if repeat == REPEAT_MODE_OFF:
384-
self._client.repeat(0)
385-
self._client.single(0)
385+
await self._client.repeat(0)
386+
await self._client.single(0)
386387
else:
387-
self._client.repeat(1)
388+
await self._client.repeat(1)
388389
if repeat == REPEAT_MODE_ONE:
389-
self._client.single(1)
390+
await self._client.single(1)
390391
else:
391-
self._client.single(0)
392+
await self._client.single(0)
392393

393394
@property
394395
def shuffle(self):
395396
"""Boolean if shuffle is enabled."""
396397
return bool(int(self._status["random"]))
397398

398-
def set_shuffle(self, shuffle):
399+
async def async_set_shuffle(self, shuffle):
399400
"""Enable/disable shuffle mode."""
400-
self._client.random(int(shuffle))
401+
await self._client.random(int(shuffle))
401402

402-
def turn_off(self):
403+
async def async_turn_off(self):
403404
"""Service to send the MPD the command to stop playing."""
404-
self._client.stop()
405+
await self._client.stop()
405406

406-
def turn_on(self):
407+
async def async_turn_on(self):
407408
"""Service to send the MPD the command to start playing."""
408-
self._client.play()
409-
self._update_playlists(no_throttle=True)
409+
await self._client.play()
410+
await self._update_playlists(no_throttle=True)
410411

411-
def clear_playlist(self):
412+
async def async_clear_playlist(self):
412413
"""Clear players playlist."""
413-
self._client.clear()
414+
await self._client.clear()
414415

415-
def media_seek(self, position):
416+
async def async_media_seek(self, position):
416417
"""Send seek command."""
417-
self._client.seekcur(position)
418+
await self._client.seekcur(position)

requirements_all.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1786,7 +1786,7 @@ python-juicenet==1.0.1
17861786
python-miio==0.5.4
17871787

17881788
# homeassistant.components.mpd
1789-
python-mpd2==1.0.0
1789+
python-mpd2==3.0.1
17901790

17911791
# homeassistant.components.mystrom
17921792
python-mystrom==1.1.2

0 commit comments

Comments
 (0)