diff --git a/custom_components/teslemetry/entity.py b/custom_components/teslemetry/entity.py index 4ac59e7..0a39eec 100644 --- a/custom_components/teslemetry/entity.py +++ b/custom_components/teslemetry/entity.py @@ -3,19 +3,19 @@ import asyncio from typing import Any +from tesla_fleet_api.exceptions import TeslaFleetError + +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN, MODELS, TeslemetryState -from .coordinator import ( - TeslemetryEnergyDataCoordinator, - TeslemetryVehicleDataCoordinator, -) -from .models import TeslemetryEnergyData, TeslemetryVehicleData +from .coordinator import TeslemetryVehicleDataCoordinator +from .models import TeslemetryVehicleData class TeslemetryVehicleEntity(CoordinatorEntity[TeslemetryVehicleDataCoordinator]): - """Parent class for Teslemetry Vehicle Entities.""" + """Parent class for Teslemetry Entities.""" _attr_has_entity_name = True @@ -48,11 +48,18 @@ def __init__( async def wake_up_if_asleep(self) -> None: """Wake up the vehicle if its asleep.""" async with self._wakelock: + wait = 0 while self.coordinator.data["state"] != TeslemetryState.ONLINE: - state = (await self.api.wake_up())["response"]["state"] + try: + state = (await self.api.wake_up())["response"]["state"] + except TeslaFleetError as err: + raise HomeAssistantError(str(err)) from err self.coordinator.data["state"] = state if state != TeslemetryState.ONLINE: - await asyncio.sleep(5) + wait += 5 + if wait >= 15: # Give up after 30 seconds total + raise HomeAssistantError("Could not wake up vehicle") + await asyncio.sleep(wait) def get(self, key: str | None = None, default: Any | None = None) -> Any: """Return a specific value from coordinator data.""" @@ -63,65 +70,3 @@ def set(self, *args: Any) -> None: for key, value in args: self.coordinator.data[key] = value self.async_write_ha_state() - - -class TeslemetryEnergyEntity(CoordinatorEntity[TeslemetryEnergyDataCoordinator]): - """Parent class for Teslemetry Energy Entities.""" - - _attr_has_entity_name = True - - def __init__( - self, - energysite: TeslemetryEnergyData, - key: str, - ) -> None: - """Initialize common aspects of a Teslemetry entity.""" - super().__init__(energysite.coordinator) - self.key = key - self.api = energysite.api - - self._attr_translation_key = key - self._attr_unique_id = f"{energysite.id}-{key}" - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, str(energysite.id))}, - manufacturer="Tesla", - configuration_url="https://teslemetry.com/console", - name=self.coordinator.data.get("site_name", "Energy Site"), - ) - - def get(self, key: str | None = None, default: Any | None = None) -> Any: - """Return a specific value from coordinator data.""" - return self.coordinator.data.get(key or self.key, default) - - -class TeslemetryWallConnectorEntity(CoordinatorEntity[TeslemetryEnergyDataCoordinator]): - """Parent class for Teslemetry Wall Connector Entities.""" - - _attr_has_entity_name = True - - def __init__( - self, - energysite: TeslemetryEnergyData, - din: str, - key: str, - ) -> None: - """Initialize common aspects of a Teslemetry entity.""" - super().__init__(energysite.coordinator) - self.din = din - self.key = key - - self._attr_translation_key = key - self._attr_unique_id = f"{energysite.id}-{din}-{key}" - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, din)}, - manufacturer="Tesla", - configuration_url="https://teslemetry.com/console", - name="Wall Connector", - via_device=(DOMAIN, str(energysite.id)), - serial_number=din.split("-")[-1], - ) - - @property - def _value(self) -> int: - """Return a specific wall connector value from coordinator data.""" - return self.coordinator.data["wall_connectors"][self.din].get(self.key) diff --git a/custom_components/teslemetry/sensor.py b/custom_components/teslemetry/sensor.py index e4cd17d..dd43e58 100644 --- a/custom_components/teslemetry/sensor.py +++ b/custom_components/teslemetry/sensor.py @@ -42,7 +42,7 @@ @callback def minutes_to_datetime(value: StateType) -> datetime | None: """Convert relative hours into absolute datetime.""" - if isinstance(value, (int, float)) and value > 0: + if isinstance(value, int | float) and value > 0: return dt_util.now() + timedelta(minutes=value) return None