ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
am43_sensor.cpp
Go to the documentation of this file.
1#include "am43_sensor.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6
7namespace esphome::am43 {
8
9static const char *const TAG = "am43";
10
12 ESP_LOGCONFIG(TAG, "AM43");
13 LOG_SENSOR(" ", "Battery", this->battery_);
14 LOG_SENSOR(" ", "Illuminance", this->illuminance_);
15}
16
18 this->encoder_ = make_unique<Am43Encoder>();
19 this->decoder_ = make_unique<Am43Decoder>();
20 this->logged_in_ = false;
21 this->last_battery_update_ = 0;
22 this->current_sensor_ = 0;
23}
24
25void Am43::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) {
26 switch (event) {
27 case ESP_GATTC_OPEN_EVT: {
28 if (param->open.status == ESP_GATT_OK) {
29 this->logged_in_ = false;
30 }
31 break;
32 }
33 case ESP_GATTC_DISCONNECT_EVT: {
34 this->logged_in_ = false;
35 this->node_state = espbt::ClientState::IDLE;
36 if (this->battery_ != nullptr)
37 this->battery_->publish_state(NAN);
38 if (this->illuminance_ != nullptr)
39 this->illuminance_->publish_state(NAN);
40 break;
41 }
42 case ESP_GATTC_SEARCH_CMPL_EVT: {
43 auto *chr = this->parent_->get_characteristic(AM43_SERVICE_UUID, AM43_CHARACTERISTIC_UUID);
44 if (chr == nullptr) {
45 if (this->parent_->get_characteristic(AM43_TUYA_SERVICE_UUID, AM43_TUYA_CHARACTERISTIC_UUID) != nullptr) {
46 ESP_LOGE(TAG, "[%s] Detected a Tuya AM43 which is not supported, sorry.", this->parent_->address_str());
47 } else {
48 ESP_LOGE(TAG, "[%s] No control service found at device, not an AM43..?", this->parent_->address_str());
49 }
50 break;
51 }
52 this->char_handle_ = chr->handle;
53 break;
54 }
55 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
56 this->node_state = espbt::ClientState::ESTABLISHED;
57 this->update();
58 break;
59 }
60 case ESP_GATTC_NOTIFY_EVT: {
61 if (param->notify.handle != this->char_handle_)
62 break;
63 this->decoder_->decode(param->notify.value, param->notify.value_len);
64
65 if (this->battery_ != nullptr && this->decoder_->has_battery_level() &&
66 millis() - this->last_battery_update_ > 10000) {
67 this->battery_->publish_state(this->decoder_->battery_level_);
69 }
70
71 if (this->illuminance_ != nullptr && this->decoder_->has_light_level()) {
72 this->illuminance_->publish_state(this->decoder_->light_level_);
73 }
74
75 if (this->current_sensor_ > 0) {
76 if (this->illuminance_ != nullptr) {
77 auto *packet = this->encoder_->get_light_level_request();
78 auto status = esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(),
79 this->char_handle_, packet->length, packet->data,
80 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
81 if (status) {
82 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
83 }
84 }
85 this->current_sensor_ = 0;
86 }
87 break;
88 }
89 default:
90 break;
91 }
92}
93
95 if (this->node_state != espbt::ClientState::ESTABLISHED) {
96 ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->parent_->address_str());
97 return;
98 }
99 if (this->current_sensor_ == 0) {
100 if (this->battery_ != nullptr) {
101 auto *packet = this->encoder_->get_battery_level_request();
102 auto status =
103 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
104 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
105 if (status) {
106 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
107 }
108 }
109 this->current_sensor_++;
110 }
111}
112
113} // namespace esphome::am43
114
115#endif
uint8_t status
Definition bl0942.h:8
void update() override
uint32_t last_battery_update_
Definition am43_sensor.h:37
std::unique_ptr< Am43Decoder > decoder_
Definition am43_sensor.h:30
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
sensor::Sensor * illuminance_
Definition am43_sensor.h:33
void setup() override
sensor::Sensor * battery_
Definition am43_sensor.h:32
void dump_config() override
std::unique_ptr< Am43Encoder > encoder_
Definition am43_sensor.h:29
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28