mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve editing of device conditions referencing non-added humidifier (#51834)
This commit is contained in:
parent
33ec6621c7
commit
af05543744
@ -7,16 +7,16 @@ from homeassistant.components.device_automation import toggle_entity
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_MODE,
|
ATTR_MODE,
|
||||||
ATTR_SUPPORTED_FEATURES,
|
|
||||||
CONF_CONDITION,
|
CONF_CONDITION,
|
||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
CONF_DOMAIN,
|
CONF_DOMAIN,
|
||||||
CONF_ENTITY_ID,
|
CONF_ENTITY_ID,
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, HomeAssistantError, callback
|
||||||
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_capability, get_supported_features
|
||||||
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
from homeassistant.helpers.typing import ConfigType, TemplateVarsType
|
||||||
|
|
||||||
from . import DOMAIN, const
|
from . import DOMAIN, const
|
||||||
@ -48,9 +48,9 @@ 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)
|
||||||
|
|
||||||
if state and state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_MODES:
|
if supported_features & const.SUPPORT_MODES:
|
||||||
conditions.append(
|
conditions.append(
|
||||||
{
|
{
|
||||||
CONF_CONDITION: "device",
|
CONF_CONDITION: "device",
|
||||||
@ -87,15 +87,17 @@ def async_condition_from_config(
|
|||||||
|
|
||||||
async def async_get_condition_capabilities(hass, config):
|
async def async_get_condition_capabilities(hass, config):
|
||||||
"""List condition capabilities."""
|
"""List condition capabilities."""
|
||||||
state = hass.states.get(config[CONF_ENTITY_ID])
|
|
||||||
condition_type = config[CONF_TYPE]
|
condition_type = config[CONF_TYPE]
|
||||||
|
|
||||||
fields = {}
|
fields = {}
|
||||||
|
|
||||||
if condition_type == "is_mode":
|
if condition_type == "is_mode":
|
||||||
if state:
|
try:
|
||||||
modes = state.attributes.get(const.ATTR_AVAILABLE_MODES, [])
|
modes = (
|
||||||
else:
|
get_capability(hass, config[ATTR_ENTITY_ID], const.ATTR_AVAILABLE_MODES)
|
||||||
|
or []
|
||||||
|
)
|
||||||
|
except HomeAssistantError:
|
||||||
modes = []
|
modes = []
|
||||||
|
|
||||||
fields[vol.Required(ATTR_MODE)] = vol.In(modes)
|
fields[vol.Required(ATTR_MODE)] = vol.In(modes)
|
||||||
|
@ -11,7 +11,6 @@ from homeassistant.setup import async_setup_component
|
|||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
assert_lists_same,
|
assert_lists_same,
|
||||||
async_get_device_automation_capabilities,
|
|
||||||
async_get_device_automations,
|
async_get_device_automations,
|
||||||
async_mock_service,
|
async_mock_service,
|
||||||
mock_device_registry,
|
mock_device_registry,
|
||||||
@ -38,7 +37,24 @@ def calls(hass):
|
|||||||
return async_mock_service(hass, "test", "automation")
|
return async_mock_service(hass, "test", "automation")
|
||||||
|
|
||||||
|
|
||||||
async def test_get_conditions(hass, device_reg, entity_reg):
|
@pytest.mark.parametrize(
|
||||||
|
"set_state,features_reg,features_state,expected_condition_types",
|
||||||
|
[
|
||||||
|
(False, 0, 0, []),
|
||||||
|
(False, const.SUPPORT_MODES, 0, ["is_mode"]),
|
||||||
|
(True, 0, 0, []),
|
||||||
|
(True, 0, const.SUPPORT_MODES, ["is_mode"]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
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 humidifier."""
|
"""Test we get the expected conditions from a humidifier."""
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
@ -46,80 +62,38 @@ async def test_get_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(
|
||||||
hass.states.async_set(
|
DOMAIN,
|
||||||
f"{DOMAIN}.test_5678",
|
"test",
|
||||||
STATE_ON,
|
"5678",
|
||||||
{
|
device_id=device_entry.id,
|
||||||
ATTR_MODE: const.MODE_AWAY,
|
supported_features=features_reg,
|
||||||
const.ATTR_AVAILABLE_MODES: [const.MODE_HOME, const.MODE_AWAY],
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
if set_state:
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
"humidifier.test_5678", "attributes", {"supported_features": 1}
|
f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state}
|
||||||
)
|
)
|
||||||
expected_conditions = [
|
expected_conditions = []
|
||||||
|
basic_condition_types = ["is_on", "is_off"]
|
||||||
|
expected_conditions += [
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "is_off",
|
"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_on",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_mode",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
expected_conditions += [
|
||||||
assert_lists_same(conditions, expected_conditions)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_get_conditions_toggle_only(hass, device_reg, entity_reg):
|
|
||||||
"""Test we get the expected conditions from a humidifier."""
|
|
||||||
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(
|
|
||||||
f"{DOMAIN}.test_5678",
|
|
||||||
STATE_ON,
|
|
||||||
{
|
|
||||||
ATTR_MODE: const.MODE_AWAY,
|
|
||||||
const.ATTR_AVAILABLE_MODES: [const.MODE_HOME, const.MODE_AWAY],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
hass.states.async_set(
|
|
||||||
"humidifier.test_5678", "attributes", {"supported_features": 0}
|
|
||||||
)
|
|
||||||
expected_conditions = [
|
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "is_off",
|
"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_on",
|
|
||||||
"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)
|
||||||
@ -227,81 +201,206 @@ async def test_if_state(hass, calls):
|
|||||||
assert len(calls) == 3
|
assert len(calls) == 3
|
||||||
|
|
||||||
|
|
||||||
async def test_capabilities(hass):
|
@pytest.mark.parametrize(
|
||||||
"""Test capabilities."""
|
"set_state,capabilities_reg,capabilities_state,condition,expected_capabilities",
|
||||||
hass.states.async_set(
|
[
|
||||||
"humidifier.entity",
|
(
|
||||||
STATE_ON,
|
False,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
"is_mode",
|
||||||
|
[
|
||||||
{
|
{
|
||||||
ATTR_MODE: const.MODE_AWAY,
|
"name": "mode",
|
||||||
const.ATTR_AVAILABLE_MODES: [const.MODE_HOME, const.MODE_AWAY],
|
"options": [],
|
||||||
},
|
"required": True,
|
||||||
)
|
"type": "select",
|
||||||
|
}
|
||||||
# Test mode
|
],
|
||||||
capabilities = await device_condition.async_get_condition_capabilities(
|
),
|
||||||
hass,
|
(
|
||||||
{
|
False,
|
||||||
"condition": "device",
|
{const.ATTR_AVAILABLE_MODES: [const.MODE_HOME, const.MODE_AWAY]},
|
||||||
"domain": DOMAIN,
|
{},
|
||||||
"device_id": "",
|
"is_mode",
|
||||||
"entity_id": "humidifier.entity",
|
[
|
||||||
"type": "is_mode",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
assert capabilities and "extra_fields" in capabilities
|
|
||||||
|
|
||||||
assert voluptuous_serialize.convert(
|
|
||||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
|
||||||
) == [
|
|
||||||
{
|
{
|
||||||
"name": "mode",
|
"name": "mode",
|
||||||
"options": [("home", "home"), ("away", "away")],
|
"options": [("home", "home"), ("away", "away")],
|
||||||
"required": True,
|
"required": True,
|
||||||
"type": "select",
|
"type": "select",
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
async def test_capabilities_no_state(hass):
|
False,
|
||||||
"""Test capabilities while state not available."""
|
{},
|
||||||
# Test mode
|
{},
|
||||||
capabilities = await device_condition.async_get_condition_capabilities(
|
"is_off",
|
||||||
hass,
|
[
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"name": "for",
|
||||||
"domain": DOMAIN,
|
"optional": True,
|
||||||
"device_id": "",
|
"type": "positive_time_period_dict",
|
||||||
"entity_id": "humidifier.entity",
|
}
|
||||||
"type": "is_mode",
|
],
|
||||||
},
|
),
|
||||||
|
(
|
||||||
|
False,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
"is_on",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "for",
|
||||||
|
"optional": True,
|
||||||
|
"type": "positive_time_period_dict",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
True,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
"is_mode",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "mode",
|
||||||
|
"options": [],
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
True,
|
||||||
|
{},
|
||||||
|
{const.ATTR_AVAILABLE_MODES: [const.MODE_HOME, const.MODE_AWAY]},
|
||||||
|
"is_mode",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "mode",
|
||||||
|
"options": [("home", "home"), ("away", "away")],
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
True,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
"is_off",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "for",
|
||||||
|
"optional": True,
|
||||||
|
"type": "positive_time_period_dict",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
True,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
"is_on",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "for",
|
||||||
|
"optional": True,
|
||||||
|
"type": "positive_time_period_dict",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
async def test_capabilities(
|
||||||
assert capabilities and "extra_fields" in capabilities
|
hass,
|
||||||
|
device_reg,
|
||||||
assert voluptuous_serialize.convert(
|
entity_reg,
|
||||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
set_state,
|
||||||
) == [{"name": "mode", "options": [], "required": True, "type": "select"}]
|
capabilities_reg,
|
||||||
|
capabilities_state,
|
||||||
|
condition,
|
||||||
async def test_get_condition_capabilities(hass, device_reg, entity_reg):
|
expected_capabilities,
|
||||||
"""Test we get the expected toggle capabilities."""
|
):
|
||||||
|
"""Test getting capabilities."""
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
device_entry = device_reg.async_get_or_create(
|
device_entry = device_reg.async_get_or_create(
|
||||||
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(
|
||||||
expected_capabilities = {
|
DOMAIN,
|
||||||
"extra_fields": [
|
"test",
|
||||||
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
|
"5678",
|
||||||
]
|
device_id=device_entry.id,
|
||||||
}
|
capabilities=capabilities_reg,
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
)
|
||||||
for condition in conditions:
|
if set_state:
|
||||||
capabilities = await async_get_device_automation_capabilities(
|
hass.states.async_set(
|
||||||
hass, "condition", condition
|
f"{DOMAIN}.test_5678",
|
||||||
|
STATE_ON,
|
||||||
|
capabilities_state,
|
||||||
|
)
|
||||||
|
|
||||||
|
capabilities = await device_condition.async_get_condition_capabilities(
|
||||||
|
hass,
|
||||||
|
{
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "abcdefgh",
|
||||||
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
|
"type": condition,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert capabilities and "extra_fields" in capabilities
|
||||||
|
|
||||||
|
assert (
|
||||||
|
voluptuous_serialize.convert(
|
||||||
|
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||||
|
)
|
||||||
|
== expected_capabilities
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"condition,capability_name,extra",
|
||||||
|
[
|
||||||
|
("is_mode", "mode", {"type": "select", "options": []}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_capabilities_missing_entity(
|
||||||
|
hass, device_reg, entity_reg, condition, capability_name, extra
|
||||||
|
):
|
||||||
|
"""Test getting capabilities."""
|
||||||
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
capabilities = await device_condition.async_get_condition_capabilities(
|
||||||
|
hass,
|
||||||
|
{
|
||||||
|
"domain": DOMAIN,
|
||||||
|
"device_id": "abcdefgh",
|
||||||
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
|
"type": condition,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
expected_capabilities = [
|
||||||
|
{
|
||||||
|
"name": capability_name,
|
||||||
|
"required": True,
|
||||||
|
**extra,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
assert capabilities and "extra_fields" in capabilities
|
||||||
|
|
||||||
|
assert (
|
||||||
|
voluptuous_serialize.convert(
|
||||||
|
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||||
|
)
|
||||||
|
== expected_capabilities
|
||||||
)
|
)
|
||||||
assert capabilities == expected_capabilities
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user