ESPHome 2026.1.5
Loading...
Searching...
No Matches
anova.cpp
Go to the documentation of this file.
1#include "anova.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
6namespace esphome {
7namespace anova {
8
9static const char *const TAG = "anova";
10
11using namespace esphome::climate;
12
13void Anova::dump_config() { LOG_CLIMATE("", "Anova BLE Cooker", this); }
14
16 this->codec_ = make_unique<AnovaCodec>();
17 this->current_request_ = 0;
18}
19
21 // Parent BLEClientNode has a loop() method, but this component uses
22 // polling via update() and BLE callbacks so loop isn't needed
23 this->disable_loop();
24}
25
26void Anova::control(const ClimateCall &call) {
27 if (call.get_mode().has_value()) {
28 ClimateMode mode = *call.get_mode();
29 AnovaPacket *pkt;
30 switch (mode) {
32 pkt = this->codec_->get_stop_request();
33 break;
35 pkt = this->codec_->get_start_request();
36 break;
37 default:
38 ESP_LOGW(TAG, "Unsupported mode: %d", mode);
39 return;
40 }
41 auto status =
42 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
43 pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
44 if (status) {
45 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
46 }
47 }
48 if (call.get_target_temperature().has_value()) {
49 auto *pkt = this->codec_->get_set_target_temp_request(*call.get_target_temperature());
50 auto status =
51 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
52 pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
53 if (status) {
54 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
55 }
56 }
57}
58
59void Anova::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) {
60 switch (event) {
61 case ESP_GATTC_DISCONNECT_EVT: {
62 this->current_temperature = NAN;
63 this->target_temperature = NAN;
64 this->publish_state();
65 break;
66 }
67 case ESP_GATTC_SEARCH_CMPL_EVT: {
68 auto *chr = this->parent_->get_characteristic(ANOVA_SERVICE_UUID, ANOVA_CHARACTERISTIC_UUID);
69 if (chr == nullptr) {
70 ESP_LOGW(TAG,
71 "[%s] No control service found at device, not an Anova..?\n"
72 "[%s] Note, this component does not currently support Anova Nano.",
73 this->get_name().c_str(), this->get_name().c_str());
74 break;
75 }
76 this->char_handle_ = chr->handle;
77
78 auto status = esp_ble_gattc_register_for_notify(this->parent_->get_gattc_if(), this->parent_->get_remote_bda(),
79 chr->handle);
80 if (status) {
81 ESP_LOGW(TAG, "[%s] esp_ble_gattc_register_for_notify failed, status=%d", this->get_name().c_str(), status);
82 }
83 break;
84 }
85 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
86 this->node_state = espbt::ClientState::ESTABLISHED;
87 this->current_request_ = 0;
88 this->update();
89 break;
90 }
91 case ESP_GATTC_NOTIFY_EVT: {
92 if (param->notify.handle != this->char_handle_)
93 break;
94 this->codec_->decode(param->notify.value, param->notify.value_len);
95 if (this->codec_->has_target_temp()) {
96 this->target_temperature = this->codec_->target_temp_;
97 }
98 if (this->codec_->has_current_temp()) {
99 this->current_temperature = this->codec_->current_temp_;
100 }
101 if (this->codec_->has_running()) {
103 }
104 if (this->codec_->has_unit()) {
105 this->fahrenheit_ = (this->codec_->unit_ == 'f');
106 ESP_LOGD(TAG, "Anova units is %s", this->fahrenheit_ ? "fahrenheit" : "celsius");
107 this->current_request_++;
108 }
109 this->publish_state();
110
111 if (this->current_request_ > 1) {
112 AnovaPacket *pkt = nullptr;
113 switch (this->current_request_++) {
114 case 2:
115 pkt = this->codec_->get_read_target_temp_request();
116 break;
117 case 3:
118 pkt = this->codec_->get_read_current_temp_request();
119 break;
120 default:
121 this->current_request_ = 1;
122 break;
123 }
124 if (pkt != nullptr) {
125 auto status =
126 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
127 pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
128 if (status) {
129 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
130 }
131 }
132 }
133 break;
134 }
135 default:
136 break;
137 }
138}
139
140void Anova::set_unit_of_measurement(const char *unit) { this->fahrenheit_ = !strncmp(unit, "f", 1); }
141
143 if (this->node_state != espbt::ClientState::ESTABLISHED)
144 return;
145
146 if (this->current_request_ < 2) {
147 auto *pkt = this->codec_->get_read_device_status_request();
148 if (this->current_request_ == 0)
149 this->codec_->get_set_unit_request(this->fahrenheit_ ? 'f' : 'c');
150 auto status =
151 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
152 pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
153 if (status) {
154 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
155 }
156 this->current_request_++;
157 }
158}
159
160} // namespace anova
161} // namespace esphome
162
163#endif
uint8_t status
Definition bl0942.h:8
void disable_loop()
Disable this component's loop.
const StringRef & get_name() const
void dump_config() override
Definition anova.cpp:13
void update() override
Definition anova.cpp:142
void setup() override
Definition anova.cpp:15
void loop() override
Definition anova.cpp:20
std::unique_ptr< AnovaCodec > codec_
Definition anova.h:41
uint8_t current_request_
Definition anova.h:44
uint16_t char_handle_
Definition anova.h:43
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 anova.cpp:59
void control(const climate::ClimateCall &call) override
Definition anova.cpp:26
void set_unit_of_measurement(const char *unit)
Definition anova.cpp:140
This class is used to encode all control actions on a climate device.
Definition climate.h:33
const optional< float > & get_target_temperature() const
Definition climate.cpp:299
ClimateMode mode
The active mode of the climate device.
Definition climate.h:262
float target_temperature
The target temperature of the climate device.
Definition climate.h:243
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:236
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:438
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
ClimateMode
Enum for all modes a climate device can be in.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7