diff --git a/packages/modules/vehicles/bmwbc/api.py b/packages/modules/vehicles/bmwbc/api.py index 0159102e82..4d232f0390 100755 --- a/packages/modules/vehicles/bmwbc/api.py +++ b/packages/modules/vehicles/bmwbc/api.py @@ -20,6 +20,9 @@ from modules.common.component_state import CarState from modules.common.store import RAMDISK_PATH +from pathlib import Path + +DATA_PATH = Path(__file__).resolve().parents[4] / "data" / "bmwbc" log = logging.getLogger(__name__) @@ -36,6 +39,7 @@ def init_store(): # load store from file, if no store file exists initialize store structure def load_store(): + global storeFile try: with open(storeFile, 'r', encoding='utf-8') as tf: store = json.load(tf) @@ -54,6 +58,7 @@ def load_store(): # write store file def write_store(store: dict): + global storeFile with open(storeFile, 'w', encoding='utf-8') as tf: json.dump(store, tf, indent=4) @@ -66,9 +71,17 @@ def dump_json(data: dict, fout: str): # ---------------fetch Function called by core ------------------------------------ -async def _fetch_soc(user_id: str, password: str, vin: str, vnum: int) -> Union[int, float]: +async def _fetch_soc(user_id: str, password: str, vin: str, captcha_token: str, vnum: int) -> Union[int, float]: global storeFile - storeFile = str(RAMDISK_PATH) + '/soc_bmwbc_vh_' + str(vnum) + '.json' + try: + log.debug("dataPath=" + str(DATA_PATH)) + DATA_PATH.mkdir(parents=True, exist_ok=True) + except Exception as e: + log.error("init: dataPath creation failed, dataPath: " + + str(DATA_PATH) + ", error=" + str(e)) + store = init_store() + return store + storeFile = str(DATA_PATH) + '/soc_bmwbc_vh_' + str(vnum) + '.json' try: # 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[ expires_at=expires_at) else: # no token, authenticate via user_id and password only - auth = MyBMWAuthentication(user_id, password, Regions.REST_OF_WORLD) + auth = MyBMWAuthentication(user_id, + password, + Regions.REST_OF_WORLD, + hcaptcha_token=captcha_token) clconf = MyBMWClientConfiguration(auth) # account = MyBMWAccount(user_id, password, Regions.REST_OF_WORLD, config=clconf) # user, password and region already set in BMWAuthentication/ClientConfiguration! - account = MyBMWAccount(None, None, None, config=clconf) + account = MyBMWAccount(None, None, None, config=clconf, hcaptcha_token=captcha_token) # get vehicle list - needs to be called async await account.get_vehicles() @@ -130,13 +146,13 @@ async def _fetch_soc(user_id: str, password: str, vin: str, vnum: int) -> Union[ # main entry - _fetch needs to be run async -def fetch_soc(user_id: str, password: str, vin: str, vnum: int) -> CarState: +def fetch_soc(user_id: str, password: str, vin: str, captcha_token: str, vnum: int) -> CarState: # prepare and call async method loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # get soc, range from server - soc, range = loop.run_until_complete(_fetch_soc(user_id, password, vin, vnum)) + soc, range = loop.run_until_complete(_fetch_soc(user_id, password, vin, captcha_token, vnum)) return CarState(soc, range) diff --git a/packages/modules/vehicles/bmwbc/config.py b/packages/modules/vehicles/bmwbc/config.py index ce178d7fd4..20a8143da9 100755 --- a/packages/modules/vehicles/bmwbc/config.py +++ b/packages/modules/vehicles/bmwbc/config.py @@ -6,10 +6,12 @@ def __init__(self, user_id: Optional[str] = None, password: Optional[str] = None, vin: Optional[str] = None, + captcha_token: Optional[str] = None, calculate_soc: bool = False): self.user_id = user_id self.password = password self.vin = vin + self.captcha_token = captcha_token self.calculate_soc = calculate_soc diff --git a/packages/modules/vehicles/bmwbc/soc.py b/packages/modules/vehicles/bmwbc/soc.py index 4cd9dafd3c..796582f471 100755 --- a/packages/modules/vehicles/bmwbc/soc.py +++ b/packages/modules/vehicles/bmwbc/soc.py @@ -21,6 +21,7 @@ def updater(vehicle_update_data: VehicleUpdateData) -> CarState: vehicle_config.configuration.user_id, vehicle_config.configuration.password, vehicle_config.configuration.vin, + vehicle_config.configuration.captcha_token, vehicle) return ConfigurableVehicle(vehicle_config=vehicle_config, component_updater=updater, @@ -28,13 +29,19 @@ def updater(vehicle_update_data: VehicleUpdateData) -> CarState: calc_while_charging=vehicle_config.configuration.calculate_soc) -def bmwbc_update(user_id: str, password: str, vin: str, charge_point: int): +def bmwbc_update(user_id: str, password: str, vin: str, captcha_token: str, charge_point: int): log.debug("bmwbc: user_id="+user_id+"vin="+vin+"charge_point="+str(charge_point)) - vehicle_config = BMWbc(configuration=BMWbcConfiguration(charge_point, user_id, password, vin)) + log.debug("bmwbc: captcha_token="+captcha_token) + vehicle_config = BMWbc(configuration=BMWbcConfiguration(charge_point, + user_id, + password, + vin, + captcha_token)) store.get_car_value_store(charge_point).store.set(api.fetch_soc( vehicle_config.configuration.user_id, vehicle_config.configuration.password, vehicle_config.configuration.vin, + vehicle_config.configuration.captcha_token, charge_point)) diff --git a/requirements.txt b/requirements.txt index 9c69a5933d..bb8711ba1f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,6 +20,6 @@ pysmb==1.2.9.1 pytz==2023.3.post1 grpcio==1.60.1 protobuf==4.25.3 -bimmer_connected==0.16.1 +bimmer_connected==0.17.2 ocpp==1.0.0 websockets==12.0