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: selector:
entity: entity:
domain: zone domain: zone
notify_service: notify_device:
name: The notify service to use name: Device to notify
description: Device needs to run the official Home Assistant app to receive notifications.
selector:
device:
integration: mobile_app
trigger: trigger:
platform: state platform: state
@ -29,6 +33,7 @@ condition:
value_template: "{{ trigger.from_state.state == zone_state and trigger.to_state.state != zone_state }}" value_template: "{{ trigger.from_state.state == zone_state and trigger.to_state.state != zone_state }}"
action: action:
- service: !input notify_service domain: mobile_app
data: type: notify
device_id: !input notify_device
message: "{{ trigger.to_state.name }} has left {{ zone_state }}" message: "{{ trigger.to_state.name }} has left {{ zone_state }}"

View File

@ -20,6 +20,9 @@ if TYPE_CHECKING:
@callback @callback
def webhook_id_from_device_id(hass, device_id: str) -> Optional[str]: def webhook_id_from_device_id(hass, device_id: str) -> Optional[str]:
"""Get webhook ID from device ID.""" """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(): for cur_webhook_id, cur_device in hass.data[DOMAIN][DATA_DEVICES].items():
if cur_device.id == device_id: if cur_device.id == device_id:
return cur_webhook_id return cur_webhook_id

View File

@ -66,45 +66,54 @@ async def test_notify_leaving_zone(hass):
"input": { "input": {
"person_entity": "person.test_person", "person_entity": "person.test_person",
"zone_entity": "zone.school", "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 # Leaving zone to no zone
set_person_state("not_home") set_person_state("not_home")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(mock_call_action.mock_calls) == 1
assert calls[0].data["message"] == "Paulus has left School" _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"
# Should not increase when we go to another zone # Should not increase when we go to another zone
set_person_state("bla") set_person_state("bla")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(mock_call_action.mock_calls) == 1
# Should not increase when we go into the zone # Should not increase when we go into the zone
set_person_state("School") set_person_state("School")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(mock_call_action.mock_calls) == 1
# Should not increase when we move in the zone # Should not increase when we move in the zone
set_person_state("School", {"extra_key": "triggers change with same state"}) set_person_state("School", {"extra_key": "triggers change with same state"})
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 1 assert len(mock_call_action.mock_calls) == 1
# Should increase when leaving zone for another zone # Should increase when leaving zone for another zone
set_person_state("Just Outside School") set_person_state("Just Outside School")
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(mock_call_action.mock_calls) == 2
async def test_motion_light(hass): async def test_motion_light(hass):