-
- Notifications
You must be signed in to change notification settings - Fork 36.1k
Add button pressed trigger #158745
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
base: dev
Are you sure you want to change the base?
Add button pressed trigger #158745
Conversation
| Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
| Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
| Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
| def is_valid_transition(self, from_state: State, to_state: State) -> bool: | ||
| """Check if the origin state is not an expected target states.""" | ||
| return not self.is_valid_state(from_state) | ||
| if from_state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Button triggers need to trigger when coming from unknown, otherwise they would not trigger the first time they get pressed, so this check needs to be moved from state_change_listener() to here. That way, subclasses can override it.
I've also changed the not self.is_valid_state(from_state) to just compare the old and new states, and implemented the attribute diff on the attribute trigger subclass. Calling not is_valid_state here is confusing and may make it harder to implement is_valid_state for new triggers in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new "button pressed" trigger for automations, allowing users to trigger automations when button entities are pressed. The trigger is gated behind the experimental "New triggers and conditions" feature flag in Home Assistant Labs.
Key changes:
- Implements
ButtonPressedTriggerclass that detects when a button entity's state changes (representing a press) - Refactors base trigger classes to support the new trigger type with appropriate state transition handling
- Adds comprehensive test coverage including edge cases for unavailable/unknown states
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
homeassistant/components/button/trigger.py | New trigger implementation that handles button press detection with special logic for STATE_UNKNOWN transitions |
homeassistant/components/button/triggers.yaml | Defines the "pressed" trigger configuration schema |
homeassistant/components/button/strings.json | Adds user-facing name and description for the button pressed trigger |
homeassistant/components/button/icons.json | Specifies icon for the button pressed trigger in UI |
homeassistant/components/automation/__init__.py | Registers button domain in the list of domains with triggers |
homeassistant/helpers/trigger.py | Refactors base EntityTriggerBase to consolidate state transition validation and adds filtering for UNAVAILABLE/UNKNOWN states |
homeassistant/components/text/trigger.py | Removes redundant is_valid_transition override now handled by base class |
tests/components/button/test_trigger.py | Comprehensive test suite covering labs flag gating, various state transitions, and target selector functionality |
Proposed change
SSIA.
Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: