2020
2121from modules .common .component_state import CarState
2222from modules .common .store import RAMDISK_PATH
23+ from pathlib import Path
24+
25+ DATA_PATH = Path (__file__ ).resolve ().parents [4 ] / "data" / "bmwbc"
2326
2427log = 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
3841def 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
5660def 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 )
0 commit comments