Skip to content

Commit

Permalink
fix: enable polling on update_devices changes
Browse files Browse the repository at this point in the history
This will propagate changes from the centralized polling to individual
media players.
Closes #529
  • Loading branch information
alandtse committed Jan 18, 2020
1 parent bc1c0c9 commit dfe07f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
5 changes: 4 additions & 1 deletion custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
39 changes: 21 additions & 18 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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:
Expand Down

0 comments on commit dfe07f2

Please sign in to comment.