DiedB / Homey-TeslaEnergy

GNU General Public License v3.0
1 stars 3 forks source link

Connection seems no more working with Powerwall firmware version 20.49 #3

Closed alechenne closed 3 years ago

alechenne commented 3 years ago

Everything went well up the Powerwall was updated with the firmware version 20.49. With firmware 20.40, it was working perfect.

It’s like now the device isn’t updated with the values (grid, home, battery, solar) read by the command /api/powermeters/aggregates.

DiedB commented 3 years ago

Interesting, could you visit http://[powerwall_ip]/api/meters/aggregates and post the output here? Does Homey give any error messages?

alechenne commented 3 years ago

Interesting, could you visit http://[powerwall_ip]/api/meters/aggregates and post the output here? Does Homey give any error messages? http://[powerwall_ip]/api/meters/aggregates --> {"code":403,"error":"Unable to GET to resource","message":"User does not have adequate access rights"}

I remarked the issue when I saw that the powerwall device had a problem (signaled with "!"). I did first a reboot of the app, and it solved the problem of the "!" but nevertheless the device cannot read the values of the powerwall and all values (home, solar, battery, ...) remain frozen to the last good access (for me, it was 5 hours ago). I saw the Powerwall has in between updated it's firmware from 20.40 to 20.49.

I found some other blogs which a pointing to the same issue. It seems Tesla has changed the authorization process.

alechenne commented 3 years ago

Interesting, could you visit http://[powerwall_ip]/api/meters/aggregates and post the output here? Does Homey give any error messages? http://[powerwall_ip]/api/meters/aggregates --> {"code":403,"error":"Unable to GET to resource","message":"User does not have adequate access rights"}

I remarked the issue when I saw that the powerwall device had a problem (signaled with "!"). I did first a reboot of the app, and it solved the problem of the "!" but nevertheless the device cannot read the values of the powerwall and all values (home, solar, battery, ...) remain frozen to the last good access (for me, it was 5 hours ago). I saw the Powerwall has in between updated it's firmware from 20.40 to 20.49.

I found some other blogs which a pointing to the same issue. It seems Tesla has changed the auth process to OAuth2.

I found an interesting link which is also related to that issue: https://github.com/home-assistant/core/issues/45955#issuecomment-776214194

I tried the given solution with postman and it seems working (integrating in the GET commands the 2 cookies I received when I do the login POST)

DiedB commented 3 years ago

Are you using your Tesla credentials for the initial POST request or are these credentials local to the Powerwall Gateway?

alechenne commented 3 years ago

Are you using your Tesla credentials for the initial POST request or are these credentials local to the Powerwall Gateway?

I used the Tesla username (email) and password given at the registration of the Powerwall at the installation. But I gave the username and password for client ans not the installer.

alechenne commented 3 years ago

Certainly, the only solution is to add 2 fields in the pair.html additionnaly to the ip address: one for the email and one for the password.

Then modifiy the api of the driver for the connect() function with the url = https://ip_address/api/login/Basic and a POST request with the body

            "username": "customer",
            "password": pwd_field,
            "email": email_field,
            "force_sm_off": false

You should then save the cookie in the response header (set-cookie) (Authcookie=XXXX et UserRecord=XXXX) to provide it as a cookie to the GET request /api/meters/aggregates and /api/system_status/soe.

It's working; I did it in a NODE-RED dashboard.

Please don't hesitate to ask me if you need more explanations.

DiedB commented 3 years ago

Thanks for the help! Will change the app this week. I do not have a Powerwall myself, so you might have to test a few versions before we get it working.

alechenne commented 3 years ago

Thanks for the help! Will change the app this week. I do not have a Powerwall myself, so you might have to test a few versions before we get it working.

Sure, I'll help you as I've 3 Powerwall, I can test in the evening after work. Many thanks in advance.

alechenne commented 3 years ago

Thanks for the help! Will change the app this week. I do not have a Powerwall myself, so you might have to test a few versions before we get it working.

Sure, I'll help you as I've 3 Powerwall, I can test in the evening after work. Many thanks in advance.

If it could help, I implemented the Powerwall access through the Homeyscript and it's workinf fine:

// my script
// -------- o - Configure these parameters -------- o -------- o 
const authorize_url ='https://192.168.1.xxx/api/login/Basic'
const meters_url ='https://192.168.1.xxx/api/meters/aggregates'
const soe_url ='https://192.168.1.xxx/api/system_status/soe'
const username ='customer'
const email ='xxxx@xxxx.com'
const password ='xxxx'
const debug =false
const to_del =false
// -------- o - Configure these parameters -------- o -------- o 

let cookie1
let cookie2

let reqHeaders = {
    "Content-Type":"application/json"
};

const httpsAgent = new https.Agent({
    keepAlive: true,
    rejectUnauthorized: false
});

const reqBody = JSON.stringify({
    "username":username,
    "password":password,
    "email":email,
    "force_sm_off":false
});

let reqOptions = {
    method: 'POST',
    headers: reqHeaders,
    body: reqBody,
    agent: httpsAgent,
};

let response
let body

    if (debug) console.log('Log to ',authorize_url);
    try
    {
        response =await fetch(authorize_url, reqOptions);
    }
    catch (err) {console.log(err);return false;}

    if (debug) console.log('Response =',response.status,response.statusText);
    if (! response.ok) throw Error('Invalid response');

    if (debug) console.log('Headers =\n',response.headers);
    body =JSON.parse(await response.text());
    if (debug) console.log('Body =\n',body);
    let token =body.token;
    if (debug) console.log('Token =',token);
    cookie1 =response.headers.raw()['set-cookie'][0].split(';')[0];
    console.log('Cookie, part 1 =',cookie1);
    global.set('Powerwall_cookie1',cookie1);
    cookie2 =response.headers.raw()['set-cookie'][1].split(';')[0];
    console.log('Cookie, part 2 =',cookie2);
    global.set('Powerwall_cookie2',cookie2);
}

reqHeaders = {
    "Content-Type":"application/json",
    "Cookie": cookie1+";"+cookie2
};

reqOptions = {
    method: 'GET',
    headers: reqHeaders,
    agent: httpsAgent
};

let value

try
{
    response =await fetch(meters_url, reqOptions);
}
catch (err) {console.log(err);return false;}

if (debug) console.log('Response =',response.status,response.statusText);
if (! response.ok) throw Error('Invalid response');

body =JSON.parse(await response.text());
if (debug) console.log('Body =\n',body);
value =Math.round(body.site.instant_power*10)/10;
await tag('HS_grid_power',(to_del ? null : value));
console.log('Grid power =',value,'W');
value =Math.round(body.solar.instant_power*10)/10;
await tag('HS_solar_power',(to_del ? null : value));
console.log('Solar power =',value,'W');
value =Math.round(body.battery.instant_power*10)/10;
await tag('HS_batt_power',(to_del ? null : value));
console.log('Battery power =',value,'W');
value =Math.round(body.load.instant_power*10)/10;
await tag('HS_home_power',(to_del ? null : value));
console.log('Home power =',value,'W');

try
{
    response =await fetch(soe_url, reqOptions);
}
catch (err) {console.log(err);return false;}

if (debug) console.log('Response =',response.status,response.statusText);
if (! response.ok) throw Error('Invalid response');

body =JSON.parse(await response.text());
if (debug) console.log('Body =\n',body);
value =Math.round(body.percentage*10)/10;
await tag('HS_batt_SOC',(to_del ? null : value));
console.log('SOC =',value,'%');

return true;
DiedB commented 3 years ago

Could you try the version at https://homey.app/a/com.tesla.energy/test/?

If it fails (which is highly probable), just send in a diagnostics report through the app settings.

alechenne commented 3 years ago

a6e63ec9-506d-43f4-82ed-33e72ce708be

First diag report (without removing the already existing device), I got the error “this.log is not a function” (I’m running Homey rev. 5.0.0) -> see screenshot 2F2E3497-8FB2-4D5C-8003-3D2A79BEAB08 C3B773BE-9BDB-49EC-AB6B-96DCC66C3E4B

alechenne commented 3 years ago

After removing the already existing device and trying to install it again, I got the following errors (see screenshot) B0C74A2B-B879-4FAB-91C6-CFC45B61BC98 CF1A8EE9-D100-4117-B8FD-E96BA9708A3C

alechenne commented 3 years ago

After removing the already existing device and trying to install it again, I got the following errors (see screenshot) B0C74A2B-B879-4FAB-91C6-CFC45B61BC98 CF1A8EE9-D100-4117-B8FD-E96BA9708A3C

Are the email and password requested the one used in the Powerwall registration process and not the ones of Tesla account, right?

DiedB commented 3 years ago

The email and password are indeed the ones that are set up with the Powerwall (which you are also using in your script). I'll make that more clear in the setup.

I've pushed a new version which should fix all issues you have found. I might need you to test the upgrade from the old to the new version again when everything works. I've taken the chance to upgrade the app to the newest Apps SDK as well so there are some things that may break in the process while it's untested.

The current version does not implement refreshing the session cookies yet, could you let me know when they expire?

alechenne commented 3 years ago

The email and password are indeed the ones that are set up with the Powerwall (which you are also using in your script). I'll make that more clear in the setup.

I've pushed a new version which should fix all issues you have found. I might need you to test the upgrade from the old to the new version again when everything works. I've taken the chance to upgrade the app to the newest Apps SDK as well so there are some things that may break in the process while it's untested.

The current version does not implement refreshing the session cookies yet, could you let me know when they expire?

Thanks for this new version. I can successfully installed it and create a new device.

Please note that I cannot update the already existing device as I remove it yesterday for the previous test, and for an unknown reason, I cannot create a new one with the validated app which is in the Homey store. Despite given the right ip address I received the following error (unknown error) (see the following screenshot). 943D7166-FD9F-406E-A53F-B26E2972DBB0

New reagrding the new version you created, as said installation is ok, creation of a new device is also ok. I just received after the validation of the credentials, an error Timeout after 30sec (see the following screenshot) but nevertheless the device is created. 1E699E93-B0CD-4C75-9F27-F83276C86F3B

After creation, the value are retreived from the Powerwall, but never refreshed (I check after 4 minutes and more than one hour) (see the following screenshot). 8C0C26D4-BA5F-4DCF-98C6-E6A3CD78AE3F

Regarding your question about when you should refresh the cookie, unfortunately I don't have the response and it seems to me that, in the various blog I read when I search a solution to the problem, I don't find any information about. What I did in the Homeyscript is "each time I read the meters/aggregates and the /soe, I send the http POST request at url .../api/login/Basic first. Anotherway to do is sending the http GET request to .../api/meters/aggregates ou .../api/system_status/soe and if you receive a response code 401v (if I'm not wrong) or at least different if 200, you can restart the process by sending the http POST request at url .../api/login/Basic first. Hope it will help.

Many thanks for your cooperation.

DiedB commented 3 years ago

Thanks a lot for the detailed information! Good point about not being able to test migration from an old version. I'll message someone else to check that before releasing this version.

I've found the reason for the timeout messages, those should not appear anymore. The same goes for the updates not occurring, the logic that handles this has changed between Apps SDK v2 and v3 and I made a small mistake there. The new test version should be in the App Store now.

For now, I implemented your session refresh mechanism (just login every time you fetch data). Not sure if that causes too much overhead because updates occur at a 5 second interval.

alechenne commented 3 years ago

Thanks a lot for the detailed information! Good point about not being able to test migration from an old version. I'll message someone else to check that before releasing this version.

I've found the reason for the timeout messages, those should not appear anymore. The same goes for the updates not occurring, the logic that handles this has changed between Apps SDK v2 and v3 and I made a small mistake there. The new test version should be in the App Store now.

For now, I implemented your session refresh mechanism (just login every time you fetch data). Not sure if that causes too much overhead because updates occur at a 5 second interval.

Thanks a lot for the new official version. It works: installed, without error and measures are refreshed. Just one additionnal thoing: it seems (but I could be wrong) that originaly, once installed, the device was also integrated to the list of devices that can push MQTT messages. If I'm not wrong, with this version, I don't see the device in the MQTT Hub. If course, it's always possible to write a script or some flows in order to push the wanted measure in a MQTT message. And last question (may be totaly stupid): why you dont move the battery level (SOC/SOE) in the battery tab of the device (you've a tab for the mesaures, a tab which is displaying a list of associated flows and you can activate a tab which is showing a battery with an indicateur of the level in %; in that case it could be easely montored (for example in the power/battery tab of the Homey app, or with the App Battery Monitor, ...

Thanks and kind regards.

DiedB commented 3 years ago

Nice, great to hear that it works. This app is not actively doing anything that makes it show up in the MQTT Hub app. A possible explanation would be that the MQTT Hub app has not been updated to the newest SDK and therefore cannot access devices that are created under the new SDK? You could create an issue on their GitHub, but it seems that the app has not been updated in a while.

We can move battery statistics to the battery tab, but I'm not sure whether that allows for showing battery percentages with two decimals. Could you make an issue for that?

alechenne commented 3 years ago

Nice, great to hear that it works. This app is not actively doing anything that makes it show up in the MQTT Hub app. A possible explanation would be that the MQTT Hub app has not been updated to the newest SDK and therefore cannot access devices that are created under the new SDK? You could create an issue on their GitHub, but it seems that the app has not been updated in a while.

We can move battery statistics to the battery tab, but I'm not sure whether that allows for showing battery percentages with two decimals. Could you make an issue for that?

Sorry for the delay of my reply, I was out of my home yesterday. Thanks for your reply and explanations. For MQTT, sorry I did a mistake and find the device in the middle of my 120 devices :-) ... but it won't update the msg. I'll try to rise an issue for the app MQTT hub.

For the battery, no problem I would rise an issue / improvement for that.