ESPHome 2026.10.0-dev
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::anova {
7
8static const char *const TAG = "anova";
9
10using namespace esphome::climate;
11
12void Anova::dump_config() { LOG_CLIMATE("", "Anova BLE Cooker", this); }
13
15 this->codec_ = make_unique<AnovaCodec>();
17}
18
20 // Parent BLEClientNode has a loop() method, but this component uses
21 // polling via update() and BLE callbacks so loop isn't needed
22 this->disable_loop();
23}
24
26 auto status =
27 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
28 pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
29 if (status) {
30 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str(), status);
31 }
32}
33
34void Anova::control(const ClimateCall &call) {
35 auto mode_val = call.get_mode();
36 if (mode_val.has_value()) {
37 ClimateMode mode = *mode_val;
38 AnovaPacket *pkt;
39 switch (mode) {
41 pkt = this->codec_->get_stop_request();
42 break;
44 pkt = this->codec_->get_start_request();
45 break;
46 default:
47 ESP_LOGW(TAG, "Unsupported mode: %d", mode);
48 return;
49 }
50 this->write_request_(pkt);
51 }
52 auto target_temp = call.get_target_temperature();
53 if (target_temp.has_value()) {
54 this->write_request_(this->codec_->get_set_target_temp_request(*target_temp));
55 }
56}
57
58void Anova::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) {
59 switch (event) {
60 case ESP_GATTC_DISCONNECT_EVT: {
61 this->current_temperature = NAN;
62 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, "[%s] No control service found at device, not an Anova..?", this->get_name().c_str());
71 ESP_LOGW(TAG, "[%s] Note, this component does not currently support Anova Nano.", this->get_name().c_str());
72 break;
73 }
74 this->char_handle_ = chr->handle;
75
76 auto status = esp_ble_gattc_register_for_notify(this->parent_->get_gattc_if(), this->parent_->get_remote_bda(),
77 chr->handle);
78 if (status) {
79 ESP_LOGW(TAG, "[%s] esp_ble_gattc_register_for_notify failed, status=%d", this->get_name().c_str(), status);
80 }
81 break;
82 }
83 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
84 this->node_state = espbt::ClientState::ESTABLISHED;
86 this->update(); // begin the first poll cycle immediately
87 break;
88 }
89 case ESP_GATTC_NOTIFY_EVT: {
90 if (param->notify.handle != this->char_handle_)
91 break;
92 this->codec_->decode(param->notify.value, param->notify.value_len);
93 if (this->codec_->has_target_temp()) {
94 this->target_temperature = this->codec_->target_temp_;
95 }
96 if (this->codec_->has_current_temp()) {
97 this->current_temperature = this->codec_->current_temp_;
98 }
99 if (this->codec_->has_running()) {
101 }
102 if (this->codec_->has_unit()) {
103 ESP_LOGD(TAG, "Anova units is %s", (this->codec_->unit_ == 'f') ? "fahrenheit" : "celsius");
104 }
105 this->publish_state();
106
107 // Advance the poll cycle to its next request based on the reply we got.
108 switch (this->poll_step_) {
111 this->write_request_(this->codec_->get_read_device_status_request());
112 break;
113 case PollStep::STATUS:
115 this->write_request_(this->codec_->get_read_target_temp_request());
116 break;
117 case PollStep::TARGET:
119 this->write_request_(this->codec_->get_read_current_temp_request());
120 break;
122 this->poll_step_ = PollStep::IDLE; // full cycle complete
123 break;
124 default:
125 // A reply to an ad-hoc control() write, outside a managed cycle.
126 break;
127 }
128 break;
129 }
130 default:
131 break;
132 }
133}
134
135void Anova::set_unit_of_measurement(const char *unit) { this->want_fahrenheit_ = !strncmp(unit, "f", 1); }
136
138 if (this->node_state != espbt::ClientState::ESTABLISHED)
139 return;
140 if (this->poll_step_ != PollStep::IDLE) {
141 // The previous cycle never finished within a full polling interval -- a
142 // reply was missed or a write failed. Restart the cycle rather than stall;
143 // the polling interval itself acts as the timeout. A late reply from the
144 // abandoned cycle is harmless: state decoding happens on every notify
145 // regardless of step, and each notify sends at most one follow-up request.
146 ESP_LOGW(TAG, "[%s] Poll cycle incomplete (step %u); restarting cycle", this->parent_->address_str(),
147 static_cast<uint8_t>(this->poll_step_));
148 }
149 // Re-assert the configured unit at the start of every poll cycle, then fall
150 // through the status/temperature reads via the notification handler. Always
151 // command the configured unit (want_fahrenheit_) -- never the last value the
152 // device reported, or a drift to 'c' would lock itself in.
154 this->write_request_(this->codec_->get_set_unit_request(this->want_fahrenheit_ ? 'f' : 'c'));
155}
156
157} // namespace esphome::anova
158
159#endif
uint8_t status
Definition bl0942.h:8
void disable_loop()
Disable this component's loop.
const StringRef & get_name() const
Definition entity_base.h:71
void dump_config() override
Definition anova.cpp:12
void update() override
Definition anova.cpp:137
void setup() override
Definition anova.cpp:14
void loop() override
Definition anova.cpp:19
PollStep poll_step_
Definition anova.h:53
std::unique_ptr< AnovaCodec > codec_
Definition anova.h:49
uint16_t char_handle_
Definition anova.h:51
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:58
void control(const climate::ClimateCall &call) override
Definition anova.cpp:34
void write_request_(AnovaPacket *pkt)
Definition anova.cpp:25
void set_unit_of_measurement(const char *unit)
Definition anova.cpp:135
This class is used to encode all control actions on a climate device.
Definition climate.h:34
ClimateMode mode
The active mode of the climate device.
Definition climate.h:304
float target_temperature
The target temperature of the climate device.
Definition climate.h:285
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:278
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:437
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.