From c3ad00f504ef07920f84f1ee1e0257c529563198 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 4 Feb 2024 03:58:45 +0000 Subject: [PATCH] Improvements --- custom_components/teslemetry/binary_sensor.py | 56 +++++++++++++++++-- custom_components/teslemetry/sensor.py | 4 ++ custom_components/teslemetry/switch.py | 2 +- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/custom_components/teslemetry/binary_sensor.py b/custom_components/teslemetry/binary_sensor.py index 8e96a34..9b05b47 100644 --- a/custom_components/teslemetry/binary_sensor.py +++ b/custom_components/teslemetry/binary_sensor.py @@ -17,8 +17,10 @@ from .const import DOMAIN, TeslemetryState from .entity import ( TeslemetryVehicleEntity, + TeslemetryEnergyLiveEntity, + TeslemetryEnergyInfoEntity ) -from .models import TeslemetryVehicleData +from .models import TeslemetryVehicleData, TeslemetryEnergyData @dataclass(frozen=True, kw_only=True) @@ -28,7 +30,7 @@ class TeslemetryBinarySensorEntityDescription(BinarySensorEntityDescription): is_on: Callable[..., bool] = lambda x: x -DESCRIPTIONS: tuple[TeslemetryBinarySensorEntityDescription, ...] = ( +VEHICLE_DESCRIPTIONS: tuple[TeslemetryBinarySensorEntityDescription, ...] = ( TeslemetryBinarySensorEntityDescription( key="state", device_class=BinarySensorDeviceClass.CONNECTIVITY, @@ -140,6 +142,25 @@ class TeslemetryBinarySensorEntityDescription(BinarySensorEntityDescription): ), ) +ENERGY_LIVE_DESCRIPTIONS: tuple[TeslemetryBinarySensorEntityDescription, ...] = ( + TeslemetryBinarySensorEntityDescription( + key="backup_capable" + ), + TeslemetryBinarySensorEntityDescription( + key="grid_services_active" + ), +) + + +ENERGY_INFO_DESCRIPTIONS: tuple[TeslemetryBinarySensorEntityDescription, ...] = ( + TeslemetryBinarySensorEntityDescription( + key="components_disallow_charge_from_grid_with_solar_installed", + ), + TeslemetryBinarySensorEntityDescription( + key="components_grid_services_enabled", + ), +) + async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback @@ -148,14 +169,25 @@ async def async_setup_entry( data = hass.data[DOMAIN][entry.entry_id] async_add_entities( - TeslemetryBinarySensorEntity(vehicle, description) + TeslemetryVehicleBinarySensorEntity(vehicle, description) for vehicle in data.vehicles - for description in DESCRIPTIONS + for description in VEHICLE_DESCRIPTIONS + ) + + async_add_entities( + TeslemetryEnergyLiveBinarySensorEntity(energysite, description) + for energysite in data.energysites + for description in ENERGY_LIVE_DESCRIPTIONS ) + async_add_entities( + TeslemetryEnergyInfoBinarySensorEntity(energysite, description) + for energysite in data.energysites + for description in ENERGY_INFO_DESCRIPTIONS + ) -class TeslemetryBinarySensorEntity(TeslemetryVehicleEntity, BinarySensorEntity): - """Base class for Teslemetry binary sensors.""" +class TeslemetryBinarySensorEntity(): + """Base class for all Teslemetry binary sensors""" entity_description: TeslemetryBinarySensorEntityDescription @@ -177,3 +209,15 @@ def is_on(self) -> bool: 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.""" + + +class TeslemetryEnergyLiveBinarySensorEntity(TeslemetryBinarySensorEntity, TeslemetryEnergyLiveEntity, BinarySensorEntity): + """Base class for Teslemetry energy live binary sensors.""" + + +class TeslemetryEnergyInfoBinarySensorEntity(TeslemetryBinarySensorEntity, TeslemetryEnergyInfoEntity, BinarySensorEntity): + """Base class for Teslemetry energy info binary sensors.""" diff --git a/custom_components/teslemetry/sensor.py b/custom_components/teslemetry/sensor.py index 3a81e02..fca6e21 100644 --- a/custom_components/teslemetry/sensor.py +++ b/custom_components/teslemetry/sensor.py @@ -323,6 +323,10 @@ class TeslemetrySensorEntityDescription(SensorEntityDescription): suggested_display_precision=2, device_class=SensorDeviceClass.POWER, ), + SensorEntityDescription( + key="island_status" + ), + ) WALL_CONNECTOR_DESCRIPTIONS: tuple[SensorEntityDescription, ...] = ( diff --git a/custom_components/teslemetry/switch.py b/custom_components/teslemetry/switch.py index f5d7c3d..ff9fc9b 100644 --- a/custom_components/teslemetry/switch.py +++ b/custom_components/teslemetry/switch.py @@ -88,7 +88,7 @@ class TeslemetrySwitchEntityDescription(SwitchEntityDescription): scopes=[Scopes.ENERGY_CMDS], ), TeslemetrySwitchEntityDescription( - key="user_settings_storm_mode_enabled", + key="user_settings_storm_mode_enabled", #this is also in the live data as storm_mode_enabled on_func=lambda api: api.storm_mode(enabled=True), off_func=lambda api: api.storm_mode(enabled=False), scopes=[Scopes.ENERGY_CMDS],