Fix charge behavior in Tessie ()

This commit is contained in:
Brett Adams 2024-06-21 19:30:57 +10:00 committed by GitHub
parent 64cef6e082
commit fde7ddfa71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 14 deletions
homeassistant/components/tessie
tests/components/tessie/fixtures

View file

@ -46,7 +46,7 @@ class TessieEntity(CoordinatorEntity[TessieStateUpdateCoordinator]):
@property @property
def _value(self) -> Any: def _value(self) -> Any:
"""Return value from coordinator data.""" """Return value from coordinator data."""
return self.coordinator.data[self.key] return self.coordinator.data.get(self.key)
def get(self, key: str | None = None, default: Any | None = None) -> Any: def get(self, key: str | None = None, default: Any | None = None) -> Any:
"""Return a specific value from coordinator data.""" """Return a specific value from coordinator data."""

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from itertools import chain
from typing import Any from typing import Any
from tessie_api import ( from tessie_api import (
@ -41,11 +42,6 @@ class TessieSwitchEntityDescription(SwitchEntityDescription):
DESCRIPTIONS: tuple[TessieSwitchEntityDescription, ...] = ( DESCRIPTIONS: tuple[TessieSwitchEntityDescription, ...] = (
TessieSwitchEntityDescription(
key="charge_state_charge_enable_request",
on_func=lambda: start_charging,
off_func=lambda: stop_charging,
),
TessieSwitchEntityDescription( TessieSwitchEntityDescription(
key="climate_state_defrost_mode", key="climate_state_defrost_mode",
on_func=lambda: start_defrost, on_func=lambda: start_defrost,
@ -68,6 +64,12 @@ DESCRIPTIONS: tuple[TessieSwitchEntityDescription, ...] = (
), ),
) )
CHARGE_DESCRIPTION: TessieSwitchEntityDescription = TessieSwitchEntityDescription(
key="charge_state_charge_enable_request",
on_func=lambda: start_charging,
off_func=lambda: stop_charging,
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
@ -75,15 +77,20 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the Tessie Switch platform from a config entry.""" """Set up the Tessie Switch platform from a config entry."""
data = entry.runtime_data
async_add_entities( async_add_entities(
[ chain(
TessieSwitchEntity(vehicle, description) (
for vehicle in data.vehicles TessieSwitchEntity(vehicle, description)
for description in DESCRIPTIONS for vehicle in entry.runtime_data.vehicles
if description.key in vehicle.data for description in DESCRIPTIONS
] if description.key in vehicle.data
),
(
TessieChargeSwitchEntity(vehicle, CHARGE_DESCRIPTION)
for vehicle in entry.runtime_data.vehicles
),
)
) )
@ -116,3 +123,15 @@ class TessieSwitchEntity(TessieEntity, SwitchEntity):
"""Turn off the Switch.""" """Turn off the Switch."""
await self.run(self.entity_description.off_func()) await self.run(self.entity_description.off_func())
self.set((self.entity_description.key, False)) self.set((self.entity_description.key, False))
class TessieChargeSwitchEntity(TessieSwitchEntity):
"""Entity class for Tessie charge switch."""
@property
def is_on(self) -> bool:
"""Return the state of the Switch."""
if (charge := self.get("charge_state_user_charge_enable_request")) is not None:
return charge
return self._value

View file

@ -68,7 +68,7 @@
"timestamp": 1701139037461, "timestamp": 1701139037461,
"trip_charging": false, "trip_charging": false,
"usable_battery_level": 75, "usable_battery_level": 75,
"user_charge_enable_request": null "user_charge_enable_request": true
}, },
"climate_state": { "climate_state": {
"allow_cabin_overheat_protection": true, "allow_cabin_overheat_protection": true,