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

0.8.0: No support for `china` #347

Closed rikroe closed 2 years ago

rikroe commented 2 years ago

Describe the issue

Starting with 0.8.0, support for region china had to be dropped.

We are currently unable to figure out the login mechanism used. If you want to jump in and help us fixing it, we can provide full network traffic.

Workaround: Use bimmer_connected<0.8.0 or the Home Assistant custom component BMW ConnectedDrive China (to be released soon).

Expected behavior

Login to china possible, reuse of existing vehicle endpoints.

Which Home Assistant version are you using?

none

What was the last working version of Home Assistant Core?

No response

What is your region?

China

ConnectedDrive website

Number of cars

Output of bimmer_connected fingerprint

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

muxiachuixue commented 2 years ago

Hi, I am a bimmer and HA user from China. I have figured out the login mechanism in MyBMW. However, I am not a programmer and know nothing about python. You can find the login mechanism in the Siri Shortcuts I wrote for BMW:

All Shortcuts download link MyBMW login Shortcuts

Hope this helps.

zhaozengxiao commented 2 years ago

Hello,BMW ConnectedDrive China How long is it expected to be released

rikroe commented 2 years ago

@muxiachuixue thanks! Unfortunately I am not able to translate what you have written. @zhaozengxiao no idea :( If the login issue is understood, will be very quick.

zhaozengxiao commented 2 years ago

\"command_url_prefix\":\"https://myprofile.bmw.com.cn/eadrax-vrccs/v2/presentation/remote-commands/\", \"climate_on_url_postfix\":\"/climate-now?action=START\", \"climate_off_url_postfix\":\"/climate-now?action=STOP\", \"lock_url_postfix\":\"/door-lock\", \"unlock_url_postfix\":\"/door-unlock\", \"light_url_postfix\":\"/light-flash\", \"horn_url_postfix\":\"/horn-blow\", \"event_url_postfix\":\"eventStatus?eventId=\", \"status_url\":\"https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles?apptimezone=480&amp;appDateTime=\", \"status_vin_url_prefix\":\"https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles/\", \"status_vin_url_postfix\":\"/state?apptimezone=480&appDateTime=\", \"token_url\":\"https://myprofile.bmw.com.cn/eadrax-coas/v1/oauth/token\", \"login_url\":\"https://myprofile.bmw.com.cn/eadrax-coas/v1/login/pwd\", \"publickey_url\":\"https://myprofile.bmw.com.cn/eadrax-coas/v1/cop/publickey\", \"jsencrypt_url\":\"https://gitee.com/ichuixue/bmw_shortcuts/raw/master/BMW_JSEncrypt.js\", \"check_in_url\":\"https://myprofile.bmw.com.cn/cis/eadrax-community/private-api/v1/mine/check-in\", \"joy_list_url\":\"https://myprofile.bmw.com.cn/cis/eadrax-membership/api/v2/joy-list\", \"article_list_url\":\"https://myprofile.bmw.com.cn/cis/eadrax-ocommunity/public-api/v1/article-list\", \"share_article_url\":\"https://myprofile.bmw.com.cn/cis/eadrax-oarticle/open/article/api/v2/share-article\", \"joy_info_url\":\"https://myprofile.bmw.com.cn/cis/eadrax-membership/api/v1/joy-info\",

Excuse me, is this useful?

muxiachuixue commented 2 years ago

@muxiachuixue thanks! Unfortunately I am not able to translate what you have written. @zhaozengxiao no idea :( If the login issue is understood, will be very quick.

I will make a clear one for you.

muxiachuixue commented 2 years ago

@rikroe Now the Shortcuts for MyBMW in China will be more easier to read and understand: MyBMW login Lock MyBMW Status of MyBMW

Anything I can help, please let me know.

rikroe commented 2 years ago

When I click on one of the shortcuts, it says "not found". Do I need an iPhone for this? Also on MacOS I get a "not found".

Just saw your link to BMW_JSEncrypt.js. This is the only thing missing on is how to encrypt the password for login/pwd. From what you posted I understand the password is send using RSA public key encryption.

However I am not able to (re-)create valid passwort hashes that I have seen. Do you only need to encrypt the password? Or More?

EDIT: Nevermind, I figured it out! Will try to implement it before it gets merged into Home Assistant.

EDIT2: got a first implementation working. Will create some tests and then give it to you for testing.

zhaozengxiao commented 2 years ago

@rikroe I'm just a porter, All his credit @muxiachuixue ,I really want to take the test, thank you

muxiachuixue commented 2 years ago

When I click on one of the shortcuts, it says "not found". Do I need an iPhone for this? Also on MacOS I get a "not found".

Just saw your link to BMW_JSEncrypt.js. This is the only thing missing on is how to encrypt the password for login/pwd. From what you posted I understand the password is send using RSA public key encryption.

However I am not able to (re-)create valid passwort hashes that I have seen. Do you only need to encrypt the password? Or More?

EDIT: Nevermind, I figured it out! Will try to implement it before it gets merged into Home Assistant.

EDIT2: got a first implementation working. Will create some tests and then give it to you for testing.

The IOS Shortcuts need the App "Scriptable" to run the JavaScript codes. If you have not installed the App, a "not found" error will come out. If you need a Chinese BMW account for testing, please let me and @zhaozengxiao know. Thanks again for the efforts.

muxiachuixue commented 2 years ago

@rikroe After installing the bimmer-connected-0.8.1.dev1 version on my raspberry pi 4B, I executed the following command: bimmerconnected status 8615201231234 password1234 china

And got the error outputs:

`DEBUG:bimmer_connected.account:Getting vehicle list DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-coas/v1/cop/publickey HTTP/1.1" 200 356 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "POST /eadrax-coas/v1/login/pwd HTTP/1.1" 200 731 DEBUG:bimmer_connected.account:got new token eyJhbGciOiJIUzI1NiIsInR5xxxxxxxxImV4cCI6MTYzNzc1OTY0NywiaWF0Ijxxxxxxxr5V62o5yw9urSL2IvMG9IzyF7s with expiration date 2021-11-24 13:14:07 DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637727547.929913&tireGuardMode=ENABLED HTTP/1.1" 200 7517 DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637727549.340775&tireGuardMode=ENABLED HTTP/1.1" 200 2 DEBUG:bimmer_connected.account:Getting vehicle list DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637727550.503834&tireGuardMode=ENABLED HTTP/1.1" 200 7517 DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637727551.604555&tireGuardMode=ENABLED HTTP/1.1" 200 2 Found 1 vehicles: 325i VIN: LBV5F340ABCD01234 Mileage: (13547, 'km') Vehicle data: DEBUG:bimmer_connected.vehicle_status:No data available for attribute <function VehicleStatus.charging_level_hv at 0xb56f18e8>! ValueError: '已上锁' is not a valid LockState

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/pi/.local/bin/bimmerconnected", line 10, in sys.exit(main()) File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/cli.py", line 279, in main args.func(args) File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/cli.py", line 117, in get_status print(to_json(vehicle, indent=4)) File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/utils.py", line 41, in to_json return json.dumps(obj, default=serialize, args, kwargs) File "/usr/lib/python3.7/json/init.py", line 238, in dumps kw).encode(obj) File "/usr/lib/python3.7/json/encoder.py", line 201, in encode chunks = list(chunks) File "/usr/lib/python3.7/json/encoder.py", line 439, in _iterencode yield from _iterencode(o, _current_indent_level) File "/usr/lib/python3.7/json/encoder.py", line 431, in _iterencode yield from _iterencode_dict(o, _current_indent_level) File "/usr/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict yield from chunks File "/usr/lib/python3.7/json/encoder.py", line 438, in _iterencode o = _default(o) File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/utils.py", line 40, in serialize return getattr(obj, 'to_json', getattr(obj, 'dict') if hasattr(obj, 'dict') else str(obj)) File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/utils.py", line 64, in to_json return serialize_for_json(self) File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/utils.py", line 25, in serialize_for_json {a: getattr(obj, a) for a in get_class_property_names(obj) if a not in excluded + ["to_json"]} File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/utils.py", line 25, in {a: getattr(obj, a) for a in get_class_property_names(obj) if a not in excluded + ["to_json"]} File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/vehicle_status.py", line 140, in _func_wrapper return func(self, args, **kwargs) File "/home/pi/.local/lib/python3.7/site-packages/bimmer_connected/vehicle_status.py", line 306, in door_lock_state return LockState(self.status['doorsGeneralState'].upper()) File "/usr/lib/python3.7/enum.py", line 310, in call return cls.new(cls, value) File "/usr/lib/python3.7/enum.py", line 564, in new raise exc File "/usr/lib/python3.7/enum.py", line 548, in new result = cls.missing(value) File "/usr/lib/python3.7/enum.py", line 577, in missing raise ValueError("%r is not a valid %s" % (value, cls.name)) ValueError: '已上锁' is not a valid LockState `

zhaozengxiao commented 2 years ago

@rikroe Thank you for your efforts!I want to know how to use the new version in Home Assistant?Thank you!!!

rikroe commented 2 years ago

@muxiachuixue ouch, seems the API is returning chinese instead of english...

Could you please try one thing for me and and edit /home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py L272-L276 so it looks like this (added accept-language):

        headers = {
            "accept": "application/json",
            "x-user-agent": X_USER_AGENT.format(brand.value),
            "Authorization": "Bearer {}".format(self._oauth_token),
            "accept-language": "en",
        }

Let me know if that works for you!

@zhaozengxiao it just got merged into Home Assistant, so it should be included in the next major version.

muxiachuixue commented 2 years ago

@muxiachuixue ouch, seems the API is returning chinese instead of english...

Could you please try one thing for me and and edit /home/pi/.local/lib/python3.7/site-packages/bimmer_connected/account.py L272-L276 so it looks like this (added accept-language):

        headers = {
            "accept": "application/json",
            "x-user-agent": X_USER_AGENT.format(brand.value),
            "Authorization": "Bearer {}".format(self._oauth_token),
            "accept-language": "en",
        }

Let me know if that works for you!

@zhaozengxiao it just got merged into Home Assistant, so it should be included in the next major version.

After adding the "accept-language" line as you told, I can get the correct outputs of the car status json data. However, before the json data, you can see the following lines. Is it OK?

DEBUG:bimmer_connected.account:Getting vehicle list DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-coas/v1/cop/publickey HTTP/1.1" 200 356 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "POST /eadrax-coas/v1/login/pwd HTTP/1.1" 200 731 DEBUG:bimmer_connected.account:got new token eyJhbGciOiJIUzI1NiIsIxxxxxBJDE2Mzc4MTAyMjUwNTMiLCJuYmYiOjE2Mxxxxxx3ODEwMjI1fQ._F33fIjXbh5Sn0srxxxxxpcQWTqdVBY with expiration date 2021-11-25 04:12:05 DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637781425.01832&tireGuardMode=ENABLED HTTP/1.1" 200 7410 DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637781426.85447&tireGuardMode=ENABLED HTTP/1.1" 200 2 DEBUG:bimmer_connected.account:Getting vehicle list DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637781429.366527&tireGuardMode=ENABLED HTTP/1.1" 200 7410 DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Request to: https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:bimmer_connected.account:Old token is still valid. Not getting a new one. DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): myprofile.bmw.com.cn:443 DEBUG:urllib3.connectionpool:https://myprofile.bmw.com.cn:443 "GET /eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637781430.841364&tireGuardMode=ENABLED HTTP/1.1" 200 2 Found 1 vehicles: 325i VIN: LBV5xxxxABCD12345 Mileage: (13569, 'km') Vehicle data: DEBUG:bimmer_connected.vehicle_status:No data available for attribute <function VehicleStatus.charging_level_hv at 0xb56c1858>!

{
"attributes": { "vin": "LBV5xxxxABCD12345", "vehicleType": "connected", "licensePlate": "XXXXXX", "cdActiveStatus": "ACTIVATED", ... }

muxiachuixue commented 2 years ago

It is quite strange that, the car status json data from the IOS shortcut shows less information than the data from your bimmerconnected program in linux. For example, the "last_update_reason" does not show in the shortcut result. The POST request is like this in IOS shortcut:

GET 'https://myprofile.bmw.com.cn/eadrax-vcs/v1/vehicles?apptimezone=480.0&appDateTime=1637781426.85447&tireGuardMode=ENABLED'

Header: { "accept":"application/json" "Authorization":"Bearer $access_token", "x-user-agent":"ios(14.8);bmw;1.4.0(7743)" "accept-language":"en" }

Same in postman as IOS shortcut. What did I miss?

Edit: I got it. The information I missed is defined in your codes. As for the "inMotion" attribute, when I start the engine for several minutes (not move), the "inMotion" value is still false. Haven't try to check it when the car is moving.

rikroe commented 2 years ago

Thanks, this is looking good then!

zhaozengxiao commented 2 years ago

@rikroe Hello, I am a beginner in python. After I pull the package, what command should I use to test? ? ?

'bimmerconnected fingerprint <8618860629999> <123456789> '

Enter this in the console?

rikroe commented 2 years ago

It is not a implemented in the official package yet, I will do that after work and let you know.

Also I'll give you the command you need to use.

zhaozengxiao commented 2 years ago

It is not a implemented in the official package yet, I will do that after work and let you know.

Also I'll give you the command you need to use.

Thanks @rikroe

muxiachuixue commented 2 years ago

It is not a implemented in the official package yet, I will do that after work and let you know.

Also I'll give you the command you need to use.

bimmerconnected status 8615201231234 password1234 china

zhaozengxiao commented 2 years ago

It is not a implemented in the official package yet, I will do that after work and let you know. Also I'll give you the command you need to use.

bimmerconnected status 8615201231234 password1234 china

Thanks @muxiachuixue

rikroe commented 2 years ago

Just released 0.8.3.0b1.

It can be installed via pip3 install --upgrade bimmer_connected==0.8.3.0b1 and should continue the fix for chinese responses in the My BMW API.

I have just updated the custom component as well to try it out.

muxiachuixue commented 2 years ago

Just released 0.8.3.0b1.

It can be installed via pip3 install --upgrade bimmer_connected==0.8.3.0b1 and should continue the fix for chinese responses in the My BMW API.

I have just updated the custom component as well to try it out.

Nice, I have tried the HA custom component 0.8.3.0b4. It works well. Thanks again.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.