]> kaliko git repositories - mpd-sima.git/blobdiff - sima/plugins/core/uniq.py
Mainly use literal for list/dict and f-strings when possible
[mpd-sima.git] / sima / plugins / core / uniq.py
index b61afa616278d2aec52ebac5d348a3b80e4666e8..a46d7fc7e736f79b3fb5888d8bb2e8eb890c9c4b 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (c) 2014 Jack Kaliko <kaliko@azylum.org>
+# Copyright (c) 2014, 2020 kaliko <kaliko@azylum.org>
 #
 #  This file is part of sima
 #
 """
 
 # standard library import
-from os import  getpid
+from os import getpid
 from socket import getfqdn
 
 # third parties components
 
 # local import
+from ...mpdclient import PlayerError
 from ...lib.plugin import Plugin
 
 
@@ -40,20 +41,23 @@ class Uniq(Plugin):
 
     def __init__(self, daemon):
         Plugin.__init__(self, daemon)
-        self.chan = 'mpd_sima:{0}.{1}'.format(getfqdn(), getpid())
+        self.chan = None
         self.channels = []
-        self.uniq = True
+        self._registred = False
 
     def start(self):
         if not self.is_capable():
             self.log.warning('MPD does not provide client to client')
             return
         self.is_uniq()
-        self.sub_chan()
+        if not self._registred:
+            self.sub_chan()
 
     def is_capable(self):
-        if 'channels' in self.player.commands():
-            return True
+        # Groove Basin compatibility
+        # For some reason Groove Basin have channels command but no
+        # subscribe command‽
+        return {'channels', 'subscribe'}.issubset(set(self.player.commands()))
 
     def get_channels(self):
         return [chan for chan in self.player.channels() if
@@ -64,11 +68,15 @@ class Uniq(Plugin):
         if channels:
             self.log.warning('Another instance is queueing on this MPD host')
             self.log.warning(' '.join(channels))
-            self.uniq = False
 
     def sub_chan(self):
-        self.log.debug('Registering as {}'.format(self.chan))
-        self.player.subscribe(self.chan)
+        self.chan = f'mpd_sima:{getfqdn()}.{getpid()}'
+        self.log.debug('Registering as %s', self.chan)
+        try:
+            self.player.subscribe(self.chan)
+            self._registred = True
+        except PlayerError as err:
+            self.log.error('Failed to register: %s', err)
 
     def callback_need_track(self):
         if self.is_capable():