8000 Update PIR triggered from sensor to binary sensor by alams154 · Pull Request #1490 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Update PIR triggered from sensor to binary sensor #1490

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alams154
Copy link

Binary sensor may better fit this entity since Home Assistant classifies motion into the following device class BinarySensorDeviceClass.MOTION

Copy link
codecov bot commented Jan 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.66%. Comparing base (e21ab90) to head (7905fde).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1490   +/-   ##
=======================================
  Coverage   92.66%   92.66%           
=======================================
  Files         150      150           
  Lines        9538     9538           
  Branches      974      974           
=======================================
  Hits         8838     8838           
  Misses        499      499           
  Partials      201      201           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rytilahti rytilahti added enhancement New feature or request breaking change Breaking change and removed breaking change Breaking change labels Jan 27, 2025
Copy link
Member
@rytilahti rytilahti left a comment

Choose a reason for hiding this comment

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

Sounds indeed reasonable, thanks for the PR @alams154! I marked this initially as a breaking change, but removed the label as 1) we were already returning a boolean and 2) this was not ever exposed in homeassistant users, where we would have an issue.

@ryenitcher what do you think?

@sdb9696
Copy link
Collaborator
sdb9696 commented Jan 27, 2025

If we’re thinking about HA this should really be an event entity rather than a binary sensor.

@alams154
Copy link
Author

If we’re thinking about HA this should really be an event entity rather than a binary sensor.

Will this be a change in the type or require a change somewhere else? My motivation for this change was to add a corresponding change in binary_sensor.py so we could see the motion detection in HA using a KS200M.

Right now I'm using the Kasa app for motion detection and migrating all the functionality of the switch to HA would be awesome.

@sdb9696
Copy link
Collaborator
sdb9696 commented Jan 28, 2025

See this HA architecture discussion regarding this. It seems the question in HA is around whether the duration of motion is known or not to determine whether something should be an event or a binary_sensor. Quite how we would model an event-like feature in python-kasa I'm not too sure at the moment but probably requires registering some kind of callback via the feature.

@alams154
Copy link
Author

Probably best to close the PR then?

@sdb9696
Copy link
Collaborator
sdb9696 commented Jan 28, 2025

Maybe let's mark it as draft until we've figured out the best api for events.

@alams154 alams154 marked this pull request as draft January 28, 2025 15:00
@ryenitcher
Copy link
Contributor

Sounds indeed reasonable, thanks for the PR @alams154! I marked this initially as a breaking change, but removed the label as 1) we were already returning a boolean and 2) this was not ever exposed in homeassistant users, where we would have an issue.

@ryenitcher what do you think?

I’m not certain I know enough about HA to have any valid input on this. However, I will note that the PIR state reading feature works via low rate polling of the switch, so it’s not exactly event driven. So it’s not reporting an event, per se, but rather a snapshot of a state that may, by chance, contain an event.

@rytilahti
Copy link
Member

As we are polling currently every 5s, and based on my understanding that this state stays keeps reported for a while, I think we should merge this and worry about events when we are ready for that.

This way users can already use this, or what so you think, @sdb9696?

@sdb9696
Copy link
Collaborator
sdb9696 commented Jan 28, 2025

I think we should wait a bit until we’ve thrashed out the event api a bit more. We should incorporate this into that discussion, but I’d envisaged the motion query running automatically every second once a listener is started. That’s thinking based around the iot pir detection which has its own update method and similarly for get_trigger_logs.

@ryenitcher
Copy link
Contributor

As we are polling currently every 5s, and based on my understanding that this state stays keeps reported for a while, I think we should merge this and worry about events when we are ready for that.

For the two Kasa motion switches I have access to, the ADC value reported by the API is not latching, and reports real-time values only. For continuous motion events, low rate polling will likely pick up the event, so long as the that motion triggering the event is generated continuously. However, for short lived motion events, low rate polling will more oft than not miss the event entirely. Further, it is worth noting that there is no mechanism exposed within the API itself to distinguish a long motion event from multiple short motion events in close temporal proximity.

@alams154
Copy link
Author

I think we should wait a bit until we’ve thrashed out the event api a bit more. We should incorporate this into that discussion, but I’d envisaged the motion query running automatically every second once a listener is started. That’s thinking based around the iot pir detection which has its own update method and similarly for get_trigger_logs.

Is there an issue somewhere that has this discussion? I'd like to follow any updates on that discussion and if there's anything I could do to help.

Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added stale and removed stale labels May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0