mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Improve editing of device triggers referencing non-added alarm (#51701)
This commit is contained in:
parent
3a739563b4
commit
f9e9202e2d
@ -14,7 +14,6 @@ from homeassistant.components.automation import AutomationActionType
|
||||
from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA
|
||||
from homeassistant.components.homeassistant.triggers import state as state_trigger
|
||||
from homeassistant.const import (
|
||||
ATTR_SUPPORTED_FEATURES,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_DOMAIN,
|
||||
CONF_ENTITY_ID,
|
||||
@ -30,6 +29,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv, entity_registry
|
||||
from homeassistant.helpers.entity import get_supported_features
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from . import DOMAIN
|
||||
@ -62,13 +62,7 @@ async def async_get_triggers(
|
||||
if entry.domain != DOMAIN:
|
||||
continue
|
||||
|
||||
entity_state = hass.states.get(entry.entity_id)
|
||||
|
||||
# We need a state or else we can't populate the HVAC and preset modes.
|
||||
if entity_state is None:
|
||||
continue
|
||||
|
||||
supported_features = entity_state.attributes[ATTR_SUPPORTED_FEATURES]
|
||||
supported_features = get_supported_features(hass, entry.entity_id)
|
||||
|
||||
# Add triggers for each entity that belongs to this integration
|
||||
base_trigger = {
|
||||
|
@ -48,7 +48,48 @@ def calls(hass):
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
async def test_get_triggers(hass, device_reg, entity_reg):
|
||||
@pytest.mark.parametrize(
|
||||
"set_state,features_reg,features_state,expected_trigger_types",
|
||||
[
|
||||
(False, 0, 0, ["triggered", "disarmed", "arming"]),
|
||||
(
|
||||
False,
|
||||
15,
|
||||
0,
|
||||
[
|
||||
"triggered",
|
||||
"disarmed",
|
||||
"arming",
|
||||
"armed_home",
|
||||
"armed_away",
|
||||
"armed_night",
|
||||
],
|
||||
),
|
||||
(True, 0, 0, ["triggered", "disarmed", "arming"]),
|
||||
(
|
||||
True,
|
||||
0,
|
||||
15,
|
||||
[
|
||||
"triggered",
|
||||
"disarmed",
|
||||
"arming",
|
||||
"armed_home",
|
||||
"armed_away",
|
||||
"armed_night",
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_get_triggers(
|
||||
hass,
|
||||
device_reg,
|
||||
entity_reg,
|
||||
set_state,
|
||||
features_reg,
|
||||
features_state,
|
||||
expected_trigger_types,
|
||||
):
|
||||
"""Test we get the expected triggers from an alarm_control_panel."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
@ -56,53 +97,30 @@ async def test_get_triggers(hass, device_reg, entity_reg):
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
|
||||
hass.states.async_set(
|
||||
"alarm_control_panel.test_5678", "attributes", {"supported_features": 15}
|
||||
entity_reg.async_get_or_create(
|
||||
DOMAIN,
|
||||
"test",
|
||||
"5678",
|
||||
device_id=device_entry.id,
|
||||
supported_features=features_reg,
|
||||
)
|
||||
expected_triggers = [
|
||||
if set_state:
|
||||
hass.states.async_set(
|
||||
"alarm_control_panel.test_5678",
|
||||
"attributes",
|
||||
{"supported_features": features_state},
|
||||
)
|
||||
expected_triggers = []
|
||||
|
||||
expected_triggers += [
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "disarmed",
|
||||
"type": trigger,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
},
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "triggered",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
},
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "arming",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
},
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "armed_home",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
},
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "armed_away",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
},
|
||||
{
|
||||
"platform": "device",
|
||||
"domain": DOMAIN,
|
||||
"type": "armed_night",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
},
|
||||
}
|
||||
for trigger in expected_trigger_types
|
||||
]
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert_lists_same(triggers, expected_triggers)
|
||||
|
Loading…
x
Reference in New Issue
Block a user