HASwitch class¶
-
class HASwitch : public HABaseDeviceType¶
HASwitch allows to display on/off switch in the HA panel and receive commands on your device.
Note
You can find more information about this entity in the Home Assistant documentation: https://www.home-assistant.io/integrations/switch.mqtt/
Public Functions
-
HASwitch(const char *uniqueId)¶
- Parameters
uniqueId – The unique ID of the sensor. It needs to be unique in a scope of your device.
-
bool setState(const bool state, const bool force = false)¶
Changes state of the switch and publishes MQTT message. Please note that if a new value is the same as previous one, the MQTT message won’t be published.
- Parameters
state – New state of the switch.
force – Forces to update state without comparing it to previous known state.
- Returns
Returns
true
if MQTT message has been published successfully.
-
inline bool turnOn()¶
Alias for
setState(true)
.
-
inline bool turnOff()¶
Alias for
setState(false)
.
-
inline void setCurrentState(const bool state)¶
Sets current state of the switch without publishing it to Home Assistant. This method may be useful if you want to change state before connection with MQTT broker is acquired.
- Parameters
state – New state of the switch.
-
inline bool getCurrentState() const¶
Returns last known state of the switch. By default it’s
false
.
-
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/switch/#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.
-
inline void setRetain(const bool retain)¶
Sets retain flag for the switch command. If set to
true
the command produced by Home Assistant will be retained.- Parameters
retain –
-
inline void setOptimistic(const bool optimistic)¶
Sets optimistic flag for the switch state. In this mode the switch state doesn’t need to be reported back to the HA panel when a command is received. By default the optimistic mode is disabled.
- Parameters
optimistic – The optimistic mode (
true
- enabled,false
- disabled).
-
inline void onCommand(void (*callback)(bool state, HASwitch *sender))¶
Registers callback that will be called each time the on/off command from HA is received. Please note that it’s not possible to register multiple callbacks for the same switch.
Note
In non-optimistic mode, the state must be reported back to HA using the HASwitch::setState method.
- Parameters
callback –
Protected Functions
-
virtual void buildSerializer() override¶
-
virtual void onMqttConnected() override¶
-
virtual void onMqttMessage(const char *topic, const uint8_t *payload, const uint16_t length) override¶
Private Functions
-
bool publishState(const 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 button. It can be nullptr.
-
bool _retain¶
The retain flag for the HA commands.
-
bool _optimistic¶
The optimistic mode of the switch (
true
- enabled,false
- disabled).
-
bool _currentState¶
The current state of the switch. By default it’s
false
.
-
HASwitch(const char *uniqueId)¶