The switch_ namespace contains all switch helpers.

API Reference


class Switch : public Component, public Nameable

Base class for all switches.

A switch is basically just a combination of a binary sensor (for reporting switch values) and a write_state method that writes a state to the hardware.

Subclassed by remote::RemoteTransmitter, switch_::RestartSwitch, switch_::ShutdownSwitch, switch_::SimpleSwitch, switch_::TemplateSwitch, switch_::UARTSwitch

Public Functions

Switch(const std::string &name)
float get_setup_priority() const

priority of setup().

higher -> executed earlier

Defaults to 0.

The setup priority of this component

void setup_()
void publish_state(bool state)

Publish a state to the front-end from the back-end.

The input value is inverted if applicable. Then the internal value member is set and finally the callbacks are called.

  • state:

void set_inverted(bool inverted)
void write_state(bool state)

This method is called by the front-end components to set the state.

The internal logic will then invert it if applicable and call the turn_on or turn_off abstract method.

Note that this will not trigger sending the state. The overriden turn_on and turn_off methods should do this manually.

  • state: The binary state to write.

std::string icon()

Override this to set the Home Assistant icon for this switch.

Return “” to disable this feature.

The icon of this switch, for example “mdi:fan”.

void set_icon(const std::string &icon)

Set the icon for this switch. “” for no icon.

std::string get_icon()

Get the icon for this switch. Using icon() if not manually set.

template <typename T>
ToggleAction<T> *make_toggle_action()
template <typename T>
TurnOffAction<T> *make_turn_off_action()
template <typename T>
TurnOnAction<T> *make_turn_on_action()
bool optimistic()

Return whether this switch is optimistic - i.e.

if both the ON/OFF actions should be displayed in Home Assistant because the real state is unknown.

Defaults to false.

void add_on_state_callback(std::function<void(bool)> &&callback)

Set callback for state changes.

  • callback: The void(bool) callback.

bool do_restore_state()

Subclasses can override this to prevent the switch from automatically restoring the state.

Public Members

bool value = {false}

The current published state of the switch.

Inversion is already applied on this value.

Protected Functions

virtual void turn_on() = 0

Turn this switch on. When creating a switch, you should implement this (inversion will already be applied).

virtual void turn_off() = 0

Turn this switch off. When creating a switch, you should implement this (inversion will already be applied).

Protected Attributes

optional<std::string> icon_ = {}

The icon shown here. Not set means use default from switch. Empty means no icon.

CallbackManager<void(bool)> state_callback_ = {}
bool inverted_ = {false}
bool first_value_ = {true}


class SimpleSwitch : public switch_::Switch

A simple switch that exposes a binary output as a switch.

Public Functions

SimpleSwitch(const std::string &name, output::BinaryOutput *output)

Construct this SimpleSwitch with the provided BinaryOutput.

Protected Functions

void turn_on()

Turn this SimpleSwitch on.

void turn_off()

Turn this SimpleSwitch off.

Protected Attributes

output::BinaryOutput *output_


class MQTTSwitchComponent : public mqtt::MQTTComponent

MQTT representation of switches.

Overrides MQTTBinarySensorComponent with a callback that can write values to hardware.

Public Functions

MQTTSwitchComponent(switch_::Switch *switch_)
void setup()

Where the component’s initialization should happen.

Analogous to Arduino’s setup(). This method is guaranteed to only be called once. Defaults to doing noting.

void send_discovery(JsonBuffer &buffer, JsonObject &root, mqtt::SendDiscoveryConfig &config)

Send discovery info the Home Assistant, override this.

void send_initial_state()
bool is_internal()
void publish_state(bool state)

Protected Functions

std::string friendly_name() const

Get the friendly name of this MQTT component.

std::string component_type() const

“switch” component type.

Protected Attributes

Switch *switch_