ESPHome  2022.8.0
demo_switch.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
6 
7 namespace esphome {
8 namespace demo {
9 
10 class DemoSwitch : public switch_::Switch, public Component {
11  public:
12  void setup() override {
13  bool initial = random_float() < 0.5;
14  this->publish_state(initial);
15  }
16 
17  protected:
18  void write_state(bool state) override { this->publish_state(state); }
19 };
20 
21 } // namespace demo
22 } // namespace esphome
Base class for all switches.
Definition: switch.h:33
void write_state(bool state) override
Definition: demo_switch.h:18
void setup() override
Definition: demo_switch.h:12
Definition: a4988.cpp:4
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:31
bool state
The current reported state of the binary sensor.
Definition: switch.h:48
float random_float()
Return a random float between 0 and 1.
Definition: helpers.cpp:83