Improve editing of device actions referencing non-added HVAC (#51706)
* Improve editing of device actions referencing non-added HVAC * Improve test coveragepull/51741/head
parent
49bec86dae
commit
ba6b527d61
|
@ -5,15 +5,15 @@ import voluptuous as vol
|
|||
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_SUPPORTED_FEATURES,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_DOMAIN,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_TYPE,
|
||||
)
|
||||
from homeassistant.core import Context, HomeAssistant
|
||||
from homeassistant.core import Context, HomeAssistant, HomeAssistantError
|
||||
from homeassistant.helpers import entity_registry
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import get_capability, get_supported_features
|
||||
|
||||
from . import DOMAIN, const
|
||||
|
||||
|
@ -48,11 +48,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
|||
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 = get_supported_features(hass, entry.entity_id)
|
||||
|
||||
base_action = {
|
||||
CONF_DEVICE_ID: device_id,
|
||||
|
@ -61,7 +57,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
|||
}
|
||||
|
||||
actions.append({**base_action, CONF_TYPE: "set_hvac_mode"})
|
||||
if state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_PRESET_MODE:
|
||||
if supported_features & const.SUPPORT_PRESET_MODE:
|
||||
actions.append({**base_action, CONF_TYPE: "set_preset_mode"})
|
||||
|
||||
return actions
|
||||
|
@ -87,18 +83,26 @@ async def async_call_action_from_config(
|
|||
|
||||
async def async_get_action_capabilities(hass, config):
|
||||
"""List action capabilities."""
|
||||
state = hass.states.get(config[CONF_ENTITY_ID])
|
||||
action_type = config[CONF_TYPE]
|
||||
|
||||
fields = {}
|
||||
|
||||
if action_type == "set_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)
|
||||
elif action_type == "set_preset_mode":
|
||||
if state:
|
||||
preset_modes = state.attributes.get(const.ATTR_PRESET_MODES, [])
|
||||
else:
|
||||
try:
|
||||
preset_modes = (
|
||||
get_capability(hass, config[ATTR_ENTITY_ID], const.ATTR_PRESET_MODES)
|
||||
or []
|
||||
)
|
||||
except HomeAssistantError:
|
||||
preset_modes = []
|
||||
fields[vol.Required(const.ATTR_PRESET_MODE)] = vol.In(preset_modes)
|
||||
|
||||
|
|
|
@ -93,6 +93,23 @@ def async_generate_entity_id(
|
|||
return test_string
|
||||
|
||||
|
||||
def get_capability(hass: HomeAssistant, entity_id: str, capability: str) -> str | None:
|
||||
"""Get a capability attribute of an entity.
|
||||
|
||||
First try the statemachine, then entity registry.
|
||||
"""
|
||||
state = hass.states.get(entity_id)
|
||||
if state:
|
||||
return state.attributes.get(capability)
|
||||
|
||||
entity_registry = er.async_get(hass)
|
||||
entry = entity_registry.async_get(entity_id)
|
||||
if not entry:
|
||||
raise HomeAssistantError(f"Unknown entity {entity_id}")
|
||||
|
||||
return entry.capabilities.get(capability) if entry.capabilities else None
|
||||
|
||||
|
||||
def get_device_class(hass: HomeAssistant, entity_id: str) -> str | None:
|
||||
"""Get device class of an entity.
|
||||
|
||||
|
|
|
@ -30,7 +30,24 @@ 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, ["set_hvac_mode"]),
|
||||
(False, const.SUPPORT_PRESET_MODE, 0, ["set_hvac_mode", "set_preset_mode"]),
|
||||
(True, 0, 0, ["set_hvac_mode"]),
|
||||
(True, 0, const.SUPPORT_PRESET_MODE, ["set_hvac_mode", "set_preset_mode"]),
|
||||
],
|
||||
)
|
||||
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 climate."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
@ -38,46 +55,30 @@ 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("climate.test_5678", const.HVAC_MODE_COOL, {})
|
||||
hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 17})
|
||||
expected_actions = [
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"type": "set_hvac_mode",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "climate.test_5678",
|
||||
},
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"type": "set_preset_mode",
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "climate.test_5678",
|
||||
},
|
||||
]
|
||||
actions = await async_get_device_automations(hass, "action", device_entry.id)
|
||||
assert_lists_same(actions, expected_actions)
|
||||
|
||||
|
||||
async def test_get_action_hvac_only(hass, device_reg, entity_reg):
|
||||
"""Test we get the expected actions 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,
|
||||
supported_features=features_reg,
|
||||
)
|
||||
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
|
||||
hass.states.async_set("climate.test_5678", const.HVAC_MODE_COOL, {})
|
||||
hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 1})
|
||||
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": "set_hvac_mode",
|
||||
"type": action,
|
||||
"device_id": device_entry.id,
|
||||
"entity_id": "climate.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)
|
||||
|
||||
|
@ -142,61 +143,153 @@ async def test_action(hass):
|
|||
assert len(set_preset_mode_calls) == 1
|
||||
|
||||
|
||||
async def test_capabilities(hass):
|
||||
@pytest.mark.parametrize(
|
||||
"set_state,capabilities_reg,capabilities_state,action,expected_capabilities",
|
||||
[
|
||||
(
|
||||
False,
|
||||
{const.ATTR_HVAC_MODES: [const.HVAC_MODE_COOL, const.HVAC_MODE_OFF]},
|
||||
{},
|
||||
"set_hvac_mode",
|
||||
[
|
||||
{
|
||||
"name": "hvac_mode",
|
||||
"options": [("cool", "cool"), ("off", "off")],
|
||||
"required": True,
|
||||
"type": "select",
|
||||
}
|
||||
],
|
||||
),
|
||||
(
|
||||
False,
|
||||
{const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY]},
|
||||
{},
|
||||
"set_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]},
|
||||
"set_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]},
|
||||
"set_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,
|
||||
action,
|
||||
expected_capabilities,
|
||||
):
|
||||
"""Test getting capabilities."""
|
||||
hass.states.async_set(
|
||||
"climate.entity",
|
||||
const.HVAC_MODE_COOL,
|
||||
{
|
||||
const.ATTR_HVAC_MODES: [const.HVAC_MODE_COOL, const.HVAC_MODE_OFF],
|
||||
const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY],
|
||||
},
|
||||
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,
|
||||
)
|
||||
|
||||
# Set HVAC mode
|
||||
capabilities = await device_action.async_get_action_capabilities(
|
||||
hass,
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"device_id": "abcdefgh",
|
||||
"entity_id": "climate.entity",
|
||||
"type": "set_hvac_mode",
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
"type": action,
|
||||
},
|
||||
)
|
||||
|
||||
assert capabilities and "extra_fields" in capabilities
|
||||
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
{
|
||||
"name": "hvac_mode",
|
||||
"options": [("cool", "cool"), ("off", "off")],
|
||||
"required": True,
|
||||
"type": "select",
|
||||
}
|
||||
]
|
||||
assert (
|
||||
voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
)
|
||||
== expected_capabilities
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"action,capability_name",
|
||||
[("set_hvac_mode", "hvac_mode"), ("set_preset_mode", "preset_mode")],
|
||||
)
|
||||
async def test_capabilities_mising_entity(
|
||||
hass, device_reg, entity_reg, action, capability_name
|
||||
):
|
||||
"""Test getting capabilities."""
|
||||
config_entry = MockConfigEntry(domain="test", data={})
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
# Set preset mode
|
||||
capabilities = await device_action.async_get_action_capabilities(
|
||||
hass,
|
||||
{
|
||||
"domain": DOMAIN,
|
||||
"device_id": "abcdefgh",
|
||||
"entity_id": "climate.entity",
|
||||
"type": "set_preset_mode",
|
||||
"entity_id": f"{DOMAIN}.test_5678",
|
||||
"type": action,
|
||||
},
|
||||
)
|
||||
|
||||
assert capabilities and "extra_fields" in capabilities
|
||||
|
||||
assert voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
) == [
|
||||
expected_capabilities = [
|
||||
{
|
||||
"name": "preset_mode",
|
||||
"options": [("home", "home"), ("away", "away")],
|
||||
"name": capability_name,
|
||||
"options": [],
|
||||
"required": True,
|
||||
"type": "select",
|
||||
}
|
||||
]
|
||||
|
||||
assert capabilities and "extra_fields" in capabilities
|
||||
|
||||
assert (
|
||||
voluptuous_serialize.convert(
|
||||
capabilities["extra_fields"], custom_serializer=cv.custom_serializer
|
||||
)
|
||||
== expected_capabilities
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue