Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Renault binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from renault_api.kamereon.enums import ChargeState, PlugState
8 from renault_api.kamereon.models import KamereonVehicleBatteryStatusData
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import StateType
18 
19 from . import RenaultConfigEntry
20 from .entity import RenaultDataEntity, RenaultDataEntityDescription
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  BinarySensorEntityDescription,
26  RenaultDataEntityDescription,
27 ):
28  """Class describing Renault binary sensor entities."""
29 
30  on_key: str
31  on_value: StateType | list[StateType]
32 
33 
35  hass: HomeAssistant,
36  config_entry: RenaultConfigEntry,
37  async_add_entities: AddEntitiesCallback,
38 ) -> None:
39  """Set up the Renault entities from config entry."""
40  entities: list[RenaultBinarySensor] = [
41  RenaultBinarySensor(vehicle, description)
42  for vehicle in config_entry.runtime_data.vehicles.values()
43  for description in BINARY_SENSOR_TYPES
44  if description.coordinator in vehicle.coordinators
45  ]
46  async_add_entities(entities)
47 
48 
50  RenaultDataEntity[KamereonVehicleBatteryStatusData], BinarySensorEntity
51 ):
52  """Mixin for binary sensor specific attributes."""
53 
54  entity_description: RenaultBinarySensorEntityDescription
55 
56  @property
57  def is_on(self) -> bool | None:
58  """Return true if the binary sensor is on."""
59  if (data := self._get_data_attr_get_data_attr(self.entity_descriptionentity_description.on_key)) is None:
60  return None
61 
62  if isinstance(self.entity_descriptionentity_description.on_value, list):
63  return data in self.entity_descriptionentity_description.on_value
64  return data == self.entity_descriptionentity_description.on_value
65 
66 
67 BINARY_SENSOR_TYPES: tuple[RenaultBinarySensorEntityDescription, ...] = tuple(
68  [
70  key="plugged_in",
71  coordinator="battery",
72  device_class=BinarySensorDeviceClass.PLUG,
73  on_key="plugStatus",
74  on_value=[
75  PlugState.PLUGGED.value,
76  PlugState.PLUGGED_WAITING_FOR_CHARGE.value,
77  ],
78  ),
80  key="charging",
81  coordinator="battery",
82  device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
83  on_key="chargingStatus",
84  on_value=ChargeState.CHARGE_IN_PROGRESS.value,
85  ),
87  key="hvac_status",
88  coordinator="hvac_status",
89  on_key="hvacStatus",
90  on_value="on",
91  translation_key="hvac_status",
92  ),
94  key="lock_status",
95  coordinator="lock_status",
96  # lock: on means open (unlocked), off means closed (locked)
97  device_class=BinarySensorDeviceClass.LOCK,
98  on_key="lockStatus",
99  on_value="unlocked",
100  ),
102  key="hatch_status",
103  coordinator="lock_status",
104  # On means open, Off means closed
105  device_class=BinarySensorDeviceClass.DOOR,
106  on_key="hatchStatus",
107  on_value="open",
108  translation_key="hatch_status",
109  ),
110  ]
111  + [
113  key=f"{door.replace(' ', '_').lower()}_door_status",
114  coordinator="lock_status",
115  # On means open, Off means closed
116  device_class=BinarySensorDeviceClass.DOOR,
117  on_key=f"doorStatus{door.replace(' ', '')}",
118  on_value="open",
119  translation_key=f"{door.lower().replace(' ', '_')}_door_status",
120  )
121  for door in ("Rear Left", "Rear Right", "Driver", "Passenger")
122  ],
123 )
None async_setup_entry(HomeAssistant hass, RenaultConfigEntry config_entry, AddEntitiesCallback async_add_entities)