mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Migrate notify-leaving-zone to use mobile app device action (#43832)
This commit is contained in:
parent
b092430d5b
commit
6c9c280bbb
@ -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
|
||||||
message: "{{ trigger.to_state.name }} has left {{ zone_state }}"
|
device_id: !input notify_device
|
||||||
|
message: "{{ trigger.to_state.name }} has left {{ zone_state }}"
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
set_person_state("not_home")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# Leaving zone to no zone
|
assert len(mock_call_action.mock_calls) == 1
|
||||||
set_person_state("not_home")
|
_hass, config, variables, _context = mock_call_action.mock_calls[0][1]
|
||||||
await hass.async_block_till_done()
|
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
|
# Should not increase when we go to another zone
|
||||||
assert calls[0].data["message"] == "Paulus has left School"
|
set_person_state("bla")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# Should not increase when we go to another zone
|
assert len(mock_call_action.mock_calls) == 1
|
||||||
set_person_state("bla")
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
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
|
assert len(mock_call_action.mock_calls) == 1
|
||||||
set_person_state("School")
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
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
|
assert len(mock_call_action.mock_calls) == 1
|
||||||
set_person_state("School", {"extra_key": "triggers change with same state"})
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
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
|
assert len(mock_call_action.mock_calls) == 2
|
||||||
set_person_state("Just Outside School")
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert len(calls) == 2
|
|
||||||
|
|
||||||
|
|
||||||
async def test_motion_light(hass):
|
async def test_motion_light(hass):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user