Skip to content

Commit

Permalink
style: auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 2, 2025
1 parent b08e00e commit 06dd3ce
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions custom_components/alexa_media/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
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
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import config_validation as cv, entity_registry as er
import voluptuous as vol

from .const import (
Expand All @@ -37,9 +36,7 @@
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 @@ -53,10 +50,7 @@ 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 @@ -73,10 +67,7 @@ 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 @@ -147,43 +138,46 @@ 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.
"""
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 state and attributes
state = self.hass.states.get(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')

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)
_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(
domain="media_player",
Expand All @@ -193,6 +187,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 06dd3ce

Please sign in to comment.