Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore volume service - issue warnings instead of errors #2786

Merged
merged 14 commits into from
Jan 3, 2025
Merged
30 changes: 22 additions & 8 deletions custom_components/alexa_media/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ async def last_call_handler(self, call):
async def restore_volume(self, call) -> bool:
"""Handle restore volume service request.

Arguments
call.ATTR_ENTITY_ID {str: None} -- Alexa media player entity.
Arguments:
call.ATTR_ENTITY_ID {str: None} -- Alexa Media Player entity.

"""
entity_id = call.data.get(ATTR_ENTITY_ID)
Expand All @@ -154,15 +154,29 @@ async def restore_volume(self, call) -> bool:
_LOGGER.error("Entity %s not found in registry", entity_id)
return False

# Retrieve the previous volume from the entity's state attributes
# Retrieve the state and attributes
state = self.hass.states.get(entity_id)
if not state or "previous_volume" not in state.attributes:
_LOGGER.error(
"Previous volume attribute not found for entity %s", entity_id
)
if not state:
_LOGGER.warning("Entity %s has no state; cannot restore volume", entity_id)
return False

previous_volume = state.attributes["previous_volume"]
previous_volume = state.attributes.get("previous_volume")
current_volume = state.attributes.get("volume_level")

if previous_volume is None:
_LOGGER.warning(
"Previous volume not found for %s; attempting to use current volume level: %s",
entity_id,
current_volume,
)
previous_volume = current_volume

if previous_volume is None:
_LOGGER.warning(
"No valid volume levels found for entity %s; cannot restore volume",
entity_id,
)
return False

# Call the volume_set service with the retrieved volume
await self.hass.services.async_call(
Expand Down
Loading