Skip to content

Commit

Permalink
add last_kwh and last_date of transaction to do less complex code
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyuentuen committed Apr 23, 2024
1 parent 15bd1aa commit 27a2431
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions custom_components/alfen_wallbox/alfen.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ async def _get_transaction(self):
self.latest_tag[socket,"stop","date"] = date
self.latest_tag[socket,"stop","kWh"] = kWh

# store the latest start kwh and date
if self.latest_tag[socket,"start","kWh"] is not None:
self.latest_tag[socket,"last_start","kWh"] = self.latest_tag[socket,"start","kWh"]
if self.latest_tag[socket,"start","date"] is not None:
self.latest_tag[socket,"last_start","date"] = self.latest_tag[socket,"start","date"]

elif "mv" in line:
#_LOGGER.debug("mv line: " + line)
tid = splitline[0].split("_", 2)[0]
Expand Down
22 changes: 15 additions & 7 deletions custom_components/alfen_wallbox/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,7 @@ def _processTransactionKWh(self, socket:str, entity_description:AlfenSensorDescr
startkWh = None
mvkWh = None
stopkWh = None
lastkWh = None

for (key,value) in self._device.latest_tag.items():
if key[0] == socket and key[1] == "start" and key[2] == "kWh":
Expand All @@ -1674,6 +1675,9 @@ def _processTransactionKWh(self, socket:str, entity_description:AlfenSensorDescr
if key[0] == socket and key[1] == "stop" and key[2] == "kWh":
stopkWh = value
continue
if key[0] == socket and key[1] == "last_start" and key[2] == "kWh":
lastkWh = value
continue

# if the entity_key end with _charging, then we are calculating the charging
if startkWh is not None and mvkWh is not None and entity_description.key.endswith('_charging'):
Expand All @@ -1686,9 +1690,9 @@ def _processTransactionKWh(self, socket:str, entity_description:AlfenSensorDescr
return value

# if the entity_key end with _charged, then we are calculating the charged
if startkWh is not None and stopkWh is not None and entity_description.key.endswith('_charged'):
if float(stopkWh) >= float(startkWh):
value = round(float(stopkWh) - float(startkWh), 2)
if lastkWh is not None and stopkWh is not None and entity_description.key.endswith('_charged'):
if float(stopkWh) >= float(lastkWh):
value = round(float(stopkWh) - float(lastkWh), 2)
if entity_description.round_digits is not None:
return round(value, entity_description.round_digits if entity_description.round_digits > 0 else None)
return value
Expand All @@ -1701,6 +1705,7 @@ def _processTransactionTime(self, socket:str, entity_description:AlfenSensorDesc
startDate = None
mvDate = None
stopDate = None
lastDate = None

for (key,value) in self._device.latest_tag.items():
if key[0] == "socket 1" and key[1] == "start" and key[2] == "date":
Expand All @@ -1712,6 +1717,9 @@ def _processTransactionTime(self, socket:str, entity_description:AlfenSensorDesc
if key[0] == "socket 1" and key[1] == "stop" and key[2] == "date":
stopDate = value
continue
if key[0] == "socket 1" and key[1] == "last_start" and key[2] == "date":
lastDate = value
continue

if startDate is not None and mvDate is not None and entity_description.key.endswith('_charging_time'):
startDate = datetime.datetime.strptime(startDate, '%Y-%m-%d %H:%M:%S')
Expand All @@ -1729,14 +1737,14 @@ def _processTransactionTime(self, socket:str, entity_description:AlfenSensorDesc
return value


if startDate is not None and stopDate is not None and entity_description.key.endswith('_charged_time'):
startDate = datetime.datetime.strptime(startDate, '%Y-%m-%d %H:%M:%S')
if lastDate is not None and stopDate is not None and entity_description.key.endswith('_charged_time'):
lastDate = datetime.datetime.strptime(lastDate, '%Y-%m-%d %H:%M:%S')
stopDate = datetime.datetime.strptime(stopDate, '%Y-%m-%d %H:%M:%S')

if stopDate < startDate:
if stopDate < lastDate:
return None
# return the value in minutes
value = round((stopDate - startDate).total_seconds() / 60, 2)
value = round((stopDate - lastDate).total_seconds() / 60, 2)
if entity_description.round_digits is not None:
return round(value, entity_description.round_digits if entity_description.round_digits > 0 else None)
return value
Expand Down

0 comments on commit 27a2431

Please sign in to comment.