ESPHome 2025.9.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 {
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().c_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().c_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, "[%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;
85 this->current_request_ = 0;
86 this->update();
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 this->fahrenheit_ = (this->codec_->unit_ == 'f');
104 ESP_LOGD(TAG, "Anova units is %s", this->fahrenheit_ ? "fahrenheit" : "celsius");
105 this->current_request_++;
106 }
107 this->publish_state();
108
109 if (this->current_request_ > 1) {
110 AnovaPacket *pkt = nullptr;
111 switch (this->current_request_++) {
112 case 2:
113 pkt = this->codec_->get_read_target_temp_request();
114 break;
115 case 3:
116 pkt = this->codec_->get_read_current_temp_request();
117 break;
118 default:
119 this->current_request_ = 1;
120 break;
121 }
122 if (pkt != nullptr) {
123 auto status =
124 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
125 pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
126 if (status) {
127 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(),
128 status);
129 }
130 }
131 }
132 break;
133 }
134 default:
135 break;
136 }
137}
138
139void Anova::set_unit_of_measurement(const char *unit) { this->fahrenheit_ = !strncmp(unit, "f", 1); }
140
142 if (this->node_state != espbt::ClientState::ESTABLISHED)
143 return;
144
145 if (this->current_request_ < 2) {
146 auto *pkt = this->codec_->get_read_device_status_request();
147 if (this->current_request_ == 0)
148 this->codec_->get_set_unit_request(this->fahrenheit_ ? 'f' : 'c');
149 auto status =
150 esp_ble_gattc_write_char(this->parent_->get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
151 pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
152 if (status) {
153 ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
154 }
155 this->current_request_++;
156 }
157}
158
159} // namespace anova
160} // namespace esphome
161
162#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:141
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:139
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:274
ClimateMode mode
The active mode of the climate device.
Definition climate.h:173
float target_temperature
The target temperature of the climate device.
Definition climate.h:186
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:179
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:395
const std::string & address_str() const
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