bimmerconnected / bimmer_connected

🚘 Library to query the status of your BMW or Mini from the ConnectedDrive portal
Apache License 2.0
362 stars 79 forks source link

Add socMax for electric/hybrid cars #158

Closed havrla closed 1 year ago

havrla commented 4 years ago

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

damienheiser commented 4 years ago

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!

rikroe commented 4 years ago

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.

stale[bot] commented 4 years ago

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.

havrla commented 4 years ago

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 ...

z8i commented 4 years ago

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?

Markinus commented 3 years ago

This is supported now in master.

z8i commented 3 years ago

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?

rikroe commented 3 years ago

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:

So please be a little patient 😉

Markinus commented 3 years ago

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?

havrla commented 3 years ago

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)
Markinus commented 3 years ago

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

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

@gerard33 or @rikroe Could you change the code?

github-actions[bot] commented 3 years ago

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.

rikroe commented 3 years ago

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

havrla commented 3 years ago

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

vielleicht commented 2 years ago

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

rikroe commented 2 years ago

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.

havrla commented 2 years ago

Hello this is a functional no problem:

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

[root@CL-ITNK-BMW ~]#

obhammer commented 2 years ago

@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)

jollyjinx commented 2 years ago

Hello this is a functional no problem:

[root@CL-ITNK-BMW ~]# /usr/local/bin/bmw-going.py -v WBY1Z41000VZ75700 -u "xxxx@xxx.cz" -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:

havrla commented 2 years ago

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

gerard33 commented 1 year ago

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