Skip to content

Commit ba0a161

Browse files
authored
soc_vwid: send more detailed error info to status window (#3030)
* soc_vwid: send more detailed error info to status window * add error codes SOCERR-xx
1 parent b61e6e5 commit ba0a161

1 file changed

Lines changed: 75 additions & 63 deletions

File tree

packages/modules/vehicles/vwid/libvwid.py

Lines changed: 75 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,10 @@ def set_jobs(self, jobs):
14111411
self.jobs_string = ','.join(jobs)
14121412

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

1425-
try:
1426-
_k = str(vwid.connection.keys())
1427-
_LOGGER.info(f"libvwid.get_status connections at entry: vwid.connections.keys={_k}")
1428-
if self.username not in vwid.connection:
1429-
_LOGGER.info(f"create new connection, key={self.username}")
1430-
vwid.connection[self.username] = Connection(session, self.username, self.password)
1431-
self._connection = vwid.connection[self.username]
1432-
vwid.connection[self.username]._session_tokens['identity'] = {}
1433-
vwid.connection[self.username]._session_tokens['Legacy'] = {}
1434-
for token in self.tokens:
1435-
vwid.connection[self.username]._session_tokens['identity'][token] = self.tokens[token]
1436-
vwid.connection[self.username]._session_tokens['Legacy'][token] = self.tokens[token]
1437-
_conn_reuse = False
1429+
_k = str(vwid.connection.keys())
1430+
_LOGGER.info(f"libvwid.get_status connections at entry: vwid.connections.keys={_k}")
1431+
_update_result = False
1432+
if self.username not in vwid.connection:
1433+
_LOGGER.info(f"create new connection, key={self.username}")
1434+
vwid.connection[self.username] = Connection(session, self.username, self.password)
1435+
self._connection = vwid.connection[self.username]
1436+
vwid.connection[self.username]._session_tokens['identity'] = {}
1437+
vwid.connection[self.username]._session_tokens['Legacy'] = {}
1438+
for token in self.tokens:
1439+
vwid.connection[self.username]._session_tokens['identity'][token] = self.tokens[token]
1440+
vwid.connection[self.username]._session_tokens['Legacy'][token] = self.tokens[token]
1441+
_conn_reuse = False
1442+
else:
1443+
_LOGGER.info(f"reuse existing connection, key={self.username}")
1444+
vwid.connection[self.username]._session = session
1445+
_conn_reuse = True
1446+
if not _conn_reuse:
1447+
_doLogin_result = await vwid.connection[self.username].doLogin()
1448+
_LOGGER.debug("after 1st doLogin, result=" + str(_doLogin_result))
1449+
if _doLogin_result:
1450+
_update_result = True
14381451
else:
1439-
_LOGGER.info(f"reuse existing connection, key={self.username}")
1440-
vwid.connection[self.username]._session = session
1441-
_conn_reuse = True
1442-
if not _conn_reuse:
1452+
raise Exception(f"SOCERR-01: Login für User {self.username} fehlgeschlagen")
1453+
else:
1454+
_update_result = await vwid.connection[self.username].update()
1455+
_LOGGER.debug("after 1st connection.update without doLogin, result=" + str(_update_result))
1456+
if not _update_result:
14431457
_doLogin_result = await vwid.connection[self.username].doLogin()
1444-
_LOGGER.debug("after 1st doLogin, result=" + str(_doLogin_result))
1458+
_LOGGER.debug("after 2nd doLogin, result=" + str(_doLogin_result))
14451459
if _doLogin_result:
1446-
_update_result = True
1447-
else:
1448-
_update_result = await vwid.connection[self.username].update()
1449-
_LOGGER.debug("after 1st connection.update without doLogin, result=" + str(_update_result))
1450-
if not _update_result:
1451-
_doLogin_result = await vwid.connection[self.username].doLogin()
1452-
_LOGGER.debug("after 2nd doLogin, result=" + str(_doLogin_result))
1453-
if _doLogin_result:
1454-
_update_result = await vwid.connection[self.username].update()
1455-
_LOGGER.debug("after 2nd connection.update, result=" + str(_update_result))
1456-
else:
1457-
_LOGGER.error("retry doLogin failed, exit")
1458-
raise Exception("Login failed")
1459-
if _update_result:
1460-
_LOGGER.debug("update/doLogin look OK, get results")
1461-
for vehicle in vwid.connection[self.username].vehicles:
1462-
_LOGGER.debug("vehicle loop: " + str(vehicle) + ", self.vin=" + str(self.vin))
1463-
if str(vehicle) == str(self.vin):
1464-
_LOGGER.debug("vehicle loop match: " + str(vehicle) + ", self.vin=" + str(self.vin))
1465-
soc = vehicle._states['charging']['batteryStatus']['value']['currentSOC_pct']
1466-
range =\
1467-
vehicle._states['charging']['batteryStatus']['value']['cruisingRangeElectric_km']
1468-
ts = vehicle._states['charging']['batteryStatus']['value']['carCapturedTimestamp']
1469-
_LOGGER.debug("vehicle =" + str(vehicle))
1470-
_LOGGER.debug("soc =" + str(soc))
1471-
_LOGGER.debug("range =" + str(range))
1472-
_LOGGER.debug("timestamp=" + str(ts))
1473-
tsxx = ts.strftime('%Y-%m-%dT%H:%M:%SZ')
1474-
_LOGGER.debug("timestampxx=" + str(tsxx))
1475-
data['charging']['batteryStatus']['value']['currentSOC_pct'] = str(soc)
1476-
data['charging']['batteryStatus']['value']['cruisingRangeElectric_km'] = str(range)
1477-
data['charging']['batteryStatus']['value']['carCapturedTimestamp'] = str(tsxx)
1478-
_LOGGER.debug("return data =" + to_json(data, indent=4))
1479-
for token in vwid.connection[self.username]._session_tokens['identity']:
1480-
self.tokens[token] =\
1481-
vwid.connection[self.username]._session_tokens['identity'][token]
1482-
return data
1460+
_update_result = await vwid.connection[self.username].update()
1461+
_LOGGER.debug("after 2nd connection.update, result=" + str(_update_result))
1462+
else:
1463+
_LOGGER.error(f"retry doLogin for user {self.username} failed, exit")
1464+
raise Exception(f"SOCERR-01: Login für User {self.username} fehlgeschlagen")
1465+
if _update_result:
1466+
_LOGGER.debug("update/doLogin look OK, get results")
1467+
for vehicle in vwid.connection[self.username].vehicles:
1468+
_LOGGER.debug("vehicle loop: " + str(vehicle) + ", self.vin=" + str(self.vin))
1469+
if str(vehicle) == str(self.vin):
1470+
_LOGGER.debug("vehicle loop match: " + str(vehicle) + ", self.vin=" + str(self.vin))
1471+
soc = vehicle._states['charging']['batteryStatus']['value']['currentSOC_pct']
1472+
range =\
1473+
vehicle._states['charging']['batteryStatus']['value']['cruisingRangeElectric_km']
1474+
ts = vehicle._states['charging']['batteryStatus']['value']['carCapturedTimestamp']
1475+
_LOGGER.debug("vehicle =" + str(vehicle))
1476+
_LOGGER.debug("soc =" + str(soc))
1477+
_LOGGER.debug("range =" + str(range))
1478+
_LOGGER.debug("timestamp=" + str(ts))
1479+
tsxx = ts.strftime('%Y-%m-%dT%H:%M:%SZ')
1480+
_LOGGER.debug("timestampxx=" + str(tsxx))
1481+
data['charging']['batteryStatus']['value']['currentSOC_pct'] = str(soc)
1482+
data['charging']['batteryStatus']['value']['cruisingRangeElectric_km'] = str(range)
1483+
data['charging']['batteryStatus']['value']['carCapturedTimestamp'] = str(tsxx)
1484+
_LOGGER.debug("return data =" + to_json(data, indent=4))
1485+
for token in vwid.connection[self.username]._session_tokens['identity']:
1486+
self.tokens[token] =\
1487+
vwid.connection[self.username]._session_tokens['identity'][token]
1488+
return data
14831489
else:
1484-
_LOGGER.error("get_status rsp. update failed, raise exception")
1485-
raise Exception("get_status: keine Daten empfangen")
1486-
except Exception as error:
1487-
_LOGGER.exception("get_status failed 1, raise exception, exception=" + str(error))
1488-
raise Exception(error)
1489-
except Exception as error:
1490-
_LOGGER.exception("get_status failed 0, raise exception=" + str(error))
1491-
raise Exception(error)
1490+
_LOGGER.error(f"SOCERR-02: Fahrzeug mit VIN {self.vin} nicht gefunden")
1491+
raise Exception(f"SOCERR-02: Fahrzeug mit VIN {self.vin} nicht gefunden")
1492+
else:
1493+
_t = f"SOCERR-00: Für User {self.username} und VIN {self.vin} wurden keine Daten empfangen."
1494+
_LOGGER.error(f"{_t}: get_status update failed")
1495+
raise Exception(_t)
1496+
except Exception as e:
1497+
_LOGGER.exception(f"get_status failed 0, exception={e}")
1498+
# if exception is a SOCERR reraise it, otherwise raise general SOCERR-00
1499+
if "SOCERR" in str(e):
1500+
raise e
1501+
else:
1502+
_t = f"SOCERR-00: Für User {self.username} und VIN {self.vin} wurden keine Daten empfangen"
1503+
raise Exception(f"{_t} {e}")

0 commit comments

Comments
 (0)