diff --git a/homeassistant/components/alexa/state_report.py b/homeassistant/components/alexa/state_report.py index d3e476c2e8c..783397ca047 100644 --- a/homeassistant/components/alexa/state_report.py +++ b/homeassistant/components/alexa/state_report.py @@ -86,7 +86,9 @@ async def async_enable_proactive_mode(hass, smart_home_config): return if should_doorbell: - if new_state.state == STATE_ON: + if new_state.state == STATE_ON and ( + old_state is None or old_state.state != STATE_ON + ): await async_send_doorbell_event_message( hass, smart_home_config, alexa_changed_entity ) diff --git a/tests/components/alexa/test_state_report.py b/tests/components/alexa/test_state_report.py index b2d36b9d179..bb61cea2413 100644 --- a/tests/components/alexa/test_state_report.py +++ b/tests/components/alexa/test_state_report.py @@ -346,7 +346,11 @@ async def test_doorbell_event(hass, aioclient_mock): hass.states.async_set( "binary_sensor.test_doorbell", "off", - {"friendly_name": "Test Doorbell Sensor", "device_class": "occupancy"}, + { + "friendly_name": "Test Doorbell Sensor", + "device_class": "occupancy", + "linkquality": 42, + }, ) await state_report.async_enable_proactive_mode(hass, get_default_config(hass)) @@ -354,7 +358,21 @@ async def test_doorbell_event(hass, aioclient_mock): hass.states.async_set( "binary_sensor.test_doorbell", "on", - {"friendly_name": "Test Doorbell Sensor", "device_class": "occupancy"}, + { + "friendly_name": "Test Doorbell Sensor", + "device_class": "occupancy", + "linkquality": 42, + }, + ) + + hass.states.async_set( + "binary_sensor.test_doorbell", + "on", + { + "friendly_name": "Test Doorbell Sensor", + "device_class": "occupancy", + "linkquality": 99, + }, ) # To trigger event listener @@ -386,6 +404,34 @@ async def test_doorbell_event(hass, aioclient_mock): assert len(aioclient_mock.mock_calls) == 2 +async def test_doorbell_event_from_unknown(hass, aioclient_mock): + """Test doorbell press reports.""" + aioclient_mock.post(TEST_URL, text="", status=202) + + await state_report.async_enable_proactive_mode(hass, get_default_config(hass)) + + hass.states.async_set( + "binary_sensor.test_doorbell", + "on", + { + "friendly_name": "Test Doorbell Sensor", + "device_class": "occupancy", + }, + ) + + # To trigger event listener + await hass.async_block_till_done() + + assert len(aioclient_mock.mock_calls) == 1 + call = aioclient_mock.mock_calls + + call_json = call[0][2] + assert call_json["event"]["header"]["namespace"] == "Alexa.DoorbellEventSource" + assert call_json["event"]["header"]["name"] == "DoorbellPress" + assert call_json["event"]["payload"]["cause"]["type"] == "PHYSICAL_INTERACTION" + assert call_json["event"]["endpoint"]["endpointId"] == "binary_sensor#test_doorbell" + + async def test_doorbell_event_fail(hass, aioclient_mock, caplog): """Test proactive state retries once.""" aioclient_mock.post(