Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Extend smartcam detection support #1552

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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

gpongelli
Copy link

New detection added with tests:

  • bark detection
  • glass detection
  • line crossing detection
  • meow detection
  • vehicle detection

those module should be ready for home assistant, a change is needed into core repo adding more entity like this one .

closes #1551

Copy link

codecov bot commented Jun 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.74%. Comparing base (e21ab90) to head (b695653).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1552      +/-   ##
==========================================
+ Coverage   92.66%   92.74%   +0.08%     
==========================================
  Files         150      155       +5     
  Lines        9538     9648     +110     
  Branches      974      974              
==========================================
+ Hits         8838     8948     +110     
  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.

This avoid typos when defining entity keys in Home Assistant that must
be same as Feature ID.
@gpongelli gpongelli marked this pull request as draft June 17, 2025 11:43
@gpongelli gpongelli marked this pull request as ready for review June 17, 2025 11:55
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.

First of all, thanks for the PR and looking into this, I'm sure many will find this useful in the future! Just a quick initial review, with a couple of points:

  • I think we should generalize the togglables/enablables under a "TogglableFeature" or something similar to avoid copy&pasting boilerplate for both the implementation and the tests. We could also leverage this for other features in the future.
  • This PR should be kept as small as possible, so please avoid adding the feature identifier class as a part of this. We can discuss having such later, but it's better to keep PRs contained and clean for the future maintainers' sake.

kasa/feature.py Outdated
@@ -81,6 +81,44 @@
_LOGGER = logging.getLogger(__name__)


class FeatureIdentifier(StrEnum):
Copy link
Member

Choose a reason for hiding this comment

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

Let's not add this, at least not yet, to keep the PR as small as possible.

Copy link
Author

Choose a reason for hiding this comment

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

ok, I've added it after looking at home assistant code, where I've found that feature's id must be the same as the home assistant entity's key, and this can be easily mistyped.

I could revert those changes and create a different PR after this will be merged.

Copy link
Author

Choose a reason for hiding this comment

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

reverted

_LOGGER = logging.getLogger(__name__)


class BarkDetection(SmartCamModule):
Copy link
Member

Choose a reason for hiding this comment

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

Considering all detection modules follow the same format, only with a different section and method names, I'm wondering if we could generalize this to avoid boilerplate? Maybe the generic detection module could read the available components and create the entities, having an enum defining the available detection types that could also be passed to a generic "set_enabled".

This will allow keeping the codebase easier to maintain, and also the tests much simpler.

Copy link
Author

Choose a reason for hiding this comment

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

I'll work on this

Copy link
Author

Choose a reason for hiding this comment

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

refactored with a common class for all the detections

@@ -506,6 +506,22 @@
}
}
},
"getLinecrossingDetectionConfig": {
Copy link
Member

Choose a reason for hiding this comment

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

Do you have all these devices that have new fixtures including this information? If yes, I would suggest only including one fixture update alongside the dump_devinfo updates in this initial PR, and creating a new PR to update the rest.

Copy link
Author

Choose a reason for hiding this comment

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

no I haven't, I've put those blocks to have test passed.

code looks at this dict when fixture has

                {
                    "name": "linecrossingDetection",
                    "version": 2
                },

let me know if you prefer having failure tests or not.

Copy link
Author

Choose a reason for hiding this comment

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

as example of why I did this change, with commit add LineCrossingDetectionRegion on dump_devinfo I've added a new section about linedetection region and then dumped the C220 webcam I have: the tests now fail for other webcams that have declared the linecrossingDetection feature but they have no "region_info" block.

To fix this, there are two ways:

  1. remove the change on devtools/helpers/smartcamrequests.py
  2. add missing block on all the fixture files

let me know which is the way to do, thanks.

Copy link
Member

@rytilahti rytilahti Jun 20, 2025

Choose a reason for hiding this comment

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

There is a dictionary in one of the fake protocol implementation files containing dummy payloads where none is available, that's the place to add one so that the fixtures remain intact. I'll try to review other changes now on the train or latest later this weekend :)

edit: https://github.com/python-kasa/python-kasa/blob/master/tests%2Ffakeprotocol_smart.py#L91

Copy link
Author

Choose a reason for hiding this comment

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

There is a dictionary in one of the fake protocol implementation files containing dummy payloads where none is available, that's the place to add one so that the fixtures remain intact. I'll try to review other changes now on the train or latest later this weekend :)

edit: https://github.com/python-kasa/python-kasa/blob/master/tests%2Ffakeprotocol_smart.py#L91

tryied to use that file, and also fakeprotocol_smartcam, but none working as you can see by all the failing pipeline.

@gpongelli gpongelli requested a review from rytilahti June 20, 2025 14:46
@@ -0,0 +1,45 @@
"""Tests for smartcam bark detection module."""
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should also parameterize the tests to avoid code duplication for these similar modules? Just an idea 🤔

Copy link
Author

Choose a reason for hiding this comment

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

reverted to multiple SmartCamModule with repeated code, it's the only version that passes tests.

I've tried parametrize_combine but I cannot get it to work; same end also using @pytest.mark.parametrize .

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the late response, but let's rather fix the tests to avoid code duplication to keep it more maintainable.
If you want to bring your generalized revision back to this PR, I will try to find some time soon to look into fixing the tests.

Copy link
Author

Choose a reason for hiding this comment

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

hi @rytilahti , sorry for this late response, I've pushed the branch with detectionmodule common class, all the detections inherits from it.

I've tried some test changes but I've no idea actually on how to fix it, thanks.

@gpongelli gpongelli requested a review from rytilahti June 23, 2025 08:29
@rytilahti rytilahti changed the title Add more detections Extend smartcam detection support Jul 3, 2025
@rytilahti rytilahti added the enhancement New feature or request label Jul 3, 2025
@gpongelli gpongelli force-pushed the linecrossing_detection branch from b695653 to 745342a Compare July 14, 2025 07:14
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.

[Feature Request] C220 camera line crossing detection on-off switch
2 participants