ESPHome  2024.3.1
bedjet_fan.cpp
Go to the documentation of this file.
1 #include "bedjet_fan.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace bedjet {
8 
9 using namespace esphome::fan;
10 
11 void BedJetFan::dump_config() { LOG_FAN("", "BedJet Fan", this); }
12 std::string BedJetFan::describe() { return "BedJet Fan"; }
13 
14 void BedJetFan::control(const fan::FanCall &call) {
15  ESP_LOGD(TAG, "Received BedJetFan::control");
16  if (!this->parent_->is_connected()) {
17  ESP_LOGW(TAG, "Not connected, cannot handle control call yet.");
18  return;
19  }
20  bool did_change = false;
21 
22  if (call.get_state().has_value() && this->state != *call.get_state()) {
23  // Turning off is easy:
24  if (this->state && this->parent_->button_off()) {
25  this->state = false;
26  this->publish_state();
27  return;
28  }
29 
30  // Turning on, we have to choose a specific mode; for now, use "COOL" mode
31  // In the future we could configure the mode to use for fan.turn_on.
32  if (this->parent_->button_cool()) {
33  this->state = true;
34  did_change = true;
35  }
36  }
37 
38  // ignore speed changes if not on or turning on
39  if (this->state && call.get_speed().has_value()) {
40  auto speed = *call.get_speed();
41  if (speed >= 1) {
42  this->speed = speed;
43  // Fan.speed is 1-20, but Bedjet expects 0-19, so subtract 1
44  this->parent_->set_fan_index(this->speed - 1);
45  did_change = true;
46  }
47  }
48 
49  if (did_change) {
50  this->publish_state();
51  }
52 }
53 
55  ESP_LOGVV(TAG, "[%s] Handling on_status with data=%p", this->get_name().c_str(), (void *) data);
56  bool did_change = false;
57  bool new_state = data->mode != MODE_STANDBY && data->mode != MODE_WAIT;
58 
59  if (new_state != this->state) {
60  this->state = new_state;
61  did_change = true;
62  }
63 
64  // BedjetStatusPacket.fan_step is in range 0-19, but Fan.speed wants 1-20.
65  if (data->fan_step + 1 != this->speed) {
66  this->speed = data->fan_step + 1;
67  did_change = true;
68  }
69 
70  if (did_change) {
71  this->publish_state();
72  }
73 }
74 
82 bool BedJetFan::update_status_() {
83  if (!this->parent_->is_connected())
84  return false;
85  if (!this->parent_->has_status())
86  return false;
87 
88  auto *status = this->parent_->get_status_packet();
89 
90  if (status == nullptr)
91  return false;
92 
93  this->on_status(status);
94  return true;
95 }
96 
98  ESP_LOGD(TAG, "[%s] update()", this->get_name().c_str());
99  // TODO: if the hub component is already polling, do we also need to include polling?
100  // We're already going to get on_status() at the hub's polling interval.
101  auto result = this->update_status_();
102  ESP_LOGD(TAG, "[%s] update_status result=%s", this->get_name().c_str(), result ? "true" : "false");
103 }
104 
106 void BedJetFan::reset_state_() {
107  this->state = false;
108  this->publish_state();
109 }
110 } // namespace bedjet
111 } // namespace esphome
112 
113 #endif
optional< int > get_speed() const
Definition: fan.h:65
void update() override
Definition: bedjet_fan.cpp:97
int speed
Definition: fan.h:35
BedJet is in "wait" mode, a step during a biorhythm program.
Definition: bedjet_const.h:32
bool has_value() const
Definition: optional.h:87
The format of a BedJet V3 status packet.
Definition: bedjet_codec.h:39
void dump_config() override
Definition: bedjet_fan.cpp:11
optional< bool > get_state() const
Definition: fan.h:49
void on_status(const BedjetStatusPacket *data) override
Definition: bedjet_fan.cpp:54
uint8_t status
Definition: bl0942.h:23
std::string describe() override
Definition: bedjet_fan.cpp:12
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void control(const fan::FanCall &call) override
Definition: bedjet_fan.cpp:14
bool state
Definition: fan.h:34