Small code style improvements to Alexa integration (#33660)
This commit is contained in:
parent
db72039b8f
commit
feed139ae7
5 changed files with 34 additions and 37 deletions
homeassistant/components/alexa
|
@ -734,12 +734,11 @@ class AlexaPlaybackController(AlexaCapability):
|
||||||
media_player.SUPPORT_STOP: "Stop",
|
media_player.SUPPORT_STOP: "Stop",
|
||||||
}
|
}
|
||||||
|
|
||||||
supported_operations = []
|
return [
|
||||||
for operation in operations:
|
value
|
||||||
if operation & supported_features:
|
for operation, value in operations.items()
|
||||||
supported_operations.append(operations[operation])
|
if operation & supported_features
|
||||||
|
]
|
||||||
return supported_operations
|
|
||||||
|
|
||||||
|
|
||||||
class AlexaInputController(AlexaCapability):
|
class AlexaInputController(AlexaCapability):
|
||||||
|
@ -759,9 +758,7 @@ class AlexaInputController(AlexaCapability):
|
||||||
source_list = self.entity.attributes.get(
|
source_list = self.entity.attributes.get(
|
||||||
media_player.ATTR_INPUT_SOURCE_LIST, []
|
media_player.ATTR_INPUT_SOURCE_LIST, []
|
||||||
)
|
)
|
||||||
input_list = AlexaInputController.get_valid_inputs(source_list)
|
return AlexaInputController.get_valid_inputs(source_list)
|
||||||
|
|
||||||
return input_list
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_valid_inputs(source_list):
|
def get_valid_inputs(source_list):
|
||||||
|
@ -1829,10 +1826,11 @@ class AlexaEqualizerController(AlexaCapability):
|
||||||
configurations = None
|
configurations = None
|
||||||
sound_mode_list = self.entity.attributes.get(media_player.ATTR_SOUND_MODE_LIST)
|
sound_mode_list = self.entity.attributes.get(media_player.ATTR_SOUND_MODE_LIST)
|
||||||
if sound_mode_list:
|
if sound_mode_list:
|
||||||
supported_sound_modes = []
|
supported_sound_modes = [
|
||||||
for sound_mode in sound_mode_list:
|
{"name": sound_mode.upper()}
|
||||||
if sound_mode.upper() in ("MOVIE", "MUSIC", "NIGHT", "SPORT", "TV"):
|
for sound_mode in sound_mode_list
|
||||||
supported_sound_modes.append({"name": sound_mode.upper()})
|
if sound_mode.upper() in ("MOVIE", "MUSIC", "NIGHT", "SPORT", "TV")
|
||||||
|
]
|
||||||
|
|
||||||
configurations = {"modes": {"supported": supported_sound_modes}}
|
configurations = {"modes": {"supported": supported_sound_modes}}
|
||||||
|
|
||||||
|
@ -1890,7 +1888,7 @@ class AlexaCameraStreamController(AlexaCapability):
|
||||||
|
|
||||||
def camera_stream_configurations(self):
|
def camera_stream_configurations(self):
|
||||||
"""Return cameraStreamConfigurations object."""
|
"""Return cameraStreamConfigurations object."""
|
||||||
camera_stream_configurations = [
|
return [
|
||||||
{
|
{
|
||||||
"protocols": ["HLS"],
|
"protocols": ["HLS"],
|
||||||
"resolutions": [{"width": 1280, "height": 720}],
|
"resolutions": [{"width": 1280, "height": 720}],
|
||||||
|
@ -1899,4 +1897,3 @@ class AlexaCameraStreamController(AlexaCapability):
|
||||||
"audioCodecs": ["AAC"],
|
"audioCodecs": ["AAC"],
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
return camera_stream_configurations
|
|
||||||
|
|
|
@ -268,8 +268,7 @@ class AlexaEntity:
|
||||||
if not interface.properties_proactively_reported():
|
if not interface.properties_proactively_reported():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for prop in interface.serialize_properties():
|
yield from interface.serialize_properties()
|
||||||
yield prop
|
|
||||||
|
|
||||||
def serialize_discovery(self):
|
def serialize_discovery(self):
|
||||||
"""Serialize the entity for discovery."""
|
"""Serialize the entity for discovery."""
|
||||||
|
@ -283,10 +282,12 @@ class AlexaEntity:
|
||||||
}
|
}
|
||||||
|
|
||||||
locale = self.config.locale
|
locale = self.config.locale
|
||||||
capabilities = []
|
capabilities = [
|
||||||
for i in self.interfaces():
|
i.serialize_discovery()
|
||||||
if locale in i.supported_locales:
|
for i in self.interfaces()
|
||||||
capabilities.append(i.serialize_discovery())
|
if locale in i.supported_locales
|
||||||
|
]
|
||||||
|
|
||||||
result["capabilities"] = capabilities
|
result["capabilities"] = capabilities
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -355,7 +355,6 @@ async def async_api_deactivate(hass, config, directive, context):
|
||||||
async def async_api_set_percentage(hass, config, directive, context):
|
async def async_api_set_percentage(hass, config, directive, context):
|
||||||
"""Process a set percentage request."""
|
"""Process a set percentage request."""
|
||||||
entity = directive.entity
|
entity = directive.entity
|
||||||
percentage = int(directive.payload["percentage"])
|
|
||||||
service = None
|
service = None
|
||||||
data = {ATTR_ENTITY_ID: entity.entity_id}
|
data = {ATTR_ENTITY_ID: entity.entity_id}
|
||||||
|
|
||||||
|
@ -363,6 +362,7 @@ async def async_api_set_percentage(hass, config, directive, context):
|
||||||
service = fan.SERVICE_SET_SPEED
|
service = fan.SERVICE_SET_SPEED
|
||||||
speed = "off"
|
speed = "off"
|
||||||
|
|
||||||
|
percentage = int(directive.payload["percentage"])
|
||||||
if percentage <= 33:
|
if percentage <= 33:
|
||||||
speed = "low"
|
speed = "low"
|
||||||
elif percentage <= 66:
|
elif percentage <= 66:
|
||||||
|
@ -568,7 +568,7 @@ async def async_api_adjust_volume_step(hass, config, directive, context):
|
||||||
|
|
||||||
data = {ATTR_ENTITY_ID: entity.entity_id}
|
data = {ATTR_ENTITY_ID: entity.entity_id}
|
||||||
|
|
||||||
for _ in range(0, abs(volume_int)):
|
for _ in range(abs(volume_int)):
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
entity.domain, service_volume, data, blocking=False, context=context
|
entity.domain, service_volume, data, blocking=False, context=context
|
||||||
)
|
)
|
||||||
|
@ -849,7 +849,6 @@ async def async_api_reportstate(hass, config, directive, context):
|
||||||
async def async_api_set_power_level(hass, config, directive, context):
|
async def async_api_set_power_level(hass, config, directive, context):
|
||||||
"""Process a SetPowerLevel request."""
|
"""Process a SetPowerLevel request."""
|
||||||
entity = directive.entity
|
entity = directive.entity
|
||||||
percentage = int(directive.payload["powerLevel"])
|
|
||||||
service = None
|
service = None
|
||||||
data = {ATTR_ENTITY_ID: entity.entity_id}
|
data = {ATTR_ENTITY_ID: entity.entity_id}
|
||||||
|
|
||||||
|
@ -857,6 +856,7 @@ async def async_api_set_power_level(hass, config, directive, context):
|
||||||
service = fan.SERVICE_SET_SPEED
|
service = fan.SERVICE_SET_SPEED
|
||||||
speed = "off"
|
speed = "off"
|
||||||
|
|
||||||
|
percentage = int(directive.payload["powerLevel"])
|
||||||
if percentage <= 33:
|
if percentage <= 33:
|
||||||
speed = "low"
|
speed = "low"
|
||||||
elif percentage <= 66:
|
elif percentage <= 66:
|
||||||
|
@ -920,10 +920,10 @@ async def async_api_arm(hass, config, directive, context):
|
||||||
|
|
||||||
if arm_state == "ARMED_AWAY":
|
if arm_state == "ARMED_AWAY":
|
||||||
service = SERVICE_ALARM_ARM_AWAY
|
service = SERVICE_ALARM_ARM_AWAY
|
||||||
if arm_state == "ARMED_STAY":
|
elif arm_state == "ARMED_NIGHT":
|
||||||
service = SERVICE_ALARM_ARM_HOME
|
|
||||||
if arm_state == "ARMED_NIGHT":
|
|
||||||
service = SERVICE_ALARM_ARM_NIGHT
|
service = SERVICE_ALARM_ARM_NIGHT
|
||||||
|
elif arm_state == "ARMED_STAY":
|
||||||
|
service = SERVICE_ALARM_ARM_HOME
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
entity.domain, service, data, blocking=False, context=context
|
entity.domain, service, data, blocking=False, context=context
|
||||||
|
@ -1383,7 +1383,7 @@ async def async_api_skipchannel(hass, config, directive, context):
|
||||||
else:
|
else:
|
||||||
service_media = SERVICE_MEDIA_NEXT_TRACK
|
service_media = SERVICE_MEDIA_NEXT_TRACK
|
||||||
|
|
||||||
for _ in range(0, abs(channel)):
|
for _ in range(abs(channel)):
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
entity.domain, service_media, data, blocking=False, context=context
|
entity.domain, service_media, data, blocking=False, context=context
|
||||||
)
|
)
|
||||||
|
|
|
@ -201,7 +201,7 @@ def resolve_slot_synonyms(key, request):
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Found multiple synonym resolutions for slot value: {%s: %s}",
|
"Found multiple synonym resolutions for slot value: {%s: %s}",
|
||||||
key,
|
key,
|
||||||
request["value"],
|
resolved_value,
|
||||||
)
|
)
|
||||||
|
|
||||||
return resolved_value
|
return resolved_value
|
||||||
|
|
|
@ -296,14 +296,13 @@ class AlexaPresetResource(AlexaCapabilityResource):
|
||||||
configuration["unitOfMeasure"] = self._unit_of_measure
|
configuration["unitOfMeasure"] = self._unit_of_measure
|
||||||
|
|
||||||
if self._presets:
|
if self._presets:
|
||||||
preset_resources = []
|
preset_resources = [
|
||||||
for preset in self._presets:
|
{
|
||||||
preset_resources.append(
|
"rangeValue": preset["value"],
|
||||||
{
|
"presetResources": self.serialize_labels(preset["labels"]),
|
||||||
"rangeValue": preset["value"],
|
}
|
||||||
"presetResources": self.serialize_labels(preset["labels"]),
|
for preset in self._presets
|
||||||
}
|
]
|
||||||
)
|
|
||||||
configuration["presets"] = preset_resources
|
configuration["presets"] = preset_resources
|
||||||
|
|
||||||
return configuration
|
return configuration
|
||||||
|
|
Loading…
Add table
Reference in a new issue