From 7a0580eff591504af680781eb37d585ffa18b191 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Sun, 27 Apr 2025 15:36:42 +0200 Subject: [PATCH] Import media player constants at integration level for alexa smart home (#143767) --- homeassistant/components/alexa/entities.py | 9 +++----- homeassistant/components/alexa/handlers.py | 24 +++++++++++----------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/alexa/entities.py b/homeassistant/components/alexa/entities.py index 6a0b1830b7e..7088b624e21 100644 --- a/homeassistant/components/alexa/entities.py +++ b/homeassistant/components/alexa/entities.py @@ -719,7 +719,7 @@ class LockCapabilities(AlexaEntity): yield Alexa(self.entity) -@ENTITY_ADAPTERS.register(media_player.const.DOMAIN) +@ENTITY_ADAPTERS.register(media_player.DOMAIN) class MediaPlayerCapabilities(AlexaEntity): """Class to represent MediaPlayer capabilities.""" @@ -757,9 +757,7 @@ class MediaPlayerCapabilities(AlexaEntity): if supported & media_player.MediaPlayerEntityFeature.SELECT_SOURCE: inputs = AlexaInputController.get_valid_inputs( - self.entity.attributes.get( - media_player.const.ATTR_INPUT_SOURCE_LIST, [] - ) + self.entity.attributes.get(media_player.ATTR_INPUT_SOURCE_LIST, []) ) if len(inputs) > 0: yield AlexaInputController(self.entity) @@ -776,8 +774,7 @@ class MediaPlayerCapabilities(AlexaEntity): and domain != "denonavr" ): inputs = AlexaEqualizerController.get_valid_inputs( - self.entity.attributes.get(media_player.const.ATTR_SOUND_MODE_LIST) - or [] + self.entity.attributes.get(media_player.ATTR_SOUND_MODE_LIST) or [] ) if len(inputs) > 0: yield AlexaEqualizerController(self.entity) diff --git a/homeassistant/components/alexa/handlers.py b/homeassistant/components/alexa/handlers.py index bb4ea5cd526..747cbd85adb 100644 --- a/homeassistant/components/alexa/handlers.py +++ b/homeassistant/components/alexa/handlers.py @@ -566,7 +566,7 @@ async def async_api_set_volume( data: dict[str, Any] = { ATTR_ENTITY_ID: entity.entity_id, - media_player.const.ATTR_MEDIA_VOLUME_LEVEL: volume, + media_player.ATTR_MEDIA_VOLUME_LEVEL: volume, } await hass.services.async_call( @@ -589,7 +589,7 @@ async def async_api_select_input( # Attempt to map the ALL UPPERCASE payload name to a source. # Strips trailing 1 to match single input devices. - source_list = entity.attributes.get(media_player.const.ATTR_INPUT_SOURCE_LIST) or [] + source_list = entity.attributes.get(media_player.ATTR_INPUT_SOURCE_LIST) or [] for source in source_list: formatted_source = ( source.lower().replace("-", "").replace("_", "").replace(" ", "") @@ -611,7 +611,7 @@ async def async_api_select_input( data: dict[str, Any] = { ATTR_ENTITY_ID: entity.entity_id, - media_player.const.ATTR_INPUT_SOURCE: media_input, + media_player.ATTR_INPUT_SOURCE: media_input, } await hass.services.async_call( @@ -636,7 +636,7 @@ async def async_api_adjust_volume( volume_delta = int(directive.payload["volume"]) entity = directive.entity - current_level = entity.attributes[media_player.const.ATTR_MEDIA_VOLUME_LEVEL] + current_level = entity.attributes[media_player.ATTR_MEDIA_VOLUME_LEVEL] # read current state try: @@ -648,7 +648,7 @@ async def async_api_adjust_volume( data: dict[str, Any] = { ATTR_ENTITY_ID: entity.entity_id, - media_player.const.ATTR_MEDIA_VOLUME_LEVEL: volume, + media_player.ATTR_MEDIA_VOLUME_LEVEL: volume, } await hass.services.async_call( @@ -709,7 +709,7 @@ async def async_api_set_mute( entity = directive.entity data: dict[str, Any] = { ATTR_ENTITY_ID: entity.entity_id, - media_player.const.ATTR_MEDIA_VOLUME_MUTED: mute, + media_player.ATTR_MEDIA_VOLUME_MUTED: mute, } await hass.services.async_call( @@ -1708,13 +1708,13 @@ async def async_api_changechannel( data: dict[str, Any] = { ATTR_ENTITY_ID: entity.entity_id, - media_player.const.ATTR_MEDIA_CONTENT_ID: channel, - media_player.const.ATTR_MEDIA_CONTENT_TYPE: media_player.MediaType.CHANNEL, + media_player.ATTR_MEDIA_CONTENT_ID: channel, + media_player.ATTR_MEDIA_CONTENT_TYPE: (media_player.MediaType.CHANNEL), } await hass.services.async_call( entity.domain, - media_player.const.SERVICE_PLAY_MEDIA, + media_player.SERVICE_PLAY_MEDIA, data, blocking=False, context=context, @@ -1823,13 +1823,13 @@ async def async_api_set_eq_mode( context: ha.Context, ) -> AlexaResponse: """Process a SetMode request for EqualizerController.""" - mode = directive.payload["mode"] + mode: str = directive.payload["mode"] entity = directive.entity data: dict[str, Any] = {ATTR_ENTITY_ID: entity.entity_id} - sound_mode_list = entity.attributes.get(media_player.const.ATTR_SOUND_MODE_LIST) + sound_mode_list = entity.attributes.get(media_player.ATTR_SOUND_MODE_LIST) if sound_mode_list and mode.lower() in sound_mode_list: - data[media_player.const.ATTR_SOUND_MODE] = mode.lower() + data[media_player.ATTR_SOUND_MODE] = mode.lower() else: msg = f"failed to map sound mode {mode} to a mode on {entity.entity_id}" raise AlexaInvalidValueError(msg)