Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Provides a binary sensor for Home Connect."""
2 
3 from dataclasses import dataclass
4 import logging
5 
6 from homeassistant.components.automation import automations_with_entity
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10  BinarySensorEntityDescription,
11 )
12 from homeassistant.components.script import scripts_with_entity
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers import entity_registry as er
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17  IssueSeverity,
18  async_create_issue,
19  async_delete_issue,
20 )
21 
22 from . import HomeConnectConfigEntry
23 from .api import HomeConnectDevice
24 from .const import (
25  ATTR_VALUE,
26  BSH_DOOR_STATE,
27  BSH_DOOR_STATE_CLOSED,
28  BSH_DOOR_STATE_LOCKED,
29  BSH_DOOR_STATE_OPEN,
30  BSH_REMOTE_CONTROL_ACTIVATION_STATE,
31  BSH_REMOTE_START_ALLOWANCE_STATE,
32  DOMAIN,
33  REFRIGERATION_STATUS_DOOR_CHILLER,
34  REFRIGERATION_STATUS_DOOR_CLOSED,
35  REFRIGERATION_STATUS_DOOR_FREEZER,
36  REFRIGERATION_STATUS_DOOR_OPEN,
37  REFRIGERATION_STATUS_DOOR_REFRIGERATOR,
38 )
39 from .entity import HomeConnectEntity
40 
41 _LOGGER = logging.getLogger(__name__)
42 REFRIGERATION_DOOR_BOOLEAN_MAP = {
43  REFRIGERATION_STATUS_DOOR_CLOSED: False,
44  REFRIGERATION_STATUS_DOOR_OPEN: True,
45 }
46 
47 
48 @dataclass(frozen=True, kw_only=True)
50  """Entity Description class for binary sensors."""
51 
52  boolean_map: dict[str, bool] | None = None
53 
54 
55 BINARY_SENSORS = (
57  key=BSH_REMOTE_CONTROL_ACTIVATION_STATE,
58  translation_key="remote_control",
59  ),
61  key=BSH_REMOTE_START_ALLOWANCE_STATE,
62  translation_key="remote_start",
63  ),
65  key="BSH.Common.Status.LocalControlActive",
66  translation_key="local_control",
67  ),
69  key="BSH.Common.Status.BatteryChargingState",
70  device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
71  boolean_map={
72  "BSH.Common.EnumType.BatteryChargingState.Charging": True,
73  "BSH.Common.EnumType.BatteryChargingState.Discharging": False,
74  },
75  translation_key="battery_charging_state",
76  ),
78  key="BSH.Common.Status.ChargingConnection",
79  device_class=BinarySensorDeviceClass.PLUG,
80  boolean_map={
81  "BSH.Common.EnumType.ChargingConnection.Connected": True,
82  "BSH.Common.EnumType.ChargingConnection.Disconnected": False,
83  },
84  translation_key="charging_connection",
85  ),
87  key="ConsumerProducts.CleaningRobot.Status.DustBoxInserted",
88  translation_key="dust_box_inserted",
89  ),
91  key="ConsumerProducts.CleaningRobot.Status.Lifted",
92  translation_key="lifted",
93  ),
95  key="ConsumerProducts.CleaningRobot.Status.Lost",
96  translation_key="lost",
97  ),
99  key=REFRIGERATION_STATUS_DOOR_CHILLER,
100  boolean_map=REFRIGERATION_DOOR_BOOLEAN_MAP,
101  device_class=BinarySensorDeviceClass.DOOR,
102  translation_key="chiller_door",
103  ),
105  key=REFRIGERATION_STATUS_DOOR_FREEZER,
106  boolean_map=REFRIGERATION_DOOR_BOOLEAN_MAP,
107  device_class=BinarySensorDeviceClass.DOOR,
108  translation_key="freezer_door",
109  ),
111  key=REFRIGERATION_STATUS_DOOR_REFRIGERATOR,
112  boolean_map=REFRIGERATION_DOOR_BOOLEAN_MAP,
113  device_class=BinarySensorDeviceClass.DOOR,
114  translation_key="refrigerator_door",
115  ),
116 )
117 
118 
120  hass: HomeAssistant,
121  entry: HomeConnectConfigEntry,
122  async_add_entities: AddEntitiesCallback,
123 ) -> None:
124  """Set up the Home Connect binary sensor."""
125 
126  def get_entities() -> list[BinarySensorEntity]:
127  entities: list[BinarySensorEntity] = []
128  for device in entry.runtime_data.devices:
129  entities.extend(
130  HomeConnectBinarySensor(device, description)
131  for description in BINARY_SENSORS
132  if description.key in device.appliance.status
133  )
134  if BSH_DOOR_STATE in device.appliance.status:
135  entities.append(HomeConnectDoorBinarySensor(device))
136  return entities
137 
138  async_add_entities(await hass.async_add_executor_job(get_entities), True)
139 
140 
142  """Binary sensor for Home Connect."""
143 
144  entity_description: HomeConnectBinarySensorEntityDescription
145 
146  @property
147  def available(self) -> bool:
148  """Return true if the binary sensor is available."""
149  return self._attr_is_on_attr_is_on is not None
150 
151  async def async_update(self) -> None:
152  """Update the binary sensor's status."""
153  if not self.devicedevice.appliance.status or not (
154  status := self.devicedevice.appliance.status.get(self.bsh_keybsh_key, {}).get(ATTR_VALUE)
155  ):
156  self._attr_is_on_attr_is_on = None
157  return
158  if self.entity_descriptionentity_description.boolean_map:
159  self._attr_is_on_attr_is_on = self.entity_descriptionentity_description.boolean_map.get(status)
160  elif status not in [True, False]:
161  self._attr_is_on_attr_is_on = None
162  else:
163  self._attr_is_on_attr_is_on = status
164  _LOGGER.debug("Updated, new state: %s", self._attr_is_on_attr_is_on)
165 
166 
168  """Binary sensor for Home Connect Generic Door."""
169 
170  _attr_has_entity_name = False
171 
172  def __init__(
173  self,
174  device: HomeConnectDevice,
175  ) -> None:
176  """Initialize the entity."""
177  super().__init__(
178  device,
180  key=BSH_DOOR_STATE,
181  device_class=BinarySensorDeviceClass.DOOR,
182  boolean_map={
183  BSH_DOOR_STATE_CLOSED: False,
184  BSH_DOOR_STATE_LOCKED: False,
185  BSH_DOOR_STATE_OPEN: True,
186  },
187  ),
188  )
189  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{device.appliance.haId}-Door"
190  self._attr_name_attr_name = f"{device.appliance.name} Door"
191 
192  async def async_added_to_hass(self) -> None:
193  """Call when entity is added to hass."""
194  await super().async_added_to_hass()
195  automations = automations_with_entity(self.hasshass, self.entity_identity_id)
196  scripts = scripts_with_entity(self.hasshass, self.entity_identity_id)
197  items = automations + scripts
198  if not items:
199  return
200 
201  entity_reg: er.EntityRegistry = er.async_get(self.hasshass)
202  entity_automations = [
203  automation_entity
204  for automation_id in automations
205  if (automation_entity := entity_reg.async_get(automation_id))
206  ]
207  entity_scripts = [
208  script_entity
209  for script_id in scripts
210  if (script_entity := entity_reg.async_get(script_id))
211  ]
212 
213  items_list = [
214  f"- [{item.original_name}](/config/automation/edit/{item.unique_id})"
215  for item in entity_automations
216  ] + [
217  f"- [{item.original_name}](/config/script/edit/{item.unique_id})"
218  for item in entity_scripts
219  ]
220 
222  self.hasshass,
223  DOMAIN,
224  f"deprecated_binary_common_door_sensor_{self.entity_id}",
225  breaks_in_ha_version="2025.5.0",
226  is_fixable=False,
227  severity=IssueSeverity.WARNING,
228  translation_key="deprecated_binary_common_door_sensor",
229  translation_placeholders={
230  "entity": self.entity_identity_id,
231  "items": "\n".join(items_list),
232  },
233  )
234 
235  async def async_will_remove_from_hass(self) -> None:
236  """Call when entity will be removed from hass."""
238  self.hasshass, DOMAIN, f"deprecated_binary_common_door_sensor_{self.entity_id}"
239  )
list[str] automations_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:191
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, HomeConnectConfigEntry entry, AddEntitiesCallback async_add_entities)
list[OneWireBinarySensor] get_entities(OneWireHub onewire_hub)
None async_create_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:69
None async_delete_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:85
list[str] scripts_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:126