Base Class
All binary sensor entities inherit fromBinarySensorEntity, which is defined in homeassistant.components.binary_sensor.
Entity Description
Binary sensor entities can optionally useBinarySensorEntityDescription to define static metadata:
Required Properties
is_on
ReturnsTrue if the binary sensor is on, False if off, or None if unknown.
Optional Properties
device_class
Indicates the type of binary sensor. This affects the icon displayed in the UI and what the on/off states represent.BATTERY- On = low, Off = normalBATTERY_CHARGING- On = charging, Off = not chargingDOOR- On = open, Off = closedGARAGE_DOOR- On = open, Off = closedMOTION- On = motion detected, Off = no motionOCCUPANCY- On = occupied, Off = not occupiedOPENING- On = open, Off = closedPLUG- On = plugged in, Off = unpluggedPRESENCE- On = home, Off = awayPROBLEM- On = problem detected, Off = OKSMOKE- On = smoke detected, Off = clearWINDOW- On = open, Off = closed
State Property
Thestate property is final and automatically returns “on” or “off” based on is_on:
Example Implementation
Push vs. Poll
For binary sensors that receive push updates (like webhooks or event-based systems):Real-World Example
From the demo integration (homeassistant/components/demo/binary_sensor.py):
Entity Attributes
Binary sensors typically don’t have additional state attributes, but you can add them if needed:Important Notes
- Binary sensors should set
_attr_is_onrather than implementingstatedirectly - The
stateproperty is final and automatically convertsis_onto “on”/“off” - Set
_attr_should_poll = Falsefor sensors that receive push updates - Binary sensors with
EntityCategory.CONFIGcannot be added (will raiseHomeAssistantError) - Always set a
device_classto get the appropriate icon and on/off semantics