mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve Renault plug status binary sensor (#143931)
improve binary plug sensor
This commit is contained in:
parent
4b6fa12925
commit
69c387a360
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from renault_api.kamereon.enums import ChargeState, PlugState
|
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
|
# Coordinator is used to centralize the data updates
|
||||||
PARALLEL_UPDATES = 0
|
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)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class RenaultBinarySensorEntityDescription(
|
class RenaultBinarySensorEntityDescription(
|
||||||
@ -30,8 +41,9 @@ class RenaultBinarySensorEntityDescription(
|
|||||||
):
|
):
|
||||||
"""Class describing Renault binary sensor entities."""
|
"""Class describing Renault binary sensor entities."""
|
||||||
|
|
||||||
on_key: str
|
on_key: str | None = None
|
||||||
on_value: StateType
|
on_value: StateType | None = None
|
||||||
|
value_lambda: Callable[[RenaultBinarySensor], bool | None] | None = None
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -59,20 +71,40 @@ class RenaultBinarySensor(
|
|||||||
@property
|
@property
|
||||||
def is_on(self) -> bool | None:
|
def is_on(self) -> bool | None:
|
||||||
"""Return true if the binary sensor is on."""
|
"""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:
|
if (data := self._get_data_attr(self.entity_description.on_key)) is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return data == self.entity_description.on_value
|
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(
|
BINARY_SENSOR_TYPES: tuple[RenaultBinarySensorEntityDescription, ...] = tuple(
|
||||||
[
|
[
|
||||||
RenaultBinarySensorEntityDescription(
|
RenaultBinarySensorEntityDescription(
|
||||||
key="plugged_in",
|
key="plugged_in",
|
||||||
coordinator="battery",
|
coordinator="battery",
|
||||||
device_class=BinarySensorDeviceClass.PLUG,
|
device_class=BinarySensorDeviceClass.PLUG,
|
||||||
on_key="plugStatus",
|
value_lambda=_plugged_in_value_lambda,
|
||||||
on_value=PlugState.PLUGGED.value,
|
|
||||||
),
|
),
|
||||||
RenaultBinarySensorEntityDescription(
|
RenaultBinarySensorEntityDescription(
|
||||||
key="charging",
|
key="charging",
|
||||||
|
@ -1289,7 +1289,7 @@
|
|||||||
'last_changed': <ANY>,
|
'last_changed': <ANY>,
|
||||||
'last_reported': <ANY>,
|
'last_reported': <ANY>,
|
||||||
'last_updated': <ANY>,
|
'last_updated': <ANY>,
|
||||||
'state': 'off',
|
'state': 'on',
|
||||||
})
|
})
|
||||||
# ---
|
# ---
|
||||||
# name: test_binary_sensors[twingo_3_electric][binary_sensor.reg_number_rear_left_door-entry]
|
# name: test_binary_sensors[twingo_3_electric][binary_sensor.reg_number_rear_left_door-entry]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user