Template Switch

See Application::make_template_switch().

API Reference

class TemplateSwitch : public switch_::Switch, public Component

Public Functions

TemplateSwitch(const std::string &name)
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 nothing.

void dump_config()
void set_state_lambda(std::function<optional<bool>()> &&f)
void set_restore_state(bool restore_state)
Trigger<NoArg> *get_turn_on_trigger() const
Trigger<NoArg> *get_turn_off_trigger() const
void set_optimistic(bool optimistic)
void loop()

This method will be called repeatedly.

Analogous to Arduino’s loop(). setup() is guaranteed to be called before this. Defaults to doing nothing.

float get_setup_priority() const

priority of setup().

higher -> executed earlier

Defaults to 0.

The setup priority of this component

Protected Functions

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 write_state(bool state)

Write the given state to hardware.

You should implement this abstract method if you want to create your own switch.

In the implementation of this method, you should also call publish_state to acknowledge that the state was written to the hardware.

  • state: The state to write. Inversion is already applied if user specified it.

Protected Attributes

optional<std::function<optional<bool>)> > switch_::TemplateSwitch::f_
bool optimistic_ = {false}
optional<bool> last_state_ = {}
Trigger<NoArg> *turn_on_trigger_
Trigger<NoArg> *turn_off_trigger_
bool restore_state_ = {true}