The switch_ namespace contains all switch helpers.

API Reference


class switch_::Switch

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.

Inherits from Component, Nameable

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

Public Functions

switch_::SwitchSwitch(const std::string &name)
float switch_::Switchget_setup_priority() const

priority of setup().

higher -> executed earlier

Defaults to 0.

The setup priority of this component

void switch_::Switchsetup_()
void switch_::Switchpublish_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 switch_::Switchset_inverted(bool inverted)
void switch_::Switchwrite_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 switch_::Switchicon()

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 switch_::Switchset_icon(const std::string &icon)

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

std::string switch_::Switchget_icon()

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

template <typename T>
ToggleAction<T> *switch_::Switchmake_toggle_action()
template <typename T>
TurnOffAction<T> *switch_::Switchmake_turn_off_action()
template <typename T>
TurnOnAction<T> *switch_::Switchmake_turn_on_action()
bool switch_::Switchoptimistic()

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 switch_::Switchadd_on_state_callback(std::function<void(bool)> &&callback)

Set callback for state changes.

  • callback: The void(bool) callback.

Public Members

bool switch_::Switchvalue = {false}

The current published state of the switch.

Inversion is already applied on this value.

Protected Functions

virtual void switch_::Switchturn_on() = 0

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

virtual void switch_::Switchturn_off() = 0

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

Protected Attributes

optional<std::string> switch_::Switchicon_ = {}

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

CallbackManager<void(bool)> switch_::Switchstate_callback_ = {}
bool switch_::Switchinverted_ = {false}
bool switch_::Switchfirst_value_ = {true}


class switch_::SimpleSwitch

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

Inherits from switch_::Switch

Public Functions

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

Construct this SimpleSwitch with the provided BinaryOutput.

Protected Functions

void switch_::SimpleSwitchturn_on()

Turn this SimpleSwitch on.

void switch_::SimpleSwitchturn_off()

Turn this SimpleSwitch off.

Protected Attributes

output::BinaryOutput *switch_::SimpleSwitchoutput_


class switch_::MQTTSwitchComponent

MQTT representation of switches.

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

Inherits from mqtt::MQTTComponent

Public Functions

switch_::MQTTSwitchComponentMQTTSwitchComponent(switch_::Switch *switch_)
void switch_::MQTTSwitchComponentsetup()

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 switch_::MQTTSwitchComponentsend_discovery(JsonBuffer &buffer, JsonObject &root, mqtt::SendDiscoveryConfig &config)

Send discovery info the Home Assistant, override this.

void switch_::MQTTSwitchComponentsend_initial_state()
bool switch_::MQTTSwitchComponentis_internal()
void switch_::MQTTSwitchComponentpublish_state(bool state)

Protected Functions

std::string switch_::MQTTSwitchComponentfriendly_name() const

Get the friendly name of this MQTT component.

std::string switch_::MQTTSwitchComponentcomponent_type() const

“switch” component type.

Protected Attributes

Switch *switch_::MQTTSwitchComponentswitch_