ESPHome 2026.11.0b6
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
9ESPHOME_LOG_TAG(TAG, "am43");
10
11void Am43::dump_config() {
12 ESP_LOGCONFIG(TAG, "AM43");
13 LOG_SENSOR(" ", "Battery", this->battery_sensor_);
14 LOG_SENSOR(" ", "Illuminance", this->illuminance_sensor_);
15}
16
17void Am43::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) {
18 switch (event) {
19 case ESP_GATTC_DISCONNECT_EVT: {
20 this->node_state = espbt::ClientState::IDLE;
21 PUBLISH_STATE(this->battery_sensor_, NAN);
22 PUBLISH_STATE(this->illuminance_sensor_, NAN);
23 break;
24 }
25 case ESP_GATTC_SEARCH_CMPL_EVT: {
26 auto *chr = this->parent_->get_characteristic(AM43_SERVICE_UUID, AM43_CHARACTERISTIC_UUID);
27 if (chr == nullptr) {
28 if (this->parent_->get_characteristic(AM43_TUYA_SERVICE_UUID, AM43_TUYA_CHARACTERISTIC_UUID) != nullptr) {
29 ESP_LOGE(TAG, "[%s] Detected a Tuya AM43 which is not supported, sorry.", this->parent_->address_str());
30 } else {
31 ESP_LOGE(TAG, "[%s] No control service found at device, not an AM43..?", this->parent_->address_str());
32 }
33 break;
34 }
35 this->char_handle_ = chr->handle;
36 break;
37 }
38 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
39 this->node_state = espbt::ClientState::ESTABLISHED;
40 this->update();
41 break;
42 }
43 case ESP_GATTC_NOTIFY_EVT: {
44 if (param->notify.handle != this->char_handle_)
45 break;
46 Am43Decoder decoder;
47 decoder.decode(param->notify.value, param->notify.value_len);
48
49 if (this->battery_sensor_ != nullptr && decoder.has_battery_level() &&
50 millis() - this->last_battery_update_ > 10000) {
51 this->battery_sensor_->publish_state(decoder.battery_level_);
53 }
54
55 if (this->illuminance_sensor_ != nullptr && decoder.has_light_level()) {
56 this->illuminance_sensor_->publish_state(decoder.light_level_);
57 }
58
59 if (this->current_sensor_ > 0) {
60 if (this->illuminance_sensor_ != nullptr)
62 this->current_sensor_ = 0;
63 }
64 break;
65 }
66 default:
67 break;
68 }
69}
70
71void Am43::update() {
72 if (this->node_state != espbt::ClientState::ESTABLISHED) {
73 ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->parent_->address_str());
74 return;
75 }
76 if (this->current_sensor_ == 0) {
77 if (this->battery_sensor_ != nullptr)
79 this->current_sensor_++;
80 }
81}
82
83void Am43::write_packet_(const Am43Packet &packet) {
84 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) the IDF API takes a mutable buffer it only reads
85 auto status = esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(),
86 this->char_handle_, packet.length, const_cast<uint8_t *>(packet.data),
87 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
88 if (status) {
89 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
90 }
91}
92
93} // namespace esphome::am43
94
95#endif
uint8_t status
Definition bl0942.h:8
virtual void update()=0
static Am43Packet get_battery_level_request()
Definition am43_base.cpp:12
static Am43Packet get_light_level_request()
Definition am43_base.cpp:17
uint32_t last_battery_update_
Definition am43_sensor.h:32
void write_packet_(const Am43Packet &packet)
Writes a request to the control characteristic; logs on failure.
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
ESPHOME_LOG_TAG(TAG, "am43")
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25