Skip to content

Commit

Permalink
fix: memoize unique id (#534)
Browse files Browse the repository at this point in the history
* Memorize unique id

* Slugify once instead of every state write
  • Loading branch information
bdraco authored Mar 14, 2023
1 parent ca3907f commit 05f1689
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions custom_components/tesla_custom/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
self._enabled_by_default: bool = True
self.hass = hass
self.type = None
self._memorized_unique_id = None

def refresh(self) -> None:
"""Refresh the device data.
Expand Down Expand Up @@ -106,7 +107,9 @@ def vehicle_name(self) -> str:
@property
def unique_id(self) -> str:
"""Return unique id for car entity."""
return slugify(f"{self._car.vin} {self.type}")
if not self._memorized_unique_id:
self._memorized_unique_id = slugify(f"{self._car.vin} {self.type}")
return self._memorized_unique_id

@property
def device_info(self) -> DeviceInfo:
Expand Down Expand Up @@ -145,7 +148,11 @@ def __init__(
@property
def unique_id(self) -> str:
"""Return unique id for energy site device."""
return slugify(f"{self._energysite.energysite_id} {self.type}")
if not self._memorized_unique_id:
self._memorized_unique_id = slugify(
f"{self._energysite.energysite_id} {self.type}"
)
return self._memorized_unique_id

@property
def sw_version(self) -> bool:
Expand Down

0 comments on commit 05f1689

Please sign in to comment.