Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: only write state when data changes (#954) #978

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +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.assumed_state = True

update_interval = timedelta(seconds=MIN_SCAN_INTERVAL)
Expand Down Expand Up @@ -477,8 +478,11 @@ 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.assumed_state = not controller.is_car_online(vin=vin) and (
controller.get_last_update_time(vin=vin)
self.last_controller_update_time
- controller.get_last_wake_up_time(vin=vin)
> controller.update_interval
)
Expand Down
28 changes: 22 additions & 6 deletions custom_components/tesla_custom/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Tesla cars and energy sites."""

from homeassistant.core import callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify
Expand Down Expand Up @@ -29,12 +30,6 @@ def __init__(
self._attr_name = self.type.capitalize()
self._attr_entity_registry_enabled_default = self._enabled_by_default

async def async_added_to_hass(self) -> None:
"""Register state update callback."""
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
)


class TeslaCarEntity(TeslaBaseEntity):
"""Representation of a Tesla car device."""
Expand All @@ -61,6 +56,27 @@ def __init__(
model=car.car_type,
sw_version=car.car_version,
)
self._last_update_success: bool | None = None
self._last_controller_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
coordinator = self.coordinator
current_last_update_success = coordinator.last_update_success
current_last_controller_update_time = coordinator.last_controller_update_time
self._last_update_success = current_last_update_success
self._last_controller_update_time = current_last_controller_update_time
if (
prev_last_update_success == current_last_update_success
and prev_last_controller_update_time == current_last_controller_update_time
):
# If there was no change in the last update success or time,
# avoid writing state to prevent unnecessary entity updates.
return
super()._handle_coordinator_update()

async def update_controller(
self, *, wake_if_asleep: bool = False, force: bool = True, blocking: bool = True
Expand Down
Loading