Skip to content

Commit

Permalink
feat: add new attribute: previous_volume (#2766)
Browse files Browse the repository at this point in the history
closes #2765
  • Loading branch information
danielbrunt57 authored Dec 22, 2024
1 parent bc8ffcd commit 330e046
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def __init__(self, device, login, second_account_index=0):
self._media_is_muted = None
self._media_vol_level = None
self._previous_volume = None
self._saved_volume = None
self._source = None
self._source_list = []
self._connected_bluetooth = None
Expand Down Expand Up @@ -1129,14 +1130,23 @@ async def async_set_volume_level(self, volume):
"""Set volume level, range 0..1."""
if not self.available:
return

# Save the current volume level before we change it
_LOGGER.debug("Saving previous volume level: %s", self.volume_level)
self._previous_volume = self.volume_level

# Change the volume level on the device
if self.hass:
self.hass.async_create_task(self.alexa_api.set_volume(volume))
else:
await self.alexa_api.set_volume(volume)
self._media_vol_level = volume

# Let http2push update the new volume level
if not (
self.hass.data[DATA_ALEXAMEDIA]["accounts"][self._login.email]["http2"]
):
# Otherwise we do it ourselves
await self.async_update()

@property
Expand Down Expand Up @@ -1164,19 +1174,19 @@ async def async_mute_volume(self, mute):

self._media_is_muted = mute
if mute:
self._previous_volume = self.volume_level
self._saved_volume = self.volume_level
if self.hass:
self.hass.async_create_task(self.alexa_api.set_volume(0))
else:
await self.alexa_api.set_volume(0)
else:
if self._previous_volume is not None:
if self._saved_volume is not None:
if self.hass:
self.hass.async_create_task(
self.alexa_api.set_volume(self._previous_volume)
self.alexa_api.set_volume(self._saved_volume)
)
else:
await self.alexa_api.set_volume(self._previous_volume)
await self.alexa_api.set_volume(self._saved_volume)
else:
if self.hass:
self.hass.async_create_task(self.alexa_api.set_volume(50))
Expand Down Expand Up @@ -1620,6 +1630,7 @@ def extra_state_attributes(self):
"last_called_summary": self._last_called_summary,
"connected_bluetooth": self._connected_bluetooth,
"bluetooth_list": self._bluetooth_list,
"previous_volume": self._previous_volume,
}
return attr

Expand Down

0 comments on commit 330e046

Please sign in to comment.