From 6c9c280bbb54bc30034dfda8821e072cc12841b6 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 2 Dec 2020 13:07:04 +0100 Subject: [PATCH] Migrate notify-leaving-zone to use mobile app device action (#43832) --- .../blueprints/notify_leaving_zone.yaml | 15 +++-- homeassistant/components/mobile_app/util.py | 3 + tests/components/automation/test_blueprint.py | 57 +++++++++++-------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/automation/blueprints/notify_leaving_zone.yaml b/homeassistant/components/automation/blueprints/notify_leaving_zone.yaml index 178c3222c0a..5b64a090a07 100644 --- a/homeassistant/components/automation/blueprints/notify_leaving_zone.yaml +++ b/homeassistant/components/automation/blueprints/notify_leaving_zone.yaml @@ -13,8 +13,12 @@ blueprint: selector: entity: domain: zone - notify_service: - name: The notify service to use + notify_device: + name: Device to notify + description: Device needs to run the official Home Assistant app to receive notifications. + selector: + device: + integration: mobile_app trigger: platform: state @@ -29,6 +33,7 @@ condition: value_template: "{{ trigger.from_state.state == zone_state and trigger.to_state.state != zone_state }}" action: - - service: !input notify_service - data: - message: "{{ trigger.to_state.name }} has left {{ zone_state }}" + domain: mobile_app + type: notify + device_id: !input notify_device + message: "{{ trigger.to_state.name }} has left {{ zone_state }}" diff --git a/homeassistant/components/mobile_app/util.py b/homeassistant/components/mobile_app/util.py index d9a5f1643c7..60dfe242e04 100644 --- a/homeassistant/components/mobile_app/util.py +++ b/homeassistant/components/mobile_app/util.py @@ -20,6 +20,9 @@ if TYPE_CHECKING: @callback def webhook_id_from_device_id(hass, device_id: str) -> Optional[str]: """Get webhook ID from device ID.""" + if DOMAIN not in hass.data: + return None + for cur_webhook_id, cur_device in hass.data[DOMAIN][DATA_DEVICES].items(): if cur_device.id == device_id: return cur_webhook_id diff --git a/tests/components/automation/test_blueprint.py b/tests/components/automation/test_blueprint.py index 0a651b501c5..568087840c4 100644 --- a/tests/components/automation/test_blueprint.py +++ b/tests/components/automation/test_blueprint.py @@ -66,45 +66,54 @@ async def test_notify_leaving_zone(hass): "input": { "person_entity": "person.test_person", "zone_entity": "zone.school", - "notify_service": "notify.test_service", + "notify_device": "abcdefgh", }, } } }, ) - calls = async_mock_service(hass, "notify", "test_service") + with patch( + "homeassistant.components.mobile_app.device_action.async_call_action_from_config" + ) as mock_call_action: + # Leaving zone to no zone + set_person_state("not_home") + await hass.async_block_till_done() - # Leaving zone to no zone - set_person_state("not_home") - await hass.async_block_till_done() + assert len(mock_call_action.mock_calls) == 1 + _hass, config, variables, _context = mock_call_action.mock_calls[0][1] + message_tpl = config.pop("message") + assert config == { + "domain": "mobile_app", + "type": "notify", + "device_id": "abcdefgh", + } + message_tpl.hass = hass + assert message_tpl.async_render(variables) == "Paulus has left School" - assert len(calls) == 1 - assert calls[0].data["message"] == "Paulus has left School" + # Should not increase when we go to another zone + set_person_state("bla") + await hass.async_block_till_done() - # Should not increase when we go to another zone - set_person_state("bla") - await hass.async_block_till_done() + assert len(mock_call_action.mock_calls) == 1 - assert len(calls) == 1 + # Should not increase when we go into the zone + set_person_state("School") + await hass.async_block_till_done() - # Should not increase when we go into the zone - set_person_state("School") - await hass.async_block_till_done() + assert len(mock_call_action.mock_calls) == 1 - assert len(calls) == 1 + # Should not increase when we move in the zone + set_person_state("School", {"extra_key": "triggers change with same state"}) + await hass.async_block_till_done() - # Should not increase when we move in the zone - set_person_state("School", {"extra_key": "triggers change with same state"}) - await hass.async_block_till_done() + assert len(mock_call_action.mock_calls) == 1 - assert len(calls) == 1 + # Should increase when leaving zone for another zone + set_person_state("Just Outside School") + await hass.async_block_till_done() - # Should increase when leaving zone for another zone - set_person_state("Just Outside School") - await hass.async_block_till_done() - - assert len(calls) == 2 + assert len(mock_call_action.mock_calls) == 2 async def test_motion_light(hass):