BMW ConnectedDrive component

Edit: BMW ConnectedDrive is available in Home Assistant 0.64.
Check this post for further explanation.


I came across this URL today https://github.com/jupe76/bmwcdapi which contains a Python script to access the BMW ConnectedDrive which is used for Openhab. This is really awesome :slight_smile:

All you need is your username, password and vehicle identification number (VIN) to get access to the data of your car.
In other projects I saw there was an API key and secret needed which is difficult to get, but for this script that’s not necessary.

I have changed the Python script a bit for testing purposes so it works as a standalone script. See the working example with some data of my car in the screenshot below.
The original script is developed for the BMW i3, but it’s working for my BMW X1 as well.

Edit: check this post with instructions where to download the Python script I have made. It also contains the explanation what you need to add in it so you can run it from CLI.

I also found this link which has info on the url which contains a json with 36 urls to rendered images of your car. All you need is the VIN for this.

I will dive into this to see if I can make a custom component based on the Volvo On Call component.
Once a first version is available I will post it here. Please allow me some time for it as I am quite busy and I am not a developer so it will take me a lot of trial and error :roll_eyes:

screenshot

10 Likes

Great! Keep us posted. Really want this to work. I have one Volvo and one BMW.

That makes you an ideal tester :wink:
Do you already use the Volvo On Call component? And which type of BMW you have?

There is also a PR for Mercedes me done from which I can reuse quite some code, so that will be helping me a lot.

Would you mind sharing your current modified script that you have used for the screenshot? I’m curious if it works with my X1 as well. When using the App im limited to (un)locking the doors, locateing the car and controlling the climate. Would be neat if I could get the milage and fuel like you can.

HELL YES! Thank you. BTW- I have a BMW i3 so I can test once you have something ready.

I’m looking forward for your progress! Currently I’ve got an BMW X1 (2017 model) and using the BMW connected android app for connected drive services.

| parameter long         | parameter short | Description                                                 |
| ---------------------- | ----------------|-------------------------------------------------------------|
|--help                  | -h              | show commandline help                                       |
|--printall              | -p              | print all values, usefull for debuging or just for exploring|

When I try to call the bmwcdapi.py -p i receive the following error:

[email protected]:~/bmwcdapi $ python3 bmwcdapi.py -p
...running bmwcdapi.py
Traceback (most recent call last):
  File "bmwcdapi.py", line 263, in <module>
    main()
  File "bmwcdapi.py", line 234, in main
    c = ConnectedDrive()
  File "bmwcdapi.py", line 43, in __init__
    self.bmwUsername = self.ohGetValue("Bmw_Username").json()["label"]
  File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Hi Gerard,

I am using the VOC component for our Volvo V60 Twin Engine (would also be nice to get more info on battery and charging for that). The BMW is a BMW M2 Model Year 2017. With Connected Drive.
Tell me what to do and I can gladly be your guinea pig. :slight_smile:

You can download the script at https://github.com/gerard33/bmwcd/blob/master/bmwcd/bmwcdapi.py.
Script is still very messy and under a lot of development, but ready for some test on the CLI.

The request module needs to be available on your server (sudo pip install requests).

In the script add the information where it now states None, this starts at line 58. Place the information between quotes as shown in the examples.

# ----=================================================----
# Enter the data below to be able to login
# Your BMW ConnectedDrive username
USERNAME = None             # "[email protected]"

# Your BMW ConnectedDrive password
PASSWORD = None             # "your_password"

# 17!! chars Vehicle Identification Number (VIN) of the car, can be found in the app or on BMW ConnectedDrive online
VIN = None                  # "vin_code"

# This is the URL you use to login to BMW ConnectedDrive, e.g. www.bmw-connecteddrive.nl or www.bmw-connecteddrive.de
URL = None                  # "www.bmw-connecteddrive.nl"

Then you are able to run it with:
./bmwcdapi.py -p

Curious to hear if you have more information available than in the app.
Be aware that the output contains your gps data, so edit that before copying any data over here :face_with_monocle:

@irqnet, you can’t run the script from the link in my first post as that is intended to be used with Openhab.
But you can use my script from this post.

@JesseWebDotCom, could you also run this script and send me the output (remove gps data)?
I would like to see how the i3 specific data (charging etc.) is shown, so I know how to put that in some sensors.

1 Like

I just realize I really need a test drive in your M2 before I can add this component to Home Assistant :wink:
That’s a really nice car!

Can you send me some screenshots on how the Volvo stuff (sensors etc) are shown in Home Assistant? I can use that as inspiration for how to show the BMW ConnectedDrive data.

1 Like

Been waiting for a post like this, I have a 2017 4 GC, I’ll download the script and have a play, happy to help with testing!

I get an error:

Traceback (most recent call last):
  File "/Users/me/PycharmProjects/untitled/bmwcdapi.py", line 375, in <module>
    main()
  File "/Users/me/PycharmProjects/untitled/bmwcdapi.py", line 342, in main
    c = ConnectedDrive()
  File "/Users/me/PycharmProjects/untitled/bmwcdapi.py", line 84, in __init__
    self.generate_credentials()
  File "/Users/me/PycharmProjects/untitled/bmwcdapi.py", line 125, in generate_credentials
    tokenType = (m.group(2))
AttributeError: 'NoneType' object has no attribute 'group'

Process finished with exit code 1

and the mypayload variable was set to: https://www.bmw-connecteddrive.com/app/default/static/external-dispatch.html?error=access_denied

Well, I got further. It looks like I have a connecteddrive.bmwusa.com account where I can see my car, its status, and everything and separately I have a customer.bmwgroup.com where I have no cars listed. It looks like this code lists cars from the second account and then would get the details from the first. Because it can’t find my car, I now get:

UnboundLocalError: local variable 'map_car_data' referenced before assignment
<Response [404]>

I’ll try and call BMW tomorrow to see if they can merge the accounts or minimally ensure my car is on the second account.

I’ve got nearly the same error:

…running bmwcdapi.py
400
Traceback (most recent call last):
File “bmwcdapi.py”, line 345, in
main()
File “bmwcdapi.py”, line 333, in main
execStatusCode = c.get_car_data()
File “bmwcdapi.py”, line 163, in get_car_data
return map_car_data
UnboundLocalError: local variable ‘map_car_data’ referenced before assignment

It looks like the variables map_car_data and map_car_navigation where not defined before return in the specified functions.

I’ve tested that by adding two lines in the script.

in function:

def get_car_data(self):
map_car_data = 0

and

def get_car_navigation(self):
map_car_navigation = 0

result:

[email protected]:~/bmwcd/bmwcd $ python3 bmwcdapi.py -p
...running bmwcdapi.py
400
400
400
400

It seems that is a return of the http request… 400 bad request?

I indeed noticed that the url for USA differs from other countries, where it is www.bmw-connecteddrive.[countrycode].
I will have a look at the customer.bmwgroup.com url to see if my car is registered there.

Edit: I go to https://www.bmw-connecteddrive.nl and when pressing the Login button that forwards me to https://customer.bmwgroup.com where I can enter my credentials.

Which url did you use in the script?
And have you used the 17 digits VIN code, because the short code of 5 digits doesn’t work?

In the meanwhile I have made some data from BMW ConnectedDrive available in Home Assistant :slight_smile:

Still a lot of work to do, but here some screenshots.

Device tracker

Binary sensor

Ah i will check that. Using the german URL with .de ending but using the short VIN Code like in the BMW App. Have to grab my papers out of the car to check the full number.

The full VIN is shown on the bottom of the BMW ConnectedDrive app (at least in the Dutch version).

And the url shoud not contain https:// in front of it, should be just like the example I have put in the comments in the script at that line.

I will add some code in the script to check for this.

Changing the VIN to the full number causes an 404 return of the script, and yes I’m using the URL without https or http.

Did you put all these fields within quotes in the script? So like this example:
URL = "www.bmw-connecteddrive.de"