From dfe07f201f3c578e1666f91f8facf1e4a08e6b14 Mon Sep 17 00:00:00 2001 From: "Alan D. Tse" Date: Sat, 18 Jan 2020 10:22:53 -0800 Subject: [PATCH] fix: enable polling on update_devices changes This will propagate changes from the centralized polling to individual media players. Closes #529 --- custom_components/alexa_media/__init__.py | 5 ++- custom_components/alexa_media/media_player.py | 39 ++++++++++--------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/custom_components/alexa_media/__init__.py b/custom_components/alexa_media/__init__.py index 61bc401d..9818f59c 100644 --- a/custom_components/alexa_media/__init__.py +++ b/custom_components/alexa_media/__init__.py @@ -536,7 +536,10 @@ async def update_devices(login_obj): if serial not in existing_serials: new_alexa_clients.append(dev_name) - + elif serial in existing_entities: + await hass.data[DATA_ALEXAMEDIA]["accounts"][email]["entities"][ + "media_player" + ].get(serial).refresh(device, no_api=True) _LOGGER.debug( "%s: Existing: %s New: %s;" " Filtered out by not being in include: %s " diff --git a/custom_components/alexa_media/media_player.py b/custom_components/alexa_media/media_player.py index a3ab716a..acf7bf3a 100644 --- a/custom_components/alexa_media/media_player.py +++ b/custom_components/alexa_media/media_player.py @@ -395,7 +395,7 @@ async def _set_authentication_details(self, auth): @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS) @_catch_login_errors - async def refresh(self, device=None): + async def refresh(self, device=None, skip_api: bool = False): """Refresh device data. This is a per device refresh and for many Alexa devices can result in @@ -406,6 +406,7 @@ async def refresh(self, device=None): device (json): A refreshed device json from Amazon. For efficiency, an individual device does not refresh if it's reported as offline. + no_api (bool): Whether to only due a device json update and not hit the API """ if device is not None: @@ -429,23 +430,6 @@ async def refresh(self, device=None): session = None if self.available: _LOGGER.debug("%s: Refreshing %s", self.account, self.name) - if self._parent_clusters and self.hass: - playing_parents = list( - filter( - lambda x: ( - self.hass.data[DATA_ALEXAMEDIA]["accounts"][ - self._login.email - ]["entities"]["media_player"].get(x) - and self.hass.data[DATA_ALEXAMEDIA]["accounts"][ - self._login.email - ]["entities"]["media_player"][x].state - == STATE_PLAYING - ), - self._parent_clusters, - ) - ) - else: - playing_parents = [] if "PAIR_BT_SOURCE" in self._capabilities: self._source = await self._get_source() self._source_list = await self._get_source_list() @@ -454,7 +438,26 @@ async def refresh(self, device=None): self._last_called_timestamp = self.hass.data[DATA_ALEXAMEDIA][ "accounts" ][self._login.email]["last_called"]["timestamp"] + if skip_api: + return if "MUSIC_SKILL" in self._capabilities: + if self._parent_clusters and self.hass: + playing_parents = list( + filter( + lambda x: ( + self.hass.data[DATA_ALEXAMEDIA]["accounts"][ + self._login.email + ]["entities"]["media_player"].get(x) + and self.hass.data[DATA_ALEXAMEDIA]["accounts"][ + self._login.email + ]["entities"]["media_player"][x].state + == STATE_PLAYING + ), + self._parent_clusters, + ) + ) + else: + playing_parents = [] parent_session = {} if playing_parents: if len(playing_parents) > 1: