ESPHome  2022.11.3
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 
6 namespace esphome {
7 namespace am43 {
8 
9 static const char *const TAG = "am43_cover";
10 
11 using namespace esphome::cover;
12 
14  LOG_COVER("", "AM43 Cover", this);
15  ESP_LOGCONFIG(TAG, " Device Pin: %d", this->pin_);
16  ESP_LOGCONFIG(TAG, " Invert Position: %d", (int) this->invert_position_);
17 }
18 
20  this->position = COVER_OPEN;
21  this->encoder_ = make_unique<Am43Encoder>();
22  this->decoder_ = make_unique<Am43Decoder>();
23  this->logged_in_ = false;
24 }
25 
27  if (this->node_state == espbt::ClientState::ESTABLISHED && !this->logged_in_) {
28  auto *packet = this->encoder_->get_send_pin_request(this->pin_);
29  auto status =
30  esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
31  packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
32  ESP_LOGI(TAG, "[%s] Logging into AM43", this->get_name().c_str());
33  if (status) {
34  ESP_LOGW(TAG, "[%s] Error writing set_pin to device, error = %d", this->get_name().c_str(), status);
35  } else {
36  this->logged_in_ = true;
37  }
38  }
39 }
40 
42  auto traits = CoverTraits();
43  traits.set_supports_position(true);
44  traits.set_supports_tilt(false);
45  traits.set_is_assumed_state(false);
46  return traits;
47 }
48 
49 void Am43Component::control(const CoverCall &call) {
50  if (this->node_state != espbt::ClientState::ESTABLISHED) {
51  ESP_LOGW(TAG, "[%s] Cannot send cover control, not connected", this->get_name().c_str());
52  return;
53  }
54  if (call.get_stop()) {
55  auto *packet = this->encoder_->get_stop_request();
56  auto status =
57  esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
58  packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
59  if (status)
60  ESP_LOGW(TAG, "[%s] Error writing stop command to device, error = %d", this->get_name().c_str(), status);
61  }
62  if (call.get_position().has_value()) {
63  auto pos = *call.get_position();
64 
65  if (this->invert_position_)
66  pos = 1 - pos;
67  auto *packet = this->encoder_->get_set_position_request(100 - (uint8_t)(pos * 100));
68  auto status =
69  esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
70  packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
71  if (status)
72  ESP_LOGW(TAG, "[%s] Error writing set_position command to device, error = %d", this->get_name().c_str(), status);
73  }
74 }
75 
76 void Am43Component::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
77  esp_ble_gattc_cb_param_t *param) {
78  switch (event) {
79  case ESP_GATTC_DISCONNECT_EVT: {
80  this->logged_in_ = false;
81  break;
82  }
83  case ESP_GATTC_SEARCH_CMPL_EVT: {
84  auto *chr = this->parent_->get_characteristic(AM43_SERVICE_UUID, AM43_CHARACTERISTIC_UUID);
85  if (chr == nullptr) {
86  if (this->parent_->get_characteristic(AM43_TUYA_SERVICE_UUID, AM43_TUYA_CHARACTERISTIC_UUID) != nullptr) {
87  ESP_LOGE(TAG, "[%s] Detected a Tuya AM43 which is not supported, sorry.", this->get_name().c_str());
88  } else {
89  ESP_LOGE(TAG, "[%s] No control service found at device, not an AM43..?", this->get_name().c_str());
90  }
91  break;
92  }
93  this->char_handle_ = chr->handle;
94 
95  auto status = esp_ble_gattc_register_for_notify(this->parent_->get_gattc_if(), this->parent_->get_remote_bda(),
96  chr->handle);
97  if (status) {
98  ESP_LOGW(TAG, "[%s] esp_ble_gattc_register_for_notify failed, status=%d", this->get_name().c_str(), status);
99  }
100  break;
101  }
102  case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
103  this->node_state = espbt::ClientState::ESTABLISHED;
104  break;
105  }
106  case ESP_GATTC_NOTIFY_EVT: {
107  if (param->notify.handle != this->char_handle_)
108  break;
109  this->decoder_->decode(param->notify.value, param->notify.value_len);
110 
111  if (this->decoder_->has_position()) {
112  this->position = ((float) this->decoder_->position_ / 100.0);
113  if (!this->invert_position_)
114  this->position = 1 - this->position;
115  if (this->position > 0.97)
116  this->position = 1.0;
117  if (this->position < 0.02)
118  this->position = 0.0;
119  this->publish_state();
120  }
121 
122  if (this->decoder_->has_pin_response()) {
123  if (this->decoder_->pin_ok_) {
124  ESP_LOGI(TAG, "[%s] AM43 pin accepted.", this->get_name().c_str());
125  auto *packet = this->encoder_->get_position_request();
126  auto status = esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(),
127  this->char_handle_, packet->length, packet->data,
128  ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
129  if (status)
130  ESP_LOGW(TAG, "[%s] Error writing set_position to device, error = %d", this->get_name().c_str(), status);
131  } else {
132  ESP_LOGW(TAG, "[%s] AM43 pin rejected!", this->get_name().c_str());
133  }
134  }
135 
136  if (this->decoder_->has_set_position_response() && !this->decoder_->set_position_ok_)
137  ESP_LOGW(TAG, "[%s] Got nack after sending set_position. Bad pin?", this->get_name().c_str());
138 
139  if (this->decoder_->has_set_state_response() && !this->decoder_->set_state_ok_)
140  ESP_LOGW(TAG, "[%s] Got nack after sending set_state. Bad pin?", this->get_name().c_str());
141  break;
142  }
143  default:
144  break;
145  }
146 }
147 
148 } // namespace am43
149 } // namespace esphome
150 
151 #endif
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition: am43_cover.cpp:76
bool has_value() const
Definition: optional.h:87
const float COVER_OPEN
Definition: cover.cpp:9
void dump_config() override
Definition: am43_cover.cpp:13
void control(const cover::CoverCall &call) override
Definition: am43_cover.cpp:49
uint8_t status
Definition: bl0942.h:23
Definition: a4988.cpp:4
float position
Definition: cover.h:14
cover::CoverTraits get_traits() override
Definition: am43_cover.cpp:41
const optional< float > & get_position() const
Definition: cover.cpp:97
bool get_stop() const
Definition: cover.cpp:147