]> kaliko git repositories - python-musicpdaio.git/blobdiff - mpdaio/client.py
Add password (issue #4) and refactoring
[python-musicpdaio.git] / mpdaio / client.py
index 8a7e9af7eea5e471c6a31d004838d87f94c46932..48b37bdded25075def30db52f214d4d5db45c978 100644 (file)
@@ -10,8 +10,8 @@ from .connection import ConnectionPool, Connection
 from .exceptions import MPDConnectionError, MPDProtocolError, MPDCommandError
 from .utils import Range, escape
 
-from . import CONNECTION_MAX, CONNECTION_TIMEOUT
-from . import ERROR_PREFIX, SUCCESS, NEXT
+from .const import CONNECTION_MAX, CONNECTION_TIMEOUT
+from .const import ERROR_PREFIX, SUCCESS, NEXT
 
 log = logging.getLogger(__name__)
 
@@ -24,7 +24,7 @@ class MPDClient:
         #: host used with the current connection (:py:obj:`str`)
         self.host = host or self.server_discovery[0]
         #: password detected in :envvar:`MPD_HOST` environment variable (:py:obj:`str`)
-        self.password = password or self.server_discovery[2]
+        self.pwd = password or self.server_discovery[2]
         #: port used with the current connection (:py:obj:`int`, :py:obj:`str`)
         self.port = port or self.server_discovery[1]
         self.mpd_timeout = CONNECTION_TIMEOUT
@@ -78,7 +78,7 @@ class MPDClient:
 
     def __getattr__(self, attr):
         command = attr
-        wrapper = CmdHandler(self._pool, self.host, self.port, self.password, self.mpd_timeout)
+        wrapper = CmdHandler(self._pool, self.host, self.port, self.pwd, self.mpd_timeout)
         if command not in wrapper._commands:
             command = command.replace("_", " ")
             if command not in wrapper._commands:
@@ -116,7 +116,7 @@ class CmdHandler:
             "clearerror":         self._fetch_nothing,
             "currentsong":        self._fetch_object,
             "idle":               self._fetch_list,
-            # "noidle":             None,
+            "noidle":             self._fetch_nothing,
             "status":             self._fetch_object,
             "stats":              self._fetch_object,
             # Playback Option Commands
@@ -261,14 +261,27 @@ class CmdHandler:
         server, port = self.host
         self.command = command
         self.args = args or ''
-        self.connection = await self.pool.connect(server, port, timeout=self.timeout)
+        self.connection = await self.pool.connect(server, port, self.timeout)
         async with self.connection:
+            await self._init_connection()
             retval = self._commands[command]
             await self._write_command(command, args)
             if callable(retval):
                 return await retval()
             return retval
 
+    async def _init_connection(self):
+        """Init connection if needed"""
+        if not self.connection.version:
+            # TODO: move hello here instead of connection?
+            # Need to consume hello
+            pass
+        if self.password and not self.connection.auth:
+            # Need to send password
+            await self._write_command('password', [self.password])
+            await self._fetch_nothing()
+            self.connection.auth = True
+
     async def _write_line(self, line):
         self.connection.write(f"{line!s}\n".encode())
         await self.connection.drain()