-
Notifications
You must be signed in to change notification settings - Fork 16
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
New libbi action chargefromgrid
#16
Conversation
How could the authentication work, if an account is using one of the other methods myenergi support? |
Once this is merged I have more stuff to add. Got a number of useful endpoints from the Android app... |
@videojedi - my curl test script uses the serial number of the vhub (my libbi) as the user and the APIKey as the password. This is available from via the 'Advanced' button on the location products page from the web UI, https://myaccount.myenergi.com/location#products. |
@FlipGFlop , this project has arguments for Perhaps they should be renamed while keeping the old names as aliases for backwards compatibility? |
Sorry, I'm getting an exception when I try to run....
|
No need to say sorry, will def be my fault, thanks for the feedback!
I think you can pass |
@FlipGFlop This specific API endpoint uses OAuth, which requires the app username and password. I have just tested it and it does not work with serial number + API key. The serial + API key combo works for API endpoints that use digest auth. Those endpoints are served by sX.myenergi.net. |
@mattburns I am trying to test your fork, but running into issues. I am getting I have double-checked both and they are correct. I have copied your example cognito test from above and have been successfully using various API endpoints. I will do some more testing/debugging later this afternoon and report back. |
Ok, the auth error was due to me having special characters in my password. I can confirm that enabling/disabling charging works fine! Excellent work! |
tried again, removed special characters from password, but still fails for me.
|
If it was a username/password issue, then you'd get a different exception. Authentication is done on lines 39-40 of connection.py Silly question: your debug log shows your S/N as xxxxxxxxx - did you remove it before you posted the log? |
Heres the complete log.. (yes I did remove serial number, not sure if thats strictly necessary...)
|
This is really puzzling. Can you run the following python code and see what happens? (substitute username_here, password_here and libbi_serialnumber_here)
This should produce an output like this;
The status should be True of False based on whether you have charging from grid enabled or not. |
Ah, OK. Because my account is 'sign in with apple', I registered a new account with email/password and shared my original account with full permissions. I am able to login with the new email/password credentials and control my libbi grid charging from myaccount.myenergi.com...... |
So, all OK using pymyenergi as well? |
Sorry, don't understand. I get the exception listed above from pymyenergi when trying to use the credentials from this 'secondary' account. But, if I log into myaccount.myenergi.com with secondary credentials I am able to see and control all devices. |
Apologies, I didn't understand, but now I do! Let me test this at my end and see if it's workable. I'll set up a secondary account and see if there's a way to make it work. |
Just a note that I am running into issues when trying to use Cognito from the HA component due to sync vs async calls. The error in detail:
|
@trizmark , I'm busy at the mo, but perhaps putting it in the |
@mattburns It's actually fine, I have found a way around it. I tried to move things away from |
|
@videojedi To keep the good news rolling: I finally had time to test your special case and you're right, I got the same error. However, there's a solution! After a bit of network sniffing, I figured out what's needed. Can you try the following test with your secondary account?
Tech details: with OAuth API endpoints, we need to make a call to api/Locations. From this response we can see if it's a guest location or not. If it's a guest location, we need to include the invitationId in further API calls. Once you've confirmed that it works for you as well, I can incorporate these changes into pymyenergi. |
@trizmark Brilliant work!
Many thanks. Richard |
For those who like to live dangerously... |
Working for me. Thanks everso! |
@mattburns Do you mind if I PR your fork, so we could get all the libbi charge from grid control related pymyenergy changes into this PR? |
Go for it, do whatever you like, I'm a bit busy at the moment to work on this. Thanks |
Add extra libbi functionality
OK, PR in. If you merge it, that should update this PR as well, which should set everything up nicely for the HA component update. |
@trizmark Either I've broken it, or sadly the secondary login stuff seems to have stopped working? Even the test code above fails with the following now.
It has been working all week.... something changed on the myenergi server is only possible explanation? |
Add extra libbi functionality
@videojedi it's not just you - it's myenergi I use the https://myaccount.myenergi.com/api/Locations API endpoint to determine if I need to add the invitation_id to the OAuth calls. This used to return a JSON object with all the accessible locations. Now it returns a 404. 😠 I'll do some network sniffing in the morning to figure out what I need to change. |
Update API endpoint for location discover
The fix was simple. API endpoint has changed from |
Updated location discovery API endpoint
Yep all good and confirmed working again. Many thanks |
Added export mode for libbi
Add libbi charge target handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution, I'm sorry for the late response!
Before we merge this I would like to make the new authentication optional.
If you don't have a libbi there is no need to for the cognito authentication right?
pymyenergi/connection.py
Outdated
self.auth = httpx.DigestAuth(self.username, self.password) | ||
self.headers = {"User-Agent": "Wget/1.14 (linux-gnu)"} | ||
self.oauth = Cognito(_USER_POOL_ID, _CLIENT_ID, username=self.app_email) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be optional, check if self.app_email is not empty
pymyenergi/connection.py
Outdated
self.auth = httpx.DigestAuth(self.username, self.password) | ||
self.headers = {"User-Agent": "Wget/1.14 (linux-gnu)"} | ||
self.oauth = Cognito(_USER_POOL_ID, _CLIENT_ID, username=self.app_email) | ||
self.oauth.authenticate(password=self.app_password) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be optional, check I self.app_password if not empty
pymyenergi/connection.py
Outdated
self.auth = httpx.DigestAuth(self.username, self.password) | ||
self.headers = {"User-Agent": "Wget/1.14 (linux-gnu)"} | ||
self.oauth = Cognito(_USER_POOL_ID, _CLIENT_ID, username=self.app_email) | ||
self.oauth.authenticate(password=self.app_password) | ||
self.oauth_headers = {"Authorization": f"Bearer {self.oauth.access_token}"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if we did the authentication
|
||
# Update the extra information available on libbi | ||
# this is the bit that requires OAuth | ||
if existing_device.kind == LIBBI: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add a check here to see if app email and password is set and print an error if not?
@CJNE No worries. Right now OAuth is only required if you have a libbi. In the future this will change as I saw a note from myenergi that they're slowly trying to migrate from digest to oauth. |
Added status 173
OAuth now optional
New libbi mode - 'export'
@CJNE I've implemented the requested changes. The app_email and app_password parameter is now optional and if they're missing all OAuth stuff is skipped. As part of the libbi only relies on digest, I have left those parts in even if OAuth is missing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, will get this merged :)
refs issue #15
This change allows you to toggle charging from grid. Toggle on:
Disable charging from grid:
I won't be offended if you want to re-write my implementation, it just proves the mechanism.
For background:
These actions happen over a different api endpoint that uses AWS Cognito for OAuth authentication.
A simple demonstration of how to interact with this api is like this: