Use EntityFeature enums in alexa (#69570)

This commit is contained in:
epenet 2022-04-07 14:46:50 +02:00 committed by GitHub
parent 1ec08d2fe0
commit 4f494a876e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 50 deletions

View File

@ -12,11 +12,9 @@ from homeassistant.components import (
timer, timer,
vacuum, vacuum,
) )
from homeassistant.components.alarm_control_panel import FORMAT_NUMBER from homeassistant.components.alarm_control_panel import (
from homeassistant.components.alarm_control_panel.const import ( FORMAT_NUMBER,
SUPPORT_ALARM_ARM_AWAY, AlarmControlPanelEntityFeature,
SUPPORT_ALARM_ARM_HOME,
SUPPORT_ALARM_ARM_NIGHT,
) )
import homeassistant.components.climate.const as climate import homeassistant.components.climate.const as climate
import homeassistant.components.media_player.const as media_player import homeassistant.components.media_player.const as media_player
@ -701,7 +699,7 @@ class AlexaSpeaker(AlexaCapability):
properties = [{"name": "volume"}] properties = [{"name": "volume"}]
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported & media_player.SUPPORT_VOLUME_MUTE: if supported & media_player.MediaPlayerEntityFeature.VOLUME_MUTE:
properties.append({"name": "muted"}) properties.append({"name": "muted"})
return properties return properties
@ -774,11 +772,11 @@ class AlexaPlaybackController(AlexaCapability):
supported_features = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported_features = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
operations = { operations = {
media_player.SUPPORT_NEXT_TRACK: "Next", media_player.MediaPlayerEntityFeature.NEXT_TRACK: "Next",
media_player.SUPPORT_PAUSE: "Pause", media_player.MediaPlayerEntityFeature.PAUSE: "Pause",
media_player.SUPPORT_PLAY: "Play", media_player.MediaPlayerEntityFeature.PLAY: "Play",
media_player.SUPPORT_PREVIOUS_TRACK: "Previous", media_player.MediaPlayerEntityFeature.PREVIOUS_TRACK: "Previous",
media_player.SUPPORT_STOP: "Stop", media_player.MediaPlayerEntityFeature.STOP: "Stop",
} }
return [ return [
@ -1023,9 +1021,9 @@ class AlexaThermostatController(AlexaCapability):
"""Return what properties this entity supports.""" """Return what properties this entity supports."""
properties = [{"name": "thermostatMode"}] properties = [{"name": "thermostatMode"}]
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported & climate.SUPPORT_TARGET_TEMPERATURE: if supported & climate.ClimateEntityFeature.TARGET_TEMPERATURE:
properties.append({"name": "targetSetpoint"}) properties.append({"name": "targetSetpoint"})
if supported & climate.SUPPORT_TARGET_TEMPERATURE_RANGE: if supported & climate.ClimateEntityFeature.TARGET_TEMPERATURE_RANGE:
properties.append({"name": "lowerSetpoint"}) properties.append({"name": "lowerSetpoint"})
properties.append({"name": "upperSetpoint"}) properties.append({"name": "upperSetpoint"})
return properties return properties
@ -1219,11 +1217,11 @@ class AlexaSecurityPanelController(AlexaCapability):
configuration = {} configuration = {}
supported_arm_states = [{"value": "DISARMED"}] supported_arm_states = [{"value": "DISARMED"}]
if supported & SUPPORT_ALARM_ARM_AWAY: if supported & AlarmControlPanelEntityFeature.ARM_AWAY:
supported_arm_states.append({"value": "ARMED_AWAY"}) supported_arm_states.append({"value": "ARMED_AWAY"})
if supported & SUPPORT_ALARM_ARM_HOME: if supported & AlarmControlPanelEntityFeature.ARM_HOME:
supported_arm_states.append({"value": "ARMED_STAY"}) supported_arm_states.append({"value": "ARMED_STAY"})
if supported & SUPPORT_ALARM_ARM_NIGHT: if supported & AlarmControlPanelEntityFeature.ARM_NIGHT:
supported_arm_states.append({"value": "ARMED_NIGHT"}) supported_arm_states.append({"value": "ARMED_NIGHT"})
configuration["supportedArmStates"] = supported_arm_states configuration["supportedArmStates"] = supported_arm_states
@ -1392,7 +1390,7 @@ class AlexaModeController(AlexaCapability):
self._semantics = AlexaSemantics() self._semantics = AlexaSemantics()
# Add open/close semantics if tilt is not supported. # Add open/close semantics if tilt is not supported.
if not supported & cover.SUPPORT_SET_TILT_POSITION: if not supported & cover.CoverEntityFeature.SET_TILT_POSITION:
lower_labels.append(AlexaSemantics.ACTION_CLOSE) lower_labels.append(AlexaSemantics.ACTION_CLOSE)
raise_labels.append(AlexaSemantics.ACTION_OPEN) raise_labels.append(AlexaSemantics.ACTION_OPEN)
self._semantics.add_states_to_value( self._semantics.add_states_to_value(
@ -1494,7 +1492,7 @@ class AlexaRangeController(AlexaCapability):
# Fan speed percentage # Fan speed percentage
if self.instance == f"{fan.DOMAIN}.{fan.ATTR_PERCENTAGE}": if self.instance == f"{fan.DOMAIN}.{fan.ATTR_PERCENTAGE}":
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported and fan.SUPPORT_SET_SPEED: if supported and fan.FanEntityFeature.SET_SPEED:
return self.entity.attributes.get(fan.ATTR_PERCENTAGE) return self.entity.attributes.get(fan.ATTR_PERCENTAGE)
return 100 if self.entity.state == fan.STATE_ON else 0 return 100 if self.entity.state == fan.STATE_ON else 0
@ -1615,7 +1613,7 @@ class AlexaRangeController(AlexaCapability):
self._semantics = AlexaSemantics() self._semantics = AlexaSemantics()
# Add open/close semantics if tilt is not supported. # Add open/close semantics if tilt is not supported.
if not supported & cover.SUPPORT_SET_TILT_POSITION: if not supported & cover.CoverEntityFeature.SET_TILT_POSITION:
lower_labels.append(AlexaSemantics.ACTION_CLOSE) lower_labels.append(AlexaSemantics.ACTION_CLOSE)
raise_labels.append(AlexaSemantics.ACTION_OPEN) raise_labels.append(AlexaSemantics.ACTION_OPEN)
self._semantics.add_states_to_value( self._semantics.add_states_to_value(

View File

@ -501,15 +501,17 @@ class CoverCapabilities(AlexaEntity):
yield AlexaPowerController(self.entity) yield AlexaPowerController(self.entity)
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported & cover.SUPPORT_SET_POSITION: if supported & cover.CoverEntityFeature.SET_POSITION:
yield AlexaRangeController( yield AlexaRangeController(
self.entity, instance=f"{cover.DOMAIN}.{cover.ATTR_POSITION}" self.entity, instance=f"{cover.DOMAIN}.{cover.ATTR_POSITION}"
) )
elif supported & (cover.SUPPORT_CLOSE | cover.SUPPORT_OPEN): elif supported & (
cover.CoverEntityFeature.CLOSE | cover.CoverEntityFeature.OPEN
):
yield AlexaModeController( yield AlexaModeController(
self.entity, instance=f"{cover.DOMAIN}.{cover.ATTR_POSITION}" self.entity, instance=f"{cover.DOMAIN}.{cover.ATTR_POSITION}"
) )
if supported & cover.SUPPORT_SET_TILT_POSITION: if supported & cover.CoverEntityFeature.SET_TILT_POSITION:
yield AlexaRangeController(self.entity, instance=f"{cover.DOMAIN}.tilt") yield AlexaRangeController(self.entity, instance=f"{cover.DOMAIN}.tilt")
yield AlexaEndpointHealth(self.hass, self.entity) yield AlexaEndpointHealth(self.hass, self.entity)
yield Alexa(self.hass) yield Alexa(self.hass)
@ -552,17 +554,17 @@ class FanCapabilities(AlexaEntity):
yield AlexaPowerController(self.entity) yield AlexaPowerController(self.entity)
force_range_controller = True force_range_controller = True
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported & fan.SUPPORT_OSCILLATE: if supported & fan.FanEntityFeature.OSCILLATE:
yield AlexaToggleController( yield AlexaToggleController(
self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_OSCILLATING}" self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_OSCILLATING}"
) )
force_range_controller = False force_range_controller = False
if supported & fan.SUPPORT_PRESET_MODE: if supported & fan.FanEntityFeature.PRESET_MODE:
yield AlexaModeController( yield AlexaModeController(
self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_PRESET_MODE}" self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_PRESET_MODE}"
) )
force_range_controller = False force_range_controller = False
if supported & fan.SUPPORT_DIRECTION: if supported & fan.FanEntityFeature.DIRECTION:
yield AlexaModeController( yield AlexaModeController(
self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_DIRECTION}" self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_DIRECTION}"
) )
@ -572,7 +574,7 @@ class FanCapabilities(AlexaEntity):
# For fans which only support on/off, no controller is added. This makes the # For fans which only support on/off, no controller is added. This makes the
# fan impossible to turn on or off through Alexa, most likely due to a bug in Alexa. # fan impossible to turn on or off through Alexa, most likely due to a bug in Alexa.
# As a workaround, we add a range controller which can only be set to 0% or 100%. # As a workaround, we add a range controller which can only be set to 0% or 100%.
if force_range_controller or supported & fan.SUPPORT_SET_SPEED: if force_range_controller or supported & fan.FanEntityFeature.SET_SPEED:
yield AlexaRangeController( yield AlexaRangeController(
self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_PERCENTAGE}" self.entity, instance=f"{fan.DOMAIN}.{fan.ATTR_PERCENTAGE}"
) )
@ -615,26 +617,26 @@ class MediaPlayerCapabilities(AlexaEntity):
yield AlexaPowerController(self.entity) yield AlexaPowerController(self.entity)
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported & media_player.const.SUPPORT_VOLUME_SET: if supported & media_player.MediaPlayerEntityFeature.VOLUME_SET:
yield AlexaSpeaker(self.entity) yield AlexaSpeaker(self.entity)
elif supported & media_player.const.SUPPORT_VOLUME_STEP: elif supported & media_player.MediaPlayerEntityFeature.VOLUME_STEP:
yield AlexaStepSpeaker(self.entity) yield AlexaStepSpeaker(self.entity)
playback_features = ( playback_features = (
media_player.const.SUPPORT_PLAY media_player.MediaPlayerEntityFeature.PLAY
| media_player.const.SUPPORT_PAUSE | media_player.MediaPlayerEntityFeature.PAUSE
| media_player.const.SUPPORT_STOP | media_player.MediaPlayerEntityFeature.STOP
| media_player.const.SUPPORT_NEXT_TRACK | media_player.MediaPlayerEntityFeature.NEXT_TRACK
| media_player.const.SUPPORT_PREVIOUS_TRACK | media_player.MediaPlayerEntityFeature.PREVIOUS_TRACK
) )
if supported & playback_features: if supported & playback_features:
yield AlexaPlaybackController(self.entity) yield AlexaPlaybackController(self.entity)
yield AlexaPlaybackStateReporter(self.entity) yield AlexaPlaybackStateReporter(self.entity)
if supported & media_player.const.SUPPORT_SEEK: if supported & media_player.MediaPlayerEntityFeature.SEEK:
yield AlexaSeekController(self.entity) yield AlexaSeekController(self.entity)
if supported & media_player.SUPPORT_SELECT_SOURCE: if supported & media_player.MediaPlayerEntityFeature.SELECT_SOURCE:
inputs = AlexaInputController.get_valid_inputs( inputs = AlexaInputController.get_valid_inputs(
self.entity.attributes.get( self.entity.attributes.get(
media_player.const.ATTR_INPUT_SOURCE_LIST, [] media_player.const.ATTR_INPUT_SOURCE_LIST, []
@ -643,14 +645,14 @@ class MediaPlayerCapabilities(AlexaEntity):
if len(inputs) > 0: if len(inputs) > 0:
yield AlexaInputController(self.entity) yield AlexaInputController(self.entity)
if supported & media_player.const.SUPPORT_PLAY_MEDIA: if supported & media_player.MediaPlayerEntityFeature.PLAY_MEDIA:
yield AlexaChannelController(self.entity) yield AlexaChannelController(self.entity)
# AlexaEqualizerController is disabled for denonavr # AlexaEqualizerController is disabled for denonavr
# since it blocks alexa from discovering any devices. # since it blocks alexa from discovering any devices.
domain = entity_sources(self.hass).get(self.entity_id, {}).get("domain") domain = entity_sources(self.hass).get(self.entity_id, {}).get("domain")
if ( if (
supported & media_player.const.SUPPORT_SELECT_SOUND_MODE supported & media_player.MediaPlayerEntityFeature.SELECT_SOUND_MODE
and domain != "denonavr" and domain != "denonavr"
): ):
inputs = AlexaEqualizerController.get_valid_inputs( inputs = AlexaEqualizerController.get_valid_inputs(
@ -861,20 +863,21 @@ class VacuumCapabilities(AlexaEntity):
"""Yield the supported interfaces.""" """Yield the supported interfaces."""
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if ( if (
(supported & vacuum.SUPPORT_TURN_ON) or (supported & vacuum.SUPPORT_START) (supported & vacuum.VacuumEntityFeature.TURN_ON)
or (supported & vacuum.VacuumEntityFeature.START)
) and ( ) and (
(supported & vacuum.SUPPORT_TURN_OFF) (supported & vacuum.VacuumEntityFeature.TURN_OFF)
or (supported & vacuum.SUPPORT_RETURN_HOME) or (supported & vacuum.VacuumEntityFeature.RETURN_HOME)
): ):
yield AlexaPowerController(self.entity) yield AlexaPowerController(self.entity)
if supported & vacuum.SUPPORT_FAN_SPEED: if supported & vacuum.VacuumEntityFeature.FAN_SPEED:
yield AlexaRangeController( yield AlexaRangeController(
self.entity, instance=f"{vacuum.DOMAIN}.{vacuum.ATTR_FAN_SPEED}" self.entity, instance=f"{vacuum.DOMAIN}.{vacuum.ATTR_FAN_SPEED}"
) )
if supported & vacuum.SUPPORT_PAUSE: if supported & vacuum.VacuumEntityFeature.PAUSE:
support_resume = bool(supported & vacuum.SUPPORT_START) support_resume = bool(supported & vacuum.VacuumEntityFeature.START)
yield AlexaTimeHoldController( yield AlexaTimeHoldController(
self.entity, allow_remote_resume=support_resume self.entity, allow_remote_resume=support_resume
) )
@ -895,7 +898,7 @@ class CameraCapabilities(AlexaEntity):
"""Yield the supported interfaces.""" """Yield the supported interfaces."""
if self._check_requirements(): if self._check_requirements():
supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported & camera.SUPPORT_STREAM: if supported & camera.CameraEntityFeature.STREAM:
yield AlexaCameraStreamController(self.entity) yield AlexaCameraStreamController(self.entity)
yield AlexaEndpointHealth(self.hass, self.entity) yield AlexaEndpointHealth(self.hass, self.entity)

View File

@ -129,13 +129,19 @@ async def async_api_turn_on(hass, config, directive, context):
service = fan.SERVICE_TURN_ON service = fan.SERVICE_TURN_ON
elif domain == vacuum.DOMAIN: elif domain == vacuum.DOMAIN:
supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if not supported & vacuum.SUPPORT_TURN_ON and supported & vacuum.SUPPORT_START: if (
not supported & vacuum.VacuumEntityFeature.TURN_ON
and supported & vacuum.VacuumEntityFeature.START
):
service = vacuum.SERVICE_START service = vacuum.SERVICE_START
elif domain == timer.DOMAIN: elif domain == timer.DOMAIN:
service = timer.SERVICE_START service = timer.SERVICE_START
elif domain == media_player.DOMAIN: elif domain == media_player.DOMAIN:
supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
power_features = media_player.SUPPORT_TURN_ON | media_player.SUPPORT_TURN_OFF power_features = (
media_player.MediaPlayerEntityFeature.TURN_ON
| media_player.MediaPlayerEntityFeature.TURN_OFF
)
if not supported & power_features: if not supported & power_features:
service = media_player.SERVICE_MEDIA_PLAY service = media_player.SERVICE_MEDIA_PLAY
@ -166,15 +172,18 @@ async def async_api_turn_off(hass, config, directive, context):
elif domain == vacuum.DOMAIN: elif domain == vacuum.DOMAIN:
supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if ( if (
not supported & vacuum.SUPPORT_TURN_OFF not supported & vacuum.VacuumEntityFeature.TURN_OFF
and supported & vacuum.SUPPORT_RETURN_HOME and supported & vacuum.VacuumEntityFeature.RETURN_HOME
): ):
service = vacuum.SERVICE_RETURN_TO_BASE service = vacuum.SERVICE_RETURN_TO_BASE
elif domain == timer.DOMAIN: elif domain == timer.DOMAIN:
service = timer.SERVICE_CANCEL service = timer.SERVICE_CANCEL
elif domain == media_player.DOMAIN: elif domain == media_player.DOMAIN:
supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
power_features = media_player.SUPPORT_TURN_ON | media_player.SUPPORT_TURN_OFF power_features = (
media_player.MediaPlayerEntityFeature.TURN_ON
| media_player.MediaPlayerEntityFeature.TURN_OFF
)
if not supported & power_features: if not supported & power_features:
service = media_player.SERVICE_MEDIA_STOP service = media_player.SERVICE_MEDIA_STOP
@ -1087,7 +1096,7 @@ async def async_api_set_range(hass, config, directive, context):
service = fan.SERVICE_TURN_OFF service = fan.SERVICE_TURN_OFF
else: else:
supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) supported = entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
if supported and fan.SUPPORT_SET_SPEED: if supported and fan.FanEntityFeature.SET_SPEED:
service = fan.SERVICE_SET_PERCENTAGE service = fan.SERVICE_SET_PERCENTAGE
data[fan.ATTR_PERCENTAGE] = range_value data[fan.ATTR_PERCENTAGE] = range_value
else: else: