mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Improve editing of device actions referencing non-added lock (#51750)
This commit is contained in:
parent
c242e56b8c
commit
6ab37881c9
@ -5,7 +5,6 @@ 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,
|
||||||
@ -17,6 +16,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import Context, HomeAssistant
|
from homeassistant.core import Context, HomeAssistant
|
||||||
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_supported_features
|
||||||
|
|
||||||
from . import DOMAIN, SUPPORT_OPEN
|
from . import DOMAIN, SUPPORT_OPEN
|
||||||
|
|
||||||
@ -40,6 +40,8 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
|||||||
if entry.domain != DOMAIN:
|
if entry.domain != DOMAIN:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
supported_features = get_supported_features(hass, entry.entity_id)
|
||||||
|
|
||||||
# Add actions for each entity that belongs to this integration
|
# Add actions for each entity that belongs to this integration
|
||||||
base_action = {
|
base_action = {
|
||||||
CONF_DEVICE_ID: device_id,
|
CONF_DEVICE_ID: device_id,
|
||||||
@ -50,10 +52,7 @@ async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
|
|||||||
actions.append({**base_action, CONF_TYPE: "lock"})
|
actions.append({**base_action, CONF_TYPE: "lock"})
|
||||||
actions.append({**base_action, CONF_TYPE: "unlock"})
|
actions.append({**base_action, CONF_TYPE: "unlock"})
|
||||||
|
|
||||||
state = hass.states.get(entry.entity_id)
|
if supported_features & (SUPPORT_OPEN):
|
||||||
if state:
|
|
||||||
features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
|
|
||||||
if features & (SUPPORT_OPEN):
|
|
||||||
actions.append({**base_action, CONF_TYPE: "open"})
|
actions.append({**base_action, CONF_TYPE: "open"})
|
||||||
|
|
||||||
return actions
|
return actions
|
||||||
|
@ -2,8 +2,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import homeassistant.components.automation as automation
|
import homeassistant.components.automation as automation
|
||||||
from homeassistant.components.lock import DOMAIN
|
from homeassistant.components.lock import DOMAIN, SUPPORT_OPEN
|
||||||
from homeassistant.const import CONF_PLATFORM
|
|
||||||
from homeassistant.helpers import device_registry
|
from homeassistant.helpers import device_registry
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
@ -30,15 +29,25 @@ def entity_reg(hass):
|
|||||||
return mock_registry(hass)
|
return mock_registry(hass)
|
||||||
|
|
||||||
|
|
||||||
async def test_get_actions_support_open(
|
@pytest.mark.parametrize(
|
||||||
hass, device_reg, entity_reg, enable_custom_integrations
|
"set_state,features_reg,features_state,expected_action_types",
|
||||||
|
[
|
||||||
|
(False, 0, 0, []),
|
||||||
|
(False, SUPPORT_OPEN, 0, ["open"]),
|
||||||
|
(True, 0, 0, []),
|
||||||
|
(True, 0, SUPPORT_OPEN, ["open"]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
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 lock which supports open."""
|
"""Test we get the expected actions from a lock."""
|
||||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
|
||||||
platform.init()
|
|
||||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
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(
|
||||||
@ -48,69 +57,33 @@ async def test_get_actions_support_open(
|
|||||||
entity_reg.async_get_or_create(
|
entity_reg.async_get_or_create(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
"test",
|
"test",
|
||||||
platform.ENTITIES["support_open"].unique_id,
|
"5678",
|
||||||
device_id=device_entry.id,
|
device_id=device_entry.id,
|
||||||
|
supported_features=features_reg,
|
||||||
)
|
)
|
||||||
|
if set_state:
|
||||||
expected_actions = [
|
hass.states.async_set(
|
||||||
|
f"{DOMAIN}.test_5678", "attributes", {"supported_features": features_state}
|
||||||
|
)
|
||||||
|
expected_actions = []
|
||||||
|
basic_action_types = ["lock", "unlock"]
|
||||||
|
expected_actions += [
|
||||||
{
|
{
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "lock",
|
"type": action,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": "lock.support_open_lock",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
},
|
}
|
||||||
{
|
for action in basic_action_types
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "unlock",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": "lock.support_open_lock",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "open",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": "lock.support_open_lock",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
actions = await async_get_device_automations(hass, "action", device_entry.id)
|
expected_actions += [
|
||||||
assert_lists_same(actions, expected_actions)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_get_actions_not_support_open(
|
|
||||||
hass, device_reg, entity_reg, enable_custom_integrations
|
|
||||||
):
|
|
||||||
"""Test we get the expected actions from a lock which doesn't support open."""
|
|
||||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
|
||||||
platform.init()
|
|
||||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
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",
|
|
||||||
platform.ENTITIES["no_support_open"].unique_id,
|
|
||||||
device_id=device_entry.id,
|
|
||||||
)
|
|
||||||
|
|
||||||
expected_actions = [
|
|
||||||
{
|
{
|
||||||
"domain": DOMAIN,
|
"domain": DOMAIN,
|
||||||
"type": "lock",
|
"type": action,
|
||||||
"device_id": device_entry.id,
|
"device_id": device_entry.id,
|
||||||
"entity_id": "lock.no_support_open_lock",
|
"entity_id": f"{DOMAIN}.test_5678",
|
||||||
},
|
}
|
||||||
{
|
for action in expected_action_types
|
||||||
"domain": DOMAIN,
|
|
||||||
"type": "unlock",
|
|
||||||
"device_id": device_entry.id,
|
|
||||||
"entity_id": "lock.no_support_open_lock",
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user