ESPHome  2021.11.3
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 
6 namespace esphome {
7 namespace anova {
8 
9 static const char *const TAG = "anova";
10 
11 using namespace esphome::climate;
12 
13 void Anova::dump_config() { LOG_CLIMATE("", "Anova BLE Cooker", this); }
14 
15 void Anova::setup() {
16  this->codec_ = make_unique<AnovaCodec>();
17  this->current_request_ = 0;
18 }
19 
20 void Anova::loop() {}
21 
22 void Anova::control(const ClimateCall &call) {
23  if (call.get_mode().has_value()) {
24  ClimateMode mode = *call.get_mode();
25  AnovaPacket *pkt;
26  switch (mode) {
28  pkt = this->codec_->get_stop_request();
29  break;
31  pkt = this->codec_->get_start_request();
32  break;
33  default:
34  ESP_LOGW(TAG, "Unsupported mode: %d", mode);
35  return;
36  }
37  auto status = esp_ble_gattc_write_char(this->parent_->gattc_if, this->parent_->conn_id, this->char_handle_,
38  pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
39  if (status)
40  ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
41  }
42  if (call.get_target_temperature().has_value()) {
43  auto pkt = this->codec_->get_set_target_temp_request(*call.get_target_temperature());
44  auto status = esp_ble_gattc_write_char(this->parent_->gattc_if, this->parent_->conn_id, this->char_handle_,
45  pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
46  if (status)
47  ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
48  }
49 }
50 
51 void Anova::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) {
52  switch (event) {
53  case ESP_GATTC_DISCONNECT_EVT: {
54  this->current_temperature = NAN;
55  this->target_temperature = NAN;
56  this->publish_state();
57  break;
58  }
59  case ESP_GATTC_SEARCH_CMPL_EVT: {
60  auto chr = this->parent_->get_characteristic(ANOVA_SERVICE_UUID, ANOVA_CHARACTERISTIC_UUID);
61  if (chr == nullptr) {
62  ESP_LOGW(TAG, "[%s] No control service found at device, not an Anova..?", this->get_name().c_str());
63  ESP_LOGW(TAG, "[%s] Note, this component does not currently support Anova Nano.", this->get_name().c_str());
64  break;
65  }
66  this->char_handle_ = chr->handle;
67 
68  auto status = esp_ble_gattc_register_for_notify(this->parent_->gattc_if, this->parent_->remote_bda, chr->handle);
69  if (status) {
70  ESP_LOGW(TAG, "[%s] esp_ble_gattc_register_for_notify failed, status=%d", this->get_name().c_str(), status);
71  }
72  break;
73  }
74  case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
75  this->node_state = espbt::ClientState::ESTABLISHED;
76  this->current_request_ = 0;
77  this->update();
78  break;
79  }
80  case ESP_GATTC_NOTIFY_EVT: {
81  if (param->notify.handle != this->char_handle_)
82  break;
83  this->codec_->decode(param->notify.value, param->notify.value_len);
84  if (this->codec_->has_target_temp()) {
85  this->target_temperature = this->codec_->target_temp_;
86  }
87  if (this->codec_->has_current_temp()) {
88  this->current_temperature = this->codec_->current_temp_;
89  }
90  if (this->codec_->has_running()) {
91  this->mode = this->codec_->running_ ? climate::CLIMATE_MODE_HEAT : climate::CLIMATE_MODE_OFF;
92  }
93  if (this->codec_->has_unit()) {
94  this->fahrenheit_ = (this->codec_->unit_ == 'f');
95  ESP_LOGD(TAG, "Anova units is %s", this->fahrenheit_ ? "fahrenheit" : "celcius");
96  this->current_request_++;
97  }
98  this->publish_state();
99 
100  if (this->current_request_ > 1) {
101  AnovaPacket *pkt = nullptr;
102  switch (this->current_request_++) {
103  case 2:
104  pkt = this->codec_->get_read_target_temp_request();
105  break;
106  case 3:
107  pkt = this->codec_->get_read_current_temp_request();
108  break;
109  default:
110  this->current_request_ = 1;
111  break;
112  }
113  if (pkt != nullptr) {
114  auto status =
115  esp_ble_gattc_write_char(this->parent_->gattc_if, this->parent_->conn_id, this->char_handle_, pkt->length,
116  pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
117  if (status)
118  ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(),
119  status);
120  }
121  }
122  break;
123  }
124  default:
125  break;
126  }
127 }
128 
129 void Anova::set_unit_of_measurement(const char *unit) { this->fahrenheit_ = !strncmp(unit, "f", 1); }
130 
132  if (this->node_state != espbt::ClientState::ESTABLISHED)
133  return;
134 
135  if (this->current_request_ < 2) {
136  auto pkt = this->codec_->get_read_device_status_request();
137  if (this->current_request_ == 0)
138  this->codec_->get_set_unit_request(this->fahrenheit_ ? 'f' : 'c');
139  auto status = esp_ble_gattc_write_char(this->parent_->gattc_if, this->parent_->conn_id, this->char_handle_,
140  pkt->length, pkt->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
141  if (status)
142  ESP_LOGW(TAG, "[%s] esp_ble_gattc_write_char failed, status=%d", this->parent_->address_str().c_str(), status);
143  this->current_request_++;
144  }
145 }
146 
147 } // namespace anova
148 } // namespace esphome
149 
150 #endif
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
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:51
void control(const climate::ClimateCall &call) override
Definition: anova.cpp:22
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:259
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
void set_unit_of_measurement(const char *)
Definition: anova.cpp:129
bool has_value() const
Definition: optional.h:87
void update() override
Definition: anova.cpp:131
void setup() override
Definition: anova.cpp:15
void dump_config() override
Definition: anova.cpp:13
void loop() override
Definition: anova.cpp:20
const optional< float > & get_target_temperature() const
Definition: climate.cpp:260
ClimateMode
Enum for all modes a climate device can be in.
Definition: climate_mode.h:10
The climate device is off.
Definition: climate_mode.h:12
ClimateMode mode
Definition: climate.h:534
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
float target_temperature
Definition: climate.h:547