Skip to content

Commit

Permalink
Fix attributes expected for batpred
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Jul 7, 2024
1 parent 9781621 commit 5460095
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions custom_components/ohme/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _calculate_state(self) -> bool:
return power > 0

# See if we are in a charge slot now and if we were for the last reading
in_charge_slot = in_slot(self.coordinator.data['allSessionSlots'])
in_charge_slot = in_slot(self.coordinator.data)
lr_in_charge_slot = self._last_reading_in_slot
# Store this for next time
self._last_reading_in_slot = in_charge_slot
Expand Down Expand Up @@ -312,7 +312,7 @@ def _handle_coordinator_update(self) -> None:
elif self.coordinator.data["mode"] == "DISCONNECTED":
self._state = False
else:
self._state = in_slot(self.coordinator.data['allSessionSlots'])
self._state = in_slot(self.coordinator.data)

self._last_updated = utcnow()

Expand Down
6 changes: 3 additions & 3 deletions custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def _handle_coordinator_update(self) -> None:
if self.coordinator.data is None or self.coordinator.data["mode"] == "DISCONNECTED":
self._state = None
else:
self._state = next_slot(self.coordinator.data['allSessionSlots'])['start']
self._state = next_slot(self.coordinator.data)['start']

self._last_updated = utcnow()

Expand Down Expand Up @@ -411,7 +411,7 @@ def _handle_coordinator_update(self) -> None:
if self.coordinator.data is None or self.coordinator.data["mode"] == "DISCONNECTED":
self._state = None
else:
self._state = next_slot(self.coordinator.data['allSessionSlots'])['end']
self._state = next_slot(self.coordinator.data)['end']

self._last_updated = utcnow()

Expand Down Expand Up @@ -462,7 +462,7 @@ def _handle_coordinator_update(self) -> None:
if self.coordinator.data is None or self.coordinator.data["mode"] == "DISCONNECTED" or self.coordinator.data["mode"] == "FINISHED_CHARGE":
self._state = None
else:
slots = slot_list(self.coordinator.data['allSessionSlots'])
slots = slot_list(self.coordinator.data)

# Store slots for external use
self._hass.data[DOMAIN][DATA_SLOTS] = slots
Expand Down
10 changes: 7 additions & 3 deletions custom_components/ohme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ def next_slot(data):

def slot_list(data):
"""Get list of charge slots."""
if data is None or len(data) == 0:
session_slots = data['allSessionSlots']
if session_slots is None or len(session_slots) == 0:
return None

slots = []
for slot in data:
wh_tally = data['batterySocBefore']['wh'] # Get the wh value we start from
for slot in session_slots:
slots.append(
{
"start": datetime.utcfromtimestamp(slot['startTimeMs'] / 1000).replace(tzinfo=pytz.utc).astimezone(),
"end": datetime.utcfromtimestamp(slot['endTimeMs'] / 1000).replace(tzinfo=pytz.utc).astimezone(),
"charge_in_kwh": -(slot['estimatedSoc']['wh'] / 1000),
"charge_in_kwh": -((slot['estimatedSoc']['wh'] - wh_tally) / 1000), # Work out how much we add in just this slot
"source": "smart-charge",
"location": None
}
)

wh_tally = slot['estimatedSoc']['wh']

return slots

Expand Down

0 comments on commit 5460095

Please sign in to comment.