4
4
import os
5
5
6
6
import mpd
7
+ from mpd .asyncio import MPDClient
7
8
import voluptuous as vol
8
9
9
10
from homeassistant .components .media_player import PLATFORM_SCHEMA , MediaPlayerEntity
75
76
)
76
77
77
78
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 ):
79
80
"""Set up the MPD platform."""
80
81
host = config .get (CONF_HOST )
81
82
port = config .get (CONF_PORT )
82
83
name = config .get (CONF_NAME )
83
84
password = config .get (CONF_PASSWORD )
84
85
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 )
87
88
88
89
89
90
class MpdDevice (MediaPlayerEntity ):
@@ -108,17 +109,17 @@ def __init__(self, server, port, password, name):
108
109
self ._media_position = None
109
110
110
111
# set up MPD client
111
- self ._client = mpd . MPDClient ()
112
+ self ._client = MPDClient ()
112
113
self ._client .timeout = 30
113
114
self ._client .idletimeout = None
114
115
115
- def _connect (self ):
116
+ async def _connect (self ):
116
117
"""Connect to MPD."""
117
118
try :
118
- self ._client .connect (self .server , self .port )
119
+ await self ._client .connect (self .server , self .port )
119
120
120
121
if self .password is not None :
121
- self ._client .password (self .password )
122
+ await self ._client .password (self .password )
122
123
except mpd .ConnectionError :
123
124
return
124
125
@@ -133,10 +134,10 @@ def _disconnect(self):
133
134
self ._is_connected = False
134
135
self ._status = None
135
136
136
- def _fetch_status (self ):
137
+ async def _fetch_status (self ):
137
138
"""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 ()
140
141
141
142
position = self ._status .get ("elapsed" )
142
143
@@ -150,20 +151,20 @@ def _fetch_status(self):
150
151
self ._media_position_updated_at = dt_util .utcnow ()
151
152
self ._media_position = int (float (position ))
152
153
153
- self ._update_playlists ()
154
+ await self ._update_playlists ()
154
155
155
156
@property
156
157
def available (self ):
157
158
"""Return true if MPD is available and connected."""
158
159
return self ._is_connected
159
160
160
- def update (self ):
161
+ async def async_update (self ):
161
162
"""Get the latest data and update the state."""
162
163
try :
163
164
if not self ._is_connected :
164
- self ._connect ()
165
+ await self ._connect ()
165
166
166
- self ._fetch_status ()
167
+ await self ._fetch_status ()
167
168
except (mpd .ConnectionError , OSError , BrokenPipeError , ValueError ) as error :
168
169
# Cleanly disconnect in case connection is not in valid state
169
170
_LOGGER .debug ("Error updating status: %s" , error )
@@ -282,76 +283,76 @@ def source_list(self):
282
283
"""Return the list of available input sources."""
283
284
return self ._playlists
284
285
285
- def select_source (self , source ):
286
+ async def async_select_source (self , source ):
286
287
"""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 )
288
289
289
290
@Throttle (PLAYLIST_UPDATE_INTERVAL )
290
- def _update_playlists (self , ** kwargs ):
291
+ async def _update_playlists (self , ** kwargs ):
291
292
"""Update available MPD playlists."""
292
293
try :
293
294
self ._playlists = []
294
- for playlist_data in self ._client .listplaylists ():
295
+ for playlist_data in await self ._client .listplaylists ():
295
296
self ._playlists .append (playlist_data ["playlist" ])
296
297
except mpd .CommandError as error :
297
298
self ._playlists = None
298
299
_LOGGER .warning ("Playlists could not be updated: %s:" , error )
299
300
300
- def set_volume_level (self , volume ):
301
+ async def async_set_volume_level (self , volume ):
301
302
"""Set volume of media player."""
302
303
if "volume" in self ._status :
303
- self ._client .setvol (int (volume * 100 ))
304
+ await self ._client .setvol (int (volume * 100 ))
304
305
305
- def volume_up (self ):
306
+ async def async_volume_up (self ):
306
307
"""Service to send the MPD the command for volume up."""
307
308
if "volume" in self ._status :
308
309
current_volume = int (self ._status ["volume" ])
309
310
310
311
if current_volume <= 100 :
311
312
self ._client .setvol (current_volume + 5 )
312
313
313
- def volume_down (self ):
314
+ async def async_volume_down (self ):
314
315
"""Service to send the MPD the command for volume down."""
315
316
if "volume" in self ._status :
316
317
current_volume = int (self ._status ["volume" ])
317
318
318
319
if current_volume >= 0 :
319
- self ._client .setvol (current_volume - 5 )
320
+ await self ._client .setvol (current_volume - 5 )
320
321
321
- def media_play (self ):
322
+ async def async_media_play (self ):
322
323
"""Service to send the MPD the command for play/pause."""
323
324
if self ._status ["state" ] == "pause" :
324
- self ._client .pause (0 )
325
+ await self ._client .pause (0 )
325
326
else :
326
- self ._client .play ()
327
+ await self ._client .play ()
327
328
328
- def media_pause (self ):
329
+ async def async_media_pause (self ):
329
330
"""Service to send the MPD the command for play/pause."""
330
- self ._client .pause (1 )
331
+ await self ._client .pause (1 )
331
332
332
- def media_stop (self ):
333
+ async def async_media_stop (self ):
333
334
"""Service to send the MPD the command for stop."""
334
- self ._client .stop ()
335
+ await self ._client .stop ()
335
336
336
- def media_next_track (self ):
337
+ async def async_media_next_track (self ):
337
338
"""Service to send the MPD the command for next track."""
338
- self ._client .next ()
339
+ await self ._client .next ()
339
340
340
- def media_previous_track (self ):
341
+ async def async_media_previous_track (self ):
341
342
"""Service to send the MPD the command for previous track."""
342
- self ._client .previous ()
343
+ await self ._client .previous ()
343
344
344
- def mute_volume (self , mute ):
345
+ async def async_mute_volume (self , mute ):
345
346
"""Mute. Emulated with set_volume_level."""
346
347
if "volume" in self ._status :
347
348
if mute :
348
349
self ._muted_volume = self .volume_level
349
- self .set_volume_level (0 )
350
+ await self .async_set_volume_level (0 )
350
351
else :
351
- self .set_volume_level (self ._muted_volume )
352
+ await self .async_set_volume_level (self ._muted_volume )
352
353
self ._muted = mute
353
354
354
- def play_media (self , media_type , media_id , ** kwargs ):
355
+ async def async_play_media (self , media_type , media_id , ** kwargs ):
355
356
"""Send the media player the command for playing a playlist."""
356
357
_LOGGER .debug ("Playing playlist: %s" , media_id )
357
358
if media_type == MEDIA_TYPE_PLAYLIST :
@@ -360,14 +361,14 @@ def play_media(self, media_type, media_id, **kwargs):
360
361
else :
361
362
self ._currentplaylist = None
362
363
_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 ()
366
367
else :
367
- self ._client .clear ()
368
+ await self ._client .clear ()
368
369
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 ()
371
372
372
373
@property
373
374
def repeat (self ):
@@ -378,40 +379,40 @@ def repeat(self):
378
379
return REPEAT_MODE_ALL
379
380
return REPEAT_MODE_OFF
380
381
381
- def set_repeat (self , repeat ):
382
+ async def async_set_repeat (self , repeat ):
382
383
"""Set repeat mode."""
383
384
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 )
386
387
else :
387
- self ._client .repeat (1 )
388
+ await self ._client .repeat (1 )
388
389
if repeat == REPEAT_MODE_ONE :
389
- self ._client .single (1 )
390
+ await self ._client .single (1 )
390
391
else :
391
- self ._client .single (0 )
392
+ await self ._client .single (0 )
392
393
393
394
@property
394
395
def shuffle (self ):
395
396
"""Boolean if shuffle is enabled."""
396
397
return bool (int (self ._status ["random" ]))
397
398
398
- def set_shuffle (self , shuffle ):
399
+ async def async_set_shuffle (self , shuffle ):
399
400
"""Enable/disable shuffle mode."""
400
- self ._client .random (int (shuffle ))
401
+ await self ._client .random (int (shuffle ))
401
402
402
- def turn_off (self ):
403
+ async def async_turn_off (self ):
403
404
"""Service to send the MPD the command to stop playing."""
404
- self ._client .stop ()
405
+ await self ._client .stop ()
405
406
406
- def turn_on (self ):
407
+ async def async_turn_on (self ):
407
408
"""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 )
410
411
411
- def clear_playlist (self ):
412
+ async def async_clear_playlist (self ):
412
413
"""Clear players playlist."""
413
- self ._client .clear ()
414
+ await self ._client .clear ()
414
415
415
- def media_seek (self , position ):
416
+ async def async_media_seek (self , position ):
416
417
"""Send seek command."""
417
- self ._client .seekcur (position )
418
+ await self ._client .seekcur (position )
0 commit comments