Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Tasmota binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from datetime import datetime
7 from typing import Any
8 
9 from hatasmota import switch as tasmota_switch
10 from hatasmota.entity import TasmotaEntity as HATasmotaEntity
11 from hatasmota.models import DiscoveryHashType
12 
13 from homeassistant.components import binary_sensor
14 from homeassistant.components.binary_sensor import BinarySensorEntity
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.dispatcher import async_dispatcher_connect
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 import homeassistant.helpers.event as evt
20 
21 from .const import DATA_REMOVE_DISCOVER_COMPONENT
22 from .discovery import TASMOTA_DISCOVERY_ENTITY_NEW
23 from .entity import TasmotaAvailability, TasmotaDiscoveryUpdate
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up Tasmota binary sensor dynamically through discovery."""
32 
33  @callback
34  def async_discover(
35  tasmota_entity: HATasmotaEntity, discovery_hash: DiscoveryHashType
36  ) -> None:
37  """Discover and add a Tasmota binary sensor."""
39  [
41  tasmota_entity=tasmota_entity, discovery_hash=discovery_hash
42  )
43  ]
44  )
45 
46  hass.data[DATA_REMOVE_DISCOVER_COMPONENT.format(binary_sensor.DOMAIN)] = (
48  hass,
49  TASMOTA_DISCOVERY_ENTITY_NEW.format(binary_sensor.DOMAIN),
50  async_discover,
51  )
52  )
53 
54 
56  TasmotaAvailability,
57  TasmotaDiscoveryUpdate,
58  BinarySensorEntity,
59 ):
60  """Representation a Tasmota binary sensor."""
61 
62  _delay_listener: Callable | None = None
63  _on_off_state: bool | None = None
64  _tasmota_entity: tasmota_switch.TasmotaSwitch
65 
66  def __init__(self, **kwds: Any) -> None:
67  """Initialize the Tasmota binary sensor."""
68  super().__init__(
69  **kwds,
70  )
71  if self._tasmota_entity_tasmota_entity.off_delay is not None:
72  self._attr_force_update_attr_force_update = True
73 
74  async def async_added_to_hass(self) -> None:
75  """Subscribe to MQTT events."""
76  self._tasmota_entity_tasmota_entity.set_on_state_callback(self.on_off_state_updatedon_off_state_updated)
77  await super().async_added_to_hass()
78 
79  @callback
80  def off_delay_listener(self, now: datetime) -> None:
81  """Switch device off after a delay."""
82  self._delay_listener_delay_listener = None
83  self._on_off_state_on_off_state = False
84  self.async_write_ha_stateasync_write_ha_state()
85 
86  @callback
87  def on_off_state_updated(self, state: bool, **kwargs: Any) -> None:
88  """Handle state updates."""
89  self._on_off_state_on_off_state = state
90 
91  if self._delay_listener_delay_listener is not None:
92  self._delay_listener_delay_listener()
93  self._delay_listener_delay_listener = None
94 
95  off_delay = self._tasmota_entity_tasmota_entity.off_delay
96  if self._on_off_state_on_off_state and off_delay is not None:
97  self._delay_listener_delay_listener = evt.async_call_later(
98  self.hasshass, off_delay, self.off_delay_listeneroff_delay_listener
99  )
100 
101  self.async_write_ha_stateasync_write_ha_state()
102 
103  @property
104  def is_on(self) -> bool | None:
105  """Return true if the binary sensor is on."""
106  return self._on_off_state_on_off_state
None async_discover(DiscoveryInfo discovery_info)
Definition: sensor.py:217
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