Skip to content

Commit

Permalink
So many fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 4, 2024
1 parent 0d8a309 commit 587c1af
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 88 deletions.
60 changes: 43 additions & 17 deletions custom_components/teslemetry/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
TeslemetryEnergyLiveEntity,
TeslemetryEnergyInfoEntity,
)
from .models import TeslemetryVehicleData
from .models import TeslemetryVehicleData, TeslemetryEnergyData


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -180,44 +180,70 @@ async def async_setup_entry(
)


class TeslemetryBinarySensorEntity:
"""Base class for all Teslemetry binary sensors."""
class TeslemetryVehicleBinarySensorEntity(TeslemetryVehicleEntity, BinarySensorEntity):
"""Base class for Teslemetry vehicle binary sensors."""

entity_description: TeslemetryBinarySensorEntityDescription

def __init__(
self,
vehicle: TeslemetryVehicleData,
data: TeslemetryVehicleData,
description: TeslemetryBinarySensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, description.key)
super().__init__(data, description.key)
self.entity_description = description

@property
def is_on(self) -> bool:
"""Return the state of the binary sensor."""
return self.entity_description.is_on(self.get())

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


class TeslemetryVehicleBinarySensorEntity(
TeslemetryBinarySensorEntity, TeslemetryVehicleEntity, BinarySensorEntity
):
"""Base class for Teslemetry vehicle binary sensors."""
@property
def is_on(self) -> bool:
"""Return the state of the binary sensor."""
return self.entity_description.is_on(self.get())


class TeslemetryEnergyLiveBinarySensorEntity(
TeslemetryBinarySensorEntity, TeslemetryEnergyLiveEntity, BinarySensorEntity
TeslemetryEnergyLiveEntity, BinarySensorEntity
):
"""Base class for Teslemetry energy live binary sensors."""

entity_description: TeslemetryBinarySensorEntityDescription

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

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


class TeslemetryEnergyInfoBinarySensorEntity(
TeslemetryBinarySensorEntity, TeslemetryEnergyInfoEntity, BinarySensorEntity
TeslemetryEnergyInfoEntity, BinarySensorEntity
):
"""Base class for Teslemetry energy info binary sensors."""

entity_description: TeslemetryBinarySensorEntityDescription

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

@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.has()
4 changes: 2 additions & 2 deletions custom_components/teslemetry/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class TeslemetryButtonEntity(TeslemetryVehicleEntity, ButtonEntity):

def __init__(
self,
vehicle: TeslemetryVehicleData,
data: TeslemetryVehicleData,
description: TeslemetryButtonEntityDescription,
) -> None:
"""Initialize the button."""
super().__init__(vehicle, description.key)
super().__init__(data, description.key)
self.entity_description = description

async def async_press(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions custom_components/teslemetry/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class TeslemetryClimateEntity(TeslemetryVehicleEntity, ClimateEntity):

def __init__(
self,
vehicle: TeslemetryVehicleData,
data: TeslemetryVehicleData,
side: TeslemetryClimateSide,
scoped: bool,
) -> None:
"""Initialize the climate."""
super().__init__(vehicle, side)
super().__init__(data, side)
self.scoped = scoped

if not scoped:
Expand Down
4 changes: 2 additions & 2 deletions custom_components/teslemetry/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class TeslemetryWindowEntity(TeslemetryVehicleEntity, CoverEntity):
_attr_device_class = CoverDeviceClass.WINDOW
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

def __init__(self, vehicle: TeslemetryVehicleData, scoped) -> None:
def __init__(self, data: TeslemetryVehicleData, scoped) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "windows")
super().__init__(data, "windows")
self.scoped = scoped
if not scoped:
self._attr_supported_features = CoverEntityFeature(0)
Expand Down
12 changes: 6 additions & 6 deletions custom_components/teslemetry/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ async def async_setup_entry(
class TeslemetryLockEntity(TeslemetryVehicleEntity, LockEntity):
"""Lock entity for Teslemetry."""

def __init__(self, vehicle: TeslemetryVehicleData, scoped: bool) -> None:
def __init__(self, data: TeslemetryVehicleData, scoped: bool) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_locked")
super().__init__(data, "vehicle_state_locked")
self.scoped = scoped

@property
Expand Down Expand Up @@ -67,11 +67,11 @@ class TeslemetryCableLockEntity(TeslemetryVehicleEntity, LockEntity):

def __init__(
self,
vehicle: TeslemetryVehicleData,
data: TeslemetryVehicleData,
scoped: bool,
) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "charge_state_charge_port_latch")
super().__init__(data, "charge_state_charge_port_latch")
self.scoped = scoped

@property
Expand Down Expand Up @@ -102,11 +102,11 @@ class TeslemetrySpeedLimitEntity(TeslemetryVehicleEntity, LockEntity):

def __init__(
self,
vehicle: TeslemetryVehicleData,
data: TeslemetryVehicleData,
scoped: bool,
) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_speed_limit_mode_active")
super().__init__(data, "vehicle_state_speed_limit_mode_active")
self.scoped = scoped

@property
Expand Down
4 changes: 2 additions & 2 deletions custom_components/teslemetry/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class TeslemetryMediaEntity(TeslemetryVehicleEntity, MediaPlayerEntity):

def __init__(
self,
vehicle: TeslemetryVehicleData,
data: TeslemetryVehicleData,
scoped: bool,
) -> None:
"""Initialize the media player entity."""
super().__init__(vehicle, "media")
super().__init__(data, "media")
self.scoped = scoped
if not scoped:
self._attr_supported_features = MediaPlayerEntityFeature(0)
Expand Down
76 changes: 57 additions & 19 deletions custom_components/teslemetry/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
from .const import DOMAIN
from .entity import (
TeslemetryVehicleEntity,
TeslemetryEnergyInfoEntity,
)
from .models import TeslemetryVehicleData
from .models import TeslemetryVehicleData, TeslemetryEnergyData


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -111,7 +112,7 @@ async def async_setup_entry(

# Add vehicle entities
async_add_entities(
TeslemetryNumberEntity(
TeslemetryVehicleNumberEntity(
vehicle,
description,
any(scope in data.scopes for scope in description.scopes),
Expand All @@ -122,7 +123,7 @@ async def async_setup_entry(

# Add energy site entities
async_add_entities(
TeslemetryNumberEntity(
TeslemetryEnergyInfoNumberSensorEntity(
energysite,
description,
any(scope in data.scopes for scope in description.scopes),
Expand All @@ -133,22 +134,11 @@ async def async_setup_entry(
)


class TeslemetryNumberEntity(TeslemetryVehicleEntity, NumberEntity):
"""Number entity for current charge."""
class TeslemetryNumberEntity:
"""Base class for all Teslemetry number entities."""

entity_description: TeslemetryNumberEntityDescription

def __init__(
self,
vehicle: TeslemetryVehicleData,
description: TeslemetryNumberEntityDescription,
scoped: bool,
) -> None:
"""Initialize the Number entity."""
super().__init__(vehicle, description.key)
self.scoped = scoped
self.entity_description = description

@property
def native_value(self) -> float | None:
"""Return the value reported by the number."""
Expand All @@ -167,13 +157,61 @@ def native_min_value(self) -> float:
@property
def native_max_value(self) -> float:
"""Return the maximum value."""
return self.get(
self.entity_description.max_key, self.entity_description.native_max_value
)
if self.entity_description.max_key:
return self.get(
self.entity_description.max_key,
self.entity_description.native_max_value,
)
return self.entity_description.native_max_value


class TeslemetryVehicleNumberEntity(
TeslemetryNumberEntity, TeslemetryVehicleEntity, NumberEntity
):
"""Number entity for current charge."""

def __init__(
self,
data: TeslemetryVehicleData,
description: TeslemetryNumberEntityDescription,
scoped: bool,
) -> None:
"""Initialize the Number entity."""
super().__init__(data, description.key)
self.scoped = scoped
self.entity_description = description

async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
self.raise_for_scope()
await self.wake_up_if_asleep()
await self.entity_description.func(self.api, value)
self.set((self.key, value))


class TeslemetryEnergyInfoNumberSensorEntity(
TeslemetryNumberEntity, TeslemetryEnergyInfoEntity, NumberEntity
):
"""Number entity for current charge."""

def __init__(
self,
data: TeslemetryEnergyData,
description: TeslemetryNumberEntityDescription,
scoped: bool,
) -> None:
"""Initialize the Number entity."""
super().__init__(data, description.key)
self.scoped = scoped
self.entity_description = description

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

async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
self.raise_for_scope()
await self.entity_description.func(self.api, value)
self.set((self.key, value))
12 changes: 8 additions & 4 deletions custom_components/teslemetry/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TeslemetryVehicleEntity,
TeslemetryEnergyInfoEntity,
)
from .models import TeslemetryEnergyData, TeslemetryVehicleData

SEAT_HEATERS = {
"climate_state_seat_heater_left": "front_left",
Expand Down Expand Up @@ -85,9 +86,9 @@ class TeslemetrySeatHeaterSelectEntity(TeslemetryVehicleEntity, SelectEntity):
TeslemetrySeatHeaterOptions.HIGH,
]

def __init__(self, vehicle, key, scoped: bool) -> None:
def __init__(self, data: TeslemetryVehicleData, key: str, scoped: bool) -> None:
"""Initialize the vehicle seat select entity."""
super().__init__(vehicle, key)
super().__init__(data, key)
self.scoped = scoped

@property
Expand All @@ -113,10 +114,13 @@ class TeslemetryEnergySiteSelectEntity(TeslemetryEnergyInfoEntity, SelectEntity)
"""Select entity for energy sites."""

def __init__(
self, vehicle, description: TeslemetrySelectEntityDescription, scoped: bool
self,
data: TeslemetryEnergyData,
description: TeslemetrySelectEntityDescription,
scoped: bool,
) -> None:
"""Initialize the operation mode select entity."""
super().__init__(vehicle, description.key)
super().__init__(data, description.key)
self.scoped = scoped
self.entity_description = description

Expand Down
Loading

0 comments on commit 587c1af

Please sign in to comment.