diff --git a/weconnect/elements/battery_status.py b/weconnect/elements/battery_status.py index 3b8ea9f..b6edff8 100644 --- a/weconnect/elements/battery_status.py +++ b/weconnect/elements/battery_status.py @@ -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 diff --git a/weconnect/elements/range_status.py b/weconnect/elements/range_status.py index 3ecd835..7f506dc 100644 --- a/weconnect/elements/range_status.py +++ b/weconnect/elements/range_status.py @@ -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 @@ -82,6 +86,7 @@ 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) @@ -89,9 +94,17 @@ 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():