jaroschek / home-assistant-myuplink

Custom Home Assistant integration for devices and sensors in myUplink account.
43 stars 10 forks source link

Reduce number of API requests #16

Closed jaroschek closed 1 year ago

jaroschek commented 1 year ago

Because of the official rate limiting of the myUplink API the number of recurring request should be reduced.

As the API client implementation was stateless, the whole system containing all devices, parameters and smart-home-zones was fetched every 60 seconds. Even with only 1 devices this caused 3 sequential requests every 60 seconds. Because of the implemented throttling the whole update process would take at least 15 seconds... sometimes much more. And just for one device. If you have more devices this number would multiply with your number of devices.

So the idea is to make the API client implementation stateful so that it remembers already fetched systems and devices to avoid unnecessary requests that would only fetch already known data. Also the requests for the so called "smart-home-zones" can be avoided, because this data is currently not used anywhere.

Finally the solution does only make 1 request per devices on each coordinator update to fetch the parameters with their updated values.

I also added some more debug logging to allow a better observation of the made requests:

When the integration is startet, home assistant will make 2 requests and produce the following log:

2023-07-21 06:03:22.786 DEBUG (MainThread) [custom_components.myuplink.api] Fetch systems
2023-07-21 06:03:23.770 DEBUG (MainThread) [custom_components.myuplink.api] Fetch parameters for device <DEVICE_ID>
2023-07-21 06:03:23.771 DEBUG (MainThread) [custom_components.myuplink.api] Delaying request by 4.999597 seconds due to throttle
2023-07-21 06:03:30.220 DEBUG (MainThread) [custom_components.myuplink] Finished fetching myUplink data in 7.434 seconds (success: True)

After that only the parameters will be fetched to update the already known systems and devices:

2023-07-21 06:09:40.505 DEBUG (MainThread) [custom_components.myuplink.api] Update systems
2023-07-21 06:09:40.505 DEBUG (MainThread) [custom_components.myuplink.api] Fetch parameters for device <DEVICE_ID>
2023-07-21 06:09:41.397 DEBUG (MainThread) [custom_components.myuplink] Finished fetching myUplink data in 0.893 seconds (success: True)