Improve editing of device conditions referencing non-added HVAC (#51832)
This commit is contained in:
parent
5469cc8fb2
commit
59a3e0f4dc
2 changed files with 175 additions and 101 deletions
|
@ -5,16 +5,16 @@ import voluptuous as vol
|
||||||
|
|
||||||
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,
|
||||||
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
|
||||||
|
@ -52,7 +52,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)
|
||||||
|
|
||||||
base_condition = {
|
base_condition = {
|
||||||
CONF_CONDITION: "device",
|
CONF_CONDITION: "device",
|
||||||
|
@ -63,10 +63,7 @@ async def async_get_conditions(
|
||||||
|
|
||||||
conditions.append({**base_condition, CONF_TYPE: "is_hvac_mode"})
|
conditions.append({**base_condition, CONF_TYPE: "is_hvac_mode"})
|
||||||
|
|
||||||
if (
|
if supported_features & const.SUPPORT_PRESET_MODE:
|
||||||
state
|
|
||||||
and state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE
|
|
||||||
):
|
|
||||||
conditions.append({**base_condition, CONF_TYPE: "is_preset_mode"})
|
conditions.append({**base_condition, CONF_TYPE: "is_preset_mode"})
|
||||||
|
|
||||||
return conditions
|
return conditions
|
||||||
|
@ -95,21 +92,28 @@ 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_hvac_mode":
|
if condition_type == "is_hvac_mode":
|
||||||
hvac_modes = state.attributes[const.ATTR_HVAC_MODES] if state else []
|
try:
|
||||||
|
hvac_modes = (
|
||||||
|
get_capability(hass, config[ATTR_ENTITY_ID], const.ATTR_HVAC_MODES)
|
||||||
|
or []
|
||||||
|
)
|
||||||
|
except HomeAssistantError:
|
||||||
|
hvac_modes = []
|
||||||
fields[vol.Required(const.ATTR_HVAC_MODE)] = vol.In(hvac_modes)
|
fields[vol.Required(const.ATTR_HVAC_MODE)] = vol.In(hvac_modes)
|
||||||
|
|
||||||
elif condition_type == "is_preset_mode":
|
elif condition_type == "is_preset_mode":
|
||||||
if state:
|
try:
|
||||||
preset_modes = state.attributes.get(const.ATTR_PRESET_MODES, [])
|
preset_modes = (
|
||||||
else:
|
get_capability(hass, config[ATTR_ENTITY_ID], const.ATTR_PRESET_MODES)
|
||||||
|
or []
|
||||||
|
)
|
||||||
|
except HomeAssistantError:
|
||||||
preset_modes = []
|
preset_modes = []
|
||||||
|
|
||||||
fields[vol.Required(const.ATTR_PRESET_MODE)] = vol.In(preset_modes)
|
fields[vol.Required(const.ATTR_PRESET_MODE)] = vol.In(preset_modes)
|
||||||
|
|
||||||
return {"extra_fields": vol.Schema(fields)}
|
return {"extra_fields": vol.Schema(fields)}
|
||||||
|
|
|
@ -36,7 +36,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, ["is_hvac_mode"]),
|
||||||
|
(False, const.SUPPORT_PRESET_MODE, 0, ["is_hvac_mode", "is_preset_mode"]),
|
||||||
|
(True, 0, 0, ["is_hvac_mode"]),
|
||||||
|
(True, 0, const.SUPPORT_PRESET_MODE, ["is_hvac_mode", "is_preset_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 climate."""
|
"""Test we get the expected conditions from a climate."""
|
||||||
config_entry = MockConfigEntry(domain="test", data={})
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
|
@ -44,64 +61,27 @@ 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",
|
||||||
const.HVAC_MODE_COOL,
|
"5678",
|
||||||
{
|
device_id=device_entry.id,
|
||||||
const.ATTR_HVAC_MODE: const.HVAC_MODE_COOL,
|
supported_features=features_reg,
|
||||||
const.ATTR_PRESET_MODE: const.PRESET_AWAY,
|
|
||||||
const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY],
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 17})
|
if set_state:
|
||||||
expected_conditions = [
|
hass.states.async_set(
|
||||||
|
f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state}
|
||||||
|
)
|
||||||
|
expected_conditions = []
|
||||||
|
expected_conditions += [
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "is_hvac_mode",
|
"type": condition,
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_preset_mode",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": f"{DOMAIN}.test_5678",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
|
|
||||||
assert_lists_same(conditions, expected_conditions)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_get_conditions_hvac_only(hass, device_reg, entity_reg):
|
|
||||||
"""Test we get the expected conditions from a climate."""
|
|
||||||
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",
|
|
||||||
const.HVAC_MODE_COOL,
|
|
||||||
{
|
|
||||||
const.ATTR_HVAC_MODE: const.HVAC_MODE_COOL,
|
|
||||||
const.ATTR_PRESET_MODE: const.PRESET_AWAY,
|
|
||||||
const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 1})
|
|
||||||
expected_conditions = [
|
|
||||||
{
|
|
||||||
"condition": "device",
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "is_hvac_mode",
|
|
||||||
"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
|
||||||
]
|
]
|
||||||
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)
|
||||||
|
@ -204,65 +184,155 @@ async def test_if_state(hass, calls):
|
||||||
assert len(calls) == 2
|
assert len(calls) == 2
|
||||||
|
|
||||||
|
|
||||||
async def test_capabilities(hass):
|
@pytest.mark.parametrize(
|
||||||
"""Bla."""
|
"set_state,capabilities_reg,capabilities_state,condition,expected_capabilities",
|
||||||
hass.states.async_set(
|
[
|
||||||
"climate.entity",
|
(
|
||||||
const.HVAC_MODE_COOL,
|
False,
|
||||||
{
|
{const.ATTR_HVAC_MODES: [const.HVAC_MODE_COOL, const.HVAC_MODE_OFF]},
|
||||||
const.ATTR_HVAC_MODE: const.HVAC_MODE_COOL,
|
{},
|
||||||
const.ATTR_PRESET_MODE: const.PRESET_AWAY,
|
"is_hvac_mode",
|
||||||
const.ATTR_HVAC_MODES: [const.HVAC_MODE_COOL, const.HVAC_MODE_OFF],
|
[
|
||||||
const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY],
|
{
|
||||||
},
|
"name": "hvac_mode",
|
||||||
|
"options": [("cool", "cool"), ("off", "off")],
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
False,
|
||||||
|
{const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY]},
|
||||||
|
{},
|
||||||
|
"is_preset_mode",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "preset_mode",
|
||||||
|
"options": [("home", "home"), ("away", "away")],
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
True,
|
||||||
|
{},
|
||||||
|
{const.ATTR_HVAC_MODES: [const.HVAC_MODE_COOL, const.HVAC_MODE_OFF]},
|
||||||
|
"is_hvac_mode",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "hvac_mode",
|
||||||
|
"options": [("cool", "cool"), ("off", "off")],
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
True,
|
||||||
|
{},
|
||||||
|
{const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY]},
|
||||||
|
"is_preset_mode",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "preset_mode",
|
||||||
|
"options": [("home", "home"), ("away", "away")],
|
||||||
|
"required": True,
|
||||||
|
"type": "select",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_capabilities(
|
||||||
|
hass,
|
||||||
|
device_reg,
|
||||||
|
entity_reg,
|
||||||
|
set_state,
|
||||||
|
capabilities_reg,
|
||||||
|
capabilities_state,
|
||||||
|
condition,
|
||||||
|
expected_capabilities,
|
||||||
|
):
|
||||||
|
"""Test getting capabilities."""
|
||||||
|
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,
|
||||||
|
capabilities=capabilities_reg,
|
||||||
|
)
|
||||||
|
if set_state:
|
||||||
|
hass.states.async_set(
|
||||||
|
f"{DOMAIN}.test_5678",
|
||||||
|
const.HVAC_MODE_COOL,
|
||||||
|
capabilities_state,
|
||||||
|
)
|
||||||
|
|
||||||
# Test hvac mode
|
|
||||||
capabilities = await device_condition.async_get_condition_capabilities(
|
capabilities = await device_condition.async_get_condition_capabilities(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "abcdefgh",
|
||||||
"entity_id": "climate.entity",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
"type": "is_hvac_mode",
|
"type": condition,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert capabilities and "extra_fields" in capabilities
|
assert capabilities and "extra_fields" in capabilities
|
||||||
|
|
||||||
assert voluptuous_serialize.convert(
|
assert (
|
||||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
voluptuous_serialize.convert(
|
||||||
) == [
|
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||||
{
|
)
|
||||||
"name": "hvac_mode",
|
== expected_capabilities
|
||||||
"options": [("cool", "cool"), ("off", "off")],
|
)
|
||||||
"required": True,
|
|
||||||
"type": "select",
|
|
||||||
}
|
@pytest.mark.parametrize(
|
||||||
]
|
"condition,capability_name",
|
||||||
|
[("is_hvac_mode", "hvac_mode"), ("is_preset_mode", "preset_mode")],
|
||||||
|
)
|
||||||
|
async def test_capabilities_missing_entity(
|
||||||
|
hass, device_reg, entity_reg, condition, capability_name
|
||||||
|
):
|
||||||
|
"""Test getting capabilities."""
|
||||||
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
# Test preset mode
|
|
||||||
capabilities = await device_condition.async_get_condition_capabilities(
|
capabilities = await device_condition.async_get_condition_capabilities(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"condition": "device",
|
"condition": "device",
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "",
|
"device_id": "abcdefgh",
|
||||||
"entity_id": "climate.entity",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
"type": "is_preset_mode",
|
"type": condition,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert capabilities and "extra_fields" in capabilities
|
expected_capabilities = [
|
||||||
|
|
||||||
assert voluptuous_serialize.convert(
|
|
||||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
|
||||||
) == [
|
|
||||||
{
|
{
|
||||||
"name": "preset_mode",
|
"name": capability_name,
|
||||||
"options": [("home", "home"), ("away", "away")],
|
"options": [],
|
||||||
"required": True,
|
"required": True,
|
||||||
"type": "select",
|
"type": "select",
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
assert capabilities and "extra_fields" in capabilities
|
||||||
|
|
||||||
|
assert (
|
||||||
|
voluptuous_serialize.convert(
|
||||||
|
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||||
|
)
|
||||||
|
== expected_capabilities
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue