1 """Component to interface with binary sensors."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from enum
import StrEnum
7 from functools
import partial
9 from typing
import Literal, final
11 from propcache
import cached_property
12 import voluptuous
as vol
20 DeprecatedConstantEnum,
21 all_with_deprecated_constants,
22 check_if_deprecated_constant,
23 dir_with_deprecated_constants,
30 _LOGGER = logging.getLogger(__name__)
32 DOMAIN =
"binary_sensor"
33 DATA_COMPONENT: HassKey[EntityComponent[BinarySensorEntity]] =
HassKey(DOMAIN)
34 ENTITY_ID_FORMAT = DOMAIN +
".{}"
35 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
36 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
41 """Device class for binary sensors."""
47 BATTERY_CHARGING =
"battery_charging"
50 CO =
"carbon_monoxide"
56 CONNECTIVITY =
"connectivity"
62 GARAGE_DOOR =
"garage_door"
86 OCCUPANCY =
"occupancy"
122 VIBRATION =
"vibration"
128 DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(BinarySensorDeviceClass))
132 DEVICE_CLASSES = [cls.value
for cls
in BinarySensorDeviceClass]
134 BinarySensorDeviceClass.BATTERY,
"2025.1"
137 BinarySensorDeviceClass.BATTERY_CHARGING,
"2025.1"
140 BinarySensorDeviceClass.CO,
"2025.1"
143 BinarySensorDeviceClass.COLD,
"2025.1"
146 BinarySensorDeviceClass.CONNECTIVITY,
"2025.1"
149 BinarySensorDeviceClass.DOOR,
"2025.1"
152 BinarySensorDeviceClass.GARAGE_DOOR,
"2025.1"
155 BinarySensorDeviceClass.GAS,
"2025.1"
158 BinarySensorDeviceClass.HEAT,
"2025.1"
161 BinarySensorDeviceClass.LIGHT,
"2025.1"
164 BinarySensorDeviceClass.LOCK,
"2025.1"
167 BinarySensorDeviceClass.MOISTURE,
"2025.1"
170 BinarySensorDeviceClass.MOTION,
"2025.1"
173 BinarySensorDeviceClass.MOVING,
"2025.1"
176 BinarySensorDeviceClass.OCCUPANCY,
"2025.1"
179 BinarySensorDeviceClass.OPENING,
"2025.1"
182 BinarySensorDeviceClass.PLUG,
"2025.1"
185 BinarySensorDeviceClass.POWER,
"2025.1"
188 BinarySensorDeviceClass.PRESENCE,
"2025.1"
191 BinarySensorDeviceClass.PROBLEM,
"2025.1"
194 BinarySensorDeviceClass.RUNNING,
"2025.1"
197 BinarySensorDeviceClass.SAFETY,
"2025.1"
200 BinarySensorDeviceClass.SMOKE,
"2025.1"
203 BinarySensorDeviceClass.SOUND,
"2025.1"
206 BinarySensorDeviceClass.TAMPER,
"2025.1"
209 BinarySensorDeviceClass.UPDATE,
"2025.1"
212 BinarySensorDeviceClass.VIBRATION,
"2025.1"
215 BinarySensorDeviceClass.WINDOW,
"2025.1"
221 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
222 """Track states and offer events for binary sensors."""
223 component = hass.data[DATA_COMPONENT] = EntityComponent[BinarySensorEntity](
224 logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL
227 await component.async_setup(config)
232 """Set up a config entry."""
237 """Unload a config entry."""
242 """A class that describes binary sensor entities."""
244 device_class: BinarySensorDeviceClass |
None =
None
247 CACHED_PROPERTIES_WITH_ATTR_ = {
254 """Represent a binary sensor."""
256 entity_description: BinarySensorEntityDescription
257 _attr_device_class: BinarySensorDeviceClass |
None
258 _attr_is_on: bool |
None =
None
259 _attr_state:
None =
None
262 """Call when the binary sensor entity is added to hass."""
266 f
"Entity {self.entity_id} cannot be added as the entity category is set to config"
270 """Return True if an unnamed entity should be named by its device class.
272 For binary sensors this is True if the entity has a device class.
277 def device_class(self) -> BinarySensorDeviceClass | None:
278 """Return the class of this entity."""
279 if hasattr(self,
"_attr_device_class"):
280 return self._attr_device_class
281 if hasattr(self,
"entity_description"):
282 return self.entity_description.device_class
286 def is_on(self) -> bool | None:
287 """Return true if the binary sensor is on."""
288 return self._attr_is_on
292 def state(self) -> Literal["on", "off"] | None:
293 """Return the state of the binary sensor."""
294 if (is_on := self.is_on)
is None:
296 return STATE_ON
if is_on
else STATE_OFF
300 __getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
302 dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
None async_internal_added_to_hass(self)
str|None device_class(self)
EntityCategory|None entity_category(self)
bool _default_to_device_class_name(self)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool is_on(HomeAssistant hass, str entity_id)
list[str] all_with_deprecated_constants(dict[str, Any] module_globals)