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

chore: release 2024-06-07 #992

Merged
merged 4 commits into from
Jun 8, 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
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ repos:
- hooks:
- id: black
repo: https://github.com/psf/black
rev: 24.4.0
rev: 24.4.2
- repo: https://github.com/pre-commit/mirrors-prettier
hooks:
- id: prettier
Expand All @@ -65,11 +65,11 @@ repos:
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/PyCQA/prospector
rev: 1.10.2
hooks:
- id: prospector
exclude: ^(tests)/.+\.py$
# - repo: https://github.com/PyCQA/prospector
# rev: 1.10.2
# hooks:
# - id: prospector
# exclude: ^(tests)/.+\.py$
- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
hooks:
Expand Down
36 changes: 19 additions & 17 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from http import HTTPStatus
import logging
import ssl
from typing import Any

import async_timeout
from homeassistant.config_entries import SOURCE_IMPORT
Expand Down Expand Up @@ -425,9 +426,9 @@ def __init__(
self.energy_site_id = energy_site_id
self.energy_site_ids = {energy_site_id} if energy_site_id else set()
self.update_vehicles = update_vehicles
self._debounce_task = None
self._cancel_debounce_timer = 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,17 +479,17 @@ 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

def async_update_listeners_debounced(self, delay_since_last=0.1, max_delay=1.0):
@callback
def async_update_listeners_debounced(
self, delay_since_last=0.1, max_delay=1.0
) -> None:
"""
Debounced version of async_update_listeners.

Expand All @@ -504,17 +505,18 @@ def async_update_listeners_debounced(self, delay_since_last=0.1, max_delay=1.0):

"""
# If there's an existing debounce task, cancel it
if self._debounce_task:
self._debounce_task()
if self._cancel_debounce_timer:
self._cancel_debounce_timer()
_LOGGER.debug("Previous debounce task cancelled")

# Schedule the call to _debounced, pass max_delay using partial
self._debounce_task = async_call_later(
self.hass, delay_since_last, partial(self._debounced, max_delay)
# Schedule the call to _async_debounced, pass max_delay using partial
self._cancel_debounce_timer = async_call_later(
self.hass, delay_since_last, partial(self._async_debounced, max_delay)
)
_LOGGER.debug("New debounce task scheduled")

async def _debounced(self, max_delay, *args):
@callback
def _async_debounced(self, max_delay: float, *args: Any) -> None:
"""
Debounce method that waits a certain delay since the last update.

Expand All @@ -537,10 +539,10 @@ async def _debounced(self, max_delay, *args):
_LOGGER.debug("Listeners updated")
else:
# If it hasn't been max_delay since the last update,
# schedule the call to _debounced again after the remaining time
self._debounce_task = async_call_later(
# schedule the call to _async_debounced again after the remaining time
self._cancel_debounce_timer = async_call_later(
self.hass,
max_delay - (now - self._last_update_time),
partial(self._debounced, max_delay),
partial(self._async_debounced, max_delay),
)
_LOGGER.debug("Max delay not reached, scheduling another debounce task")
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
Loading
Loading