mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Improve editing of device conditions referencing non-added alarm (#51830)
This commit is contained in:
parent
77f6d1f5cb
commit
08af791d4c
@ -13,7 +13,6 @@ from homeassistant.components.alarm_control_panel.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_SUPPORTED_FEATURES,
|
|
||||||
CONF_CONDITION,
|
CONF_CONDITION,
|
||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
CONF_DOMAIN,
|
CONF_DOMAIN,
|
||||||
@ -29,6 +28,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import condition, config_validation as cv, entity_registry
|
from homeassistant.helpers import condition, config_validation as cv, entity_registry
|
||||||
from homeassistant.helpers.config_validation import DEVICE_CONDITION_BASE_SCHEMA
|
from homeassistant.helpers.config_validation import DEVICE_CONDITION_BASE_SCHEMA
|
||||||
|
from homeassistant.helpers.entity import get_supported_features
|
||||||
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
||||||
|
|
||||||
from . import DOMAIN
|
from . import DOMAIN
|
||||||
@ -70,13 +70,7 @@ async def async_get_conditions(
|
|||||||
if entry.domain != DOMAIN:
|
if entry.domain != DOMAIN:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
state = hass.states.get(entry.entity_id)
|
supported_features = get_supported_features(hass, entry.entity_id)
|
||||||
|
|
||||||
# We need a state or else we can't populate the different armed conditions
|
|
||||||
if state is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
supported_features = state.attributes[ATTR_SUPPORTED_FEATURES]
|
|
||||||
|
|
||||||
# Add conditions for each entity that belongs to this integration
|
# Add conditions for each entity that belongs to this integration
|
||||||
base_condition = {
|
base_condition = {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""The tests for Alarm control panel device conditions."""
|
"""The tests for Alarm control panel device conditions."""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.alarm_control_panel import DOMAIN
|
from homeassistant.components.alarm_control_panel import DOMAIN, const
|
||||||
import homeassistant.components.automation as automation
|
import homeassistant.components.automation as automation
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
STATE_ALARM_ARMED_AWAY,
|
STATE_ALARM_ARMED_AWAY,
|
||||||
@ -43,7 +43,30 @@ def calls(hass):
|
|||||||
return async_mock_service(hass, "test", "automation")
|
return async_mock_service(hass, "test", "automation")
|
||||||
|
|
||||||
|
|
||||||
async def test_get_no_conditions(hass, device_reg, entity_reg):
|
@pytest.mark.parametrize(
|
||||||
|
"set_state,features_reg,features_state,expected_condition_types",
|
||||||
|
[
|
||||||
|
(False, 0, 0, []),
|
||||||
|
(False, const.SUPPORT_ALARM_ARM_AWAY, 0, ["is_armed_away"]),
|
||||||
|
(False, const.SUPPORT_ALARM_ARM_HOME, 0, ["is_armed_home"]),
|
||||||
|
(False, const.SUPPORT_ALARM_ARM_NIGHT, 0, ["is_armed_night"]),
|
||||||
|
(False, const.SUPPORT_ALARM_ARM_CUSTOM_BYPASS, 0, ["is_armed_custom_bypass"]),
|
||||||
|
(True, 0, 0, []),
|
||||||
|
(True, 0, const.SUPPORT_ALARM_ARM_AWAY, ["is_armed_away"]),
|
||||||
|
(True, 0, const.SUPPORT_ALARM_ARM_HOME, ["is_armed_home"]),
|
||||||
|
(True, 0, const.SUPPORT_ALARM_ARM_NIGHT, ["is_armed_night"]),
|
||||||
|
(True, 0, const.SUPPORT_ALARM_ARM_CUSTOM_BYPASS, ["is_armed_custom_bypass"]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_get_conditions(
|
||||||
|
hass,
|
||||||
|
device_reg,
|
||||||
|
entity_reg,
|
||||||
|
set_state,
|
||||||
|
features_reg,
|
||||||
|
features_state,
|
||||||
|
expected_condition_types,
|
||||||
|
):
|
||||||
"""Test we get the expected conditions from a alarm_control_panel."""
|
"""Test we get the expected conditions from a alarm_control_panel."""
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
@ -51,101 +74,41 @@ async def test_get_no_conditions(hass, device_reg, entity_reg):
|
|||||||
config_entry_id=config_entry.entry_id,
|
config_entry_id=config_entry.entry_id,
|
||||||
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
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)
|
entity_reg.async_get_or_create(
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
DOMAIN,
|
||||||
assert_lists_same(conditions, [])
|
"test",
|
||||||
|
"5678",
|
||||||
|
device_id=device_entry.id,
|
||||||
async def test_get_minimum_conditions(hass, device_reg, entity_reg):
|
supported_features=features_reg,
|
||||||
"""Test we get the expected conditions from a alarm_control_panel."""
|
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
|
||||||
config_entry.add_to_hass(hass)
|
|
||||||
device_entry = device_reg.async_get_or_create(
|
|
||||||
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)
|
if set_state:
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
"alarm_control_panel.test_5678", "attributes", {"supported_features": 0}
|
"alarm_control_panel.test_5678",
|
||||||
)
|
"attributes",
|
||||||
expected_conditions = [
|
{"supported_features": features_state},
|
||||||
|
)
|
||||||
|
expected_conditions = []
|
||||||
|
basic_condition_types = ["is_disarmed", "is_triggered"]
|
||||||
|
expected_conditions += [
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "is_disarmed",
|
"type": condition,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
},
|
}
|
||||||
{
|
for condition in basic_condition_types
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_triggered",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
|
expected_conditions += [
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
|
||||||
assert_lists_same(conditions, expected_conditions)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_get_maximum_conditions(hass, device_reg, entity_reg):
|
|
||||||
"""Test we get the expected conditions from a alarm_control_panel."""
|
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
|
||||||
config_entry.add_to_hass(hass)
|
|
||||||
device_entry = device_reg.async_get_or_create(
|
|
||||||
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": 31}
|
|
||||||
)
|
|
||||||
expected_conditions = [
|
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "is_disarmed",
|
"type": condition,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
},
|
}
|
||||||
{
|
for condition in expected_condition_types
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_triggered",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_armed_home",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_armed_away",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_armed_night",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_armed_custom_bypass",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
||||||
assert_lists_same(conditions, expected_conditions)
|
assert_lists_same(conditions, expected_conditions)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user