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

Add traffic rules to library #409

Merged
merged 25 commits into from
Oct 10, 2023
Merged

Add traffic rules to library #409

merged 25 commits into from
Oct 10, 2023

Conversation

herriejr
Copy link
Contributor

With these changes, it is possible to control the trafficrules, defined in the unifi network management.
At this moment you can enable and disable a selected traffic rule.
(pls bear with me, since this is my 1st pull request)

@Kane610
Copy link
Owner

Kane610 commented Jul 25, 2023

Hello! Thank you for your PR, I started doing a review but got lost in other stuff, I will get back to you shortly!

Copy link
Owner

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Its a good start, but some improvements are needed and of course tests.

aiounifi/interfaces/api_handlers.py Outdated Show resolved Hide resolved
aiounifi/controller.py Outdated Show resolved Hide resolved
aiounifi/models/api.py Outdated Show resolved Hide resolved
aiounifi/interfaces/trafficrules.py Outdated Show resolved Hide resolved
aiounifi/models/api.py Outdated Show resolved Hide resolved
aiounifi/models/trafficrule.py Outdated Show resolved Hide resolved
@herriejr
Copy link
Contributor Author

I was working on some other projects and was on holiday. I will start implementing the requested changes and start adding the tests.

@Kane610
Copy link
Owner

Kane610 commented Aug 22, 2023

I recommend starting with doing a rebase as I've refactored some aspects lg the library

@herriejr
Copy link
Contributor Author

herriejr commented Aug 22, 2023

Ok, I started with a rebase and I'm not completely done yet (still some cleaning up to do of the code and I still need to squeeze an "update" of the traffic rules in the code since the Unifi console does not send messages about these changes).
But the rebase solved the problem for which I added the 'v2-bool' in the first place.
I think I did something wrong, since I accidentally closed this pull request 😇. I will try to do the missing modifications in the next few days and add the tests in the following weeks.
I will also create a new pull request in the next few days.

@Kane610
Copy link
Owner

Kane610 commented Aug 23, 2023

Take your time!

@herriejr herriejr reopened this Aug 26, 2023
@herriejr
Copy link
Contributor Author

I made the requested modifications, and based it on the new structure.
I had to change a line in the api_handlers.py-file, since the response of this API is different (does not contain 'meta' nor 'data'-keys). I also added a few tests for the traffic rules to the tests.

Copy link
Owner

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

You would be helped setting up the dev env in a python3.11 venv and using the pre-commit tool. You can run the setup.sh script to do that, there are a lot of small things which would be caught by that.

aiounifi/controller.py Outdated Show resolved Hide resolved
aiounifi/controller.py Outdated Show resolved Hide resolved
aiounifi/models/traffic_rule.py Outdated Show resolved Hide resolved
aiounifi/interfaces/traffic_rules.py Outdated Show resolved Hide resolved
aiounifi/interfaces/traffic_rules.py Outdated Show resolved Hide resolved
aiounifi/models/traffic_rule.py Outdated Show resolved Hide resolved
aiounifi/models/traffic_rule.py Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
tests/test_traffic_rules.py Outdated Show resolved Hide resolved
@Kane610
Copy link
Owner

Kane610 commented Aug 26, 2023

I made the requested modifications, and based it on the new structure.
I had to change a line in the api_handlers.py-file, since the response of this API is different (does not contain 'meta' nor 'data'-keys). I also added a few tests for the traffic rules to the tests.

Yes we need to figure out a way to specify different expected data structures from different api endpoints. Maybe have some transform in the ApiRequest class

@Kane610
Copy link
Owner

Kane610 commented Aug 27, 2023

Good progress! I will be away a few days now so gonna be hard to get this in for 2023.9 release, but only a few things left to resolve

@herriejr
Copy link
Contributor Author

Did it again, mistakenly closed the PR

@herriejr herriejr reopened this Aug 28, 2023
Copy link
Owner

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Just some small comments to move you along :)

aiounifi/interfaces/traffic_rules.py Outdated Show resolved Hide resolved
aiounifi/interfaces/api_handlers.py Outdated Show resolved Hide resolved
aiounifi/models/api.py Outdated Show resolved Hide resolved
@herriejr
Copy link
Contributor Author

I hope with the latest commits I am almost there

@herriejr
Copy link
Contributor Author

I've made some modifications to the prepare_data method I introduced.
I've given that method some more responsibility:

  • parse the received response with orjson
  • inspect the result and depending on the structure of the result, put the data in the correct way in a TypedApiResponse-object to use throughout the library

Maybe it is more logical to place this elsewhere, but now the complexity of parsing different response structures is already concentrated in one place.
I still have to solve the problem I'm having when adding traffic_rule_payload to _mock_endpoints when running the tests; since I keep getting this error:
'ERROR tests/test_traffic_rules.py - Failed: In test_traffic_rules: function uses no argument 'traffic_rule_payload'

aiounifi/models/api.py Outdated Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
@Kane610
Copy link
Owner

Kane610 commented Aug 31, 2023

I only had a few minutes tonight, will look more tomorrow.

Copy link
Owner

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Even closer! Some things left. You shouldn't really need to change any existing test if we do this properly.

aiounifi/interfaces/api_handlers.py Outdated Show resolved Hide resolved
aiounifi/interfaces/connectivity.py Outdated Show resolved Hide resolved
aiounifi/interfaces/traffic_rules.py Outdated Show resolved Hide resolved
aiounifi/models/api.py Outdated Show resolved Hide resolved
aiounifi/models/traffic_rule.py Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
Copy link
Owner

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

I think we're getting there! Try to take a step back and simplify what we've discussed and I think it's ready. I will need to rework a bit about where data response is parsed and error handling and stuff, it clearly doesn't work well with multiple data structures... But lets keep that out of this PR as much as possibly.

@herriejr
Copy link
Contributor Author

herriejr commented Sep 7, 2023

I have not much time in the coming days, but I will take a closer look at everything this weekend.

@Kane610
Copy link
Owner

Kane610 commented Sep 9, 2023

I have not much time in the coming days, but I will take a closer look at everything this weekend.

Donit at your pace. I'm also away

@Kane610
Copy link
Owner

Kane610 commented Sep 27, 2023

What happened?

@herriejr
Copy link
Contributor Author

Good job! You're getting it into fantastic shape

Looks like you accidentally removed the test_outlet and test_port files

I reverted those changes back, was a bit unfortunate of me.

Copy link
Owner

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

I like your improvements it fits well in with the overall structure.

Only more would be to handle response differences in the response rather than request class, but that's more work and let's keep out of it.

aiounifi/interfaces/api_handlers.py Outdated Show resolved Hide resolved
aiounifi/models/api.py Outdated Show resolved Hide resolved
aiounifi/models/traffic_rule.py Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
aiounifi/models/api.py Outdated Show resolved Hide resolved
tests/test_ports.py Outdated Show resolved Hide resolved
tests/test_outlets.py Outdated Show resolved Hide resolved
aiounifi/models/api.py Outdated Show resolved Hide resolved
herriejr and others added 4 commits October 3, 2023 21:10
Remove unnecessary import

Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com>
Remove unnecessary import

Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com>
Remove unnecessary import

Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com>
@Kane610
Copy link
Owner

Kane610 commented Oct 3, 2023

I glanced from the phone and it looks really good. Will review tomorrow

Copy link
Owner

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Im very happy with how this turned out. You did a great job!

@Kane610
Copy link
Owner

Kane610 commented Oct 5, 2023

I saw one thing that warrants a fix, test coverage

aiounifi/interfaces/traffic_rules.py 18 6 67% 24, 28, 32-37

Copy link
Owner

@Kane610 Kane610 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! 🎉

Anything more you want to do here or are you ready to get this merged?

@herriejr
Copy link
Contributor Author

I am ready to get this merged. Thank you for your time and effort for guiding me.

@Kane610
Copy link
Owner

Kane610 commented Oct 10, 2023

Really good job!

@Kane610 Kane610 merged commit d8dd288 into Kane610:master Oct 10, 2023
1 check passed
@ViViDboarder
Copy link
Contributor

I stumbled on this PR as I was looking to do something like it myself. Not only was I glad to see it was done (yesterday, no less!), I found this PR to be overall super uplifting.

@Kane610 your feedback and attitude was terrific and inspirational.

@herriejr your persistence and open mindedness to learn was impressive. Really great job!

Reading this made me feel good about open source, so I figured I’d share so you two can feel good about being a part of it. Kudos and thank you!

@Kane610
Copy link
Owner

Kane610 commented Oct 12, 2023

Thank you very much @ViViDboarder <3

@Kane610
Copy link
Owner

Kane610 commented Oct 12, 2023

@herriejr I will wait to build a release some more time, you should be perfectly fine doing a local install into the home assistant environment directly from the aiounifi clone. Ping me when you need to.

@herriejr
Copy link
Contributor Author

@herriejr I will wait to build a release some more time, you should be perfectly fine doing a local install into the home assistant environment directly from the aiounifi clone. Ping me when you need to.

I've got it working locally. Gonna let it rest for a few days and then look at it again and try to clean the code up. I'll keep you posted.

@Kane610
Copy link
Owner

Kane610 commented Oct 24, 2023

@herriejr FYI I've published aiounifi v64 as I needed to propagate a fix to home assistant.

@Kane610
Copy link
Owner

Kane610 commented Nov 4, 2023

@herriejr I realised there is no tests with errors, Im reworking error handling a bit and would like to use that one better. Could you provide me with a couple of error examples for traffic rules (or rather v2 APIs)

New PR for reference #520

@herriejr
Copy link
Contributor Author

herriejr commented Nov 4, 2023

I will be away for work the upcoming week. I will then look into it.

@Kane610
Copy link
Owner

Kane610 commented Nov 4, 2023

I will be away for work the upcoming week. I will then look into it.

Thank you. That will be fine

ViViDboarder added a commit to ViViDboarder/aiounifi that referenced this pull request Nov 19, 2023
Adds support for reading, enabling, disabling, as well as generally
updating Traffic Routes.

I based this off the PR Kane610#409
@ViViDboarder ViViDboarder mentioned this pull request Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants