Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions packages/modules/vehicles/bmwbc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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)
2 changes: 2 additions & 0 deletions packages/modules/vehicles/bmwbc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 9 additions & 2 deletions packages/modules/vehicles/bmwbc/soc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ 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,
vehicle=vehicle,
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))


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.0
ocpp==1.0.0
websockets==12.0