ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
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
6namespace esphome::bedjet {
7
8using namespace esphome::fan;
9
10void BedJetFan::dump_config() { LOG_FAN("", "BedJet Fan", this); }
11std::string BedJetFan::describe() { return "BedJet Fan"; }
12
14 ESP_LOGD(TAG, "Received BedJetFan::control");
15 if (!this->parent_->is_connected()) {
16 ESP_LOGW(TAG, "Not connected, cannot handle control call yet.");
17 return;
18 }
19 bool did_change = false;
20
21 auto state_opt = call.get_state();
22 if (state_opt.has_value() && this->state != *state_opt) {
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 auto speed_opt = call.get_speed();
40 if (this->state && speed_opt.has_value()) {
41 auto speed = *speed_opt;
42 if (speed >= 1) {
43 this->speed = speed;
44 // Fan.speed is 1-20, but Bedjet expects 0-19, so subtract 1
45 this->parent_->set_fan_index(this->speed - 1);
46 did_change = true;
47 }
48 }
49
50 if (did_change) {
51 this->publish_state();
52 }
53}
54
56 ESP_LOGVV(TAG, "[%s] Handling on_status with data=%p", this->get_name().c_str(), (void *) data);
57 bool did_change = false;
58 bool new_state = data->mode != MODE_STANDBY && data->mode != MODE_WAIT;
59
60 if (new_state != this->state) {
61 this->state = new_state;
62 did_change = true;
63 }
64
65 // BedjetStatusPacket.fan_step is in range 0-19, but Fan.speed wants 1-20.
66 if (data->fan_step + 1 != this->speed) {
67 this->speed = data->fan_step + 1;
68 did_change = true;
69 }
70
71 if (did_change) {
72 this->publish_state();
73 }
74}
75
83bool BedJetFan::update_status_() {
84 if (!this->parent_->is_connected())
85 return false;
86 if (!this->parent_->has_status())
87 return false;
88
89 auto *status = this->parent_->get_status_packet();
90
91 if (status == nullptr)
92 return false;
93
94 this->on_status(status);
95 return true;
96}
97
99 ESP_LOGD(TAG, "[%s] update()", this->get_name().c_str());
100 // TODO: if the hub component is already polling, do we also need to include polling?
101 // We're already going to get on_status() at the hub's polling interval.
102 auto result = this->update_status_();
103 ESP_LOGD(TAG, "[%s] update_status result=%s", this->get_name().c_str(), result ? "true" : "false");
104}
105
107void BedJetFan::reset_state_() {
108 this->state = false;
109 this->publish_state();
110}
111} // namespace esphome::bedjet
112
113#endif
uint8_t status
Definition bl0942.h:8
const StringRef & get_name() const
Definition entity_base.h:71
void dump_config() override
void control(const fan::FanCall &call) override
void on_status(const BedjetStatusPacket *data) override
std::string describe() override
void publish_state()
Definition fan.cpp:223
bool state
The current on/off state of the fan.
Definition fan.h:110
int speed
The current fan speed level.
Definition fan.h:114
@ MODE_WAIT
BedJet is in "wait" mode, a step during a biorhythm program.
@ MODE_STANDBY
BedJet is Off.
The format of a BedJet V3 status packet.
uint8_t fan_step
BedJet fan speed; value is in the 0-19 range, representing 5% increments (5%-100%): 5 + 5 /< * fan_st...