Skip to content

Commit

Permalink
fix: wrap calls to alexapi in async_create_task
Browse files Browse the repository at this point in the history
This allows all function calls to return despite the queue_delay in alexaapi. This will allow successive calls in the same HA script to trigger the queue building functionality.
closes #1118
  • Loading branch information
friggeri committed Feb 15, 2021
1 parent b924af0 commit 0445b72
Showing 1 changed file with 96 additions and 50 deletions.
146 changes: 96 additions & 50 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,16 @@ def source_list(self):
async def async_select_source(self, source):
"""Select input source."""
if source == "Local Speaker":
await self.alexa_api.disconnect_bluetooth()
self.hass.async_create_task(
self.alexa_api.disconnect_bluetooth()
)
self._source = "Local Speaker"
elif self._bluetooth_state.get("pairedDeviceList"):
for devices in self._bluetooth_state["pairedDeviceList"]:
if devices["friendlyName"] == source:
await self.alexa_api.set_bluetooth(devices["address"])
self.hass.async_create_task(
self.alexa_api.set_bluetooth(devices["address"])
)
self._source = source
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
Expand Down Expand Up @@ -1027,7 +1031,9 @@ def dnd_state(self, state):
@_catch_login_errors
async def async_set_shuffle(self, shuffle):
"""Enable/disable shuffle mode."""
await self.alexa_api.shuffle(shuffle)
self.hass.async_create_task(
self.alexa_api.shuffle(shuffle)
)
self._shuffle = shuffle

@property
Expand Down Expand Up @@ -1062,7 +1068,9 @@ async def async_set_volume_level(self, volume):
"""Set volume level, range 0..1."""
if not self.available:
return
await self.alexa_api.set_volume(volume)
self.hass.async_create_task(
self.alexa_api.set_volume(volume)
)
self._media_vol_level = volume
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
Expand Down Expand Up @@ -1095,12 +1103,18 @@ async def async_mute_volume(self, mute):
self._media_is_muted = mute
if mute:
self._previous_volume = self.volume_level
await self.alexa_api.set_volume(0)
self.hass.async_create_task(
self.alexa_api.set_volume(0)
)
else:
if self._previous_volume is not None:
await self.alexa_api.set_volume(self._previous_volume)
self.hass.async_create_task(
self.alexa_api.set_volume(self._previous_volume)
)
else:
await self.alexa_api.set_volume(50)
self.hass.async_create_task(
self.alexa_api.set_volume(50)
)
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
):
Expand All @@ -1114,7 +1128,9 @@ async def async_media_play(self):
if self._playing_parent:
await self._playing_parent.async_media_play()
else:
await self.alexa_api.play()
self.hass.async_create_task(
self.alexa_api.play()
)
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
):
Expand All @@ -1128,7 +1144,9 @@ async def async_media_pause(self):
if self._playing_parent:
await self._playing_parent.async_media_pause()
else:
await self.alexa_api.pause()
self.hass.async_create_task(
self.alexa_api.pause()
)
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
):
Expand All @@ -1142,11 +1160,13 @@ async def async_media_stop(self):
if self._playing_parent:
await self._playing_parent.async_media_stop()
else:
await self.alexa_api.stop(
customer_id=self._customer_id,
queue_delay=self.hass.data[DATA_ALEXAMEDIA]["accounts"][self.email][
"options"
][CONF_QUEUE_DELAY],
self.hass.async_create_task(
self.alexa_api.stop(
customer_id=self._customer_id,
queue_delay=self.hass.data[DATA_ALEXAMEDIA]["accounts"][self.email][
"options"
][CONF_QUEUE_DELAY],
)
)
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
Expand Down Expand Up @@ -1182,7 +1202,9 @@ async def async_media_next_track(self):
if self._playing_parent:
await self._playing_parent.async_media_next_track()
else:
await self.alexa_api.next()
self.hass.async_create_task(
self.alexa_api.next()
)
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
):
Expand All @@ -1196,7 +1218,9 @@ async def async_media_previous_track(self):
if self._playing_parent:
await self._playing_parent.async_media_previous_track()
else:
await self.alexa_api.previous()
self.hass.async_create_task(
self.alexa_api.previous()
)
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
):
Expand All @@ -1208,27 +1232,35 @@ async def async_send_tts(self, message, **kwargs):
NOTE: Does not work on WHA Groups.
"""
await self.alexa_api.send_tts(message, customer_id=self._customer_id, **kwargs)
self.hass.async_create_task(
self.alexa_api.send_tts(message, customer_id=self._customer_id, **kwargs)
)

@_catch_login_errors
async def async_send_announcement(self, message, **kwargs):
"""Send announcement to the media player."""
await self.alexa_api.send_announcement(
message, customer_id=self._customer_id, **kwargs
self.hass.async_create_task(
self.alexa_api.send_announcement(
message, customer_id=self._customer_id, **kwargs
)
)

@_catch_login_errors
async def async_send_mobilepush(self, message, **kwargs):
"""Send push to the media player's associated mobile devices."""
await self.alexa_api.send_mobilepush(
message, customer_id=self._customer_id, **kwargs
self.hass.async_create_task(
self.alexa_api.send_mobilepush(
message, customer_id=self._customer_id, **kwargs
)
)

@_catch_login_errors
async def async_send_dropin_notification(self, message, **kwargs):
"""Send notification dropin to the media player's associated mobile devices."""
await self.alexa_api.send_dropin_notification(
message, customer_id=self._customer_id, **kwargs
self.hass.async_create_task(
self.alexa_api.send_dropin_notification(
message, customer_id=self._customer_id, **kwargs
)
)

@_catch_login_errors
Expand Down Expand Up @@ -1258,11 +1290,13 @@ async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
media_id,
queue_delay,
)
await self.alexa_api.send_sequence(
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
**kwargs,
self.hass.async_create_task(
self.alexa_api.send_sequence(
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
**kwargs,
)
)
elif media_type == "routine":
_LOGGER.debug(
Expand All @@ -1272,8 +1306,10 @@ async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
media_id,
queue_delay,
)
await self.alexa_api.run_routine(
media_id, queue_delay=queue_delay,
self.hass.async_create_task(
self.alexa_api.run_routine(
media_id, queue_delay=queue_delay,
)
)
elif media_type == "sound":
_LOGGER.debug(
Expand All @@ -1283,11 +1319,13 @@ async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
media_id,
queue_delay,
)
await self.alexa_api.play_sound(
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
**kwargs,
self.hass.async_create_task(
self.alexa_api.play_sound(
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
**kwargs,
)
)
elif media_type == "skill":
_LOGGER.debug(
Expand All @@ -1297,8 +1335,10 @@ async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
media_id,
queue_delay,
)
await self.alexa_api.run_skill(
media_id, queue_delay=queue_delay,
self.hass.async_create_task(
self.alexa_api.run_skill(
media_id, queue_delay=queue_delay,
)
)
elif media_type == "image":
_LOGGER.debug(
Expand All @@ -1307,7 +1347,9 @@ async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
self,
media_id,
)
await self.alexa_api.set_background(media_id)
self.hass.async_create_task(
self.alexa_api.set_background(media_id)
)
elif media_type == "custom":
_LOGGER.debug(
'%s: %s:Running custom command: "%s" with queue_delay %s',
Expand All @@ -1316,11 +1358,13 @@ async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
media_id,
queue_delay,
)
await self.alexa_api.run_custom(
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
**kwargs,
self.hass.async_create_task(
self.alexa_api.run_custom(
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
**kwargs,
)
)
else:
_LOGGER.debug(
Expand All @@ -1331,13 +1375,15 @@ async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
media_type,
queue_delay,
)
await self.alexa_api.play_music(
media_type,
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
timer=kwargs.get("extra", {}).get("timer", None),
**kwargs,
self.hass.async_create_task(
self.alexa_api.play_music(
media_type,
media_id,
customer_id=self._customer_id,
queue_delay=queue_delay,
timer=kwargs.get("extra", {}).get("timer", None),
**kwargs,
)
)
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["websocket"]
Expand Down

0 comments on commit 0445b72

Please sign in to comment.