Skip to content

Commit

Permalink
Improve avaliabilty
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 3, 2024
1 parent bded9c5 commit d858493
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion custom_components/teslemetry/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,5 @@ def is_on(self) -> bool:
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.get() is not None
return super().available and self.has()

8 changes: 8 additions & 0 deletions custom_components/teslemetry/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def set(self, *args: Any) -> None:
self.coordinator.data[key] = value
self.async_write_ha_state()

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


class TeslemetryEnergyEntity(CoordinatorEntity[TeslemetryEnergyDataCoordinator]):
"""Parent class for Teslemetry Energy Entities."""
Expand Down Expand Up @@ -106,6 +110,10 @@ 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):
"""Checks if a key exists in the coordinator data."""
return (key or self.key) in self.coordinator.data


class TeslemetryWallConnectorEntity(CoordinatorEntity[TeslemetryEnergyDataCoordinator]):
"""Parent class for Teslemetry Wall Connector Entities."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/teslemetry/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TeslemetrySeatHeaterSelectEntity(TeslemetryVehicleEntity, SelectEntity):
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.get() is not None
return super().available and self.has()

@property
def current_option(self) -> str | None:
Expand Down
18 changes: 11 additions & 7 deletions custom_components/teslemetry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
UnitOfTime,
)
from homeassistant.util.variance import ignore_variance
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util import dt as dt_util
Expand All @@ -49,12 +49,16 @@
"NoPower": "no_power",
}

ShiftStates = {
"P":"p", "D":"d", "R":"r", "N":"n"
}

@dataclass(frozen=True, kw_only=True)
class TeslemetrySensorEntityDescription(SensorEntityDescription):
"""Describes Teslemetry Sensor entity."""

value_fn: Callable[[StateType], StateType | datetime] = lambda x: x
available_fn: Callable[[StateType], bool] = lambda x: x is not None
available_fn: Callable[[StateType], bool] = lambda: True


VEHICLE_DESCRIPTIONS: tuple[TeslemetrySensorEntityDescription, ...] = (
Expand Down Expand Up @@ -136,9 +140,9 @@ class TeslemetrySensorEntityDescription(SensorEntityDescription):
TeslemetrySensorEntityDescription(
key="drive_state_shift_state",
icon="mdi:car-shift-pattern",
options=["p", "d", "r", "n"],
options=list(ShiftStates.values()),
device_class=SensorDeviceClass.ENUM,
value_fn=lambda x: cast(str, x).lower(),
value_fn=lambda x: ShiftStates.get(x,"p"),
),
TeslemetrySensorEntityDescription(
key="vehicle_state_odometer",
Expand Down Expand Up @@ -415,7 +419,7 @@ def native_value(self) -> StateType | datetime:
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.entity_description.available_fn(self.get())
return super().available and self.has() and self.entity_description.available_fn(self.get())


class TeslemetryEnergySensorEntity(TeslemetryEnergyEntity, SensorEntity):
Expand All @@ -440,7 +444,7 @@ def native_value(self) -> StateType:
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.get() is not None
return super().available and self.has()


class TeslemetryWallConnectorSensorEntity(TeslemetryWallConnectorEntity, SensorEntity):
Expand Down Expand Up @@ -470,4 +474,4 @@ def native_value(self) -> StateType:
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self._value is not None
return super().available and self.din in self.coordinator.data.get("wall_connectors", {})
2 changes: 1 addition & 1 deletion custom_components/teslemetry/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.get() is not None
return super().available and self.has()

@property
def is_on(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/teslemetry/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.get() is not None
return super().available and self.has()

@property
def supported_features(self) -> UpdateEntityFeature:
Expand Down

0 comments on commit d858493

Please sign in to comment.