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