From 5e3102b2d6c3aece0009bfbb0894ccb7b478bff5 Mon Sep 17 00:00:00 2001
From: Niall Donegan <github@nialldonegan.me>
Date: Wed, 25 Dec 2019 10:09:03 +0000
Subject: [PATCH] Pull track position from MPD status (#28579)

* Pull track position from MPD status()

This allows the progress bar to work when using the media-control card in lovelace.

* Actually commit flake8 fix!

* Extra documentation.

Mainly to trigger CI rerun.

* Updated to use self._media_position
---
 homeassistant/components/mpd/media_player.py | 22 ++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/homeassistant/components/mpd/media_player.py b/homeassistant/components/mpd/media_player.py
index 2628815727c..6460becbb3e 100644
--- a/homeassistant/components/mpd/media_player.py
+++ b/homeassistant/components/mpd/media_player.py
@@ -37,6 +37,7 @@ from homeassistant.const import (
 )
 import homeassistant.helpers.config_validation as cv
 from homeassistant.util import Throttle
+import homeassistant.util.dt as dt_util
 
 _LOGGER = logging.getLogger(__name__)
 
@@ -98,6 +99,8 @@ class MpdDevice(MediaPlayerDevice):
         self._is_connected = False
         self._muted = False
         self._muted_volume = 0
+        self._media_position_updated_at = None
+        self._media_position = None
 
         # set up MPD client
         self._client = mpd.MPDClient()
@@ -130,6 +133,11 @@ class MpdDevice(MediaPlayerDevice):
         self._status = self._client.status()
         self._currentsong = self._client.currentsong()
 
+        position = self._status["time"]
+        if self._media_position != position:
+            self._media_position_updated_at = dt_util.utcnow()
+            self._media_position = position
+
         self._update_playlists()
 
     @property
@@ -188,6 +196,20 @@ class MpdDevice(MediaPlayerDevice):
         # Time does not exist for streams
         return self._currentsong.get("time")
 
+    @property
+    def media_position(self):
+        """Position of current playing media in seconds.
+
+        This is returned as part of the mpd status rather than in the details
+        of the current song.
+        """
+        return self._media_position
+
+    @property
+    def media_position_updated_at(self):
+        """Last valid time of media position."""
+        return self._media_position_updated_at
+
     @property
     def media_title(self):
         """Return the title of current playing media."""
-- 
GitLab