Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report correct thermostat mode to Alexa #18053

Merged
merged 1 commit into from Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion homeassistant/components/alexa/smart_home.py
Expand Up @@ -1839,11 +1839,17 @@ async def async_api_set_thermostat_mode(hass, config, directive, context):
climate.ATTR_OPERATION_MODE: ha_mode,
}

response = directive.response()
await hass.services.async_call(
entity.domain, climate.SERVICE_SET_OPERATION_MODE, data,
blocking=False, context=context)
response.add_context_property({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative is to change blocking=False to blocking=True and we get the real value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know I thought I tried that once, and it didn't work. I didn't dig into it. In any case, I'd expect that to increase response latency, which might not be good for UX. There is https://developer.amazon.com/docs/device-apis/alexa-interface.html#deferredresponse, but I don't know how that would impact UX.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason it doesn't always work is because it will only work with polled devices. It won't work with devices that push their state to HA, as that state change will be received too late for the service to be marked done. I think that for now I am fine with pretending it worked.

'name': 'thermostatMode',
'namespace': 'Alexa.ThermostatController',
'value': mode,
})

return directive.response()
return response


@HANDLERS.register(('Alexa', 'ReportState'))
Expand Down
25 changes: 22 additions & 3 deletions tests/components/alexa/test_smart_home.py
Expand Up @@ -873,22 +873,41 @@ async def test_thermostat(hass):
)
assert msg['event']['payload']['type'] == 'TEMPERATURE_VALUE_OUT_OF_RANGE'

call, _ = await assert_request_calls_service(
# Setting mode, the payload can be an object with a value attribute...
call, msg = await assert_request_calls_service(
'Alexa.ThermostatController', 'SetThermostatMode',
'climate#test_thermostat', 'climate.set_operation_mode',
hass,
payload={'thermostatMode': {'value': 'HEAT'}}
)
assert call.data['operation_mode'] == 'heat'
properties = _ReportedProperties(msg['context']['properties'])
properties.assert_equal(
'Alexa.ThermostatController', 'thermostatMode', 'HEAT')

call, _ = await assert_request_calls_service(
call, msg = await assert_request_calls_service(
'Alexa.ThermostatController', 'SetThermostatMode',
'climate#test_thermostat', 'climate.set_operation_mode',
hass,
payload={'thermostatMode': 'HEAT'}
payload={'thermostatMode': {'value': 'COOL'}}
)
assert call.data['operation_mode'] == 'cool'
properties = _ReportedProperties(msg['context']['properties'])
properties.assert_equal(
'Alexa.ThermostatController', 'thermostatMode', 'COOL')

# ...it can also be just the mode.
call, msg = await assert_request_calls_service(
'Alexa.ThermostatController', 'SetThermostatMode',
'climate#test_thermostat', 'climate.set_operation_mode',
hass,
payload={'thermostatMode': 'HEAT'}
)
assert call.data['operation_mode'] == 'heat'
properties = _ReportedProperties(msg['context']['properties'])
properties.assert_equal(
'Alexa.ThermostatController', 'thermostatMode', 'HEAT')

msg = await assert_request_fails(
'Alexa.ThermostatController', 'SetThermostatMode',
'climate#test_thermostat', 'climate.set_operation_mode',
Expand Down