Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for ESPHome binary sensors."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING
6 
7 from aioesphomeapi import BinarySensorInfo, BinarySensorState, EntityInfo
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12  BinarySensorEntityDescription,
13 )
14 from homeassistant.core import HomeAssistant, callback
15 from homeassistant.helpers import issue_registry as ir
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.util.enum import try_parse_enum
18 
19 from .const import DOMAIN
20 from .entity import EsphomeAssistEntity, EsphomeEntity, platform_async_setup_entry
21 from .entry_data import ESPHomeConfigEntry
22 
23 
25  hass: HomeAssistant,
26  entry: ESPHomeConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up ESPHome binary sensors based on a config entry."""
31  hass,
32  entry,
33  async_add_entities,
34  info_type=BinarySensorInfo,
35  entity_type=EsphomeBinarySensor,
36  state_type=BinarySensorState,
37  )
38 
39  entry_data = entry.runtime_data
40  assert entry_data.device_info is not None
41  if entry_data.device_info.voice_assistant_feature_flags_compat(
42  entry_data.api_version
43  ):
45 
46 
48  EsphomeEntity[BinarySensorInfo, BinarySensorState], BinarySensorEntity
49 ):
50  """A binary sensor implementation for ESPHome."""
51 
52  @property
53  def is_on(self) -> bool | None:
54  """Return true if the binary sensor is on."""
55  if self._static_info_static_info.is_status_binary_sensor:
56  # Status binary sensors indicated connected state.
57  # So in their case what's usually _availability_ is now state
58  return self._entry_data_entry_data.available
59  if not self._has_state_has_state or self._state_state.missing_state:
60  return None
61  return self._state_state.state
62 
63  @callback
64  def _on_static_info_update(self, static_info: EntityInfo) -> None:
65  """Set attrs from static info."""
66  super()._on_static_info_update(static_info)
67  self._attr_device_class_attr_device_class = try_parse_enum(
68  BinarySensorDeviceClass, self._static_info_static_info.device_class
69  )
70 
71  @property
72  def available(self) -> bool:
73  """Return True if entity is available."""
74  return self._static_info_static_info.is_status_binary_sensor or super().available
75 
76 
78  """A binary sensor implementation for ESPHome for use with assist_pipeline."""
79 
80  entity_description = BinarySensorEntityDescription(
81  entity_registry_enabled_default=False,
82  key="assist_in_progress",
83  translation_key="assist_in_progress",
84  )
85 
86  async def async_added_to_hass(self) -> None:
87  """Create issue."""
88  await super().async_added_to_hass()
89  if TYPE_CHECKING:
90  assert self.registry_entryregistry_entry is not None
91  ir.async_create_issue(
92  self.hasshass,
93  DOMAIN,
94  f"assist_in_progress_deprecated_{self.registry_entry.id}",
95  breaks_in_ha_version="2025.4",
96  data={
97  "entity_id": self.entity_identity_id,
98  "entity_uuid": self.registry_entryregistry_entry.id,
99  "integration_name": "ESPHome",
100  },
101  is_fixable=True,
102  severity=ir.IssueSeverity.WARNING,
103  translation_key="assist_in_progress_deprecated",
104  translation_placeholders={
105  "integration_name": "ESPHome",
106  },
107  )
108 
109  async def async_will_remove_from_hass(self) -> None:
110  """Remove issue."""
111  await super().async_will_remove_from_hass()
112  if TYPE_CHECKING:
113  assert self.registry_entryregistry_entry is not None
114  ir.async_delete_issue(
115  self.hasshass,
116  DOMAIN,
117  f"assist_in_progress_deprecated_{self.registry_entry.id}",
118  )
119 
120  @property
121  def is_on(self) -> bool | None:
122  """Return true if the binary sensor is on."""
123  return self._entry_data.assist_pipeline_state
None async_setup_entry(HomeAssistant hass, ESPHomeConfigEntry entry, AddEntitiesCallback async_add_entities)
None platform_async_setup_entry(HomeAssistant hass, ESPHomeConfigEntry entry, AddEntitiesCallback async_add_entities, *type[_InfoT] info_type, type[_EntityT] entity_type, type[_StateT] state_type)
Definition: entity.py:89