HABinarySensor class¶
-
class HABinarySensor : public HABaseDeviceType¶
HABinarySensor represents a binary sensor that allows publishing on/off state to the Home Assistant panel.
Note
You can find more information about this entity in the Home Assistant documentation: https://www.home-assistant.io/integrations/binary_sensor.mqtt/
Public Functions
-
HABinarySensor(const char *uniqueId)¶
- Parameters
uniqueId – The unique ID of the button. It needs to be unique in a scope of your device.
-
bool setState(const bool state, const bool force = false)¶
Changes state of the sensor and publish MQTT message. Please note that if a new value is the same as the previous one the MQTT message won’t be published.
- Parameters
state – New state of the sensor (
true
- on,false
- off).force – Forces to update the state without comparing it to a previous known state.
- Returns
Returns
true
if the MQTT message has been published successfully.
-
void setExpireAfter(uint16_t expireAfter)¶
Sets the number of seconds after the sensor’s state expires, if it’s not updated. By default the sensors state never expires.
- Parameters
expireAfter – The number of seconds.
-
inline void setCurrentState(const bool state)¶
Sets the current state of the sensor without publishing it to Home Assistant. This method may be useful if you want to change the state before the connection with the MQTT broker is acquired.
- Parameters
state – New state of the sensor.
-
inline bool getCurrentState() const¶
Returns the last known state of the sensor.
-
inline void setDeviceClass(const char *deviceClass)¶
Sets class of the device. You can find list of available values here: https://www.home-assistant.io/integrations/binary_sensor/#device-class
- Parameters
deviceClass – The class name.
-
inline void setIcon(const char *icon)¶
Sets icon of the sensor. Any icon from MaterialDesignIcons.com (for example:
mdi:home
).- Parameters
icon – The icon name.
Protected Functions
-
virtual void buildSerializer() override¶
-
virtual void onMqttConnected() override¶
Private Functions
-
bool publishState(bool state)¶
Publishes the MQTT message with the given state.
- Parameters
state – The state to publish.
- Returns
Returns
true
if the MQTT message has been published successfully.
Private Members
-
const char *_class¶
The device class. It can be nullptr.
-
const char *_icon¶
The icon of the sensor. It can be nullptr.
-
HANumeric _expireAfter¶
It defines the number of seconds after the sensor’s state expires, if it’s not updated. By default the sensors state never expires.
-
bool _currentState¶
Current state of the sensor. By default it’s false.
-
HABinarySensor(const char *uniqueId)¶