costastf / toonapilib

A library to interact with eneco's "Toon" smart meter via their official api https://api.toon.eu
MIT License
15 stars 11 forks source link

Make cache TTL configurable #31

Closed frenck closed 5 years ago

frenck commented 5 years ago

Currently, the lib has the set the TTL for the internal stats cache set to 30 seconds.

This helps but still allows to use to hit the rate limits (which seems to be enforced lately). It would be awesome if we could configure the TTL externally and maybe consider making the default 300 seconds (5 minutes). This would cause it to be 288 calls a day (out of 500).

scheric commented 5 years ago

A question from my side. If i change STATE_CACHING_SECONDS = 30 to STATE_CACHING_SECONDS = 5 in configuration.py L7 does that mean the api will get polls every 5 seconds no matter what happens.

costastf commented 5 years ago

@frenck i am afraid that it is quite tricky to do that. I am looking into it, but it does not seem easy. @scheric Changing that value means that the library will cache things internally for 5 seconds. The library does not actively poll for values, it waits to be accessed. That means that changing that value and querying the library every 30 seconds you still get a live response every 30 seconds. If you make the caching value 5 minutes and poll every 30 seconds you will get a cached response for 5 minutes.

frenck commented 5 years ago

@costastf Thank you for your response, no problem! In that case, I will need to implement that logic into the Home Assistant component.

Reason for asking this is because each individual sensor now relies on the library for caching. I will move this polling mechanism in the HA component to a more central location in order to ensure polling happens according to a user-defined time period (with a sane default).

👍

costastf commented 5 years ago

I have looked into making the caching configurable and I am afraid that there is no simple way to get there.The decorator gets its values on the initial parsing of the code and the only way to implement something that would work is to use metaprogramming which in this case would raise the compexity of the code significantly and feels like hunting mosquitos with a chainsaw. I am closing this since I can't see a straightforward way of implementing the feature.