Skip to content

Commit c0d72d6

Browse files
authored
Merge pull request #2035 from rleidner/soc_bmwbc_p1
soc_bmwbc: add new captcha for initial login
2 parents ac93afb + 82b8df2 commit c0d72d6

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

packages/modules/vehicles/bmwbc/api.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
from modules.common.component_state import CarState
2222
from modules.common.store import RAMDISK_PATH
23+
from pathlib import Path
24+
25+
DATA_PATH = Path(__file__).resolve().parents[4] / "data" / "bmwbc"
2326

2427
log = logging.getLogger(__name__)
2528

@@ -36,6 +39,7 @@ def init_store():
3639

3740
# load store from file, if no store file exists initialize store structure
3841
def load_store():
42+
global storeFile
3943
try:
4044
with open(storeFile, 'r', encoding='utf-8') as tf:
4145
store = json.load(tf)
@@ -54,6 +58,7 @@ def load_store():
5458

5559
# write store file
5660
def write_store(store: dict):
61+
global storeFile
5762
with open(storeFile, 'w', encoding='utf-8') as tf:
5863
json.dump(store, tf, indent=4)
5964

@@ -66,9 +71,17 @@ def dump_json(data: dict, fout: str):
6671

6772

6873
# ---------------fetch Function called by core ------------------------------------
69-
async def _fetch_soc(user_id: str, password: str, vin: str, vnum: int) -> Union[int, float]:
74+
async def _fetch_soc(user_id: str, password: str, vin: str, captcha_token: str, vnum: int) -> Union[int, float]:
7075
global storeFile
71-
storeFile = str(RAMDISK_PATH) + '/soc_bmwbc_vh_' + str(vnum) + '.json'
76+
try:
77+
log.debug("dataPath=" + str(DATA_PATH))
78+
DATA_PATH.mkdir(parents=True, exist_ok=True)
79+
except Exception as e:
80+
log.error("init: dataPath creation failed, dataPath: " +
81+
str(DATA_PATH) + ", error=" + str(e))
82+
store = init_store()
83+
return store
84+
storeFile = str(DATA_PATH) + '/soc_bmwbc_vh_' + str(vnum) + '.json'
7285

7386
try:
7487
# set loggin in httpx to WARNING to prevent unwanted messages
@@ -85,12 +98,15 @@ async def _fetch_soc(user_id: str, password: str, vin: str, vnum: int) -> Union[
8598
expires_at=expires_at)
8699
else:
87100
# no token, authenticate via user_id and password only
88-
auth = MyBMWAuthentication(user_id, password, Regions.REST_OF_WORLD)
101+
auth = MyBMWAuthentication(user_id,
102+
password,
103+
Regions.REST_OF_WORLD,
104+
hcaptcha_token=captcha_token)
89105

90106
clconf = MyBMWClientConfiguration(auth)
91107
# account = MyBMWAccount(user_id, password, Regions.REST_OF_WORLD, config=clconf)
92108
# user, password and region already set in BMWAuthentication/ClientConfiguration!
93-
account = MyBMWAccount(None, None, None, config=clconf)
109+
account = MyBMWAccount(None, None, None, config=clconf, hcaptcha_token=captcha_token)
94110

95111
# get vehicle list - needs to be called async
96112
await account.get_vehicles()
@@ -130,13 +146,13 @@ async def _fetch_soc(user_id: str, password: str, vin: str, vnum: int) -> Union[
130146

131147

132148
# main entry - _fetch needs to be run async
133-
def fetch_soc(user_id: str, password: str, vin: str, vnum: int) -> CarState:
149+
def fetch_soc(user_id: str, password: str, vin: str, captcha_token: str, vnum: int) -> CarState:
134150

135151
# prepare and call async method
136152
loop = asyncio.new_event_loop()
137153
asyncio.set_event_loop(loop)
138154

139155
# get soc, range from server
140-
soc, range = loop.run_until_complete(_fetch_soc(user_id, password, vin, vnum))
156+
soc, range = loop.run_until_complete(_fetch_soc(user_id, password, vin, captcha_token, vnum))
141157

142158
return CarState(soc, range)

packages/modules/vehicles/bmwbc/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ def __init__(self,
66
user_id: Optional[str] = None,
77
password: Optional[str] = None,
88
vin: Optional[str] = None,
9+
captcha_token: Optional[str] = None,
910
calculate_soc: bool = False):
1011
self.user_id = user_id
1112
self.password = password
1213
self.vin = vin
14+
self.captcha_token = captcha_token
1315
self.calculate_soc = calculate_soc
1416

1517

packages/modules/vehicles/bmwbc/soc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,27 @@ def updater(vehicle_update_data: VehicleUpdateData) -> CarState:
2121
vehicle_config.configuration.user_id,
2222
vehicle_config.configuration.password,
2323
vehicle_config.configuration.vin,
24+
vehicle_config.configuration.captcha_token,
2425
vehicle)
2526
return ConfigurableVehicle(vehicle_config=vehicle_config,
2627
component_updater=updater,
2728
vehicle=vehicle,
2829
calc_while_charging=vehicle_config.configuration.calculate_soc)
2930

3031

31-
def bmwbc_update(user_id: str, password: str, vin: str, charge_point: int):
32+
def bmwbc_update(user_id: str, password: str, vin: str, captcha_token: str, charge_point: int):
3233
log.debug("bmwbc: user_id="+user_id+"vin="+vin+"charge_point="+str(charge_point))
33-
vehicle_config = BMWbc(configuration=BMWbcConfiguration(charge_point, user_id, password, vin))
34+
log.debug("bmwbc: captcha_token="+captcha_token)
35+
vehicle_config = BMWbc(configuration=BMWbcConfiguration(charge_point,
36+
user_id,
37+
password,
38+
vin,
39+
captcha_token))
3440
store.get_car_value_store(charge_point).store.set(api.fetch_soc(
3541
vehicle_config.configuration.user_id,
3642
vehicle_config.configuration.password,
3743
vehicle_config.configuration.vin,
44+
vehicle_config.configuration.captcha_token,
3845
charge_point))
3946

4047

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ pysmb==1.2.9.1
2020
pytz==2023.3.post1
2121
grpcio==1.60.1
2222
protobuf==4.25.3
23-
bimmer_connected==0.16.1
23+
bimmer_connected==0.17.2
2424
ocpp==1.0.0
2525
websockets==12.0

0 commit comments

Comments
 (0)