Skip to content

Commit

Permalink
Merge branch 'avaliable'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 7, 2024
2 parents c665dc0 + 1da4878 commit 43764ad
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 150 deletions.
15 changes: 0 additions & 15 deletions custom_components/teslemetry/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,6 @@ def __init__(
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()

@property
def is_on(self) -> bool:
"""Return the state of the binary sensor."""
Expand All @@ -221,11 +216,6 @@ def __init__(
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(
TeslemetryEnergyInfoEntity, BinarySensorEntity
Expand All @@ -242,8 +232,3 @@ def __init__(
"""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()
5 changes: 5 additions & 0 deletions custom_components/teslemetry/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def __init__(
super().__init__(data, description.key)
self.entity_description = description

@property
def avaliable(self) -> bool:
"""Return if the cover is available."""
return True

async def async_press(self) -> None:
"""Press the button."""
await self.wake_up_if_asleep()
Expand Down
20 changes: 20 additions & 0 deletions custom_components/teslemetry/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def __init__(self, data: TeslemetryVehicleData, scoped) -> None:
if not scoped:
self._attr_supported_features = CoverEntityFeature(0)

@property
def avaliable(self) -> bool:
"""Return if the cover is available."""
return True

@property
def is_closed(self) -> bool | None:
"""Return if the cover is closed or not."""
Expand Down Expand Up @@ -101,6 +106,11 @@ def __init__(self, vehicle: TeslemetryVehicleData, scoped) -> None:
if not scoped:
self._attr_supported_features = CoverEntityFeature(0)

@property
def avaliable(self) -> bool:
"""Return if the cover is available."""
return True

@property
def is_closed(self) -> bool | None:
"""Return if the cover is closed or not."""
Expand Down Expand Up @@ -135,6 +145,11 @@ def __init__(self, vehicle: TeslemetryVehicleData, scoped) -> None:
if not scoped:
self._attr_supported_features = CoverEntityFeature(0)

@property
def avaliable(self) -> bool:
"""Return if the cover is available."""
return True

@property
def is_closed(self) -> bool | None:
"""Return if the cover is closed or not."""
Expand All @@ -161,6 +176,11 @@ def __init__(self, vehicle: TeslemetryVehicleData, scoped) -> None:
if not scoped:
self._attr_supported_features = CoverEntityFeature(0)

@property
def avaliable(self) -> bool:
"""Return if the cover is available."""
return True

@property
def is_closed(self) -> bool | None:
"""Return if the cover is closed or not."""
Expand Down
56 changes: 30 additions & 26 deletions custom_components/teslemetry/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
from typing import Any
from tesla_fleet_api import VehicleSpecific, EnergySpecific
from tesla_fleet_api.exceptions import TeslaFleetError

from homeassistant.helpers.device_registry import DeviceInfo
Expand All @@ -17,21 +18,38 @@
from .models import TeslemetryEnergyData, TeslemetryVehicleData


class TeslemetryEntity(CoordinatorEntity):
class TeslemetryEntity(
CoordinatorEntity[
TeslemetryVehicleDataCoordinator
| TeslemetryEnergySiteLiveCoordinator
| TeslemetryEnergySiteInfoCoordinator
]
):
"""Parent class for all Teslemetry entities."""

_attr_has_entity_name = True

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

@property
def available(self) -> bool:
"""Return if sensor is available."""
return (
self.coordinator.last_update_success and self.key in self.coordinator.data
)

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)
Expand All @@ -42,10 +60,6 @@ def set(self, *args: Any) -> None:
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

def raise_for_scope(self):
"""Raise an error if a scope is not available."""
if not self.scoped:
Expand All @@ -54,9 +68,7 @@ def raise_for_scope(self):
)


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

def __init__(
Expand All @@ -65,17 +77,15 @@ def __init__(
key: str,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(data.coordinator, key)
self.api = data.api
super().__init__(data.coordinator, data.api, key)
self._attr_unique_id = f"{data.vin}-{key}"
self._wakelock = data.wakelock

if car_type := self.coordinator.data.get("vehicle_config_car_type"):
car_type = MODELS.get(car_type, car_type)
if sw_version := self.coordinator.data.get("vehicle_state_car_version"):
sw_version = sw_version.split(" ")[0]

self._attr_translation_key = key
self._attr_unique_id = f"{data.vin}-{key}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, data.vin)},
manufacturer="Tesla",
Expand Down Expand Up @@ -106,9 +116,7 @@ async def wake_up_if_asleep(self) -> None:
await asyncio.sleep(wait)


class TeslemetryEnergyLiveEntity(
TeslemetryEntity, CoordinatorEntity[TeslemetryEnergySiteLiveCoordinator]
):
class TeslemetryEnergyLiveEntity(TeslemetryEntity):
"""Parent class for Teslemetry Energy Site Live entities."""

def __init__(
Expand All @@ -117,9 +125,8 @@ def __init__(
key: str,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(data.live_coordinator, key)
super().__init__(data.live_coordinator, data.api, key)
self._attr_unique_id = f"{data.id}-{key}"
self.api = data.api

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, str(data.id))},
Expand All @@ -129,9 +136,7 @@ def __init__(
)


class TeslemetryEnergyInfoEntity(
TeslemetryEntity, CoordinatorEntity[TeslemetryEnergySiteInfoCoordinator]
):
class TeslemetryEnergyInfoEntity(TeslemetryEntity):
"""Parent class for Teslemetry Energy Site Info Entities."""

def __init__(
Expand All @@ -140,9 +145,8 @@ def __init__(
key: str,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(data.info_coordinator, key)
super().__init__(data.info_coordinator, data.api, key)
self._attr_unique_id = f"{data.id}-{key}"
self.api = data.api

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, str(data.id))},
Expand All @@ -166,10 +170,10 @@ def __init__(
key: str,
) -> None:
"""Initialize common aspects of a Teslemetry entity."""
super().__init__(data.live_coordinator, key)
super().__init__(data.live_coordinator, data.api, key)
self._attr_unique_id = f"{data.id}-{din}-{key}"
self.din = din

self._attr_unique_id = f"{data.id}-{din}-{key}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, din)},
manufacturer="Tesla",
Expand Down
5 changes: 0 additions & 5 deletions custom_components/teslemetry/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ def __init__(
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()
Expand Down
10 changes: 0 additions & 10 deletions custom_components/teslemetry/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ def __init__(self, data: TeslemetryVehicleData, key: str, scoped: bool) -> None:
super().__init__(data, key)
self.scoped = scoped

@property
def available(self) -> bool:
"""Return if seat heater is available."""
return super().available

@property
def current_option(self) -> str | None:
"""Return the current selected option."""
Expand Down Expand Up @@ -132,11 +127,6 @@ def __init__(
self.scoped = scoped
self.entity_description = description

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

@property
def current_option(self) -> str | None:
"""Return the current selected option."""
Expand Down
20 changes: 2 additions & 18 deletions custom_components/teslemetry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,7 @@ def __init__(
@property
def available(self) -> bool:
"""Return if sensor is available."""
return (
super().available
and self.has()
and self.entity_description.available_fn(self.get())
)
return super().available and self.entity_description.available_fn(self.get())

@property
def native_value(self) -> StateType | datetime:
Expand All @@ -453,11 +449,6 @@ def native_value(self) -> StateType | datetime:
"""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):
"""Base class for Teslemetry energy site metric sensors."""
Expand Down Expand Up @@ -486,9 +477,7 @@ def native_value(self) -> StateType:
@property
def available(self) -> bool:
"""Return if sensor is available."""
return super().available and self.din in self.coordinator.data.get(
"wall_connectors", {}
)
return self.coordinator.last_update_success


class TeslemetryEnergyInfoSensorEntity(TeslemetryEnergyInfoEntity, SensorEntity):
Expand All @@ -509,8 +498,3 @@ def __init__(
def native_value(self) -> StateType | datetime:
"""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()
Loading

0 comments on commit 43764ad

Please sign in to comment.