Skip to content

Conversation

@jacobbaungard
Copy link

Auth flow
Instead of doing a full login flow at every update (i.e 1 min interval from HA), we re-use the JWT token if possible. If the JWT token is expired, we refresh the token if possible, instead of running the full login flow. This should be a much more efficient flow. When checking expire of the jwt/refresh token, we add a one minute margin, to ensure clients have time to complete their requests after the login flow.

Clients will need to pass in (by reference) a CheckwattStateInfo. This is meant to keep the state in memory between multiple CheckwattManagers (HA creates a new one every update, perhaps that can be changed in the future).

Kill switch
It turns out the checking of the killswitch is what causes the 429 seen in various installations. After agreement
with CW, with this PR we check it only every 15minute, which should lower the load on CWs server significantly.

On startup we check the killswitch immediately, and then generate a random time interval for the next check. This should ensure the load gets evenly spread out between installations.

Testing
I've tested this quite a bit over the last week or so, making adjusts as I go. Everything seems to work fine, although in my installation I still get 429s when checking the killswitch - likely we'd need many people to update before we see the effect of this change.

If someone wants to test this on an actual HA installation, this is how I've run my docker-based HA installation:

  • Create a new dockerfile for HA:
FROM homeassistant/home-assistant:2025.8.3
RUN git clone -b check-jwt-expiry https://github.com/jacobbaungard/pyCheckwatt.git /tmp/pyCheckwatt
RUN pip install -e /tmp/pyCheckwatt/

You may also want to increase the logging level to debug while testing:

logger:
  logs:
    custom_components.checkwatt: debug
    pycheckwatt: debug

Signed-off-by: Jacob Baungard Hansen <me@jacobbaungard.com>
This commit ensures we can re-use JWT tokens, and refresh accordingly,
instead of relogging every time, which often causes 429/rate-limits from
checkwatt.

The client of the library should pass in an authinfo object (by
reference) when using the manager. The manager will update any info in
the authinfo object as needed.
Signed-off-by: Jacob Baungard Hansen <me@jacobbaungard.com>
The killswitch suffers often from 429/too many requests. After agreement
with CW, we check it only every 15minute.

On startup we check it immediately, and then generate a random time
interval for the next check. This should ensure the load gets evenly
spread out between installations.
Add a one minute margin to the JWT/refresh token expiry checks to ensure
the token doesn't expire shortly after the check.
So they work with the new stateinfo object.
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.

1 participant