Skip to content

Commit

Permalink
Merge pull request #805 from alandtse/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Dec 24, 2023
2 parents 2901189 + afaf8e7 commit ffb55a9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 22 deletions.
74 changes: 74 additions & 0 deletions .github/ISSUE_TEMPLATE/issue_car.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Telsa Car Issue
description: Create a report to help us improve
title: "Car Issue: "
labels: ["triage", "car"]
body:
- type: checkboxes
attributes:
label: Is there an existing issue for this?
description: Before you open a new issue, search through the existing open issues and closed issues to see if others have had the same problem.
options:
- label: I have searched both the existing open issues & recently closed issues and did not find a duplicate of this issue.
required: true
- type: markdown
attributes:
value: |
Issues not containing the minimum requirements will be closed:
- Issues without a description (using the header is not good enough) will be closed.
- Issues without debug logging will be closed.
- type: input
id: version_hacs
attributes:
label: Version of the Tesla component
description: You can find this under HACS -> Integrations -> Tesla. If you are not using the newest version, download and try that before opening an issue
placeholder: ex. v3.19.3
validations:
required: true
- type: input
id: version_tesla
attributes:
label: Version of the Tesla car software
description: Open the Tesla app -> Select car -> Scroll to bottom
placeholder: ex. 2023.44.1
validations:
required: true
- type: input
id: model_tesla
attributes:
label: Model
description: The model of the Tesla
placeholder: ex. Model 3
validations:
required: true
- type: textarea
attributes:
label: Current Behavior
description: A concise description of what you're experiencing.
validations:
required: true

- type: textarea
attributes:
label: Expected Behavior
description: A concise description of what you expected to happen.
validations:
required: true

- type: textarea
attributes:
label: Debug logs
description: Enable the debug logs ([In Home Assistant](https://my.home-assistant.io/redirect/integration/?domain=tesla_custom) & click `Enable debug logging`)
render: true
placeholder: |
Your logs here
validations:
required: true

- type: textarea
attributes:
label: Anything else?
description: |
Links? References? Anything that will give us more context about the issue you are encountering
validations:
required: false
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repos:
- id: commitizen
stages: ["commit-msg"]
repo: https://github.com/commitizen-tools/commitizen
rev: 3.5.3
rev: v3.13.0
# --- Linters ---
- hooks:
- id: dockerfile_lint
Expand Down
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
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Tesla",
"hacs": "1.6.0",
"homeassistant": "2023.6.0",
"homeassistant": "2023.7.0",
"zip_release": true,
"filename": "tesla_custom.zip"
}

0 comments on commit ffb55a9

Please sign in to comment.