Add input_button support to button.pressed trigger

This commit is contained in:
abmantis
2026-07-31 17:43:32 +01:00
parent df47a16689
commit bee5f34f42
3 changed files with 30 additions and 8 deletions
@@ -9,6 +9,7 @@ from homeassistant.components.event import (
EventDeviceClass,
EventEntityStateAttribute,
)
from homeassistant.components.input_button import DOMAIN as INPUT_BUTTON_DOMAIN
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.automation import DomainSpec
from homeassistant.helpers.trigger import (
@@ -30,6 +31,7 @@ class ButtonPressedTrigger(StatelessEntityTriggerBase):
_domain_specs = {
DOMAIN: DomainSpec(),
EVENT_DOMAIN: DomainSpec(device_class=EventDeviceClass.BUTTON),
INPUT_BUTTON_DOMAIN: DomainSpec(),
}
@override
@@ -7,6 +7,7 @@ pressed:
target:
entity:
- domain: button
- domain: input_button
- domain: event
device_class: button
double_pressed:
+27 -8
View File
@@ -44,9 +44,11 @@ def _button_event_state(
@pytest.fixture
async def target_buttons(hass: HomeAssistant) -> dict[str, list[str]]:
"""Create multiple button entities associated with different targets."""
return await target_entities(hass, "button")
async def target_entities_indirect(
request: pytest.FixtureRequest, hass: HomeAssistant
) -> dict[str, list[str]]:
"""Create multiple entities associated with different targets."""
return await target_entities(hass, request.param)
@pytest.fixture
@@ -82,8 +84,25 @@ async def test_button_trigger_options_validation(
@pytest.mark.parametrize(
("trigger_target_config", "entity_id", "entities_in_target"),
parametrize_target_entities("button"),
(
"target_entities_indirect",
"trigger_target_config",
"entity_id",
"entities_in_target",
),
[
*[
("button", config, entity_id, entities)
for (config, entity_id, entities) in parametrize_target_entities("button")
],
*[
("input_button", config, entity_id, entities)
for (config, entity_id, entities) in parametrize_target_entities(
"input_button"
)
],
],
indirect=["target_entities_indirect"],
)
@pytest.mark.parametrize(
("trigger", "states"),
@@ -198,7 +217,7 @@ async def test_button_trigger_options_validation(
)
async def test_button_state_trigger(
hass: HomeAssistant,
target_buttons: dict[str, list[str]],
target_entities_indirect: dict[str, list[str]],
trigger_target_config: dict,
entity_id: str,
entities_in_target: int,
@@ -207,10 +226,10 @@ async def test_button_state_trigger(
) -> None:
"""Test that the button state trigger fires when targeted button state changes."""
calls: list[str] = []
other_entity_ids = set(target_buttons["included_entities"]) - {entity_id}
other_entity_ids = set(target_entities_indirect["included_entities"]) - {entity_id}
# Set all buttons, including the tested button, to the initial state
for eid in target_buttons["included_entities"]:
for eid in target_entities_indirect["included_entities"]:
set_or_remove_state(hass, eid, states[0]["included_state"])
await hass.async_block_till_done()