Fix Alexa: Only trigger doorbell event on actual state change to "ON" (#74924)

* Alexa: Only trigger doorbell event on actual state change to "ON"

* Remove unnecessary check for new_state

Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>

* First check state is `on` before checking the old state

Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>
pull/75243/head
Thomas Hollstegge 2022-07-14 21:40:53 +02:00 committed by Paulus Schoutsen
parent a5693c083f
commit b1c07ac17a
2 changed files with 51 additions and 3 deletions

View File

@ -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
)

View File

@ -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(