Improve editing of device actions referencing non-added alarm (#51747)

This commit is contained in:
Erik Montnemery 2021-06-12 23:43:28 +02:00 committed by GitHub
parent f975beae77
commit 3a739563b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 41 deletions

View File

@ -8,7 +8,6 @@ import voluptuous as vol
from homeassistant.const import (
ATTR_CODE,
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
CONF_CODE,
CONF_DEVICE_ID,
CONF_DOMAIN,
@ -23,6 +22,7 @@ from homeassistant.const import (
from homeassistant.core import Context, HomeAssistant
from homeassistant.helpers import entity_registry
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import get_supported_features
from homeassistant.helpers.typing import ConfigType
from . import ATTR_CODE_ARM_REQUIRED, DOMAIN
@ -62,13 +62,7 @@ async def async_get_actions(
if entry.domain != DOMAIN:
continue
state = hass.states.get(entry.entity_id)
# We need a state or else we can't populate the HVAC and preset modes.
if state is None:
continue
supported_features = state.attributes[ATTR_SUPPORTED_FEATURES]
supported_features = get_supported_features(hass, entry.entity_id)
base_action = {
CONF_DEVICE_ID: device_id,

View File

@ -1,7 +1,7 @@
"""The tests for Alarm control panel device actions."""
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
from homeassistant.const import (
CONF_PLATFORM,
@ -38,7 +38,30 @@ def entity_reg(hass):
return mock_registry(hass)
async def test_get_actions(hass, device_reg, entity_reg):
@pytest.mark.parametrize(
"set_state,features_reg,features_state,expected_action_types",
[
(False, 0, 0, ["disarm"]),
(False, const.SUPPORT_ALARM_ARM_AWAY, 0, ["disarm", "arm_away"]),
(False, const.SUPPORT_ALARM_ARM_HOME, 0, ["disarm", "arm_home"]),
(False, const.SUPPORT_ALARM_ARM_NIGHT, 0, ["disarm", "arm_night"]),
(False, const.SUPPORT_ALARM_TRIGGER, 0, ["disarm", "trigger"]),
(True, 0, 0, ["disarm"]),
(True, 0, const.SUPPORT_ALARM_ARM_AWAY, ["disarm", "arm_away"]),
(True, 0, const.SUPPORT_ALARM_ARM_HOME, ["disarm", "arm_home"]),
(True, 0, const.SUPPORT_ALARM_ARM_NIGHT, ["disarm", "arm_night"]),
(True, 0, const.SUPPORT_ALARM_TRIGGER, ["disarm", "trigger"]),
],
)
async def test_get_actions(
hass,
device_reg,
entity_reg,
set_state,
features_reg,
features_state,
expected_action_types,
):
"""Test we get the expected actions from a alarm_control_panel."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
@ -46,41 +69,26 @@ async def test_get_actions(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_actions = [
if set_state:
hass.states.async_set(
f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state}
)
expected_actions = []
expected_actions += [
{
"domain": DOMAIN,
"type": "arm_away",
"type": action,
"device_id": device_entry.id,
"entity_id": "alarm_control_panel.test_5678",
},
{
"domain": DOMAIN,
"type": "arm_home",
"device_id": device_entry.id,
"entity_id": "alarm_control_panel.test_5678",
},
{
"domain": DOMAIN,
"type": "arm_night",
"device_id": device_entry.id,
"entity_id": "alarm_control_panel.test_5678",
},
{
"domain": DOMAIN,
"type": "disarm",
"device_id": device_entry.id,
"entity_id": "alarm_control_panel.test_5678",
},
{
"domain": DOMAIN,
"type": "trigger",
"device_id": device_entry.id,
"entity_id": "alarm_control_panel.test_5678",
},
"entity_id": f"{DOMAIN}.test_5678",
}
for action in expected_action_types
]
actions = await async_get_device_automations(hass, "action", device_entry.id)
assert_lists_same(actions, expected_actions)