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

Ha historical sensor (and MFA support) #9

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

shtrom
Copy link

@shtrom shtrom commented Jul 26, 2023

Fix for #6 (historical sensor), and also #10 (mfa)

Signed-off-by: Olivier Mehani <[email protected]>
Tariffs 31, 41, 61 and 62 are a wild guess, and totally untested.

fixes: LeighCurran#6 some more
Signed-off-by: Olivier Mehani <[email protected]>
Neither STATE_CLASS_TOTAL_INCREASING nor STATE_CLASS_MEASUREMENT work
well for the ca. 9am reset that seems to happen for daily summary data.

The `last_reset` time is set to whenever a value changes. This means
this is liable to a bug where a value will be missed it it is the same
as the previous day.

Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
shtrom added a commit to shtrom/ha-historical-sensor that referenced this pull request Aug 10, 2023
shtrom added a commit to shtrom/ha-historical-sensor that referenced this pull request Aug 10, 2023
The AuroraApi proxies everything to the session, or swallows the error
transparently.  This is not the behaviour that the Throttle wrapper
expects, as it needs to use a _throttle attribute on the wrapped object.

We add a special case in the proxy juuuuust for this.

Signed-off-by: Olivier Mehani <[email protected]>
Signed-off-by: Olivier Mehani <[email protected]>
@shtrom shtrom marked this pull request as ready for review August 10, 2023 10:46
@shtrom
Copy link
Author

shtrom commented Aug 10, 2023

@LeighCurran I'm still letting this soak on my HA instance (see #6), but I think this is now pretty much good to go.

@shtrom
Copy link
Author

shtrom commented Aug 11, 2023

Hum, no... I think my Throttle handling has broken something...

@slothking87
Copy link

Im not sure if its my system, or if its the last commit to show only available tariffs - But I seem to have lost my tariff sensors this morning, and no amount of removing and adding the integration seems to be able to get them back.

@shtrom
Copy link
Author

shtrom commented Aug 12, 2023 via email

@slothking87
Copy link

No need to be sorry - thats the joys of testing new things!

I cant see anything related to the component in the logs anywhere - Did I need to change the logging level somewhere? I tried in the config.yaml file, but that hasnt seemed to have helped - But I might have the code wrong

@slothking87
Copy link

I rolled back to the previous version, then tried to roll forward once the data for yesterday was available. Same thing happened where it marked all sensors apart from kilowatt hour, estimated balance and dollar value usage.

@shtrom
Copy link
Author

shtrom commented Aug 13, 2023 via email

@shtrom
Copy link
Author

shtrom commented Aug 21, 2023 via email

@slothking87
Copy link

Yep. All looking good so far - Thanks heaps for your help (and patience). Out of interest how far back should it be grabbing historical data?

@shtrom
Copy link
Author

shtrom commented Aug 22, 2023 via email

@shtrom
Copy link
Author

shtrom commented Sep 4, 2023

Ok. @LeighCurran I think we're in a good place. I've been running this for a few weeks with no issue and have been getting data reliably from the historical sensors and into the Energy dashboard.

This is still running with my patched version of AuroraPlus 1.1.6 from, as 1.5.0 didn't appear to be in the repo. The patch LeighCurran/AuroraPlus@1021685 is simple enough that I think it should be fine to cherry-pick onto 1.5.0 (give or take some automatic linting) if need be.

If you're happy with merging LeighCurran/AuroraPlus#1 there, I can then point the dependency back to the next PyPi version here, and we can merge this here, too.

Usage:

1. obtain an `access_token` following instructions at [0]
2. update the secrets to use an empty username (`''`), and the
   `access_token` as the password

[0] https://github.com/shtrom/AuroraPlus/tree/oauth-mfa-token\#obtain-a-token

Signed-off-by: Olivier Mehani <[email protected]>
@shtrom
Copy link
Author

shtrom commented Nov 28, 2023

@slothking87 I just pushed an interim fix to support MFA https://github.com/shtrom/AuroraPlus/tree/oauth-mfa-token to this branch.

Instructions are in this commit 7e277e5

You might have to redo the dance to trash your previous state and pull a new version of the dependency library

@slothking87
Copy link

I feel like Im missing something very obvious - But I cant get it to work.

When loading the URL mentioned in a web browser, I do get a generic error page, but with this message which makes me think Im missing something somewhere

"Sorry, but we're having trouble signing you in.
We track these errors automatically, but if the problem persists feel free to contact us. In the meantime, please try again.
Correlation ID: 2ba71a8f-221c-4b05-8d0d-1e1d6ade01c9
Timestamp: 2023-11-29 06:14:55Z
AADB2C99059: The supplied request must present a code_challenge"

@shtrom
Copy link
Author

shtrom commented Nov 29, 2023 via email

@slothking87
Copy link

slothking87 commented Nov 29, 2023

Excuse the noob questions - So the process is to run the commands as they are listed and that opens the correct link?
The code I got was identical to yours so guessing I missed something or something didnt want to work.
EDIT: Nvm - Got the code

Edit 2: So to make this work again - I update the details in the HA config file with username: '' and password being the big long string above?

@shtrom
Copy link
Author

shtrom commented Nov 29, 2023

Ok, so this URL should be pasted back into the api.redirect_uri(<redirect_uri>), and you'll get an access_token.

Then, yes, update your HA secrets to have username='', and password=<access_token>, and restart HA. If you're in luck, you'll start getting data again.

@shtrom
Copy link
Author

shtrom commented Nov 29, 2023

PS: be careful about posting redirect_uris (I was talking about posting the first URL that is returned by oauth_authorize), as they do contain secrets.

In this case, the secret is protected by the code_challenge that only your client api instance has, but best be prudent.

@shtrom
Copy link
Author

shtrom commented Nov 29, 2023

If you're in luck, you'll start getting data again.

I am!

image

@slothking87
Copy link

so close! I got the extra information, but getting the usual authentication error inside home assistant. How much of that final string is whats needed for the access token?

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_setup_platform
await asyncio.shield(task)
File "/config/custom_components/auroraplus/sensor.py", line 98, in async_setup_platform
AuroraPlus = await hass.async_add_executor_job(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/auroraplus/sensor.py", line 96, in aurora_init
session.getmonth()
File "/usr/local/lib/python3.11/site-packages/auroraplus/init.py", line 89, in getmonth
self.month = self.request("month", index)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/auroraplus/init.py", line 63, in request
+ self.serviceAgreementID
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'api' object has no attribute 'serviceAgreementID'

@shtrom
Copy link
Author

shtrom commented Nov 29, 2023

Ok, same thing here on my non-test HA deployment. Hold tight.

@shtrom shtrom mentioned this pull request Nov 29, 2023
@shtrom
Copy link
Author

shtrom commented Nov 29, 2023

You need to redo what you did there #9 (comment) to get the new version of the auroraplus lib.

Ah! Here's the command to run #9 (comment)

@shtrom
Copy link
Author

shtrom commented Nov 29, 2023

Ok, that didn't work. But I realised existing instances do need an update in the config/custom_components/auroraplus/manifest.json to point to the new branch: shtrom@7e277e5, then a restart.

I also had to delete and remove and reinstall the previous install of the auroraplus:

rm -rf /usr/local/lib/python3.11/site-packages/auroraplus*
pip install --upgrade --no-cache-dir   git+https://github.com/shtrom/AuroraPlus@oauth-mfa-token 

You can check with

cat /usr/local/lib/python3.11/site-packages/auroraplus/__init__.py

that it corresponds to the version at https://github.com/shtrom/AuroraPlus/blob/oauth-mfa-token/auroraplus/__init__.py

@slothking87
Copy link

Hmmm, Im still getting the same error as before with the no service agreement ID. Ill have a better play over the weekend - Im probably doing something wrong somewhere

@shtrom
Copy link
Author

shtrom commented Dec 1, 2023

As usual, Aurora had a wobble just when I was testing, but it came good today, so it works on mine.

If you still have the serviceAgreementID, it's likely you're not on the updated branch yet. I'm not sure how you managed to update it last time, but if you can ssh into the system that runs it, check the __init__.py and compare it to the branch. If it still is different, this means that the lib is not updated.

Worse case scenario, just delete the auroraplus directory (the Python lib, not the HA integration), and it should get reinstalled, hopefully to the correct version, on restart.

@slothking87
Copy link

Bingo - That worked!!!!!!

IF anyone uses supervised HA on a RPI like I do, You can run the pip commands via an add on (https://github.com/home-assistant/addons-development). Just add the command (pip install git+https://github.com/shtrom/AuroraPlus@oauth-mfa-token in this case) under the pypi option, save, then start the add on and it does the rest for you. keep an eye on the logs tab to see when its done.

@shtrom
Copy link
Author

shtrom commented Dec 3, 2023

Awesome!

I still need to work out how to make HA fetch and record the token, rather than having to put it in manually, but that's a good functional workaround in the meantime.

@shtrom
Copy link
Author

shtrom commented Dec 27, 2023

Ok, it looks like the access_token has a three-week expiry. Data stopped coming on the 20th, I just had to put a new one in my secrets.yaml and restart. I got data for yesterday straight away, as expected. Let's see if that expires around the 17th.

If so, that'll make the need for a way to refresh the token more pressing... If that's indeed something that the Aurora+ Web UI supports. Otherwise, we'll have to at least streamline the process of obtaining a new token.

Fixes

    Traceback (most recent call last):
    File /usr/src/homeassistant/homeassistant/loader.py, line 842, in get_platform
    cache[full_name] = self._import_platform(platform_name)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File /usr/src/homeassistant/homeassistant/loader.py, line 859, in _import_platform
    return importlib.import_module(f{self.pkg_path}.{platform_name})
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File /usr/local/lib/python3.11/importlib/init.py, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File , line 1204, in _gcd_import
    File , line 1176, in _find_and_load
    File , line 1147, in _find_and_load_unlocked
    File , line 690, in _load_unlocked
    File , line 940, in exec_module
    File , line 241, in _call_with_frames_removed
    File /config/custom_components/auroraplus/sensor.py, line 39, in
    from homeassistant_historical_sensor import (
    File /usr/local/lib/python3.11/site-packages/homeassistant_historical_sensor/init.py, line 39, in
    raise SystemError(msg)
    SystemError: Running HomeAssistant 2024.1.2, Minimum required version >=2023.6.0

See [0], [1]

[0] ldotlopez/ha-historical-sensor#12
[1] https://github.com/LeighCurran/AuroraPlus/pull/3\#issuecomment-1884213483

Signed-off-by: Olivier Mehani <[email protected]>
@shtrom
Copy link
Author

shtrom commented Jan 10, 2024

@slothking87 re: LeighCurran/AuroraPlus#3 (comment)

I just updated the ha-historical-sensor as per ldotlopez/ha-historical-sensor#12, and pushed it.

This should fix your issue on 2024.1.2, but you'll have to do the usual dance of trashing the old python dep to and make sure the new one is installed.

@shtrom shtrom mentioned this pull request Jan 28, 2024
@shtrom
Copy link
Author

shtrom commented Jan 28, 2024

Ok, it looks like the access_token has a three-week expiry. Data stopped coming on the 20th, I just had to put a new one in my secrets.yaml and restart. I got data for yesterday straight away, as expected. Let's see if that expires around the 17th.

It lasted until the 25th! Sound like a... 29 day lifetime? I'm renewing it today, so I guess we'll see on the 26th of Feb?

@shtrom shtrom mentioned this pull request Jul 3, 2024
@shtrom
Copy link
Author

shtrom commented Jul 3, 2024

(psst, have a try at #11 for a UI-based configuration of the access token).

@shtrom shtrom changed the title Ha historical sensor Ha historical sensor (and MFA support) Jul 3, 2024
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.

2 participants