Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 4, 2024
1 parent 728ff34 commit 3efebee
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 40 deletions.
4 changes: 3 additions & 1 deletion custom_components/teslemetry/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class TeslemetryButtonEntityDescription(ButtonEntityDescription):


DESCRIPTIONS: tuple[TeslemetryButtonEntityDescription, ...] = (
TeslemetryButtonEntityDescription(key="wake", func=lambda api: None), # Every button also runs wakeup
TeslemetryButtonEntityDescription(
key="wake", func=lambda api: None
), # Every button also runs wakeup
TeslemetryButtonEntityDescription(
key="flash_lights", func=lambda api: api.flash_lights()
),
Expand Down
5 changes: 3 additions & 2 deletions custom_components/teslemetry/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN, TeslemetryClimateSide
from .context import handle_command
from .entity import TeslemetryVehicleEntity
from .models import TeslemetryVehicleData

Expand All @@ -26,7 +25,9 @@ async def async_setup_entry(
data = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
TeslemetryClimateEntity(vehicle, TeslemetryClimateSide.DRIVER, Scopes.VEHICLE_CMDS in data.scopes)
TeslemetryClimateEntity(
vehicle, TeslemetryClimateSide.DRIVER, Scopes.VEHICLE_CMDS in data.scopes
)
for vehicle in data.vehicles
)

Expand Down
4 changes: 2 additions & 2 deletions custom_components/teslemetry/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def async_setup_entry(
for klass in (
TeslemetryLockEntity,
TeslemetryCableLockEntity,
TeslemetrySpeedLimitEntity
TeslemetrySpeedLimitEntity,
)
for vehicle in data.vehicles
)
Expand Down Expand Up @@ -130,4 +130,4 @@ async def async_unlock(self, **kwargs: Any) -> None:
self.raise_for_scope()
await self.wake_up_if_asleep()
await self.api.speed_limit_deactivate(code)
self.set((self.key, False))
self.set((self.key, False))
43 changes: 33 additions & 10 deletions custom_components/teslemetry/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,26 @@ class TeslemetryNumberEntityDescription(NumberEntityDescription):
),
)

ENERGY_INFO_DESCRIPTIONS: tuple[
TeslemetryNumberEntityDescription, ...
] = TeslemetryNumberEntityDescription(
key="backup_reserve_percent",
native_step=PRECISION_WHOLE,
native_min_value=0,
native_max_value=100,
native_unit_of_measurement=PERCENTAGE,
scopes=[Scopes.ENERGY_CMDS],
func=lambda api, value: api.backup(int(value)),
ENERGY_INFO_DESCRIPTIONS: tuple[TeslemetryNumberEntityDescription, ...] = (
TeslemetryNumberEntityDescription(
key="backup_reserve_percent",
native_step=PRECISION_WHOLE,
native_min_value=0,
native_max_value=100,
native_unit_of_measurement=PERCENTAGE,
scopes=[Scopes.ENERGY_CMDS],
func=lambda api, value: api.backup(int(value)),
),
TeslemetryNumberEntityDescription(
# I have no examples of this
key="off_grid_vehicle_charging_reserve",
native_step=PRECISION_WHOLE,
native_min_value=0,
native_max_value=100,
native_unit_of_measurement=PERCENTAGE,
scopes=[Scopes.ENERGY_CMDS],
func=lambda api, value: api.off_grid_vehicle_charging_reserve(int(value)),
)
)


Expand All @@ -99,6 +109,7 @@ async def async_setup_entry(
"""Set up the Teslemetry sensor platform from a config entry."""
data = hass.data[DOMAIN][entry.entry_id]

# Add vehicle entities
async_add_entities(
TeslemetryNumberEntity(
vehicle,
Expand All @@ -109,6 +120,18 @@ async def async_setup_entry(
for description in VEHICLE_DESCRIPTIONS
)

# Add energy site entities
async_add_entities(
TeslemetryNumberEntity(
energysite,
description,
any(scope in data.scopes for scope in description.scopes),
)
for energysite in data.energysites
for description in ENERGY_INFO_DESCRIPTIONS
if description.key in energysite.info_coordinator.data
)


class TeslemetryNumberEntity(TeslemetryVehicleEntity, NumberEntity):
"""Number entity for current charge."""
Expand Down
76 changes: 51 additions & 25 deletions custom_components/teslemetry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
TeslemetryEnergyLiveEntity,
TeslemetryVehicleEntity,
TeslemetryWallConnectorEntity,
TeslemetryEnergyInfoEntity,
)
from .models import TeslemetryEnergyData, TeslemetryVehicleData

Expand Down Expand Up @@ -350,6 +351,12 @@ class TeslemetrySensorEntityDescription(SensorEntityDescription):
),
)

ENERGY_INFO_DESCRIPTIONS: tuple[SensorEntityDescription, ...] = SensorEntityDescription(
key="vpp_backup_reserve_percent",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=SensorDeviceClass.BATTERY,
)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
Expand All @@ -364,9 +371,9 @@ async def async_setup_entry(
for description in VEHICLE_DESCRIPTIONS
)

# Add energy sites
# Add energy site live
async_add_entities(
TeslemetryEnergySensorEntity(energysite, description)
TeslemetryEnergyLiveSensorEntity(energysite, description)
for energysite in data.energysites
for description in ENERGY_LIVE_DESCRIPTIONS
if description.key in energysite.live_coordinator.data
Expand All @@ -380,12 +387,33 @@ async def async_setup_entry(
for description in WALL_CONNECTOR_DESCRIPTIONS
)

# Add energy site info
async_add_entities(
TeslemetryEnergyInfoSensorEntity(energysite, description)
for energysite in data.energysites
for description in ENERGY_INFO_DESCRIPTIONS
)

class TeslemetryVehicleSensorEntity(TeslemetryVehicleEntity, SensorEntity):
"""Base class for Teslemetry vehicle metric sensors."""

class TeslemetrySensorEntity(SensorEntity):
"""Base class for all Teslemetry sensors."""

entity_description: TeslemetrySensorEntityDescription

@property
def native_value(self) -> StateType | datetime:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.get())

@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.has()


class TeslemetryVehicleSensorEntity(TeslemetryVehicleEntity, TeslemetrySensorEntity):
"""Base class for Teslemetry vehicle metric sensors."""

def __init__(
self,
vehicle: TeslemetryVehicleData,
Expand All @@ -395,11 +423,6 @@ def __init__(
super().__init__(vehicle, description.key)
self.entity_description = description

@property
def native_value(self) -> StateType | datetime:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.get())

@property
def available(self) -> bool:
"""Return if sensor is available."""
Expand All @@ -410,11 +433,11 @@ def available(self) -> bool:
)


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

entity_description: SensorEntityDescription

def __init__(
self,
energysite: TeslemetryEnergyData,
Expand All @@ -424,22 +447,12 @@ def __init__(
super().__init__(energysite, description.key)
self.entity_description = description

@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.get()

@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.has()


class TeslemetryWallConnectorSensorEntity(TeslemetryWallConnectorEntity, SensorEntity):
class TeslemetryWallConnectorSensorEntity(
TeslemetryWallConnectorEntity, TeslemetrySensorEntity
):
"""Base class for Teslemetry energy site metric sensors."""

entity_description: SensorEntityDescription

def __init__(
self,
energysite: TeslemetryEnergyData,
Expand All @@ -465,3 +478,16 @@ def available(self) -> bool:
return super().available and self.din in self.coordinator.data.get(
"wall_connectors", {}
)


class TeslemetryEnergyInfoSensorEntity(TeslemetryEnergyInfoEntity, SensorEntity):
"""Base class for Teslemetry energy site metric sensors."""

def __init__(
self,
energysite: TeslemetryEnergyData,
description: SensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(energysite, description.key)
self.entity_description = description

0 comments on commit 3efebee

Please sign in to comment.