Fix Alexa doorbells (#47257)

This commit is contained in:
Paulus Schoutsen 2021-03-02 06:13:45 -08:00 committed by GitHub
parent 959181a2e9
commit 2ebca88950
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 19 deletions

View File

@ -73,10 +73,7 @@ async def async_enable_proactive_mode(hass, smart_home_config):
if not should_report and interface.properties_proactively_reported():
should_report = True
if (
interface.name() == "Alexa.DoorbellEventSource"
and new_state.state == STATE_ON
):
if interface.name() == "Alexa.DoorbellEventSource":
should_doorbell = True
break
@ -84,27 +81,22 @@ async def async_enable_proactive_mode(hass, smart_home_config):
return
if should_doorbell:
should_report = False
if new_state.state == STATE_ON:
await async_send_doorbell_event_message(
hass, smart_home_config, alexa_changed_entity
)
return
if should_report:
alexa_properties = list(alexa_changed_entity.serialize_properties())
else:
alexa_properties = None
alexa_properties = list(alexa_changed_entity.serialize_properties())
if not checker.async_is_significant_change(
new_state, extra_arg=alexa_properties
):
return
if should_report:
await async_send_changereport_message(
hass, smart_home_config, alexa_changed_entity, alexa_properties
)
elif should_doorbell:
await async_send_doorbell_event_message(
hass, smart_home_config, alexa_changed_entity
)
await async_send_changereport_message(
hass, smart_home_config, alexa_changed_entity, alexa_properties
)
return hass.helpers.event.async_track_state_change(
MATCH_ALL, async_entity_state_listener
@ -246,7 +238,7 @@ async def async_send_delete_message(hass, config, entity_ids):
async def async_send_doorbell_event_message(hass, config, alexa_entity):
"""Send a DoorbellPress event message for an Alexa entity.
https://developer.amazon.com/docs/smarthome/send-events-to-the-alexa-event-gateway.html
https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-doorbelleventsource.html
"""
token = await config.async_get_access_token()

View File

@ -175,6 +175,22 @@ async def test_doorbell_event(hass, aioclient_mock):
assert call_json["event"]["payload"]["cause"]["type"] == "PHYSICAL_INTERACTION"
assert call_json["event"]["endpoint"]["endpointId"] == "binary_sensor#test_doorbell"
hass.states.async_set(
"binary_sensor.test_doorbell",
"off",
{"friendly_name": "Test Doorbell Sensor", "device_class": "occupancy"},
)
hass.states.async_set(
"binary_sensor.test_doorbell",
"on",
{"friendly_name": "Test Doorbell Sensor", "device_class": "occupancy"},
)
await hass.async_block_till_done()
assert len(aioclient_mock.mock_calls) == 2
async def test_proactive_mode_filter_states(hass, aioclient_mock):
"""Test all the cases that filter states."""