Improve Renault plug status binary sensor (#143931)

improve binary plug sensor
This commit is contained in:
tmenguy 2025-04-30 01:03:01 -07:00 committed by GitHub
parent 4b6fa12925
commit 69c387a360
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 5 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from renault_api.kamereon.enums import ChargeState, PlugState
@ -22,6 +23,16 @@ from .entity import RenaultDataEntity, RenaultDataEntityDescription
# Coordinator is used to centralize the data updates
PARALLEL_UPDATES = 0
_PLUG_FROM_CHARGE_STATUS: set[ChargeState] = {
ChargeState.CHARGE_IN_PROGRESS,
ChargeState.WAITING_FOR_CURRENT_CHARGE,
ChargeState.CHARGE_ENDED,
ChargeState.V2G_CHARGING_NORMAL,
ChargeState.V2G_CHARGING_WAITING,
ChargeState.V2G_DISCHARGING,
ChargeState.WAITING_FOR_A_PLANNED_CHARGE,
}
@dataclass(frozen=True, kw_only=True)
class RenaultBinarySensorEntityDescription(
@ -30,8 +41,9 @@ class RenaultBinarySensorEntityDescription(
):
"""Class describing Renault binary sensor entities."""
on_key: str
on_value: StateType
on_key: str | None = None
on_value: StateType | None = None
value_lambda: Callable[[RenaultBinarySensor], bool | None] | None = None
async def async_setup_entry(
@ -59,20 +71,40 @@ class RenaultBinarySensor(
@property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
if self.entity_description.value_lambda is not None:
return self.entity_description.value_lambda(self)
if self.entity_description.on_key is None:
raise NotImplementedError("Either value_lambda or on_key must be set")
if (data := self._get_data_attr(self.entity_description.on_key)) is None:
return None
return data == self.entity_description.on_value
def _plugged_in_value_lambda(self: RenaultBinarySensor) -> bool | None:
"""Return true if the vehicle is plugged in."""
data = self.coordinator.data
plug_status = data.get_plug_status() if data else None
if plug_status is not None:
return plug_status == PlugState.PLUGGED
charging_status = data.get_charging_status() if data else None
if charging_status is not None and charging_status in _PLUG_FROM_CHARGE_STATUS:
return True
return None
BINARY_SENSOR_TYPES: tuple[RenaultBinarySensorEntityDescription, ...] = tuple(
[
RenaultBinarySensorEntityDescription(
key="plugged_in",
coordinator="battery",
device_class=BinarySensorDeviceClass.PLUG,
on_key="plugStatus",
on_value=PlugState.PLUGGED.value,
value_lambda=_plugged_in_value_lambda,
),
RenaultBinarySensorEntityDescription(
key="charging",

View File

@ -1289,7 +1289,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
'state': 'on',
})
# ---
# name: test_binary_sensors[twingo_3_electric][binary_sensor.reg_number_rear_left_door-entry]