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

Allow one timing to cancel others #6744

Conversation

lbilger
Copy link
Contributor

@lbilger lbilger commented May 14, 2024

What does this implement/fix?

See feature request #985

Adds a method cancel´ to MultiClickTrigger` so that one trigger can conditionally cancel others when it fires.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): (partially) fixes esphome/feature-requests#985

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

# ...
binary_sensor:
  - platform: gpio
    name: Momentary Switch
    id: sensor_momentary_switch
    pin:
      number: GPIO14
      mode: INPUT
    internal: false
    on_multi_click:
      # Single click, light is on -> wait to make sure it's not a long press / double click, then turn off
      - timing:
          - ON for 50ms to 300ms
          - OFF for at least 500ms
        trigger_id: sensor_momentary_switch_multi_click_trigger_short_press_when_off
        then:
          - if:
              condition:
                - light.is_on: dimmer
              then:
                - light.turn_off: dimmer
      # Single click, light is off -> turn on immediately, don't wait for long press / double click
      - timing:
          - ON for 50ms to 300ms
        then:
          - if:
              condition:
                - light.is_off: dimmer
              then:
                - lambda: |-
                    id(sensor_momentary_switch_multi_click_trigger_short_press_when_off).cancel();
                - light.turn_on:
                    id: dimmer
                    brightness: !lambda |-
                      return id(dimmer).remote_values.get_brightness();

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).
    I did not find any tests for the multi-click trigger. As I'm new to the codebase, I am not sure how I would go about testing this.
    If user exposed functionality or configuration variables are added/changed:
  • Documentation added/updated in esphome-docs.
    Please give me an indication if this PR is likely to be merged, then I will write some docs.

@probot-esphome
Copy link

Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (binary_sensor) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.14%. Comparing base (4d8b5ed) to head (62665ea).
Report is 588 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6744      +/-   ##
==========================================
+ Coverage   53.70%   54.14%   +0.43%     
==========================================
  Files          50       50              
  Lines        9408     9594     +186     
  Branches     1654     1691      +37     
==========================================
+ Hits         5053     5195     +142     
- Misses       4056     4073      +17     
- Partials      299      326      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jesserockz jesserockz merged commit 8952719 into esphome:dev May 15, 2024
66 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

on_multi_click timings: allow one timing to cancel others
3 participants