Skip to content

Commit

Permalink
Update coordinator update time when receiving data from mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jun 8, 2024
1 parent 85c6085 commit b204182
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
9 changes: 3 additions & 6 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def __init__(
self.update_vehicles = update_vehicles
self._debounce_task = None
self._last_update_time = None
self.last_controller_update_time: float | None = None
self.last_update_time: float | None = None
self.assumed_state = True

update_interval = timedelta(seconds=MIN_SCAN_INTERVAL)
Expand Down Expand Up @@ -478,12 +478,9 @@ async def _async_update_data(self):
raise UpdateFailed(f"Error communicating with API: {err}") from err
else:
if vin := self.vin:
self.last_controller_update_time = controller.get_last_update_time(
vin=vin
)
self.last_update_time = controller.get_last_update_time(vin=vin)
self.assumed_state = not controller.is_car_online(vin=vin) and (
self.last_controller_update_time
- controller.get_last_wake_up_time(vin=vin)
self.last_update_time - controller.get_last_wake_up_time(vin=vin)
> controller.update_interval
)
return data
Expand Down
10 changes: 5 additions & 5 deletions custom_components/tesla_custom/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ def __init__(
sw_version=car.car_version,
)
self._last_update_success: bool | None = None
self._last_controller_update_time: float | None = None
self._last_update_time: float | None = None

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
prev_last_update_success = self._last_update_success
prev_last_controller_update_time = self._last_controller_update_time
prev_last_update_time = self._last_update_time
coordinator = self.coordinator
current_last_update_success = coordinator.last_update_success
current_last_controller_update_time = coordinator.last_controller_update_time
current_last_update_time = coordinator.last_update_time
self._last_update_success = current_last_update_success
self._last_controller_update_time = current_last_controller_update_time
self._last_update_time = current_last_update_time
if (
prev_last_update_success == current_last_update_success
and prev_last_controller_update_time == current_last_controller_update_time
and prev_last_update_time == current_last_update_time
):
# If there was no change in the last update success or time,
# avoid writing state to prevent unnecessary entity updates.
Expand Down
5 changes: 4 additions & 1 deletion custom_components/tesla_custom/teslamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import asyncio
import logging
import time
from typing import TYPE_CHECKING

from homeassistant.components.mqtt import mqtt_config_entry_enabled
Expand Down Expand Up @@ -155,7 +156,7 @@ class TeslaMate:
def __init__(
self,
hass: HomeAssistant,
coordinators: list["TeslaDataUpdateCoordinator"],
coordinators: dict[str, "TeslaDataUpdateCoordinator"],
cars: dict[str, TeslaCar],
) -> None:
"""Init Class."""
Expand Down Expand Up @@ -372,6 +373,8 @@ async def async_handle_new_data(self, msg: ReceiveMessage):
# Nothing matched. Return without updating listeners.
return

coordinator.last_update_time = round(time.time())
coordinator.assumed_state = False
coordinator.async_update_listeners_debounced()

def update_charging_state(self, car: TeslaCar, val: str):
Expand Down

0 comments on commit b204182

Please sign in to comment.