Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Netatmo binary sensors."""
2 
4  BinarySensorDeviceClass,
5  BinarySensorEntity,
6  BinarySensorEntityDescription,
7 )
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import NETATMO_CREATE_WEATHER_SENSOR
14 from .data_handler import NetatmoDevice
15 from .entity import NetatmoWeatherModuleEntity
16 
17 BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
19  key="reachable",
20  device_class=BinarySensorDeviceClass.CONNECTIVITY,
21  ),
22 )
23 
24 
26  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
27 ) -> None:
28  """Set up Netatmo binary sensors based on a config entry."""
29 
30  @callback
31  def _create_weather_binary_sensor_entity(netatmo_device: NetatmoDevice) -> None:
33  NetatmoWeatherBinarySensor(netatmo_device, description)
34  for description in BINARY_SENSOR_TYPES
35  if description.key in netatmo_device.device.features
36  )
37 
38  entry.async_on_unload(
40  hass, NETATMO_CREATE_WEATHER_SENSOR, _create_weather_binary_sensor_entity
41  )
42  )
43 
44 
46  """Implementation of a Netatmo binary sensor."""
47 
48  def __init__(
49  self, device: NetatmoDevice, description: BinarySensorEntityDescription
50  ) -> None:
51  """Initialize a Netatmo binary sensor."""
52  super().__init__(device)
53  self.entity_descriptionentity_description = description
54  self._attr_unique_id_attr_unique_id = f"{self.device.entity_id}-{description.key}"
55 
56  @callback
57  def async_update_callback(self) -> None:
58  """Update the entity's state."""
59  self._attr_is_on_attr_is_on = self.devicedevice.reachable
60  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, NetatmoDevice device, BinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103