Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Platform for the opengarage.io binary sensor component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import cast
7 
9  BinarySensorEntity,
10  BinarySensorEntityDescription,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN
17 from .coordinator import OpenGarageDataUpdateCoordinator
18 from .entity import OpenGarageEntity
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
23 SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
25  key="vehicle",
26  translation_key="vehicle",
27  ),
28 )
29 
30 
32  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
33 ) -> None:
34  """Set up the OpenGarage binary sensors."""
35  open_garage_data_coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
36  entry.entry_id
37  ]
40  open_garage_data_coordinator,
41  cast(str, entry.unique_id),
42  description,
43  )
44  for description in SENSOR_TYPES
45  )
46 
47 
49  """Representation of a OpenGarage binary sensor."""
50 
51  def __init__(
52  self,
53  coordinator: OpenGarageDataUpdateCoordinator,
54  device_id: str,
55  description: BinarySensorEntityDescription,
56  ) -> None:
57  """Initialize the entity."""
58  self._available_available = False
59  super().__init__(coordinator, device_id, description)
60 
61  @property
62  def available(self) -> bool:
63  """Return True if entity is available."""
64  return super().available and self._available_available
65 
66  @callback
67  def _update_attr(self) -> None:
68  """Handle updated data from the coordinator."""
69  state = self.coordinator.data.get(self.entity_descriptionentity_description.key)
70  if state == 1:
71  self._attr_is_on_attr_is_on = True
72  self._available_available = True
73  elif state == 0:
74  self._attr_is_on_attr_is_on = False
75  self._available_available = True
76  else:
77  self._available_available = False
None __init__(self, OpenGarageDataUpdateCoordinator coordinator, str device_id, BinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)