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

Using DataCoordinator instead of direct update #134087

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

iprak
Copy link
Contributor

@iprak iprak commented Dec 27, 2024

Proposed change

This request switches Vesync devices to use DataCoordinator pattern instead of each entity updating itself. The latter can run over the daily webcalls limits where the web end point returns an error message like Current user's daily request quota has been used up. The quota formula is \"3200 + 1500 * user owned device number\". Please note that we may change the constant in the formula.

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)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

This should partially address #131781 which in my case was due to daily quota being used up.

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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format 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.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @markperdue, @webdjoe, @TheGardenMonkey, @cdnninja, mind taking a look at this pull request as it has been labeled with an integration (vesync) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of vesync can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign vesync Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft December 27, 2024 13:27
_LOGGER,
name=DOMAIN,
update_method=async_update_data,
update_interval=timedelta(seconds=30),
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we set this to 60? At 30 it would max the count at 3 devices. 60 will solve that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should not max out, there should be only one request regardless of the number of devices per second which would be (25 * 3600 / 30) = 2880 per day, right?

The error message mentioned "3200 + 1500 * user owned device number" which was a issue if there were lots of total entities.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh let me check the underlying code. I assumed that Vesync library was just doing a for each loop. The endpoints are different so I believe it is but I'll double check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This update on the vesync manager updates all the devices within a 30 sec interval check.

Copy link
Contributor

@webdjoe webdjoe Dec 27, 2024

Choose a reason for hiding this comment

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

There will be another 2880 calls per day due to pulling the device list in each manager.update() call, which goes beyond the limit. It would be better to use manager.update_all_devices() to avoid the unnecessary pulling of the device list. Adding / removing devices can be done manually by a service

I am going to remove the half baked rate limiting logic, that was done at the very beginning of the project and probably the 5th line of python code I ever wrote. My opinion is that the rate limiting should be done at the user level, not the library level. Happy to hear if anyone has another opinion.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't believe I never caught that.

@joostlek can I make a release that can be bumped in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

2.1.15 is published with the fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the prompt fix. I rebase my branch and bumped the pevsync version and also selected the Dependency upgrade checkbox in the PR.

Copy link
Member

Choose a reason for hiding this comment

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

Could we include the bump of the lib in a preliminary PR so it's not included in this (quite large already) PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I have submitted #134156 for consuming 2.1.15. I will adjust this PR once that has been merged.

Using DataCoordinator allows us to reduce number
of web calls being made. Some devices can have
multiple entities which increases the number of
web calls and hitting the daily limit.
DataCoordinator reduces that to just 1 call.
@iprak iprak force-pushed the use-data-coordinator-2 branch from 81995c5 to 20d01a3 Compare December 28, 2024 12:35
@iprak iprak marked this pull request as ready for review December 28, 2024 13:01
This avoids fetching device list every time.
@iprak iprak mentioned this pull request Dec 28, 2024
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants