]> kaliko git repositories - mpd-sima.git/blobdiff - sima/plugins/core/uniq.py
Fixed code smell
[mpd-sima.git] / sima / plugins / core / uniq.py
index 969af6cf4c0d13d871469c19669b2bcfd020885e..60d1680ed174862817bdf830831ab9462d448eb1 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (c) 2013, 2014 Jack Kaliko <kaliko@azylum.org>
+# Copyright (c) 2014, 2020 kaliko <kaliko@azylum.org>
 #
 #  This file is part of sima
 #
@@ -18,7 +18,9 @@
 #
 #
 """
-    Deal with MPD options ‑ idle and repeat mode
+    Publish presence on the MPD host message bus
+
+    Notifies when concurrent instance run on the same host.
 """
 
 # standard library import
@@ -28,6 +30,7 @@ from socket import getfqdn
 # third parties components
 
 # local import
+from ...mpdclient import PlayerError
 from ...lib.plugin import Plugin
 
 
@@ -38,21 +41,23 @@ class Uniq(Plugin):
 
     def __init__(self, daemon):
         Plugin.__init__(self, daemon)
-        self.capable = False
         self.chan = 'mpd_sima:{0}.{1}'.format(getfqdn(), getpid())
         self.channels = []
-        self.uniq = True
-        self.is_capable()
-        if not self.capable:
+        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():
-            self.capable = True
-            return
-        self.log.warning('MPD does not provide client to client')
+        # 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
@@ -63,14 +68,17 @@ 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.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.capable:
+        if self.is_capable():
             self.is_uniq()