1
mirror of https://github.com/home-assistant/core synced 2024-07-24 16:42:06 +02:00

Migrate notify-leaving-zone to use mobile app device action (#43832)

This commit is contained in:
Paulus Schoutsen 2020-12-02 13:07:04 +01:00 committed by GitHub
parent b092430d5b
commit 6c9c280bbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 29 deletions

View File

@ -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 }}"

View File

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

View File

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