8000 Fix Raspi GPIO binary_sensor produces unreliable responses by mburget · Pull Request #48170 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix Raspi GPIO binary_sensor produces unreliable responses #48170

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

Merged
merged 3 commits into from
Apr 4, 2021

Conversation

mburget
Copy link
Contributor
@mburget mburget commented Mar 20, 2021

Home Assistant's rpi_gpio binary sensor captures the pin state directly in the first egde interrupt.
At first glance, it looks logical and, on a real-time platform like Arduino, it would work properly.
On RPi/Linux, however, the timing is unpredictable, so that it might (actually, it often does) happen
that the signal bounces back before the edge handler has been called, and the handler captures a wrong pin
state. To fix it, we just wait until the signal stabilizes itself ("bouncetime" parameter) and capture
the pin state after that.

The changes are taken over from previous PR#31788 + proposal from balloob in discussion which was somehow never finished

Breaking change

Proposed change

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

no changes in configuration.yaml

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

mburget added 2 commits March 20, 2021 22:50
…unreliable responses ("Doorbell Scenario")

Changes overtaken from PR#31788 which was somehow never finished
…unreliable response. Changes taken over from PR31788 which was somehow never finished

rpi_gpio.edge_detect(self._port, read_gpio, self._bouncetime)
rpi_gpio.edge_detect(self._port, edge_detected, self._bouncetime)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory bounce time already processed.
Wouldn't it be better to add two separate callbacks for raising and failing edge?
In that case, you don't need to read the value.

def edge_detect(port, event_callback, bounce):
    """Add detection for RISING and FALLING events."""
    GPIO.add_event_detect(port, GPIO.BOTH, callback=event_callback, bouncetime=bounce)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi sanyatuning,
thanks for reviewing. As you can read out of the long thread in the related 10498 issue this solution is perhaps not the perfect one because still based on RPi.GPIO, gpiozero seems to be better.
I agree that reading the value could be avoided, but this add more reliability at low cost. Some of the users in the issue thread are reporting missed edged and in this case reading out the value seems to be the only workaround.
At least this solution is tested for month by many users (including me) in a custom component and it works way better then the current implementation on master.
So I would like to keep it like it is.

Copy link
Member
@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@MartinHjelmare MartinHjelmare merged commit ecec3c8 into home-assistant:dev Apr 4, 2021
@MartinHjelmare MartinHjelmare added this to the 2021.4.0 milestone Apr 4, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
P 4E37 rojects
None yet
Development

Successfully merging this pull request may close these issues.

Raspi GPIO binary_sensor produces unreliable responses ("Doorbell Scenario")
5 participants
0