ESPHome  2024.12.2
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:697
void write_state(bool state) override
Definition: tuya_switch.cpp:16
const char *const TAG
Definition: spi.cpp:8
void setup() override
Definition: tuya_switch.cpp:9
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition: tuya.cpp:569
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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:47
bool state
The current reported state of the binary sensor.
Definition: switch.h:53