Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use entity registry id in button device actions #95267

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions homeassistant/components/button/device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import voluptuous as vol

from homeassistant.components.device_automation import async_validate_entity_schema
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DEVICE_ID,
Expand All @@ -19,14 +20,21 @@

ACTION_TYPES = {"press"}

ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
_ACTION_SCHEMA = cv.DEVICE_ACTION_BASE_SCHEMA.extend(
{
vol.Required(CONF_TYPE): vol.In(ACTION_TYPES),
vol.Required(CONF_ENTITY_ID): cv.entity_domain(DOMAIN),
vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid,
}
)


async def async_validate_action_config(
hass: HomeAssistant, config: ConfigType
) -> ConfigType:
"""Validate config."""
return async_validate_entity_schema(hass, config, _ACTION_SCHEMA)


async def async_get_actions(
hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
Expand All @@ -36,7 +44,7 @@ async def async_get_actions(
{
CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id,
CONF_ENTITY_ID: entry.id,
CONF_TYPE: "press",
}
for entry in er.async_entries_for_device(registry, device_id)
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/device_automation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

ENTITY_PLATFORMS = {
Platform.ALARM_CONTROL_PANEL.value,
Platform.BUTTON.value,
Platform.FAN.value,
Platform.HUMIDIFIER.value,
Platform.LIGHT.value,
Expand Down
53 changes: 46 additions & 7 deletions tests/components/button/test_device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ async def test_get_actions(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_registry.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
DOMAIN, "test", "5678", device_id=device_entry.id
)
expected_actions = [
{
"domain": DOMAIN,
"type": "press",
"device_id": device_entry.id,
"entity_id": "button.test_5678",
"entity_id": entity_entry.id,
"metadata": {"secondary": False},
}
]
Expand Down Expand Up @@ -70,7 +70,7 @@ async def test_get_actions_hidden_auxiliary(
config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_registry.async_get_or_create(
entity_entry = entity_registry.async_get_or_create(
DOMAIN,
"test",
"5678",
Expand All @@ -84,7 +84,7 @@ async def test_get_actions_hidden_auxiliary(
"domain": DOMAIN,
"type": action,
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
"entity_id": entity_entry.id,
"metadata": {"secondary": True},
}
for action in ["press"]
Expand All @@ -95,8 +95,47 @@ async def test_get_actions_hidden_auxiliary(
assert actions == unordered(expected_actions)


async def test_action(hass: HomeAssistant) -> None:
async def test_action(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None:
"""Test for press action."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")

assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "event",
"event_type": "test_event",
},
"action": {
"domain": DOMAIN,
"device_id": "abcdefgh",
"entity_id": entry.id,
"type": "press",
},
},
]
},
)

press_calls = async_mock_service(hass, DOMAIN, "press")

hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert len(press_calls) == 1
assert press_calls[0].domain == DOMAIN
assert press_calls[0].service == "press"
assert press_calls[0].data == {"entity_id": entry.entity_id}


async def test_action_legacy(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test for press action."""
entry = entity_registry.async_get_or_create(DOMAIN, "test", "5678")

assert await async_setup_component(
hass,
automation.DOMAIN,
Expand All @@ -110,7 +149,7 @@ async def test_action(hass: HomeAssistant) -> None:
"action": {
"domain": DOMAIN,
"device_id": "abcdefgh",
"entity_id": "button.entity",
"entity_id": entry.entity_id,
"type": "press",
},
},
Expand All @@ -125,4 +164,4 @@ async def test_action(hass: HomeAssistant) -> None:
assert len(press_calls) == 1
assert press_calls[0].domain == DOMAIN
assert press_calls[0].service == "press"
assert press_calls[0].data == {"entity_id": "button.entity"}
assert press_calls[0].data == {"entity_id": entry.entity_id}
3 changes: 2 additions & 1 deletion tests/components/zha/test_device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ async def test_get_inovelli_actions(hass: HomeAssistant, device_inovelli) -> Non
{(DOMAIN, inovelli_ieee_address)}
)
ha_entity_registry = er.async_get(hass)
inovelli_button = ha_entity_registry.async_get("button.inovelli_vzm31_sn_identify")
inovelli_light = ha_entity_registry.async_get("light.inovelli_vzm31_sn_light")

actions = await async_get_device_automations(
Expand All @@ -187,7 +188,7 @@ async def test_get_inovelli_actions(hass: HomeAssistant, device_inovelli) -> Non
{
"device_id": inovelli_reg_device.id,
"domain": Platform.BUTTON,
"entity_id": "button.inovelli_vzm31_sn_identify",
"entity_id": inovelli_button.id,
"metadata": {"secondary": True},
"type": "press",
},
Expand Down