Skip to content
Merged
Changes from all 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
138 changes: 75 additions & 63 deletions packages/modules/vehicles/vwid/libvwid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,10 @@ def set_jobs(self, jobs):
self.jobs_string = ','.join(jobs)

async def get_status(self):
# error codes SOCERR-xx raised:
# SOCERR-00: general error
# SOCERR-01: login problem, username, password wrong, account locked, etc.
# SOCERR-02: vehicle not found in account, VIN wrong?
try:
async with aiohttp.ClientSession(headers={'Connection': 'keep-alive'}) as session:
_now = datetime.now(UTC).strftime('%Y-%m-%dT%H:%M:%SZ')
Expand All @@ -1422,70 +1426,78 @@ async def get_status(self):
data['charging']['batteryStatus']['value']['cruisingRangeElectric_km'] = str(0)
data['charging']['batteryStatus']['value']['carCapturedTimestamp'] = _now

try:
_k = str(vwid.connection.keys())
_LOGGER.info(f"libvwid.get_status connections at entry: vwid.connections.keys={_k}")
if self.username not in vwid.connection:
_LOGGER.info(f"create new connection, key={self.username}")
vwid.connection[self.username] = Connection(session, self.username, self.password)
self._connection = vwid.connection[self.username]
vwid.connection[self.username]._session_tokens['identity'] = {}
vwid.connection[self.username]._session_tokens['Legacy'] = {}
for token in self.tokens:
vwid.connection[self.username]._session_tokens['identity'][token] = self.tokens[token]
vwid.connection[self.username]._session_tokens['Legacy'][token] = self.tokens[token]
_conn_reuse = False
_k = str(vwid.connection.keys())
_LOGGER.info(f"libvwid.get_status connections at entry: vwid.connections.keys={_k}")
_update_result = False
if self.username not in vwid.connection:
_LOGGER.info(f"create new connection, key={self.username}")
vwid.connection[self.username] = Connection(session, self.username, self.password)
self._connection = vwid.connection[self.username]
vwid.connection[self.username]._session_tokens['identity'] = {}
vwid.connection[self.username]._session_tokens['Legacy'] = {}
for token in self.tokens:
vwid.connection[self.username]._session_tokens['identity'][token] = self.tokens[token]
vwid.connection[self.username]._session_tokens['Legacy'][token] = self.tokens[token]
_conn_reuse = False
else:
_LOGGER.info(f"reuse existing connection, key={self.username}")
vwid.connection[self.username]._session = session
_conn_reuse = True
if not _conn_reuse:
_doLogin_result = await vwid.connection[self.username].doLogin()
_LOGGER.debug("after 1st doLogin, result=" + str(_doLogin_result))
if _doLogin_result:
_update_result = True
else:
_LOGGER.info(f"reuse existing connection, key={self.username}")
vwid.connection[self.username]._session = session
_conn_reuse = True
if not _conn_reuse:
raise Exception(f"SOCERR-01: Login für User {self.username} fehlgeschlagen")
else:
_update_result = await vwid.connection[self.username].update()
_LOGGER.debug("after 1st connection.update without doLogin, result=" + str(_update_result))
if not _update_result:
_doLogin_result = await vwid.connection[self.username].doLogin()
_LOGGER.debug("after 1st doLogin, result=" + str(_doLogin_result))
_LOGGER.debug("after 2nd doLogin, result=" + str(_doLogin_result))
if _doLogin_result:
_update_result = True
else:
_update_result = await vwid.connection[self.username].update()
_LOGGER.debug("after 1st connection.update without doLogin, result=" + str(_update_result))
if not _update_result:
_doLogin_result = await vwid.connection[self.username].doLogin()
_LOGGER.debug("after 2nd doLogin, result=" + str(_doLogin_result))
if _doLogin_result:
_update_result = await vwid.connection[self.username].update()
_LOGGER.debug("after 2nd connection.update, result=" + str(_update_result))
else:
_LOGGER.error("retry doLogin failed, exit")
raise Exception("Login failed")
if _update_result:
_LOGGER.debug("update/doLogin look OK, get results")
for vehicle in vwid.connection[self.username].vehicles:
_LOGGER.debug("vehicle loop: " + str(vehicle) + ", self.vin=" + str(self.vin))
if str(vehicle) == str(self.vin):
_LOGGER.debug("vehicle loop match: " + str(vehicle) + ", self.vin=" + str(self.vin))
soc = vehicle._states['charging']['batteryStatus']['value']['currentSOC_pct']
range =\
vehicle._states['charging']['batteryStatus']['value']['cruisingRangeElectric_km']
ts = vehicle._states['charging']['batteryStatus']['value']['carCapturedTimestamp']
_LOGGER.debug("vehicle =" + str(vehicle))
_LOGGER.debug("soc =" + str(soc))
_LOGGER.debug("range =" + str(range))
_LOGGER.debug("timestamp=" + str(ts))
tsxx = ts.strftime('%Y-%m-%dT%H:%M:%SZ')
_LOGGER.debug("timestampxx=" + str(tsxx))
data['charging']['batteryStatus']['value']['currentSOC_pct'] = str(soc)
data['charging']['batteryStatus']['value']['cruisingRangeElectric_km'] = str(range)
data['charging']['batteryStatus']['value']['carCapturedTimestamp'] = str(tsxx)
_LOGGER.debug("return data =" + to_json(data, indent=4))
for token in vwid.connection[self.username]._session_tokens['identity']:
self.tokens[token] =\
vwid.connection[self.username]._session_tokens['identity'][token]
return data
_update_result = await vwid.connection[self.username].update()
_LOGGER.debug("after 2nd connection.update, result=" + str(_update_result))
else:
_LOGGER.error(f"retry doLogin for user {self.username} failed, exit")
raise Exception(f"SOCERR-01: Login für User {self.username} fehlgeschlagen")
if _update_result:
_LOGGER.debug("update/doLogin look OK, get results")
for vehicle in vwid.connection[self.username].vehicles:
_LOGGER.debug("vehicle loop: " + str(vehicle) + ", self.vin=" + str(self.vin))
if str(vehicle) == str(self.vin):
_LOGGER.debug("vehicle loop match: " + str(vehicle) + ", self.vin=" + str(self.vin))
soc = vehicle._states['charging']['batteryStatus']['value']['currentSOC_pct']
range =\
vehicle._states['charging']['batteryStatus']['value']['cruisingRangeElectric_km']
ts = vehicle._states['charging']['batteryStatus']['value']['carCapturedTimestamp']
_LOGGER.debug("vehicle =" + str(vehicle))
_LOGGER.debug("soc =" + str(soc))
_LOGGER.debug("range =" + str(range))
_LOGGER.debug("timestamp=" + str(ts))
tsxx = ts.strftime('%Y-%m-%dT%H:%M:%SZ')
_LOGGER.debug("timestampxx=" + str(tsxx))
data['charging']['batteryStatus']['value']['currentSOC_pct'] = str(soc)
data['charging']['batteryStatus']['value']['cruisingRangeElectric_km'] = str(range)
data['charging']['batteryStatus']['value']['carCapturedTimestamp'] = str(tsxx)
_LOGGER.debug("return data =" + to_json(data, indent=4))
for token in vwid.connection[self.username]._session_tokens['identity']:
self.tokens[token] =\
vwid.connection[self.username]._session_tokens['identity'][token]
return data
else:
_LOGGER.error("get_status rsp. update failed, raise exception")
raise Exception("get_status: keine Daten empfangen")
except Exception as error:
_LOGGER.exception("get_status failed 1, raise exception, exception=" + str(error))
raise Exception(error)
except Exception as error:
_LOGGER.exception("get_status failed 0, raise exception=" + str(error))
raise Exception(error)
_LOGGER.error(f"SOCERR-02: Fahrzeug mit VIN {self.vin} nicht gefunden")
raise Exception(f"SOCERR-02: Fahrzeug mit VIN {self.vin} nicht gefunden")
else:
_t = f"SOCERR-00: Für User {self.username} und VIN {self.vin} wurden keine Daten empfangen."
_LOGGER.error(f"{_t}: get_status update failed")
raise Exception(_t)
except Exception as e:
_LOGGER.exception(f"get_status failed 0, exception={e}")
# if exception is a SOCERR reraise it, otherwise raise general SOCERR-00
if "SOCERR" in str(e):
raise e
else:
_t = f"SOCERR-00: Für User {self.username} und VIN {self.vin} wurden keine Daten empfangen"
raise Exception(f"{_t} {e}")