ESPHome 2025.5.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 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
82bool 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
106void BedJetFan::reset_state_() {
107 this->state = false;
108 this->publish_state();
109}
110} // namespace bedjet
111} // namespace esphome
112
113#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
optional< int > get_speed() const
Definition fan.h:65
void publish_state()
Definition fan.cpp:117
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
bool has_value() const
Definition optional.h:87
@ 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.