bimmer_connected icon indicating copy to clipboard operation
bimmer_connected copied to clipboard

Add socMax for electric/hybrid cars

Open havrla opened this issue 5 years ago • 20 comments

Heloo after 20.2.2020 - Dead: https://github.com/jupe76/bmwcdapi https://github.com/frankjoke/iobroker.bmw https://github.com/edent/BMW-i-Remote https://github.com/ipv6freely/bmw2018

I miss the maximum battery capacity information. - socmax.

this information can be found here:

[root@CL-ITNK-BMW ~]# ./bmw/bmw.py https://b2vapi.bmwgroup.com/api/vehicle/navigation/v1/WBY1Z41000VZXXXXX <Response [200]> {'latitude': 51.333383, 'longitude': 17.333973, 'isoCountryCode': 'CZE', 'auxPowerRegular': 1.4, 'auxPowerEcoPro': 1.2, 'auxPowerEcoProPlus': 0.4, 'soc': 11.439000129699707, 'eco': '659,489,421,421,402,475,632,6d0,76f,8ac,9e9', 'norm': '698,4b6,4ac,4ac,41a,48f,632,6d0,76f,8ac,9e9', 'ecoEv': '659,489,421,421,402,475,632,6d0,76f,8ac,9e9', 'normEv': '698,4b6,4ac,4ac,41a,48f,632,6d0,76f,8ac,9e9', 'vehicleMass': '1400', 'driveTrain': 'bev_rex', 'pendingUpdate': False, 'vehicleTracking': True, 'socmax': 16.9, 'kaccReg': '2412000', 'kdecReg': '3240000', 'kaccEco': '2520000', 'kdecEco': '3240000', 'kup': '3132000', 'kdown': '3384000'} [root@CL-ITNK-BMW ~]#

Please please add infomatin to this project bimmer_connected.

My bmw.py from https://github.com/ipv6freely/bmw2018 and modify for my used. I want to use bimmer_connected

Thanks thanks Havrla

havrla avatar Feb 21 '20 13:02 havrla

In addition to socMax, this feed also produces the current state of charge in KwH 'soc' which could also be useful to keep track of.

I've been digging around in the code base and I'll see if I can make a pull request for this feature if I can get it running. Thanks for the helpful hint on where this data is still available @Havria

Appreciations for the hard work on this project and continued development!

damienheiser avatar Mar 05 '20 00:03 damienheiser

I'll have a look if theres the possibility to implement this. On issue comes with #140 as this changes the API and auth endpoints (so we cannot just use the "old" OAuth tokens anymore).

I'll check and come back to you.

rikroe avatar Mar 25 '20 20:03 rikroe

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 23 '20 22:06 stale[bot]

Heloo Please please add SOC battery.

Version bimmer-connected-0.7.7, does not have SOC yet, I would like to switch to these libraries.

thanks thanks ...

havrla avatar Aug 30 '20 17:08 havrla

I am testing ways how to integrate soc and socmax at the moment with this code. Are there any news on PR #191 ? Is this going to be merged soon, so should new code base on this code? Are there suggestions on how where to put the soc and socmax?

z8i avatar Sep 06 '20 19:09 z8i

This is supported now in master.

Markinus avatar Oct 02 '20 17:10 Markinus

Very nice, thank you! How to get socMax into home-assistant? Should ha_custom_component create a sensor if I am using the right version?

z8i avatar Oct 24 '20 07:10 z8i

We need to update the HA component (and the custom component) as well to reflect the new attribute structure (i.e. vehicle attributes moved in state.py). I would like to wait with these changes for three reasons:

  • there seem to be some (smaller issues) with the code that we have to figure out before releasing
  • some people are using the custom component as Bugfix for the login issues
  • the HA PR regarding config flows got approved so it probably be merged after the 0.117 release. As it includes quite a lot of changes I'd like to wait the merge of this one before proposing new changes.

So please be a little patient 😉

rikroe avatar Oct 24 '20 08:10 rikroe

I have some problems with these lagacy services and the last login fixes. I cannot get the data anymore:

DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): b2vapi.bmwgroup.com:443 DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/navigation/v1/WBAJPXXXXXCDXXXXX?deviceTime=2020-11-22T15%3A44%3A53&dlat=0.0&dlon=0.0 HTTP/1.1" 403 0 DEBUG:bimmer_connected.account:The BMW Connected Drive portal returned an error: UNKNOWN_ERROR (received status code 403 and expected 200). DEBUG:bimmer_connected.account:

I removed the new parameters but it's still not working... strange. @rikroe do you maybe have an idea why?

Markinus avatar Nov 22 '20 14:11 Markinus

Heloo my scripts runing OK, example:

[root@CL-ITNK-BMW bmw]# ./bmw-lazeni.py

Header: {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_1_1 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0 Mobile/15B150 Safari/604.1', 'Authorization': 'Bearer EQhuP4KtZLAVLsAMcwnaWRuY4WqQkEXz'}

URL: https://b2vapi.bmwgroup.com/api/vehicle/navigation/v1/WBY1Z41000VZ75700

Response json: {'latitude': 43.832296, 'longitude': 13.692223, 'isoCountryCode': 'CZE', 'auxPowerRegular': 1.4, 'auxPowerEcoPro': 1.2, 'auxPowerEcoProPlus': 0.4, 'soc': 28.790361404418945, 'eco': '659,489,421,421,402,475,632,6d0,76f,8ac,9e9', 'norm': '698,4b6,4ac,4ac,41a,48f,632,6d0,76f,8ac,9e9', 'ecoEv': '659,489,421,421,402,475,632,6d0,76f,8ac,9e9', 'normEv': '698,4b6,4ac,4ac,41a,48f,632,6d0,76f,8ac,9e9', 'vehicleMass': '1400', 'driveTrain': 'bev_rex', 'pendingUpdate': False, 'vehicleTracking': True, 'socmax': 38.879997, 'kaccEco': '2520000', 'kaccReg': '2412000', 'kdecEco': '3240000', 'kdecReg': '3240000', 'kup': '3132000', 'kdown': '3384000'} [root@CL-ITNK-BMW bmw]#

bimmer cli not OK after remove params in /bimmer_connected/state.py for clear URL https://b2vapi.bmwgroup.com/api/vehicle/navigation/v1/WBY1Z41000VZ75700

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): b2vapi.bmwgroup.com:443 DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/navigation/v1/WBY1Z41000VZ75700 HTTP/1.1" 403 0 DEBUG:bimmer_connected.account:The BMW Connected Drive portal returned an error: UNKNOWN_ERROR (received status code 403 and expected 200). DEBUG:bimmer_connected.account: DEBUG:bimmer_connected.state:Service NAVIGATION failed

############### bimmer cli has: Header: {'accept': 'application/json', 'Authorization': 'Bearer thQL3RxlMjwcpQUFCfun8trX4BnUoz2e', 'referer': 'https://www.bmw-connecteddrive.de/app/index.html'}

URL: https://b2vapi.bmwgroup.com/api/vehicle/navigation/v1/WBY1Z41000VZ75700

After change to me headers in ./bimmer_connected/account.py:

        if headers is None:
            headers = self.request_header
            print("hevlo")
            print(headers)
            print(url)
            headers = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_1_1 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0 Mobile/15B150 Safari/604.1', 'Authorization': 'Bearer zpHTAIY2QRlHhAgSaehy7XzMT79joJqQ'}

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): b2vapi.bmwgroup.com:443 DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/efficiency/v1/WBY1Z41000VZ75700 HTTP/1.1" 200 None DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): b2vapi.bmwgroup.com:443 DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/navigation/v1/WBY1Z41000VZ75700 HTTP/1.1" 200 None

DEBUG:bimmer_connected.state:{'STATUS': {'vin': 'WBY1Z41000VZ75700', 'mile .... ...... ...... .... ..... .... ...... f,632,6d0,76f,8ac,9e9', 'vehicleMass': '1400', 'driveTrain': 'bev_rex', 'pendingUpdate': False, 'vehicleTracking': True, 'socmax': 38.879997, 'kaccReg': '2412000', 'kdecReg': '3240000', 'kaccEco': '2520000', 'kdecEco': '3240000', 'kup': '3132000', 'kdown': '3384000'}}

Result: Another header is needed or token ? :-)

After change only token in header

Header: {'accept': 'application/json', 'Authorization': 'Bearer zpHTAIY2QRlHhAgSaehy7XzMT79joJqQ', 'referer': 'https://www.bmw-connecteddrive.de/app/index.html'}

URL: https://b2vapi.bmwgroup.com/api/vehicle/navigation/v1/WBY1Z41000VZ75700

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): b2vapi.bmwgroup.com:443 DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/efficiency/v1/WBY1Z41000VZ75700 HTTP/1.1" 200 None DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): b2vapi.bmwgroup.com:443 DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/navigation/v1/WBY1Z41000VZ75700 HTTP/1.1" 200 None

And try add params:

DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/efficiency/v1/WBY1Z41000VZ75700?deviceTime=2020-11-22T19%3A44%3A50&dlat=0.0&dlon=0.0 HTTP/1.1" 200 None DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): b2vapi.bmwgroup.com:443 DEBUG:urllib3.connectionpool:https://b2vapi.bmwgroup.com:443 "GET /api/vehicle/navigation/v1/WBY1Z41000VZ75700?deviceTime=2020-11-22T19%3A44%3A50&dlat=0.0&dlon=0.0 HTTP/1.1" 200 None

Is OK - 'socmax': 38.879997 in respons, socmax not in CLI - ""Vehicle status:

My script generate token:

 def getToken(BASE_URL, username, password, timeout=(20, 20)):
    try:
        headers = {
                "Content-Type": "application/x-www-form-urlencoded",
                "Content-Length": "124",
                "Connection": "Keep-Alive",
                "Host": BASE_URL,
                "Accept-Encoding": "gzip",
                "Authorization": "Basic blF2NkNxdHhKdVhXUDc0eGYzQ0p3VUVQOjF6REh4NnVuNGNEanli"
                                 "TEVOTjNreWZ1bVgya0VZaWdXUGNRcGR2RFJwSUJrN3JPSg==",
                "Credentials": "nQv6CqtxJuXWP74xf3CJwUEP:1zDHx6un4cDjybLENN3kyfumX2kEYigWPcQpdvDRpIBk7rOJ",
                "User-Agent": "okhttp/2.60"}       
        data = {
            'client_id': 'dbf0a542-ebd1-4ff0-a9a7-55172fbfce35',
            'response_type': 'token',
            'redirect_uri': 'https://www.bmw-connecteddrive.com/app/static/external-dispatch.html',
            'scope': 'authenticate_user vehicle_data remote_services',
            'username': username,
            'password': password}

        data = urllib.parse.urlencode(data)#       url = 'https://' + BASE_URL + '/webapi/oauth/token'
        #url = 'https://' + BASE_URL + '/webapi/oauth/token'
        url = 'https://' + BASE_URL + '/gcdm/oauth/authenticate'
        #print (url)
        r = requests.post(url, data=data, headers=headers, allow_redirects=False)
        #print (r)
        if r.status_code == 302:
            logging.info('Access token acquired')
            response_json = dict(
                    urllib.parse.parse_qsl(urllib.parse.urlparse(r.headers['Location']).fragment)
                )
            #print(response_json)
            #print(response_json['access_token'])
            return response_json['access_token']
        else:
            raise ValueError('Unable to login')

    except Exception as msg:
        logging.error(msg)

havrla avatar Nov 22 '20 19:11 havrla

Hello, Thanks! It shows the differences. There seems to be some differences in your authentification compared to bimmer cli:

  • The auth url is: 'https://' + BASE_URL + '/gcdm/oauth/authenticate' instead of 'https://' + BASE_URL + '/gcdm/oauth/token'
  • The values (data) for the auth request is different, you have some more values: 'client_id': 'dbf0a542-ebd1-4ff0-a9a7-55172fbfce35', 'response_type': 'token', 'redirect_uri': 'https://www.bmw-connecteddrive.com/app/static/external-dispatch.html',
  • The expected return state is 302 instead of 200 (token acquired)
  • the response_json is different (parse_qsl)

If I do this changes then all services are working again.

@gerard33 or @rikroe Could you change the code?

Markinus avatar Nov 22 '20 20:11 Markinus

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Feb 21 '21 03:02 github-actions[bot]

Reopen this as we still have to figure out the best way to deal with the two logins.

rikroe avatar Feb 28 '21 09:02 rikroe

Helooo :-) any news ? :-) Thanks :-)

havrla avatar Aug 03 '21 20:08 havrla

Ist there any progress? Would definately appreciate, if I could track the degrading of my battery... Or did I miss it?

vielleicht avatar Dec 30 '21 21:12 vielleicht

Unfortunately, in my testings, I was not able to access the navigation endpoint (that seems to be the only one that contains socMax) any more.

If anybody has new clues or if it is still working for some, please let me know.

rikroe avatar Dec 30 '21 22:12 rikroe

Hello this is a functional no problem:

[root@CL-ITNK-BMW ~]# /usr/local/bin/bmw-going.py -v WBY1Z41000VZ75700 -u "[email protected]" -p "supertajneheslo"
socMAX 29.24

[root@CL-ITNK-BMW ~]#

havrla avatar Jan 03 '22 09:01 havrla

@rikroe @gerard33 Hi, is there any chance that you can get socmax implemented in HA ? I can see that @havrla is able to get socmax value above... This is the MOST interesting parameter of all, when it comes to electrical drive trains, and would be highly valuable to log over time (since it clearly identifies the "useable" battery capacity over time)

obhammer avatar Feb 22 '22 11:02 obhammer

Hello this is a functional no problem:

[root@CL-ITNK-BMW ~]# /usr/local/bin/bmw-going.py -v WBY1Z41000VZ75700 -u "[email protected]" -p "supertajneheslo" socMAX 29.24

[root@CL-ITNK-BMW ~]#

@havrla does it still work for you ? I was using the /vehicle/navigation/v1/ endpoint since last week but it broke for me. Is there still a way to get to that socMax data ? one of the most important data points of all. It cleary shows the degradation over time when you plot it in a graph:

jollyjinx avatar Mar 01 '22 06:03 jollyjinx

24.2. 13:45 Stop graf https://cloud.itnaklic.cz/grafy-i3/d/SKrxZgxWk/bmw-i3-rex-vz75700?from=now-7d&to=now host/navsteva

This problem:

https://customer.bmwgroup.com/gcdm/oauth/authenticate <Response [401]>

no get token, valid respons code 302

havrla avatar Mar 01 '22 09:03 havrla

Closing this as the socMax data is currently not available in the api.

gerard33 avatar Mar 16 '23 22:03 gerard33