2021-08-19 09:27:43 +02:00
|
|
|
"""Support for Renault binary sensors."""
|
2024-03-08 15:05:07 +01:00
|
|
|
|
2021-08-19 09:27:43 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2021-08-25 23:15:49 +02:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
2021-08-19 09:27:43 +02:00
|
|
|
from renault_api.kamereon.enums import ChargeState, PlugState
|
2021-08-25 23:15:49 +02:00
|
|
|
from renault_api.kamereon.models import KamereonVehicleBatteryStatusData
|
2021-08-19 09:27:43 +02:00
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-01 17:40:56 +01:00
|
|
|
BinarySensorDeviceClass,
|
2021-08-19 09:27:43 +02:00
|
|
|
BinarySensorEntity,
|
2021-08-25 23:15:49 +02:00
|
|
|
BinarySensorEntityDescription,
|
2021-08-19 09:27:43 +02:00
|
|
|
)
|
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-08-25 23:15:49 +02:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-08-19 09:27:43 +02:00
|
|
|
|
2024-04-30 17:39:03 +02:00
|
|
|
from . import RenaultConfigEntry
|
2023-04-04 14:38:52 +02:00
|
|
|
from .entity import RenaultDataEntity, RenaultDataEntityDescription
|
2021-08-19 09:27:43 +02:00
|
|
|
|
|
|
|
|
|
2024-03-04 15:46:14 +01:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
2021-08-25 23:15:49 +02:00
|
|
|
class RenaultBinarySensorEntityDescription(
|
|
|
|
|
BinarySensorEntityDescription,
|
2021-11-15 15:50:43 +01:00
|
|
|
RenaultDataEntityDescription,
|
2021-08-25 23:15:49 +02:00
|
|
|
):
|
|
|
|
|
"""Class describing Renault binary sensor entities."""
|
|
|
|
|
|
2024-03-04 15:46:14 +01:00
|
|
|
on_key: str
|
|
|
|
|
on_value: StateType
|
2022-02-08 22:02:45 +01:00
|
|
|
|
2021-08-25 23:15:49 +02:00
|
|
|
|
2021-08-19 09:27:43 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
|
hass: HomeAssistant,
|
2024-04-30 17:39:03 +02:00
|
|
|
config_entry: RenaultConfigEntry,
|
2021-08-19 09:27:43 +02:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Set up the Renault entities from config entry."""
|
2021-08-25 23:15:49 +02:00
|
|
|
entities: list[RenaultBinarySensor] = [
|
2021-08-27 12:12:09 +02:00
|
|
|
RenaultBinarySensor(vehicle, description)
|
2024-04-30 17:39:03 +02:00
|
|
|
for vehicle in config_entry.runtime_data.vehicles.values()
|
2021-08-25 23:15:49 +02:00
|
|
|
for description in BINARY_SENSOR_TYPES
|
|
|
|
|
if description.coordinator in vehicle.coordinators
|
|
|
|
|
]
|
2021-08-19 09:27:43 +02:00
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
2021-08-27 12:12:09 +02:00
|
|
|
class RenaultBinarySensor(
|
|
|
|
|
RenaultDataEntity[KamereonVehicleBatteryStatusData], BinarySensorEntity
|
|
|
|
|
):
|
2021-08-25 23:15:49 +02:00
|
|
|
"""Mixin for binary sensor specific attributes."""
|
2021-08-19 09:27:43 +02:00
|
|
|
|
2021-08-25 23:15:49 +02:00
|
|
|
entity_description: RenaultBinarySensorEntityDescription
|
2021-08-19 09:27:43 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
|
"""Return true if the binary sensor is on."""
|
2022-02-07 14:06:40 +01:00
|
|
|
if (data := self._get_data_attr(self.entity_description.on_key)) is None:
|
|
|
|
|
return None
|
|
|
|
|
return data == self.entity_description.on_value
|
2021-08-19 09:27:43 +02:00
|
|
|
|
|
|
|
|
|
2022-02-21 20:07:43 +01:00
|
|
|
BINARY_SENSOR_TYPES: tuple[RenaultBinarySensorEntityDescription, ...] = tuple(
|
|
|
|
|
[
|
|
|
|
|
RenaultBinarySensorEntityDescription(
|
|
|
|
|
key="plugged_in",
|
|
|
|
|
coordinator="battery",
|
|
|
|
|
device_class=BinarySensorDeviceClass.PLUG,
|
|
|
|
|
on_key="plugStatus",
|
|
|
|
|
on_value=PlugState.PLUGGED.value,
|
|
|
|
|
),
|
|
|
|
|
RenaultBinarySensorEntityDescription(
|
|
|
|
|
key="charging",
|
|
|
|
|
coordinator="battery",
|
|
|
|
|
device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
|
|
|
|
|
on_key="chargingStatus",
|
|
|
|
|
on_value=ChargeState.CHARGE_IN_PROGRESS.value,
|
|
|
|
|
),
|
|
|
|
|
RenaultBinarySensorEntityDescription(
|
|
|
|
|
key="hvac_status",
|
|
|
|
|
coordinator="hvac_status",
|
|
|
|
|
on_key="hvacStatus",
|
2024-06-03 15:53:23 +02:00
|
|
|
on_value=2,
|
2023-04-03 18:20:11 +02:00
|
|
|
translation_key="hvac_status",
|
2022-02-21 20:07:43 +01:00
|
|
|
),
|
|
|
|
|
RenaultBinarySensorEntityDescription(
|
|
|
|
|
key="lock_status",
|
|
|
|
|
coordinator="lock_status",
|
|
|
|
|
# lock: on means open (unlocked), off means closed (locked)
|
|
|
|
|
device_class=BinarySensorDeviceClass.LOCK,
|
|
|
|
|
on_key="lockStatus",
|
|
|
|
|
on_value="unlocked",
|
|
|
|
|
),
|
|
|
|
|
RenaultBinarySensorEntityDescription(
|
|
|
|
|
key="hatch_status",
|
|
|
|
|
coordinator="lock_status",
|
|
|
|
|
# On means open, Off means closed
|
|
|
|
|
device_class=BinarySensorDeviceClass.DOOR,
|
|
|
|
|
on_key="hatchStatus",
|
|
|
|
|
on_value="open",
|
2023-04-03 18:20:11 +02:00
|
|
|
translation_key="hatch_status",
|
2022-02-21 20:07:43 +01:00
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
+ [
|
|
|
|
|
RenaultBinarySensorEntityDescription(
|
|
|
|
|
key=f"{door.replace(' ','_').lower()}_door_status",
|
|
|
|
|
coordinator="lock_status",
|
|
|
|
|
# On means open, Off means closed
|
|
|
|
|
device_class=BinarySensorDeviceClass.DOOR,
|
|
|
|
|
on_key=f"doorStatus{door.replace(' ','')}",
|
|
|
|
|
on_value="open",
|
2023-04-03 18:20:11 +02:00
|
|
|
translation_key=f"{door.lower().replace(' ','_')}_door_status",
|
2022-02-21 20:07:43 +01:00
|
|
|
)
|
|
|
|
|
for door in ("Rear Left", "Rear Right", "Driver", "Passenger")
|
|
|
|
|
],
|
2021-08-25 23:15:49 +02:00
|
|
|
)
|