Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for 1-Wire binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 import os
7 
9  BinarySensorDeviceClass,
10  BinarySensorEntity,
11  BinarySensorEntityDescription,
12 )
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import OneWireConfigEntry
18 from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
19 from .entity import OneWireEntity, OneWireEntityDescription
20 from .onewirehub import OneWireHub
21 
22 
23 @dataclass(frozen=True)
25  OneWireEntityDescription, BinarySensorEntityDescription
26 ):
27  """Class describing OneWire binary sensor entities."""
28 
29 
30 DEVICE_BINARY_SENSORS: dict[str, tuple[OneWireBinarySensorEntityDescription, ...]] = {
31  "12": tuple(
33  key=f"sensed.{device_key}",
34  entity_registry_enabled_default=False,
35  read_mode=READ_MODE_BOOL,
36  translation_key="sensed_id",
37  translation_placeholders={"id": str(device_key)},
38  )
39  for device_key in DEVICE_KEYS_A_B
40  ),
41  "29": tuple(
43  key=f"sensed.{device_key}",
44  entity_registry_enabled_default=False,
45  read_mode=READ_MODE_BOOL,
46  translation_key="sensed_id",
47  translation_placeholders={"id": str(device_key)},
48  )
49  for device_key in DEVICE_KEYS_0_7
50  ),
51  "3A": tuple(
53  key=f"sensed.{device_key}",
54  entity_registry_enabled_default=False,
55  read_mode=READ_MODE_BOOL,
56  translation_key="sensed_id",
57  translation_placeholders={"id": str(device_key)},
58  )
59  for device_key in DEVICE_KEYS_A_B
60  ),
61  "EF": (), # "HobbyBoard": special
62 }
63 
64 # EF sensors are usually hobbyboards specialized sensors.
65 HOBBYBOARD_EF: dict[str, tuple[OneWireBinarySensorEntityDescription, ...]] = {
66  "HB_HUB": tuple(
68  key=f"hub/short.{device_key}",
69  entity_registry_enabled_default=False,
70  read_mode=READ_MODE_BOOL,
71  entity_category=EntityCategory.DIAGNOSTIC,
72  device_class=BinarySensorDeviceClass.PROBLEM,
73  translation_key="hub_short_id",
74  translation_placeholders={"id": str(device_key)},
75  )
76  for device_key in DEVICE_KEYS_0_3
77  ),
78 }
79 
80 
82  device_sub_type: str,
83 ) -> dict[str, tuple[OneWireBinarySensorEntityDescription, ...]]:
84  """Return the proper info array for the device type."""
85  if "HobbyBoard" in device_sub_type:
86  return HOBBYBOARD_EF
87  return DEVICE_BINARY_SENSORS
88 
89 
91  hass: HomeAssistant,
92  config_entry: OneWireConfigEntry,
93  async_add_entities: AddEntitiesCallback,
94 ) -> None:
95  """Set up 1-Wire platform."""
96  entities = await hass.async_add_executor_job(
97  get_entities, config_entry.runtime_data
98  )
99  async_add_entities(entities, True)
100 
101 
102 def get_entities(onewire_hub: OneWireHub) -> list[OneWireBinarySensor]:
103  """Get a list of entities."""
104  if not onewire_hub.devices:
105  return []
106 
107  entities: list[OneWireBinarySensor] = []
108  for device in onewire_hub.devices:
109  family = device.family
110  device_id = device.id
111  device_type = device.type
112  device_info = device.device_info
113  device_sub_type = "std"
114  if device_type and "EF" in family:
115  device_sub_type = "HobbyBoard"
116  family = device_type
117 
118  if family not in get_sensor_types(device_sub_type):
119  continue
120  for description in get_sensor_types(device_sub_type)[family]:
121  device_file = os.path.join(os.path.split(device.path)[0], description.key)
122  entities.append(
124  description=description,
125  device_id=device_id,
126  device_file=device_file,
127  device_info=device_info,
128  owproxy=onewire_hub.owproxy,
129  )
130  )
131 
132  return entities
133 
134 
136  """Implementation of a 1-Wire binary sensor."""
137 
138  entity_description: OneWireBinarySensorEntityDescription
139 
140  @property
141  def is_on(self) -> bool | None:
142  """Return true if sensor is on."""
143  if self._state_state is None:
144  return None
145  return bool(self._state_state)
None async_setup_entry(HomeAssistant hass, OneWireConfigEntry config_entry, AddEntitiesCallback async_add_entities)
list[OneWireBinarySensor] get_entities(OneWireHub onewire_hub)
dict[str, tuple[OneWireBinarySensorEntityDescription,...]] get_sensor_types(str device_sub_type)