Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensor platform support for wiffi devices."""
2 
3 from homeassistant.components.binary_sensor import BinarySensorEntity
4 from homeassistant.config_entries import ConfigEntry
5 from homeassistant.core import HomeAssistant, callback
6 from homeassistant.helpers.dispatcher import async_dispatcher_connect
7 from homeassistant.helpers.entity_platform import AddEntitiesCallback
8 
9 from .const import CREATE_ENTITY_SIGNAL
10 from .entity import WiffiEntity
11 
12 
14  hass: HomeAssistant,
15  config_entry: ConfigEntry,
16  async_add_entities: AddEntitiesCallback,
17 ) -> None:
18  """Set up platform for a new integration.
19 
20  Called by the HA framework after async_forward_entry_setups has been called
21  during initialization of a new integration (= wiffi).
22  """
23 
24  @callback
25  def _create_entity(device, metric):
26  """Create platform specific entities."""
27  entities = []
28 
29  if metric.is_bool:
30  entities.append(BoolEntity(device, metric, config_entry.options))
31 
32  async_add_entities(entities)
33 
34  async_dispatcher_connect(hass, CREATE_ENTITY_SIGNAL, _create_entity)
35 
36 
38  """Entity for wiffi metrics which have a boolean value."""
39 
40  def __init__(self, device, metric, options):
41  """Initialize the entity."""
42  super().__init__(device, metric, options)
43  self._attr_is_on_attr_is_on = metric.value
44  self.reset_expiration_datereset_expiration_date()
45 
46  @property
47  def available(self):
48  """Return true if value is valid."""
49  return self._attr_is_on_attr_is_on is not None
50 
51  @callback
52  def _update_value_callback(self, device, metric):
53  """Update the value of the entity.
54 
55  Called if a new message has been received from the wiffi device.
56  """
57  self.reset_expiration_datereset_expiration_date()
58  self._attr_is_on_attr_is_on = metric.value
59  self.async_write_ha_stateasync_write_ha_state()
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