Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Platform for binary sensor integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from mypermobil import BATTERY_CHARGING
10 
11 from homeassistant import config_entries
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import DOMAIN
20 from .coordinator import MyPermobilCoordinator
21 from .entity import PermobilEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Describes Permobil binary sensor entity."""
27 
28  is_on_fn: Callable[[Any], bool]
29  available_fn: Callable[[Any], bool]
30 
31 
32 BINARY_SENSOR_DESCRIPTIONS: tuple[PermobilBinarySensorEntityDescription, ...] = (
34  is_on_fn=lambda data: data.battery[BATTERY_CHARGING[0]],
35  available_fn=lambda data: BATTERY_CHARGING[0] in data.battery,
36  key="is_charging",
37  translation_key="is_charging",
38  ),
39 )
40 
41 
43  hass: HomeAssistant,
44  config_entry: config_entries.ConfigEntry,
45  async_add_entities: AddEntitiesCallback,
46 ) -> None:
47  """Create and setup the binary sensor."""
48 
49  coordinator: MyPermobilCoordinator = hass.data[DOMAIN][config_entry.entry_id]
50 
52  PermobilbinarySensor(coordinator=coordinator, description=description)
53  for description in BINARY_SENSOR_DESCRIPTIONS
54  )
55 
56 
58  """Representation of a Binary Sensor."""
59 
60  entity_description: PermobilBinarySensorEntityDescription
61 
62  @property
63  def is_on(self) -> bool:
64  """Return True if the wheelchair is charging."""
65  return self.entity_descriptionentity_description.is_on_fn(self.coordinator.data)
66 
67  @property
68  def available(self) -> bool:
69  """Return True if the sensor has value."""
70  return super().available and self.entity_descriptionentity_description.available_fn(
71  self.coordinator.data
72  )
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)