ESPHome  2022.8.0
tuya_switch.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "tuya_switch.h"
3 
4 namespace esphome {
5 namespace tuya {
6 
7 static const char *const TAG = "tuya.switch";
8 
10  this->parent_->register_listener(this->switch_id_, [this](const TuyaDatapoint &datapoint) {
11  ESP_LOGV(TAG, "MCU reported switch %u is: %s", this->switch_id_, ONOFF(datapoint.value_bool));
12  this->publish_state(datapoint.value_bool);
13  });
14 }
15 
17  ESP_LOGV(TAG, "Setting switch %u: %s", this->switch_id_, ONOFF(state));
18  this->parent_->set_boolean_datapoint_value(this->switch_id_, state);
19  this->publish_state(state);
20 }
21 
23  LOG_SWITCH("", "Tuya Switch", this);
24  ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", this->switch_id_);
25 }
26 
27 } // namespace tuya
28 } // namespace esphome
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:621
void write_state(bool state) override
Definition: tuya_switch.cpp:16
void setup() override
Definition: tuya_switch.cpp:9
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition: tuya.cpp:493
Definition: a4988.cpp:4
void dump_config() override
Definition: tuya_switch.cpp:22
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