Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore range errors caused by VW mile/km conversion #155

Merged
merged 11 commits into from
Oct 27, 2023
15 changes: 11 additions & 4 deletions weconnect/elements/battery_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@ def update(self, fromDict, ignoreAttributes=None):
LOG.debug('Update battery status from dict')

if 'value' in fromDict:
self.currentSOC_pct.fromDict(fromDict['value'], 'currentSOC_pct')

if 'cruisingRangeElectric_km' in fromDict['value']:
cruisingRangeElectric_km = int(fromDict['value']['cruisingRangeElectric_km'])
if self.fixAPI and cruisingRangeElectric_km == 0x3FFF:
cruisingRangeElectric_km = None
LOG.info('%s: Attribute cruisingRangeElectric_km was error value 0x3FFF. Setting error state instead'
' of 16383 km.', self.getGlobalAddress())
self.cruisingRangeElectric_km.setValueWithCarTime(
cruisingRangeElectric_km, lastUpdateFromCar=None, fromServer=True)

if (self.fixAPI
and round((self.cruisingRangeElectric_km.value or 0) * 0.621371) == cruisingRangeElectric_km
and self.currentSOC_pct.value == int(fromDict['value']['currentSOC_pct'])):
LOG.info('%s: Attribute cruisingRangeElectric_km was miscalculated (miles/km) this is a bug in the API and the new value will not be used',
self.getGlobalAddress())
else:
self.cruisingRangeElectric_km.setValueWithCarTime(
cruisingRangeElectric_km, lastUpdateFromCar=None, fromServer=True)
else:
self.cruisingRangeElectric_km.enabled = False

self.currentSOC_pct.fromDict(fromDict['value'], 'currentSOC_pct')
else:
self.currentSOC_pct.enabled = False
self.cruisingRangeElectric_km.enabled = False
Expand Down
19 changes: 16 additions & 3 deletions weconnect/elements/range_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def update(self, fromDict, ignoreAttributes=None):
else:
self.secondaryEngine.enabled = False

self.totalRange_km.fromDict(fromDict['value'], 'totalRange_km')
if self.fixAPI and round((self.totalRange_km.value or 0) * 0.621371) == int(fromDict['value']['totalRange_km']):
LOG.info('%s: Attribute totalRange_km was miscalculated (miles/km) this is a bug in the API and the new value will not be used',
self.getGlobalAddress())
else:
self.totalRange_km.fromDict(fromDict['value'], 'totalRange_km')

else:
self.carType.enabled = False
Expand Down Expand Up @@ -82,16 +86,25 @@ def __init__(
localAddress='currentFuelLevel_pct', parent=self, value=None, valueType=int)
self.remainingRange_km = AddressableAttribute(
localAddress='remainingRange_km', parent=self, value=None, valueType=int)

if fromDict is not None:
self.update(fromDict)

def update(self, fromDict):
LOG.debug('Update Engine from dict')

self.type.fromDict(fromDict, 'type')
self.currentSOC_pct.fromDict(fromDict, 'currentSOC_pct')
self.currentFuelLevel_pct.fromDict(fromDict, 'currentFuelLevel_pct')
self.remainingRange_km.fromDict(fromDict, 'remainingRange_km')

if (self.parent.fixAPI
and round((self.remainingRange_km.value or 0) * 0.621371) == int(fromDict['remainingRange_km'])
and self.currentSOC_pct.value == int(fromDict['currentSOC_pct'])):
LOG.info('%s: Attribute remainingRange_km was miscalculated (miles/km) this is a bug in the API and the new value will not be used',
self.getGlobalAddress())
else:
self.remainingRange_km.fromDict(fromDict, 'remainingRange_km')

self.currentSOC_pct.fromDict(fromDict, 'currentSOC_pct')

for key, value in {key: value for key, value in fromDict.items()
if key not in ['type', 'currentSOC_pct', 'currentFuelLevel_pct', 'remainingRange_km']}.items():
Expand Down
Loading