Skip to content

Commit

Permalink
account for time between updates
Browse files Browse the repository at this point in the history
  • Loading branch information
InTheDaylight14 committed Dec 11, 2022
1 parent ff25056 commit 402fcd4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion custom_components/tesla_custom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ def __init__(
self._attr_state_class = SensorStateClass.MEASUREMENT
self._attr_icon = "mdi:timer-plus"
self._value: Optional[datetime] = None
self._last_known_value: Optional[int] = None
self._last_update_time: Optional[datetime] = None

@property
def native_value(self) -> Optional[datetime]:
Expand All @@ -509,8 +511,17 @@ def native_value(self) -> Optional[datetime]:
charge_hours = 0
else:
charge_hours = float(self._car.time_to_full_charge)

if self._last_known_value != charge_hours:
self._last_known_value = charge_hours
self._last_update_time = dt.utcnow()

if self._car.charging_state == "Charging" and charge_hours > 0:
new_value = dt.utcnow() + timedelta(hours=charge_hours)
new_value = (
dt.utcnow()
+ timedelta(hours=charge_hours)
- (dt.utcnow() - self._last_update_time)
)
if self._value is None or (new_value - self._value).total_seconds() >= 60:
self._value = new_value
if self._car.charging_state in ["Charging", "Complete"]:
Expand Down

0 comments on commit 402fcd4

Please sign in to comment.