Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary Sensor platform for Tessie integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from itertools import chain
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12  BinarySensorEntityDescription,
13 )
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import TessieConfigEntry
19 from .const import TessieState
20 from .entity import TessieEnergyEntity, TessieEntity
21 from .models import TessieEnergyData, TessieVehicleData
22 
23 PARALLEL_UPDATES = 0
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Describes Tessie binary sensor entity."""
29 
30  is_on: Callable[..., bool] = lambda x: x
31 
32 
33 VEHICLE_DESCRIPTIONS: tuple[TessieBinarySensorEntityDescription, ...] = (
35  key="state",
36  device_class=BinarySensorDeviceClass.CONNECTIVITY,
37  is_on=lambda x: x == TessieState.ONLINE,
38  ),
40  key="climate_state_battery_heater",
41  device_class=BinarySensorDeviceClass.HEAT,
42  entity_category=EntityCategory.DIAGNOSTIC,
43  ),
45  key="charge_state_charging_state",
46  device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
47  is_on=lambda x: x == "Charging",
48  entity_registry_enabled_default=False,
49  ),
51  key="charge_state_preconditioning_enabled",
52  entity_category=EntityCategory.DIAGNOSTIC,
53  ),
55  key="charge_state_scheduled_charging_pending",
56  entity_category=EntityCategory.DIAGNOSTIC,
57  ),
59  key="charge_state_trip_charging",
60  entity_category=EntityCategory.DIAGNOSTIC,
61  ),
63  key="charge_state_conn_charge_cable",
64  is_on=lambda x: x != "<invalid>",
65  entity_category=EntityCategory.DIAGNOSTIC,
66  device_class=BinarySensorDeviceClass.CONNECTIVITY,
67  ),
69  key="climate_state_auto_seat_climate_left",
70  entity_category=EntityCategory.DIAGNOSTIC,
71  ),
73  key="climate_state_auto_seat_climate_right",
74  entity_category=EntityCategory.DIAGNOSTIC,
75  ),
77  key="climate_state_auto_steering_wheel_heat",
78  entity_category=EntityCategory.DIAGNOSTIC,
79  ),
81  key="climate_state_cabin_overheat_protection",
82  device_class=BinarySensorDeviceClass.RUNNING,
83  is_on=lambda x: x == "On",
84  entity_category=EntityCategory.DIAGNOSTIC,
85  ),
87  key="climate_state_cabin_overheat_protection_actively_cooling",
88  device_class=BinarySensorDeviceClass.HEAT,
89  entity_category=EntityCategory.DIAGNOSTIC,
90  ),
92  key="vehicle_state_dashcam_state",
93  device_class=BinarySensorDeviceClass.RUNNING,
94  is_on=lambda x: x == "Recording",
95  entity_category=EntityCategory.DIAGNOSTIC,
96  ),
98  key="vehicle_state_is_user_present",
99  device_class=BinarySensorDeviceClass.OCCUPANCY,
100  ),
102  key="vehicle_state_tpms_soft_warning_fl",
103  device_class=BinarySensorDeviceClass.PROBLEM,
104  entity_category=EntityCategory.DIAGNOSTIC,
105  ),
107  key="vehicle_state_tpms_soft_warning_fr",
108  device_class=BinarySensorDeviceClass.PROBLEM,
109  entity_category=EntityCategory.DIAGNOSTIC,
110  ),
112  key="vehicle_state_tpms_soft_warning_rl",
113  device_class=BinarySensorDeviceClass.PROBLEM,
114  entity_category=EntityCategory.DIAGNOSTIC,
115  ),
117  key="vehicle_state_tpms_soft_warning_rr",
118  device_class=BinarySensorDeviceClass.PROBLEM,
119  entity_category=EntityCategory.DIAGNOSTIC,
120  ),
122  key="vehicle_state_fd_window",
123  device_class=BinarySensorDeviceClass.WINDOW,
124  entity_category=EntityCategory.DIAGNOSTIC,
125  ),
127  key="vehicle_state_fp_window",
128  device_class=BinarySensorDeviceClass.WINDOW,
129  entity_category=EntityCategory.DIAGNOSTIC,
130  ),
132  key="vehicle_state_rd_window",
133  device_class=BinarySensorDeviceClass.WINDOW,
134  entity_category=EntityCategory.DIAGNOSTIC,
135  ),
137  key="vehicle_state_rp_window",
138  device_class=BinarySensorDeviceClass.WINDOW,
139  entity_category=EntityCategory.DIAGNOSTIC,
140  ),
142  key="vehicle_state_df",
143  device_class=BinarySensorDeviceClass.DOOR,
144  entity_category=EntityCategory.DIAGNOSTIC,
145  ),
147  key="vehicle_state_dr",
148  device_class=BinarySensorDeviceClass.DOOR,
149  entity_category=EntityCategory.DIAGNOSTIC,
150  ),
152  key="vehicle_state_pf",
153  device_class=BinarySensorDeviceClass.DOOR,
154  entity_category=EntityCategory.DIAGNOSTIC,
155  ),
157  key="vehicle_state_pr",
158  device_class=BinarySensorDeviceClass.DOOR,
159  entity_category=EntityCategory.DIAGNOSTIC,
160  ),
161 )
162 
163 ENERGY_LIVE_DESCRIPTIONS: tuple[BinarySensorEntityDescription, ...] = (
164  BinarySensorEntityDescription(key="backup_capable"),
165  BinarySensorEntityDescription(key="grid_services_active"),
166  BinarySensorEntityDescription(key="storm_mode_active"),
167 )
168 
169 
170 ENERGY_INFO_DESCRIPTIONS: tuple[BinarySensorEntityDescription, ...] = (
172  key="components_grid_services_enabled",
173  ),
174 )
175 
176 
178  hass: HomeAssistant,
179  entry: TessieConfigEntry,
180  async_add_entities: AddEntitiesCallback,
181 ) -> None:
182  """Set up the Tessie binary sensor platform from a config entry."""
184  chain(
185  (
186  TessieBinarySensorEntity(vehicle, description)
187  for vehicle in entry.runtime_data.vehicles
188  for description in VEHICLE_DESCRIPTIONS
189  ),
190  (
191  TessieEnergyLiveBinarySensorEntity(energy, description)
192  for energy in entry.runtime_data.energysites
193  for description in ENERGY_LIVE_DESCRIPTIONS
194  ),
195  (
196  TessieEnergyInfoBinarySensorEntity(vehicle, description)
197  for vehicle in entry.runtime_data.energysites
198  for description in ENERGY_INFO_DESCRIPTIONS
199  ),
200  )
201  )
202 
203 
205  """Base class for Tessie binary sensors."""
206 
207  entity_description: TessieBinarySensorEntityDescription
208 
209  def __init__(
210  self,
211  vehicle: TessieVehicleData,
212  description: TessieBinarySensorEntityDescription,
213  ) -> None:
214  """Initialize the sensor."""
215  super().__init__(vehicle, description.key)
216  self.entity_descriptionentity_description = description
217 
218  @property
219  def is_on(self) -> bool:
220  """Return the state of the binary sensor."""
221  return self.entity_descriptionentity_description.is_on(self._value_value_value)
222 
223 
225  """Base class for Tessie energy live binary sensors."""
226 
227  entity_description: BinarySensorEntityDescription
228 
229  def __init__(
230  self,
231  data: TessieEnergyData,
232  description: BinarySensorEntityDescription,
233  ) -> None:
234  """Initialize the binary sensor."""
235  self.entity_descriptionentity_description = description
236  super().__init__(data, data.live_coordinator, description.key)
237 
238  def _async_update_attrs(self) -> None:
239  """Update the attributes of the binary sensor."""
240  self._attr_is_on_attr_is_on = self._value_value
241 
242 
244  """Base class for Tessie energy info binary sensors."""
245 
246  entity_description: BinarySensorEntityDescription
247 
248  def __init__(
249  self,
250  data: TessieEnergyData,
251  description: BinarySensorEntityDescription,
252  ) -> None:
253  """Initialize the binary sensor."""
254  self.entity_descriptionentity_description = description
255  super().__init__(data, data.info_coordinator, description.key)
256 
257  def _async_update_attrs(self) -> None:
258  """Update the attributes of the binary sensor."""
259  self._attr_is_on_attr_is_on = self._value_value
None __init__(self, TessieVehicleData vehicle, TessieBinarySensorEntityDescription description)
None __init__(self, TessieEnergyData data, BinarySensorEntityDescription description)
None __init__(self, TessieEnergyData data, BinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, TessieConfigEntry entry, AddEntitiesCallback async_add_entities)