Skip to content

Commit

Permalink
Add energy commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 4, 2024
1 parent 3efebee commit 6029ef1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
2 changes: 1 addition & 1 deletion custom_components/teslemetry/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TeslemetryNumberEntityDescription(NumberEntityDescription):
native_unit_of_measurement=PERCENTAGE,
scopes=[Scopes.ENERGY_CMDS],
func=lambda api, value: api.off_grid_vehicle_charging_reserve(int(value)),
)
),
)


Expand Down
77 changes: 42 additions & 35 deletions custom_components/teslemetry/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
from __future__ import annotations

from tesla_fleet_api.const import Scopes
from dataclasses import dataclass
from collections.abc import Callable

from homeassistant.components.select import SelectEntity
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN, TeslemetrySeatHeaterOptions
from .entity import (
TeslemetryVehicleEntity,
TeslemetryEnergySiteInfoEntity,
TeslemetryEnergyInfoEntity,
)

SEAT_HEATERS = {
Expand All @@ -25,6 +27,30 @@
}


@dataclass(frozen=True, kw_only=True)
class TeslemetrySelectEntityDescription(SelectEntityDescription):
"""Describes a Teslemetry Select entity."""

key: str
func: Callable


ENERGY_INFO_DESCRIPTIONS: tuple(TeslemetrySelectEntityDescription, ...) = (
TeslemetrySelectEntityDescription(
key="default_real_mode",
func=lambda api, value: api.operation(value),
options=["autonomous", "self_consumption", "backup"],
),
TeslemetrySelectEntityDescription(
key="component_net_meter_mode",
func=lambda api, value: api.grid_import_export(
customer_preferred_export_rule=value
),
options=["battery_ok", "pv_only", "never"],
),
)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
Expand All @@ -40,13 +66,15 @@ async def async_setup_entry(
)

async_add_entities(
klass(
energysite, Scopes.ENERGY_CMDS in data.scopes
TeslemetryEnergySiteSelectEntity(
energysite, description, Scopes.ENERGY_CMDS in data.scopes
)
for klass in (TeslemetryOperationMode,TeslemetryGridMode)
for energysite in data.energysites
for description in ENERGY_INFO_DESCRIPTIONS
if description.key in energysite.info_coordinator.data
)


class TeslemetrySelectEntity(SelectEntity):
"""Base class for Teslemetry select entities."""

Expand Down Expand Up @@ -89,44 +117,23 @@ async def async_select_option(self, option: str) -> None:
await self.api.remote_seat_heater_request(SEAT_HEATERS[self.key], level)
self.set((self.key, level))

class TeslemetryOperationMode(TeslemetryEnergySiteInfoEntity, TeslemetrySelectEntity):
"""Select entity for energy site operation mode."""

_attr_options = [
"autonomous",
"self_consumption",
"backup"
]
class TeslemetryEnergySiteSelectEntity(
TeslemetryEnergyInfoEntity, TeslemetrySelectEntity
):
"""Select entity for energy sites."""

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

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
self.raise_for_scope()
await self.wake_up_if_asleep()
await self.api.operation(option)
await self.entity_description.func(self.api, option)
self.set((self.key, option))

class TeslemetryGridMode(TeslemetryEnergySiteInfoEntity, TeslemetrySelectEntity):
"""Select entity for energy site grid mode."""

_attr_options = [
"battery_ok",
"pv_only",
"never"
]

def __init__(self, vehicle, scoped: bool) -> None:
"""Initialize the grid mode select entity."""
super().__init__(vehicle, "customer_preferred_export_rule")
self.scoped = scoped

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
self.raise_for_scope()
await self.wake_up_if_asleep()
await self.api.grid_import_export(customer_preferred_export_rule=option)
self.set((self.key, option))
14 changes: 10 additions & 4 deletions custom_components/teslemetry/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,15 @@ class TeslemetrySensorEntityDescription(SensorEntityDescription):
),
)

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


Expand Down Expand Up @@ -392,6 +397,7 @@ async def async_setup_entry(
TeslemetryEnergyInfoSensorEntity(energysite, description)
for energysite in data.energysites
for description in ENERGY_INFO_DESCRIPTIONS
if description.key in energysite.info_coordinator.data
)


Expand Down

0 comments on commit 6029ef1

Please sign in to comment.