Script to access the BMW ConnectedDrive portal via OH

I´m only using the exec binding in version 2.4.0 and have no issues.

Regarding python3:
I´m directly calling the python3 environment and didn´t choose python3 as default.
executeCommandLine("/usr/bin/python3 /etc/openhab2/scripts/bmwcdapi.py", 5000)

I’ve been playing with this script to get it working on a bmw 535 from 2011, untill now without success.
Filled in the required details in the .items file like username, password, VIN. I have a working ConnectedDrive account.

Copied all the files but having some issues.
my OH 2.4 is complaining about ImportError: No module named ‘requests’

when running “/usr/bin/python3 /etc/openhab2/scripts/bmw.py” I Get the error:
Traceback (most recent call last):
File “/etc/openhab2/scripts/bmw.py”, line 24, in
import requests
ImportError: No module named ‘requests’

I do not read I have to manually install python-3, I assume it is alreayd installed on OH2.4 (running on a RaspberryPi3B)

Any suggestions?

I’m not familiair with any coding, I can read and understand it a little bit.

You need to install the python module requests.
I‘m currently on mobile without access to my Pi3.
Please try to google it.

Hi, thanks for the quick response.
I already tried this:

[23:19:53] openhabian@openhab:~$ sudo pip install requests
Requirement already satisfied: requests in /usr/local/lib/python2.7/dist-packages Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python2.7/dist-packages (from requests) Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/lib/python2.7/dist-packages (from requests) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/dist-packages (from requests) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/dist-packages (from requests)

So I assume the module is already installed. What am I missing here?

That the module is only installed for Python 2.7 but not Python 3 :wink:
You need to use pip3 for Python 3.

Thank you, after some googling I managed to install Python3 using: sudo apt install python3-pip

After this I could install the module using sudo pip3 install requests
And now I can start my script. Nothing really happens yet, I’ll investigate further.

Please have a look at the .py script and search for the logging. There‘s a line to enable direct output.
Otherwise the script only trys to send the values to openHAB.

oke, I the python script working, and it is providing me outpout via ssh. great

Thanks guys, all this info on getting the Python3 env setup for it helped. I can now get it to run properly.

Still not working for me though… I am getting could not authenticate, user or password wrong?
I have just used the same UN and PW to log into the ConnectedDrive web site and can see my car in there. Any clue to what may be the issue?

OK… I did find out something…
I am based in the US and using https://connecteddrive.bmwusa.com where I can login and see my car. As a test I figured I would look for a global ConnectedDrive site and found this one: https://www.bmw-connecteddrive.my

After I completed the register process I am no longer getting the UN & PW error. However, when I try to add my vehicle there it says it cannot validate this vehicle. So obviously this plugin was written for EU based cars but has issues with the US ConnectedDrive version.

Any ideas anyone??

Yes, I’ve coded the script for my own car and I’m based in europe.

So for the States I think you’ll have to adjust these two lines:

AUTH_API = ‘https://customer.bmwgroup.com/gcdm/oauth/authenticate
VEHICLE_API = ‘https://www.bmw-connecteddrive.de/api/vehicle

Unfortunately I can’t provide working entries for you right now.
There are some similar scripts for other smarthome software, AFAIR they have urls that are work for other parts of the world as well.

Will have a look into my records an report back if I find something promising.

Thank you!
I tried to do a plain replace for your URL suffixes with this prefix: https://connecteddrive.bmwusa.com but that just returned error messages. Whatever you can find and would like me to “beta test” I am more than happy to!

Thanks you!!

@Moshe_Dahan

Could you please try if it works if you change VEHICLE_API to

VEHICLE_API = 'https://b2vapi.bmwgroup.us/api/vehicle'

don’t think it helped…
I tried this as suggested: VEHICLE_API = 'https://b2vapi.bmwgroup.us/api/vehicle'
then, since it didn’t work, tried this as well: AUTH_API = 'https://customer.bmwgroup.us/gcdm/oauth/authenticate'

doesn’t see to do the trick… I still just see this in the site map:

Openhab log just has this in it:
2019-01-18 14:27:44.815 [INFO ] [ipse.smarthome.model.script.bmwcdapi] - forced connectedDrive API call
2019-01-18 14:27:46.583 [INFO ] [ipse.smarthome.model.script.bmwcdapi] - …running bmwcdapi.py

Ok, one more try please:

AUTH_API = 'https://b2vapi.bmwgroup.us/gcdm/oauth/token'
VEHICLE_API = 'https://b2vapi.bmwgroup.us/api/vehicle'

Interesting… I get this output on both openhab.log and if running from cli with -p:
...running bmwcdapi.py
Traceback (most recent call last):
File "bmwcdapi.py", line 303, in <module>
main()
File "bmwcdapi.py", line 267, in main
c = ConnectedDrive()
File "bmwcdapi.py", line 49, in __init__
self.generateCredentials()
File "bmwcdapi.py", line 77, in generateCredentials
myPayLoad=r.headers['Location']
File "/usr/local/lib/python3.5/dist-packages/requests/structures.py", line 52, in __getitem__
return self._store[key.lower()][1]
KeyError: 'location'

Yes, same here, the persistence for accessToken tricked me …

Seems that I’ll have to rewrite the method generateCredentials to use the urls above.
Maybe I’ll have some time on the weekend to do that.

1 Like

I have exactly the same problem, also based in US. Did a lot of trial and error before I read this topic to the end. Peter, I hope you can fix it.

@Moshe_Dahan @stopeject

Ready for a another try?

I have changed the method “generateCredentials” to work with the changed urls (code borrowed from the bimmer_connected lib).
Works for me, so it hopefully would work for you as well.

I’ve made those changes in the development branch.
To try it out to a “git pull” and “git checkout development” or switch to development branch in the github webui.

IMPORTANT:
You have to change to the following lines s.b. for North America (comment in the .us server url and comment out the .com url) :

#NORTH_AMERICA:
SERVER_URL = 'b2vapi.bmwgroup.us'
#CHINA:
#SERVER_URL = 'b2vapi.bmwgroup.cn:8592'
#REST_OF_WORLD:
#SERVER_URL = 'b2vapi.bmwgroup.com'

If the change works out for you guys, I will make that configurable via an oh item.

Works for me!
Thanks so much for getting it fixed so fast!
My prompt looks weird, I’m running it on a Synology NAS.
Now need to figure out the openHAB part…

root@S:/volume1/@appstore/openHAB/conf/scripts# /volume1/@appstore/python3/bin/python3 bmwcdapi.py --printall
...running bmwcdapi.py
hood_state CLOSED
chargingLevelHv 79.0
DCS_CCH_Ongoing  None
unitOfElectricConsumption mls/kWh
vehicle_tracking 0
Segment_LastTrip_time_segment_end_formatted_date 01/18/2019
lastUpdateReason VEHCSHUTDOWN
unitOfLength mls
lastChargingEndResult SUCCESS
lights_parking OFF
unitOfEnergy kWh
window_driver_front CLOSED
lastChargingEndReason CONNECTOR_REMOVED
lsc_trigger VEHCSHUTDOWN
charging_connection_type CONDUCTIVE
charging_status NOCHARGING
updateTime_converted_date 01/19/2019
beMaxRangeElectricKm 161.0
check_control_messages 
updateTime_converted_timestamp 1547860215000
chargingHVStatus INVALID
beRemainingRangeElectricKm 126.0
kombi_current_remaining_range_fuel 0.0
soc_hv_percent 81.2
chargingSystemStatus NOCHARGING
unitOfCombustionConsumption mpg
door_passenger_front CLOSED
DCS_CCH_Activation None
chargeNowAllowed NOT_ALLOWED
condition_based_services 00003,OK,2020-10,;00017,OK,2020-10,
mileage 1611
door_lock_state SECURED