ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
am43_cover.cpp
Go to the documentation of this file.
1#include "am43_cover.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
6namespace esphome::am43 {
7
8ESPHOME_LOG_TAG(TAG, "am43_cover");
9
10using namespace esphome::cover;
11
13 LOG_COVER("", "AM43 Cover", this);
14 ESP_LOGCONFIG(TAG,
15 " Device Pin: %d\n"
16 " Invert Position: %d",
17 this->pin_, (int) this->invert_position_);
18}
19
20void Am43Component::setup() { this->position = COVER_OPEN; }
21
23 if (this->node_state == espbt::ClientState::ESTABLISHED && !this->logged_in_) {
24 ESP_LOGI(TAG, "[%s] Logging into AM43", LOG_STR_ARG(this->get_log_name()));
25 this->logged_in_ = this->write_packet_(Am43Encoder::get_send_pin_request(this->pin_), LOG_STR("set_pin"));
26 }
27}
28
30 auto traits = CoverTraits();
31 traits.set_supports_stop(true);
32 traits.set_supports_position(true);
33 traits.set_supports_tilt(false);
34 traits.set_is_assumed_state(false);
35 return traits;
36}
37
39 if (this->node_state != espbt::ClientState::ESTABLISHED) {
40 ESP_LOGW(TAG, "[%s] Cannot send cover control, not connected", LOG_STR_ARG(this->get_log_name()));
41 return;
42 }
43 if (call.get_stop())
44 this->write_packet_(Am43Encoder::get_stop_request(), LOG_STR("stop"));
45 auto opt_pos = call.get_position();
46 if (opt_pos.has_value()) {
47 auto pos = *opt_pos;
48
49 if (this->invert_position_)
50 pos = 1 - pos;
51 this->write_packet_(Am43Encoder::get_set_position_request(100 - (uint8_t) (pos * 100)), LOG_STR("set_position"));
52 }
53}
54
55void Am43Component::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
56 esp_ble_gattc_cb_param_t *param) {
57 switch (event) {
58 case ESP_GATTC_DISCONNECT_EVT: {
59 this->logged_in_ = false;
60 break;
61 }
62 case ESP_GATTC_SEARCH_CMPL_EVT: {
63 auto *chr = this->parent_->get_characteristic(AM43_SERVICE_UUID, AM43_CHARACTERISTIC_UUID);
64 if (chr == nullptr) {
65 if (this->parent_->get_characteristic(AM43_TUYA_SERVICE_UUID, AM43_TUYA_CHARACTERISTIC_UUID) != nullptr) {
66 ESP_LOGE(TAG, "[%s] Detected a Tuya AM43 which is not supported, sorry.", LOG_STR_ARG(this->get_log_name()));
67 } else {
68 ESP_LOGE(TAG, "[%s] No control service found at device, not an AM43..?", LOG_STR_ARG(this->get_log_name()));
69 }
70 break;
71 }
72 this->char_handle_ = chr->handle;
73
74 auto status = esp_ble_gattc_register_for_notify(this->parent_->get_gattc_if(), this->parent_->get_remote_bda(),
75 chr->handle);
76 if (status) {
77 ESP_LOGW(TAG, "[%s] esp_ble_gattc_register_for_notify failed, status=%d", LOG_STR_ARG(this->get_log_name()),
78 status);
79 }
80 break;
81 }
82 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
83 this->node_state = espbt::ClientState::ESTABLISHED;
84 break;
85 }
86 case ESP_GATTC_NOTIFY_EVT: {
87 if (param->notify.handle != this->char_handle_)
88 break;
89 Am43Decoder decoder;
90 decoder.decode(param->notify.value, param->notify.value_len);
91
92 if (decoder.has_position()) {
93 this->position = ((float) decoder.position_ / 100.0f);
94 if (!this->invert_position_)
95 this->position = 1 - this->position;
96 if (this->position > 0.97f)
97 this->position = 1.0f;
98 if (this->position < 0.02f)
99 this->position = 0.0f;
100 this->publish_state();
101 }
102
103 if (decoder.has_pin_response()) {
104 if (decoder.pin_ok_) {
105 ESP_LOGI(TAG, "[%s] AM43 pin accepted.", LOG_STR_ARG(this->get_log_name()));
106 this->write_packet_(Am43Encoder::get_position_request(), LOG_STR("get_position"));
107 } else {
108 ESP_LOGW(TAG, "[%s] AM43 pin rejected!", LOG_STR_ARG(this->get_log_name()));
109 }
110 }
111
112 if (decoder.has_set_position_response() && !decoder.set_position_ok_) {
113 ESP_LOGW(TAG, "[%s] Got nack after sending set_position. Bad pin?", LOG_STR_ARG(this->get_log_name()));
114 }
115
116 if (decoder.has_set_state_response() && !decoder.set_state_ok_) {
117 ESP_LOGW(TAG, "[%s] Got nack after sending set_state. Bad pin?", LOG_STR_ARG(this->get_log_name()));
118 }
119 break;
120 }
121 default:
122 break;
123 }
124}
125
126bool Am43Component::write_packet_(const Am43Packet &packet, const LogString *what) {
127 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) the IDF API takes a mutable buffer it only reads
128 auto status = esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(),
129 this->char_handle_, packet.length, const_cast<uint8_t *>(packet.data),
130 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
131 if (status) {
132 ESP_LOGW(TAG, "[%s] Error writing %s to device, error = %d", LOG_STR_ARG(this->get_log_name()), LOG_STR_ARG(what),
133 status);
134 return false;
135 }
136 return true;
137}
138
139} // namespace esphome::am43
140
141#endif
uint8_t status
Definition bl0942.h:8
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
bool write_packet_(const Am43Packet &packet, const LogString *what)
Writes a request to the control characteristic; logs and returns false on failure.
cover::CoverTraits get_traits() override
void control(const cover::CoverCall &call) override
Decodes one notification; construct it on the stack for each frame.
Definition am43_base.h:46
bool has_position() const
Definition am43_base.h:53
bool has_set_position_response() const
Definition am43_base.h:51
bool has_set_state_response() const
Definition am43_base.h:52
bool has_pin_response() const
Definition am43_base.h:54
void decode(const uint8_t *data, uint16_t length)
Definition am43_base.cpp:66
static Am43Packet get_set_position_request(uint8_t position)
Definition am43_base.cpp:49
static Am43Packet get_stop_request()
Definition am43_base.cpp:44
static Am43Packet get_send_pin_request(uint16_t pin)
Definition am43_base.cpp:27
static Am43Packet get_position_request()
Definition am43_base.cpp:22
bool get_stop() const
Definition cover.h:56
void publish_state(bool save=true)
Publish the current state of the cover.
Definition cover.cpp:158
float position
The position of the cover from 0.0 (fully closed) to 1.0 (fully open).
Definition cover.h:126
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
ESPHOME_LOG_TAG(TAG, "am43")
size_t size_t pos
Definition helpers.h:1333