ESPHome 2026.3.0
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 {
7namespace bedjet {
8
9using namespace esphome::fan;
10
11void BedJetFan::dump_config() { LOG_FAN("", "BedJet Fan", this); }
12std::string BedJetFan::describe() { return "BedJet Fan"; }
13
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 auto state_opt = call.get_state();
23 if (state_opt.has_value() && this->state != *state_opt) {
24 // Turning off is easy:
25 if (this->state && this->parent_->button_off()) {
26 this->state = false;
27 this->publish_state();
28 return;
29 }
30
31 // Turning on, we have to choose a specific mode; for now, use "COOL" mode
32 // In the future we could configure the mode to use for fan.turn_on.
33 if (this->parent_->button_cool()) {
34 this->state = true;
35 did_change = true;
36 }
37 }
38
39 // ignore speed changes if not on or turning on
40 auto speed_opt = call.get_speed();
41 if (this->state && speed_opt.has_value()) {
42 auto speed = *speed_opt;
43 if (speed >= 1) {
44 this->speed = speed;
45 // Fan.speed is 1-20, but Bedjet expects 0-19, so subtract 1
46 this->parent_->set_fan_index(this->speed - 1);
47 did_change = true;
48 }
49 }
50
51 if (did_change) {
52 this->publish_state();
53 }
54}
55
57 ESP_LOGVV(TAG, "[%s] Handling on_status with data=%p", this->get_name().c_str(), (void *) data);
58 bool did_change = false;
59 bool new_state = data->mode != MODE_STANDBY && data->mode != MODE_WAIT;
60
61 if (new_state != this->state) {
62 this->state = new_state;
63 did_change = true;
64 }
65
66 // BedjetStatusPacket.fan_step is in range 0-19, but Fan.speed wants 1-20.
67 if (data->fan_step + 1 != this->speed) {
68 this->speed = data->fan_step + 1;
69 did_change = true;
70 }
71
72 if (did_change) {
73 this->publish_state();
74 }
75}
76
84bool BedJetFan::update_status_() {
85 if (!this->parent_->is_connected())
86 return false;
87 if (!this->parent_->has_status())
88 return false;
89
90 auto *status = this->parent_->get_status_packet();
91
92 if (status == nullptr)
93 return false;
94
95 this->on_status(status);
96 return true;
97}
98
100 ESP_LOGD(TAG, "[%s] update()", this->get_name().c_str());
101 // TODO: if the hub component is already polling, do we also need to include polling?
102 // We're already going to get on_status() at the hub's polling interval.
103 auto result = this->update_status_();
104 ESP_LOGD(TAG, "[%s] update_status result=%s", this->get_name().c_str(), result ? "true" : "false");
105}
106
108void BedJetFan::reset_state_() {
109 this->state = false;
110 this->publish_state();
111}
112} // namespace bedjet
113} // namespace esphome
114
115#endif
uint8_t status
Definition bl0942.h:8
const StringRef & get_name() const
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:197
bool state
The current on/off state of the fan.
Definition fan.h:111
int speed
The current fan speed level.
Definition fan.h:115
@ MODE_WAIT
BedJet is in "wait" mode, a step during a biorhythm program.
@ MODE_STANDBY
BedJet is Off.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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...
BedjetMode mode
BedJet operating mode.