@jkbrowne wrote:
Hey guys,
Just some quick background. Among other things, I’m using HA as a hub for all of my AV equipment. I have my infrared and network commands set up to control all of the equipment via a combination of itach and shell_command configuration settings. And I built a stand-alone web UI on top of the HA API so I have a nice responsive interface for it all. All of that is working fine.
I wanted to take it to the next level and add Alexa control so I could use voice commands to do things like turn on the TVs, adjust volume, etc. I set up the Alexa Smart home skill and lambda (on my own, not via Nabu Casa) and was able to make some the commands work by configuring them and exposing them to Alexa as switches. I realized the volume control would need to use the StepSpeaker interface so I decided to build my own custom component for each TV as a media_player component which would allow for the power and volume commands. I have it mostly working but I noticed that when I say “Alexa, volume up 20 on the Kitchen TV” it only adjusts the volume a-few steps, not 20 like I asked. I dug into the code for the Alexa Smart Home skill and, if I’m reading this correctly, it appears it just does a loop for each “step” and asynchronously calls the volume up/down method on the media_player device. This essentially is sending the volume step commands way too quickly to be processed by the itach device so only some of them get interpreted. This explains the behavior I’m seeing and I confirmed it via logging. I would imagine this would be a problem for other devices besides itach also. Has anyone run into this? Seems like either 1) there needs to be a delay option for the Alexa Smart Home StepSpeaker implementation so some processing time can be inserted between the volume steps. Or 2), the step value (integer) that was specified in your voice command should be passed as data into the media_player implementation so it can be handled appropriately. A basic loop to hammer the device X number of times doesn’t seem like a good solution for the volume commands, not in my opinion anyway. Code for the handlers.py file in the Alexa Smart Home interface is below, notice the for loop. Feedback would be welcome.
@HANDLERS.register(("Alexa.StepSpeaker", "AdjustVolume")) async def async_api_adjust_volume_step(hass, config, directive, context): """Process an adjust volume step request.""" # media_player volume up/down service does not support specifying steps # each component handles it differently e.g. via config. # This workaround will simply call the volume up/Volume down the amount of steps asked for # When no steps are called in the request, Alexa sends a default of 10 steps which for most # purposes is too high. The default is set 1 in this case. entity = directive.entity volume_int = int(directive.payload["volumeSteps"]) is_default = bool(directive.payload["volumeStepsDefault"]) default_steps = 1 if volume_int < 0: service_volume = SERVICE_VOLUME_DOWN if is_default: volume_int = -default_steps else: service_volume = SERVICE_VOLUME_UP if is_default: volume_int = default_steps data = {ATTR_ENTITY_ID: entity.entity_id} for _ in range(0, abs(volume_int)): await hass.services.async_call( entity.domain, service_volume, data, blocking=False, context=context ) return directive.response()
Posts: 1
Participants: 1