Skip to content

Commit

Permalink
Update services.py (see notes)
Browse files Browse the repository at this point in the history
Change all errors to warnings.
Attempt to use current_volume if previous_volume is None and issue warning.
Issue warning if previous_volume is still None.
  • Loading branch information
danielbrunt57 authored Jan 2, 2025
1 parent 374c1ea commit b08e00e
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions custom_components/alexa_media/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from alexapy import AlexaAPI, AlexapyLoginError, hide_email
from alexapy.errors import AlexapyConnectionError
from homeassistant.const import ATTR_DEVICE_ID, ATTR_ENTITY_ID
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import entity_registry as er
import voluptuous as vol

from .const import (
Expand All @@ -36,7 +37,9 @@
LAST_CALL_UPDATE_SCHEMA = vol.Schema(
{vol.Optional(ATTR_EMAIL, default=[]): vol.All(cv.ensure_list, [cv.string])}
)
RESTORE_VOLUME_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.entity_id})
RESTORE_VOLUME_SCHEMA = vol.Schema(
{vol.Required(ATTR_ENTITY_ID): cv.entity_id}
)


class AlexaMediaServices:
Expand All @@ -50,7 +53,10 @@ def __init__(self, hass, functions: dict[str, Callable]):
async def register(self):
"""Register services to hass."""
self.hass.services.async_register(
DOMAIN, SERVICE_FORCE_LOGOUT, self.force_logout, schema=FORCE_LOGOUT_SCHEMA
DOMAIN,
SERVICE_FORCE_LOGOUT,
self.force_logout,
schema=FORCE_LOGOUT_SCHEMA
)
self.hass.services.async_register(
DOMAIN,
Expand All @@ -67,7 +73,10 @@ async def register(self):

async def unregister(self):
"""Deregister services from hass."""
self.hass.services.async_remove(DOMAIN, SERVICE_FORCE_LOGOUT)
self.hass.services.async_remove(
DOMAIN,
SERVICE_FORCE_LOGOUT
)
self.hass.services.async_remove(
DOMAIN,
SERVICE_UPDATE_LAST_CALLED,
Expand Down Expand Up @@ -138,32 +147,43 @@ 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)
_LOGGER.debug("Service restore_volume called for: %s", entity_id)

# Retrieve the entity registry and entity entry
entity_registry = er.async_get(self.hass)
entity_entry = entity_registry.async_get(entity_id)

if not entity_entry:
_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.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

previous_volume = state.attributes["previous_volume"]


# Call the volume_set service with the retrieved volume
await self.hass.services.async_call(
domain="media_player",
Expand All @@ -173,6 +193,6 @@ async def restore_volume(self, call) -> bool:
},
target={"entity_id": entity_id},
)

_LOGGER.debug("Volume restored to %s for entity %s", previous_volume, entity_id)
return True

0 comments on commit b08e00e

Please sign in to comment.