ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
bedjet_climate.cpp
Go to the documentation of this file.
1#include "bedjet_climate.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
6namespace esphome {
7namespace bedjet {
8
9using namespace esphome::climate;
10
11static const char *bedjet_fan_step_to_fan_mode(const uint8_t fan_step) {
12 if (fan_step < BEDJET_FAN_SPEED_COUNT)
13 return BEDJET_FAN_STEP_NAMES[fan_step];
14 return nullptr;
15}
16
17static uint8_t bedjet_fan_speed_to_step(const char *fan_step_percent) {
18 for (int i = 0; i < BEDJET_FAN_SPEED_COUNT; i++) {
19 if (strcmp(BEDJET_FAN_STEP_NAMES[i], fan_step_percent) == 0) {
20 return i;
21 }
22 }
23 return -1;
24}
25
26static inline BedjetButton heat_button(BedjetHeatMode mode) {
28}
29
30std::string BedJetClimate::describe() { return "BedJet Climate"; }
31
33 LOG_CLIMATE("", "BedJet Climate", this);
34 auto traits = this->get_traits();
35
36 ESP_LOGCONFIG(TAG, " Supported modes:");
37 for (auto mode : traits.get_supported_modes()) {
38 ESP_LOGCONFIG(TAG, " - %s", LOG_STR_ARG(climate_mode_to_string(mode)));
39 }
40 if (this->heating_mode_ == HEAT_MODE_EXTENDED) {
41 ESP_LOGCONFIG(TAG, " - BedJet heating mode: EXT HT");
42 } else {
43 ESP_LOGCONFIG(TAG, " - BedJet heating mode: HEAT");
44 }
45
46 ESP_LOGCONFIG(TAG, " Supported fan modes:");
47 for (const auto &mode : traits.get_supported_fan_modes()) {
48 ESP_LOGCONFIG(TAG, " - %s", LOG_STR_ARG(climate_fan_mode_to_string(mode)));
49 }
50 for (const auto &mode : traits.get_supported_custom_fan_modes()) {
51 ESP_LOGCONFIG(TAG, " - %s (c)", mode);
52 }
53
54 ESP_LOGCONFIG(TAG, " Supported presets:");
55 for (auto preset : traits.get_supported_presets()) {
56 ESP_LOGCONFIG(TAG, " - %s", LOG_STR_ARG(climate_preset_to_string(preset)));
57 }
58 for (const auto &preset : traits.get_supported_custom_presets()) {
59 ESP_LOGCONFIG(TAG, " - %s (c)", preset);
60 }
61}
62
64 // restore set points
65 auto restore = this->restore_state_();
66 if (restore.has_value()) {
67 ESP_LOGI(TAG, "Restored previous saved state.");
68 restore->apply(this);
69 } else {
70 // Initial status is unknown until we connect
71 this->reset_state_();
72 }
73}
74
77 this->mode = CLIMATE_MODE_OFF;
79 this->target_temperature = NAN;
80 this->current_temperature = NAN;
81 this->preset.reset();
83 this->publish_state();
84}
85
87 // This component is controlled via the parent BedJetHub
88 // Empty loop not needed, disable to save CPU cycles
89 this->disable_loop();
90}
91
93 ESP_LOGD(TAG, "Received BedJetClimate::control");
94 if (!this->parent_->is_connected()) {
95 ESP_LOGW(TAG, "Not connected, cannot handle control call yet.");
96 return;
97 }
98
99 if (call.get_mode().has_value()) {
100 ClimateMode mode = *call.get_mode();
101 bool button_result;
102 switch (mode) {
103 case CLIMATE_MODE_OFF:
104 button_result = this->parent_->button_off();
105 break;
107 button_result = this->parent_->send_button(heat_button(this->heating_mode_));
108 break;
110 button_result = this->parent_->button_cool();
111 break;
112 case CLIMATE_MODE_DRY:
113 button_result = this->parent_->button_dry();
114 break;
115 default:
116 ESP_LOGW(TAG, "Unsupported mode: %d", mode);
117 return;
118 }
119
120 if (button_result) {
121 this->mode = mode;
122 // We're using (custom) preset for Turbo, EXT HT, & M1-3 presets, so changing climate mode will clear those
123 this->clear_custom_preset_();
124 this->preset.reset();
125 }
126 }
127
128 if (call.get_target_temperature().has_value()) {
129 auto target_temp = *call.get_target_temperature();
130 auto result = this->parent_->set_target_temp(target_temp);
131
132 if (result) {
133 this->target_temperature = target_temp;
134 }
135 }
136
137 if (call.get_preset().has_value()) {
138 ClimatePreset preset = *call.get_preset();
139 bool result;
140
142 // We use BOOST preset for TURBO mode, which is a short-lived/high-heat mode.
143 result = this->parent_->button_turbo();
144
145 if (result) {
146 this->mode = CLIMATE_MODE_HEAT;
148 }
149 } else if (preset == CLIMATE_PRESET_NONE && this->preset.has_value()) {
150 if (this->mode == CLIMATE_MODE_HEAT && this->preset == CLIMATE_PRESET_BOOST) {
151 // We were in heat mode with Boost preset, and now preset is set to None, so revert to normal heat.
152 result = this->parent_->send_button(heat_button(this->heating_mode_));
153 if (result) {
154 this->preset.reset();
155 this->clear_custom_preset_();
156 }
157 } else {
158 ESP_LOGD(TAG, "Ignoring preset '%s' call; with current mode '%s' and preset '%s'",
159 LOG_STR_ARG(climate_preset_to_string(preset)), LOG_STR_ARG(climate_mode_to_string(this->mode)),
160 LOG_STR_ARG(climate_preset_to_string(this->preset.value_or(CLIMATE_PRESET_NONE))));
161 }
162 } else {
163 ESP_LOGW(TAG, "Unsupported preset: %d", preset);
164 return;
165 }
166 } else if (call.has_custom_preset()) {
167 const char *preset = call.get_custom_preset();
168 bool result;
169
170 if (strcmp(preset, "M1") == 0) {
171 result = this->parent_->button_memory1();
172 } else if (strcmp(preset, "M2") == 0) {
173 result = this->parent_->button_memory2();
174 } else if (strcmp(preset, "M3") == 0) {
175 result = this->parent_->button_memory3();
176 } else if (strcmp(preset, "LTD HT") == 0) {
177 result = this->parent_->button_heat();
178 } else if (strcmp(preset, "EXT HT") == 0) {
179 result = this->parent_->button_ext_heat();
180 } else {
181 ESP_LOGW(TAG, "Unsupported preset: %s", preset);
182 return;
183 }
184
185 if (result) {
186 this->set_custom_preset_(preset);
187 }
188 }
189
190 if (call.get_fan_mode().has_value()) {
191 // Climate fan mode only supports low/med/high, but the BedJet supports 5-100% increments.
192 // We can still support a ClimateCall that requests low/med/high, and just translate it to a step increment here.
193 auto fan_mode = *call.get_fan_mode();
194 bool result;
195 if (fan_mode == CLIMATE_FAN_LOW) {
196 result = this->parent_->set_fan_speed(20);
197 } else if (fan_mode == CLIMATE_FAN_MEDIUM) {
198 result = this->parent_->set_fan_speed(50);
199 } else if (fan_mode == CLIMATE_FAN_HIGH) {
200 result = this->parent_->set_fan_speed(75);
201 } else {
202 ESP_LOGW(TAG, "[%s] Unsupported fan mode: %s", this->get_name().c_str(),
204 return;
205 }
206
207 if (result) {
208 this->set_fan_mode_(fan_mode);
209 }
210 } else if (call.has_custom_fan_mode()) {
211 const char *fan_mode = call.get_custom_fan_mode();
212 auto fan_index = bedjet_fan_speed_to_step(fan_mode);
213 if (fan_index <= 19) {
214 ESP_LOGV(TAG, "[%s] Converted fan mode %s to bedjet fan step %d", this->get_name().c_str(), fan_mode, fan_index);
215 bool result = this->parent_->set_fan_index(fan_index);
216 if (result) {
217 this->set_custom_fan_mode_(fan_mode);
218 }
219 }
220 }
221}
222
223void BedJetClimate::on_bedjet_state(bool is_ready) {}
224
226 ESP_LOGV(TAG, "[%s] Handling on_status with data=%p", this->get_name().c_str(), (void *) data);
227
228 auto converted_temp = bedjet_temp_to_c(data->target_temp_step);
229 if (converted_temp > 0)
230 this->target_temperature = converted_temp;
231
233 converted_temp = bedjet_temp_to_c(data->actual_temp_step);
234 } else {
235 converted_temp = bedjet_temp_to_c(data->ambient_temp_step);
236 }
237 if (converted_temp > 0) {
238 this->current_temperature = converted_temp;
239 }
240
241 const auto *fan_mode_name = bedjet_fan_step_to_fan_mode(data->fan_step);
242 if (fan_mode_name != nullptr) {
243 this->set_custom_fan_mode_(fan_mode_name);
244 }
245
246 // TODO: Get biorhythm data to determine which preset (M1-3) is running, if any.
247 switch (data->mode) {
248 case MODE_WAIT: // Biorhythm "wait" step: device is idle
249 case MODE_STANDBY:
250 this->mode = CLIMATE_MODE_OFF;
253 this->clear_custom_preset_();
254 this->preset.reset();
255 break;
256
257 case MODE_HEAT:
258 this->mode = CLIMATE_MODE_HEAT;
260 this->preset.reset();
261 if (this->heating_mode_ == HEAT_MODE_EXTENDED) {
262 this->set_custom_preset_("LTD HT");
263 } else {
264 this->clear_custom_preset_();
265 }
266 break;
267
268 case MODE_EXTHT:
269 this->mode = CLIMATE_MODE_HEAT;
271 this->preset.reset();
272 if (this->heating_mode_ == HEAT_MODE_EXTENDED) {
273 this->clear_custom_preset_();
274 } else {
275 this->set_custom_preset_("EXT HT");
276 }
277 break;
278
279 case MODE_COOL:
282 this->clear_custom_preset_();
283 this->preset.reset();
284 break;
285
286 case MODE_DRY:
287 this->mode = CLIMATE_MODE_DRY;
289 this->clear_custom_preset_();
290 this->preset.reset();
291 break;
292
293 case MODE_TURBO:
295 this->mode = CLIMATE_MODE_HEAT;
297 break;
298
299 default:
300 ESP_LOGW(TAG, "[%s] Unexpected mode: 0x%02X", this->get_name().c_str(), data->mode);
301 break;
302 }
303
304 ESP_LOGV(TAG, "[%s] After on_status, new mode=%s", this->get_name().c_str(),
305 LOG_STR_ARG(climate_mode_to_string(this->mode)));
306 // FIXME: compare new state to previous state.
307 this->publish_state();
308}
309
318 if (!this->parent_->is_connected())
319 return false;
320 if (!this->parent_->has_status())
321 return false;
322
323 auto *status = this->parent_->get_status_packet();
324
325 if (status == nullptr)
326 return false;
327
328 this->on_status(status);
329
330 if (this->is_valid_()) {
331 // TODO: only if state changed?
332 this->publish_state();
333 this->status_clear_warning();
334 return true;
335 }
336
337 return false;
338}
339
341 ESP_LOGD(TAG, "[%s] update()", this->get_name().c_str());
342 // TODO: if the hub component is already polling, do we also need to include polling?
343 // We're already going to get on_status() at the hub's polling interval.
344 auto result = this->update_status_();
345 ESP_LOGD(TAG, "[%s] update_status result=%s", this->get_name().c_str(), result ? "true" : "false");
346}
347
348} // namespace bedjet
349} // namespace esphome
350
351#endif
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.
uint8_t status
Definition bl0942.h:8
void disable_loop()
Disable this component's loop.
void status_clear_warning()
const StringRef & get_name() const
bool update_status_()
Attempts to update the climate device from the last received BedjetStatusPacket.
BedjetTemperatureSource temperature_source_
climate::ClimateTraits traits() override
void on_bedjet_state(bool is_ready) override
void reset_state_()
Resets states to defaults.
void control(const climate::ClimateCall &call) override
std::string describe() override
void on_status(const BedjetStatusPacket *data) override
This class is used to encode all control actions on a climate device.
Definition climate.h:33
bool has_custom_fan_mode() const
Definition climate.h:112
const optional< float > & get_target_temperature() const
Definition climate.cpp:287
ClimateMode mode
The active mode of the climate device.
Definition climate.h:256
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:250
ClimateTraits get_traits()
Get the traits of this climate device with all overrides applied.
Definition climate.cpp:475
float target_temperature
The target temperature of the climate device.
Definition climate.h:237
bool set_preset_(ClimatePreset preset)
Set preset. Reset custom preset. Return true if preset has been changed.
Definition climate.cpp:678
bool set_custom_preset_(const char *preset)
Set custom preset. Reset primary preset. Return true if preset has been changed.
Definition climate.cpp:680
void clear_custom_preset_()
Clear custom preset.
Definition climate.cpp:686
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition climate.cpp:666
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:230
ClimateAction action
The active state of the climate device.
Definition climate.h:259
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:426
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:253
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition climate.cpp:350
bool set_custom_fan_mode_(const char *mode)
Set custom fan mode. Reset primary fan mode. Return true if fan mode has been changed.
Definition climate.cpp:670
const ClimatePresetMask & get_supported_presets() const
const std::vector< const char * > & get_supported_custom_fan_modes() const
const ClimateFanModeMask & get_supported_fan_modes() const
const std::vector< const char * > & get_supported_custom_presets() const
const ClimateModeMask & get_supported_modes() const
bool has_value() const
Definition optional.h:92
BedjetHeatMode
Optional heating strategies to use for climate::CLIMATE_MODE_HEAT.
@ HEAT_MODE_EXTENDED
HVACMode.HEAT is handled using BTN_EXTHT.
@ MODE_DRY
BedJet is in Dry mode (high speed, no heat)
@ MODE_EXTHT
BedJet is in Extended Heat mode (limited to 10 hours)
@ MODE_COOL
BedJet is in Cool mode (actually "Fan only" mode)
@ MODE_TURBO
BedJet is in Turbo mode (high heat, limited time)
@ MODE_HEAT
BedJet is in Heat mode (limited to 4 hours)
@ MODE_WAIT
BedJet is in "wait" mode, a step during a biorhythm program.
@ MODE_STANDBY
BedJet is Off.
float bedjet_temp_to_c(uint8_t temp)
Converts a BedJet temp step into degrees Celsius.
@ BTN_EXTHT
Enter Extended Heat mode (limited to 10 hours)
@ BTN_HEAT
Enter Heat mode (limited to 4 hours)
const LogString * climate_preset_to_string(ClimatePreset preset)
Convert the given PresetMode to a human-readable string.
ClimatePreset
Enum for all preset modes NOTE: If adding values, update ClimatePresetMask in climate_traits....
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_BOOST
Device is in boost preset.
const LogString * climate_fan_mode_to_string(ClimateFanMode fan_mode)
Convert the given ClimateFanMode to a human-readable string.
ClimateMode
Enum for all modes a climate device can be in.
@ CLIMATE_MODE_DRY
The climate device is set to dry/humidity mode.
@ CLIMATE_MODE_FAN_ONLY
The climate device only has the fan enabled, no heating or cooling is taking place.
@ CLIMATE_MODE_HEAT
The climate device is set to heat to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
const LogString * climate_mode_to_string(ClimateMode mode)
Convert the given ClimateMode to a human-readable string.
@ CLIMATE_ACTION_IDLE
The climate device is idle (monitoring climate but no action needed)
@ CLIMATE_ACTION_DRYING
The climate device is drying.
@ CLIMATE_ACTION_HEATING
The climate device is actively heating.
@ CLIMATE_ACTION_COOLING
The climate device is actively cooling.
@ CLIMATE_FAN_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_OFF
The fan mode is set to Off.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
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 target_temp_step
Target temp that the BedJet will try to heat to. See actual_temp_step.
uint8_t actual_temp_step
Actual temp of the air blown by the BedJet fan; value represents 2 * /< degrees_celsius.
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.
uint8_t ambient_temp_step
Current ambient air temp.