Skip to content

Commit

Permalink
docs: use warnings instead of errors (#2786)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
danielbrunt57 and pre-commit-ci[bot] authored Jan 3, 2025
1 parent c16bac9 commit 63c0acd
Showing 1 changed file with 22 additions and 8 deletions.
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

0 comments on commit 63c0acd

Please sign in to comment.