Skip to content

Commit

Permalink
Add new entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 3, 2024
1 parent 2b025f4 commit 5576a32
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 39 deletions.
6 changes: 3 additions & 3 deletions custom_components/teslemetry/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

from .const import LOGGER, TeslemetryState

VEHICLE_INTERVAL = timedelta(seconds=15)
ENERGY_LIVE_INTERVAL = timedelta(seconds=15)
ENERGY_INFO_INTERVAL = timedelta(seconds=60)
VEHICLE_INTERVAL = timedelta(seconds=30)
ENERGY_LIVE_INTERVAL = timedelta(seconds=30)
ENERGY_INFO_INTERVAL = timedelta(seconds=300)

def flatten(
self, data: dict[str, Any], parent: str | None = None
Expand Down
85 changes: 51 additions & 34 deletions custom_components/teslemetry/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,45 @@
from .models import TeslemetryEnergyData, TeslemetryVehicleData


class TeslemetryVehicleEntity(CoordinatorEntity[TeslemetryVehicleDataCoordinator]):
"""Parent class for Teslemetry Entities."""
class TeslemetryEntity(CoordinatorEntity):
"""Parent class for all Teslemetry entities."""

_attr_has_entity_name = True

def __init__(
self,
coordinator: CoordinatorEntity,
key: str | int,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(coordinator)
self.key = key
self._attr_translation_key = key

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)

def set(self, *args: Any) -> None:
"""Set a value in coordinator data."""
for key, value in args:
self.coordinator.data[key] = value
self.async_write_ha_state()

def has(self, key: str | None = None):
"""Check if a key exists in the coordinator data."""
return (key or self.key) in self.coordinator.data

class TeslemetryVehicleEntity(TeslemetryEntity):
"""Parent class for Teslemetry Vehicle entities."""

def __init__(
self,
vehicle: TeslemetryVehicleData,
key: str,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(vehicle.coordinator)
self.key = key
super().__init__(vehicle.coordinator, key)
self.api = vehicle.api
self._wakelock = vehicle.wakelock

Expand Down Expand Up @@ -68,53 +94,46 @@ async def wake_up_if_asleep(self) -> None:
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."""
return self.coordinator.data.get(key or self.key, default)

def set(self, *args: Any) -> None:
"""Set a value in coordinator data."""
for key, value in args:
self.coordinator.data[key] = value
self.async_write_ha_state()

def has(self, key: str | None = None):
"""Check if a key exists in the coordinator data."""
return (key or self.key) in self.coordinator.data
class TeslemetryEnergyLiveEntity(CoordinatorEntity[TeslemetryEnergySiteLiveCoordinator | TeslemetryEnergySiteInfoCoordinator]):
"""Parent class for Teslemetry Energy Site Live entities."""

def __init__(
self,
energysite: TeslemetryEnergyData,
key: str,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(energysite.live_coordinator, key)
self._attr_unique_id = f"{energysite.id}-{key}"

class TeslemetryEnergyEntity(CoordinatorEntity[TeslemetryEnergySiteLiveCoordinator | TeslemetryEnergySiteInfoCoordinator]):
"""Parent class for Teslemetry Energy Entities."""
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"),
)

_attr_has_entity_name = True
class TeslemetryEnergyInfoEntity(CoordinatorEntity[TeslemetryEnergySiteLiveCoordinator | TeslemetryEnergySiteInfoCoordinator]):
"""Parent class for Teslemetry Energy Site Info Entities."""

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
super().__init__(energysite.info_coordinator, 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)

def has(self, key: str | None = None):
"""Check if a key exists in the coordinator data."""
return (key or self.key) in self.coordinator.data


class TeslemetryWallConnectorEntity(CoordinatorEntity[TeslemetryEnergySiteLiveCoordinator]):
"""Parent class for Teslemetry Wall Connector Entities."""
Expand All @@ -128,11 +147,9 @@ def __init__(
key: str,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(energysite.coordinator)
super().__init__(energysite.live_coordinator, key)
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)},
Expand Down
4 changes: 2 additions & 2 deletions custom_components/teslemetry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from .const import DOMAIN
from .entity import (
TeslemetryEnergyEntity,
TeslemetryEnergyLiveEntity,
TeslemetryVehicleEntity,
TeslemetryWallConnectorEntity,
)
Expand Down Expand Up @@ -407,7 +407,7 @@ def available(self) -> bool:
return super().available and self.has() and self.entity_description.available_fn(self.get())


class TeslemetryEnergySensorEntity(TeslemetryEnergyEntity, SensorEntity):
class TeslemetryEnergySensorEntity(TeslemetryEnergyLiveEntity, SensorEntity):
"""Base class for Teslemetry energy site metric sensors."""

entity_description: SensorEntityDescription
Expand Down

0 comments on commit 5576a32

Please sign in to comment.