Skip to content

Commit

Permalink
fix: calculate assumed state only once (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Dec 24, 2023
1 parent 7cefa7f commit afaf8e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
36 changes: 23 additions & 13 deletions custom_components/tesla_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,13 @@ def _async_create_close_task():
config_entry=config_entry,
controller=controller,
reload_lock=reload_lock,
energy_site_ids=set(),
vins=set(),
update_vehicles=False,
)
energy_coordinators = {
energy_site_id: _partial_coordinator(energy_site_ids={energy_site_id})
energy_site_id: _partial_coordinator(energy_site_id=energy_site_id)
for energy_site_id in energysites
}
car_coordinators = {vin: _partial_coordinator(vins={vin}) for vin in cars}
car_coordinators = {vin: _partial_coordinator(vin=vin) for vin in cars}
coordinators = {**energy_coordinators, **car_coordinators}

if car_coordinators:
Expand Down Expand Up @@ -395,19 +393,22 @@ def __init__(
config_entry,
controller: TeslaAPI,
reload_lock: asyncio.Lock,
vins: set[str],
energy_site_ids: set[str],
update_vehicles: bool,
):
vin: str | None = None,
energy_site_id: str | None = None,
update_vehicles: bool = False,
) -> None:
"""Initialize global Tesla data updater."""
self.controller = controller
self.config_entry = config_entry
self.reload_lock = reload_lock
self.vins = vins
self.energy_site_ids = energy_site_ids
self.vin = vin
self.vins = {vin} if vin else set()
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._last_update_time = None
self.assumed_state = True

update_interval = timedelta(seconds=MIN_SCAN_INTERVAL)

Expand All @@ -420,10 +421,11 @@ def __init__(

async def _async_update_data(self):
"""Fetch data from API endpoint."""
if self.controller.is_token_refreshed():
controller = self.controller
if controller.is_token_refreshed():
# It doesn't matter which coordinator calls this, as long as there
# are no awaits in the below code, it will be called only once.
result = self.controller.get_tokens()
result = controller.get_tokens()
refresh_token = result["refresh_token"]
access_token = result["access_token"]
expiration = result["expiration"]
Expand All @@ -437,7 +439,7 @@ async def _async_update_data(self):
# handled by the data update coordinator.
async with async_timeout.timeout(30):
_LOGGER.debug("Running controller.update()")
return await self.controller.update(
data = await controller.update(
vins=self.vins,
energy_site_ids=self.energy_site_ids,
update_vehicles=self.update_vehicles,
Expand All @@ -453,6 +455,14 @@ async def _async_update_data(self):
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
except TeslaException as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err
else:
if vin := self.vin:
self.assumed_state = not controller.is_car_online(vin=vin) and (
controller.get_last_update_time(vin=vin)
- 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):
"""
Expand Down
8 changes: 1 addition & 7 deletions custom_components/tesla_custom/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ async def update_controller(
@property
def assumed_state(self) -> bool:
"""Return whether the data is from an online vehicle."""
vin = self._car.vin
controller = self.coordinator.controller
return not controller.is_car_online(vin=vin) and (
controller.get_last_update_time(vin=vin)
- controller.get_last_wake_up_time(vin=vin)
> controller.update_interval
)
return self.coordinator.assumed_state


class TeslaEnergyEntity(TeslaBaseEntity):
Expand Down

0 comments on commit afaf8e7

Please sign in to comment.