Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Sensors flow for Withings."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10 )
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from . import WithingsConfigEntry
17 from .const import DOMAIN
18 from .coordinator import WithingsBedPresenceDataUpdateCoordinator
19 from .entity import WithingsEntity
20 
21 
23  hass: HomeAssistant,
24  entry: WithingsConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up the sensor config entry."""
28  coordinator = entry.runtime_data.bed_presence_coordinator
29 
30  ent_reg = er.async_get(hass)
31 
32  callback: Callable[[], None] | None = None
33 
34  def _async_add_bed_presence_entity() -> None:
35  """Add bed presence entity."""
37  if callback:
38  callback()
39 
40  if ent_reg.async_get_entity_id(
41  Platform.BINARY_SENSOR, DOMAIN, f"withings_{entry.unique_id}_in_bed"
42  ):
43  _async_add_bed_presence_entity()
44  else:
45  callback = coordinator.async_add_listener(_async_add_bed_presence_entity)
46 
47 
49  """Implementation of a Withings sensor."""
50 
51  _attr_translation_key = "in_bed"
52  _attr_device_class = BinarySensorDeviceClass.OCCUPANCY
53  coordinator: WithingsBedPresenceDataUpdateCoordinator
54 
55  def __init__(self, coordinator: WithingsBedPresenceDataUpdateCoordinator) -> None:
56  """Initialize binary sensor."""
57  super().__init__(coordinator, "in_bed")
58 
59  @property
60  def is_on(self) -> bool | None:
61  """Return true if the binary sensor is on."""
62  return self.coordinator.in_bed
None __init__(self, WithingsBedPresenceDataUpdateCoordinator coordinator)
None async_setup_entry(HomeAssistant hass, WithingsConfigEntry entry, AddEntitiesCallback async_add_entities)