Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for VOC."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 
7 import voluptuous as vol
8 from volvooncall.dashboard import Instrument
9 
11  DEVICE_CLASSES_SCHEMA,
12  BinarySensorEntity,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant, callback
16 from homeassistant.helpers.dispatcher import async_dispatcher_connect
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import DOMAIN, VOLVO_DISCOVERY_NEW
20 from .coordinator import VolvoUpdateCoordinator
21 from .entity import VolvoEntity
22 
23 
25  hass: HomeAssistant,
26  config_entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Configure binary_sensors from a config entry created in the integrations UI."""
30  coordinator: VolvoUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
31  volvo_data = coordinator.volvo_data
32 
33  @callback
34  def async_discover_device(instruments: list[Instrument]) -> None:
35  """Discover and add a discovered Volvo On Call binary sensor."""
38  coordinator,
39  instrument.vehicle.vin,
40  instrument.component,
41  instrument.attr,
42  instrument.slug_attr,
43  )
44  for instrument in instruments
45  if instrument.component == "binary_sensor"
46  )
47 
48  async_discover_device([*volvo_data.instruments])
49 
50  config_entry.async_on_unload(
51  async_dispatcher_connect(hass, VOLVO_DISCOVERY_NEW, async_discover_device)
52  )
53 
54 
56  """Representation of a Volvo sensor."""
57 
58  def __init__(
59  self,
60  coordinator: VolvoUpdateCoordinator,
61  vin: str,
62  component: str,
63  attribute: str,
64  slug_attr: str,
65  ) -> None:
66  """Initialize the sensor."""
67  super().__init__(vin, component, attribute, slug_attr, coordinator)
68 
69  with suppress(vol.Invalid):
70  self._attr_device_class_attr_device_class = DEVICE_CLASSES_SCHEMA(
71  self.instrumentinstrument.device_class
72  )
73 
74  @property
75  def is_on(self) -> bool | None:
76  """Fetch from update coordinator."""
77  if self.instrumentinstrument.attr == "is_locked":
78  return not self.instrumentinstrument.is_on
79  return self.instrumentinstrument.is_on
None __init__(self, VolvoUpdateCoordinator coordinator, str vin, str component, str attribute, str slug_attr)
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103