mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Improve editing of device actions referencing non-added HVAC (#51706)
* Improve editing of device actions referencing non-added HVAC * Improve test coverage
This commit is contained in:
parent
49bec86dae
commit
ba6b527d61
@ -5,15 +5,15 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
ATTR_SUPPORTED_FEATURES,
|
|
||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
CONF_DOMAIN,
|
CONF_DOMAIN,
|
||||||
CONF_ENTITY_ID,
|
CONF_ENTITY_ID,
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Context, HomeAssistant
|
from homeassistant.core import Context, HomeAssistant, HomeAssistantError
|
||||||
from homeassistant.helpers import entity_registry
|
from homeassistant.helpers import entity_registry
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.entity import get_capability, get_supported_features
|
||||||
|
|
||||||
from . import DOMAIN, const
|
from . import DOMAIN, const
|
||||||
|
|
||||||
@ -48,11 +48,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
|||||||
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 HVAC and preset modes.
|
|
||||||
if state is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
base_action = {
|
base_action = {
|
||||||
CONF_DEVICE_ID: device_id,
|
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"})
|
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"})
|
actions.append({**base_action, CONF_TYPE: "set_preset_mode"})
|
||||||
|
|
||||||
return actions
|
return actions
|
||||||
@ -87,18 +83,26 @@ async def async_call_action_from_config(
|
|||||||
|
|
||||||
async def async_get_action_capabilities(hass, config):
|
async def async_get_action_capabilities(hass, config):
|
||||||
"""List action capabilities."""
|
"""List action capabilities."""
|
||||||
state = hass.states.get(config[CONF_ENTITY_ID])
|
|
||||||
action_type = config[CONF_TYPE]
|
action_type = config[CONF_TYPE]
|
||||||
|
|
||||||
fields = {}
|
fields = {}
|
||||||
|
|
||||||
if action_type == "set_hvac_mode":
|
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)
|
fields[vol.Required(const.ATTR_HVAC_MODE)] = vol.In(hvac_modes)
|
||||||
elif action_type == "set_preset_mode":
|
elif action_type == "set_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)
|
||||||
|
|
||||||
|
@ -93,6 +93,23 @@ def async_generate_entity_id(
|
|||||||
return test_string
|
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:
|
def get_device_class(hass: HomeAssistant, entity_id: str) -> str | None:
|
||||||
"""Get device class of an entity.
|
"""Get device class of an entity.
|
||||||
|
|
||||||
|
@ -30,7 +30,24 @@ def entity_reg(hass):
|
|||||||
return mock_registry(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."""
|
"""Test we get the expected actions 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)
|
||||||
@ -38,46 +55,30 @@ async def test_get_actions(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("climate.test_5678", const.HVAC_MODE_COOL, {})
|
DOMAIN,
|
||||||
hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 17})
|
"test",
|
||||||
expected_actions = [
|
"5678",
|
||||||
{
|
device_id=device_entry.id,
|
||||||
"domain": DOMAIN,
|
supported_features=features_reg,
|
||||||
"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)
|
if set_state:
|
||||||
hass.states.async_set("climate.test_5678", const.HVAC_MODE_COOL, {})
|
hass.states.async_set(
|
||||||
hass.states.async_set("climate.test_5678", "attributes", {"supported_features": 1})
|
f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state}
|
||||||
expected_actions = [
|
)
|
||||||
|
|
||||||
|
expected_actions = []
|
||||||
|
|
||||||
|
expected_actions += [
|
||||||
{
|
{
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "set_hvac_mode",
|
"type": action,
|
||||||
"device_id": device_entry.id,
|
"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)
|
actions = await async_get_device_automations(hass, "action", device_entry.id)
|
||||||
assert_lists_same(actions, expected_actions)
|
assert_lists_same(actions, expected_actions)
|
||||||
|
|
||||||
@ -142,61 +143,153 @@ async def test_action(hass):
|
|||||||
assert len(set_preset_mode_calls) == 1
|
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."""
|
"""Test getting capabilities."""
|
||||||
hass.states.async_set(
|
config_entry = MockConfigEntry(domain="test", data={})
|
||||||
"climate.entity",
|
config_entry.add_to_hass(hass)
|
||||||
const.HVAC_MODE_COOL,
|
device_entry = device_reg.async_get_or_create(
|
||||||
{
|
config_entry_id=config_entry.entry_id,
|
||||||
const.ATTR_HVAC_MODES: [const.HVAC_MODE_COOL, const.HVAC_MODE_OFF],
|
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
|
||||||
const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY],
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
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(
|
capabilities = await device_action.async_get_action_capabilities(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "abcdefgh",
|
"device_id": "abcdefgh",
|
||||||
"entity_id": "climate.entity",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
"type": "set_hvac_mode",
|
"type": action,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
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(
|
||||||
]
|
"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(
|
capabilities = await device_action.async_get_action_capabilities(
|
||||||
hass,
|
hass,
|
||||||
{
|
{
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"device_id": "abcdefgh",
|
"device_id": "abcdefgh",
|
||||||
"entity_id": "climate.entity",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
"type": "set_preset_mode",
|
"type": action,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
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…
x
Reference in New Issue
Block a user