ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
pioneer_wt.cpp
Go to the documentation of this file.
1#include "pioneer_wt.h"
2
3#include <cinttypes>
4#include <cmath>
5#include <cstring>
6#include <iterator>
7
9#include "esphome/core/hal.h"
10#include "esphome/core/log.h"
11
13
14ESPHOME_LOG_TAG(TAG, "pioneer_wt");
15
16static constexpr uint32_t RESEND_INTERVAL_MS = 500;
17static constexpr uint8_t MAX_ATTEMPTS = 4;
18// Ask for a full status again if the AC has reported nothing for this long.
19static constexpr uint32_t STATUS_REFRESH_MS = 5 * 60 * 1000;
20// Until the first full status has arrived, ask again this often.
21static constexpr uint32_t STATUS_RETRY_MS = 30 * 1000;
22static constexpr float MIN_TEMPERATURE_C = 16.0f;
23static constexpr float MAX_TEMPERATURE_C = 31.0f;
24
25static constexpr uint8_t FAN_SPEED_COUNT = 7;
26static constexpr const char *const FAN_SPEED_NAMES[FAN_SPEED_COUNT] PROGMEM = {
27 "Speed 1", "Speed 2", "Speed 3", "Speed 4", "Speed 5", "Speed 6", "Speed 7"};
28
29// Reply to the AC's once-a-minute check, as the stock module sends it. The last body byte is the
30// WiFi signal in dBm; a fixed -48 is enough for the AC.
31static constexpr uint8_t PRESENCE_REPLY_FRAME[] PROGMEM = {0xA5, 0x01, 0x00, 0x23, 0x00, 0x00, 0x00, 0x12, 0x47,
32 0x09, 0x80, 0x0C, 0x02, 0x64, 0xFF, 0xFF, 0xFF, 0xD0};
33
34// Climate mode for each value of ATTR_MODE
35static constexpr uint8_t CLIMATE_MODES[] PROGMEM = {
38};
39
41 this->set_supported_custom_fan_modes_static(FAN_SPEED_NAMES, FAN_SPEED_COUNT);
42 this->request_status_();
43#ifdef USE_SWITCH
44 if (this->display_switch_ != nullptr)
45 this->display_switch_->restore();
46 if (this->beeper_switch_ != nullptr)
47 this->beeper_switch_->restore();
48#endif
49}
50
59 traits.set_visual_min_temperature(MIN_TEMPERATURE_C);
60 traits.set_visual_max_temperature(MAX_TEMPERATURE_C);
61 // The AC keeps its setpoint in whole degrees Fahrenheit, so half degrees Celsius would not all be kept.
64 return traits;
65}
66
69 auto handle = [this](const uint8_t *frame, size_t len) {
70 if (!checksum_valid(frame, len)) {
71 ESP_LOGD(TAG, "Dropped a frame with a wrong checksum");
72 return false;
73 }
74 this->handle_frame_(frame, len);
75 return true;
76 };
77 this->read_frames(this->reader_, handle, now, this->last_rx_);
78 // A frame still coming in may be the reply; wait for it before resending
79 if (!this->queue_.empty() && now - this->last_send_ >= RESEND_INTERVAL_MS && !this->reader_.receiving())
80 this->send_pending_();
81 // The target temperature only arrives in a full status, so it tells whether one has been received.
82 const uint32_t status_interval = std::isnan(this->target_temperature) ? STATUS_RETRY_MS : STATUS_REFRESH_MS;
83 if (now - this->last_status_ >= status_interval)
84 this->request_status_();
85}
86
87void PioneerWT::handle_frame_(const uint8_t *frame, size_t len) {
88 const uint8_t flags = frame[2];
89 const uint8_t type = frame[3];
90 const uint8_t seq = frame[4];
91 const uint8_t ack = frame[5];
92 const uint8_t *payload = frame + HEADER_SIZE;
93 const size_t payload_len = len - HEADER_SIZE;
94 ESP_LOGVV_HEX(TAG, frame, len, MAX_FRAME_SIZE, "RX %s");
95
96 if (type == TYPE_REPLY) {
97 if (!this->queue_.empty() && this->queue_.front().seq == ack) {
98 if (this->queue_.front().power != POWER_UNCHANGED)
99 this->acked_power_ = this->queue_.front().power;
100 // Pop first: the warning log can run automations that queue another command.
101 this->pop_command_();
102 this->status_clear_warning();
103 this->last_send_ = 0; // send the next one right away
104 }
105 return;
106 }
107 if (type != TYPE_DATA || payload_len < 2)
108 return;
109
110 const uint8_t msg_class = payload[0];
111 if (flags & FLAG_WANT_ACK)
112 this->write_frame_(FLAG_WANT_ACK, TYPE_REPLY, 0, seq, CLASS_REPLY, msg_class, nullptr, 0);
113
114 if (msg_class != CLASS_STATUS)
115 return;
116 if (payload_len == 2) {
117 if (!(flags & FLAG_WANT_ACK))
118 this->write_array_progmem(PRESENCE_REPLY_FRAME, sizeof(PRESENCE_REPLY_FRAME));
119 return;
120 }
121 this->handle_status_(payload + 2, payload_len - 2);
122 // Reports count only once a full status has arrived, so the boot request is repeated until one does.
123 if (!std::isnan(this->target_temperature))
125}
126
127void PioneerWT::handle_status_(const uint8_t *body, size_t len) {
128 AttributeReader attrs(body, len);
129 bool changed = false;
130 while (attrs.next()) {
131 const uint32_t v = attrs.value();
132 switch (attrs.id()) {
133 case ATTR_POWER:
134 this->power_ = v != 0;
136 break;
137 case ATTR_MODE:
138 if (v < std::size(CLIMATE_MODES)) {
139 this->ac_mode_ = v;
140 } else {
141 ESP_LOGD(TAG, "Unknown mode %" PRIu32, v);
142 }
143 break;
144 case ATTR_SETPOINT_F: {
145 const float target = fahrenheit_to_celsius(v);
146 changed |= target != this->target_temperature;
147 this->target_temperature = target;
148 break;
149 }
150 case ATTR_INDOOR_TEMP_C100: {
151 const float current = static_cast<float>(v) / 100.0f;
152 changed |= current != this->current_temperature;
153 this->current_temperature = current;
154 break;
155 }
156 case ATTR_FAN_SPEED:
157 if (v == 0) {
159 } else if (v <= FAN_SPEED_COUNT) {
160 changed |= this->set_custom_fan_mode_(FAN_SPEED_NAMES[v - 1]);
161 } else {
162 ESP_LOGD(TAG, "Unknown fan speed %" PRIu32, v);
163 }
164 break;
165 case ATTR_SWING_VERTICAL: {
166 const auto swing = v == SWING_ON ? climate::CLIMATE_SWING_VERTICAL : climate::CLIMATE_SWING_OFF;
167 changed |= swing != this->swing_mode;
168 this->swing_mode = swing;
169 break;
170 }
171 case ATTR_ECO:
173 break;
174#ifdef USE_SWITCH
175 case ATTR_DISPLAY:
176 PUBLISH_STATE(this->display_switch_, v != 0);
177 break;
178 case ATTR_BEEPER:
179 PUBLISH_STATE(this->beeper_switch_, v != 0);
180 break;
181#endif
182 default:
183 break;
184 }
185 }
186 if (attrs.stopped_on_unknown()) {
187 ESP_LOGD(TAG, "Status attribute %04X has an unknown size; ignored the rest of the frame", attrs.id());
188 }
189
190 const auto mode = this->power_ ? static_cast<climate::ClimateMode>(progmem_read_byte(&CLIMATE_MODES[this->ac_mode_]))
192 changed |= mode != this->mode;
193 this->mode = mode;
194 if (changed)
195 this->publish_state();
196}
197
199 uint8_t body[MAX_BODY_SIZE];
200 uint8_t *p = body;
201 int8_t power = POWER_UNCHANGED;
202
203 if (call.get_mode().has_value()) {
204 const auto mode = *call.get_mode();
206 power = 0;
207 p = put_attr(p, ATTR_POWER, 0);
208 } else {
209 uint8_t ac_mode = 0;
210 for (size_t i = 0; i < std::size(CLIMATE_MODES); i++) {
211 if (progmem_read_byte(&CLIMATE_MODES[i]) == mode) {
212 ac_mode = i;
213 break;
214 }
215 }
216 // Queued commands count: an "off" still waiting to be sent is followed by a power on.
217 if (!this->wanted_power_(this->queue_.size() == TX_QUEUE_SIZE)) {
218 power = 1;
219 p = put_attr(p, ATTR_POWER, 1);
220 }
221 p = put_attr(p, ATTR_MODE, ac_mode);
222 }
223 }
224
225 if (call.get_target_temperature().has_value()) {
226 // Automations can ask for any value; the visual range is not enforced for them.
227 const float celsius = clamp(*call.get_target_temperature(), MIN_TEMPERATURE_C, MAX_TEMPERATURE_C);
228 p = put_be32(put_be16(p, ATTR_SETPOINT_F), lroundf(celsius_to_fahrenheit(celsius)));
229 }
230
231 // The app always sends fan speed with these companions; match it exactly.
232 int fan_speed = -1;
233 if (call.get_fan_mode().has_value() && *call.get_fan_mode() == climate::CLIMATE_FAN_AUTO) {
234 fan_speed = 0;
235 } else if (call.has_custom_fan_mode()) {
236 // The call holds the pointer from FAN_SPEED_NAMES, so the pointers can be compared.
237 const char *name = call.get_custom_fan_mode().c_str();
238 for (uint8_t i = 0; i < FAN_SPEED_COUNT; i++) {
239 if (name == FAN_SPEED_NAMES[i]) {
240 fan_speed = i + 1;
241 break;
242 }
243 }
244 }
245 if (fan_speed >= 0) {
246 p = put_attr(p, ATTR_FAN_EXTRA_1, 0);
247 p = put_attr(p, ATTR_FAN_EXTRA_2, 0);
248 p = put_attr(p, ATTR_FAN_SPEED, fan_speed);
249 p = put_attr(p, ATTR_FAN_AUTO, fan_speed == 0);
250 }
251
252 if (call.get_swing_mode().has_value()) {
253 const bool swing = *call.get_swing_mode() == climate::CLIMATE_SWING_VERTICAL;
254 p = put_attr(p, ATTR_SWING_VERTICAL, swing ? SWING_ON : SWING_OFF);
255 }
256
257 if (call.get_preset().has_value())
258 p = put_attr(p, ATTR_ECO, *call.get_preset() == climate::CLIMATE_PRESET_ECO);
259
260 if (p != body)
261 this->queue_command_(CLASS_COMMAND, body, p - body, power);
262 // State is published when the AC reports the new values back.
263}
264
265void PioneerWT::send_setting_(uint16_t attr, bool on) {
266 uint8_t body[3];
267 put_attr(body, attr, on);
268 this->queue_command_(CLASS_COMMAND, body, sizeof(body));
269}
270
272 // Asks the AC to send every setting, as the stock module does at boot.
273 static constexpr uint8_t REQUEST_ALL[] = {0xFF, 0xFF};
274 this->queue_command_(CLASS_NETWORK, REQUEST_ALL, sizeof(REQUEST_ALL));
276}
277
278void PioneerWT::queue_command_(uint8_t msg_class, const uint8_t *body, size_t len, int8_t power) {
279 if (this->queue_.size() == TX_QUEUE_SIZE) {
280 ESP_LOGW(TAG, "Command queue full; dropping the oldest command");
281 this->pop_command_();
282 }
283 PendingCommand pending{msg_class, this->next_seq_++, static_cast<uint8_t>(len), power, {}};
284 std::memcpy(pending.body.data(), body, len);
285 this->queue_.push(pending);
286 if (this->next_seq_ == 0)
287 this->next_seq_ = 1;
288}
289
291 this->queue_.pop();
292 this->attempts_ = 0;
293}
294
295bool PioneerWT::wanted_power_(bool skip_head) const {
296 // An acknowledged power change counts until the AC reports the power
297 bool power = this->acked_power_ == POWER_UNCHANGED ? this->power_ : this->acked_power_;
298 for (const PendingCommand &pending : this->queue_) {
299 if (skip_head) {
300 skip_head = false;
301 continue;
302 }
303 if (pending.power != POWER_UNCHANGED)
304 power = pending.power;
305 }
306 return power;
307}
308
310 const PendingCommand &pending = this->queue_.front();
311 if (this->attempts_ >= MAX_ATTEMPTS) {
312 const uint8_t seq = pending.seq;
313 // Pop first: the warning log can run automations that queue another command.
314 this->pop_command_();
315 ESP_LOGD(TAG, "No acknowledgement for command seq %02X; giving up", seq);
316 this->status_set_warning(LOG_STR("No acknowledgement"));
317 return;
318 }
319 this->attempts_++;
320 this->write_frame_(FLAG_WANT_ACK, TYPE_DATA, pending.seq, 0, pending.msg_class, pending.msg_class,
321 pending.body.data(), pending.len);
323}
324
325void PioneerWT::write_frame_(uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1, uint8_t class_2,
326 const uint8_t *body, size_t len) {
327 uint8_t frame[HEADER_SIZE + 2 + MAX_BODY_SIZE];
328 const size_t total = build_frame(frame, flags, type, seq, ack, class_1, class_2, body, len);
329 ESP_LOGVV_HEX(TAG, frame, total, sizeof(frame), "TX %s");
330 // Kept bytes of an earlier frame must not merge into the reply
331 this->reader_.drop_kept();
332 this->write_array(frame, total);
333}
334
336 LOG_CLIMATE("", "Pioneer WT", this);
337#ifdef USE_SWITCH
338 LOG_SWITCH(" ", "Display", this->display_switch_);
339 LOG_SWITCH(" ", "Beeper", this->beeper_switch_);
340#endif
341}
342
343} // namespace esphome::pioneer_wt
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void status_clear_warning()
Definition component.h:318
void drop_kept()
Forget a partial frame an idle flush kept, before a request starts a new exchange; safe inside a fram...
This class is used to encode all control actions on a climate device.
Definition climate.h:31
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
float target_temperature
The target temperature of the climate device.
Definition climate.h:298
ClimateSwingMode swing_mode
The active swing mode of the climate device.
Definition climate.h:323
virtual ClimateTraits traits()=0
Get the default traits of this climate device.
bool set_preset_(ClimatePreset preset)
Set preset. Reset custom preset. Return true if preset has been changed.
Definition climate.cpp:674
bool set_fan_mode_(ClimateFanMode mode)
Set fan mode. Reset custom fan mode. Return true if fan mode has been changed.
Definition climate.cpp:663
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition climate.h:291
void set_supported_custom_fan_modes_static(const char *const *modes, size_t count)
Points at a static table that outlives the climate (codegen); call before any runtime copy.
Definition climate.h:265
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
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.h:339
void set_visual_max_temperature(float visual_max_temperature)
void add_feature_flags(uint32_t feature_flags)
void set_visual_target_temperature_step(float temperature_step)
void set_supported_presets(ClimatePresetMask presets)
void set_supported_swing_modes(ClimateSwingModeMask modes)
void set_visual_min_temperature(float visual_min_temperature)
void set_visual_current_temperature_step(float temperature_step)
void set_supported_modes(ClimateModeMask modes)
void set_supported_fan_modes(ClimateFanModeMask modes)
Walks the attribute list in a status body. Stops at the first ID whose size is unknown.
bool stopped_on_unknown() const
True when the walk stopped early on an ID of unknown size.
bool next()
Moves to the next attribute. Returns false at the end of the body or at an unknown ID.
void write_frame_(uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1, uint8_t class_2, const uint8_t *body, size_t len)
StaticRingBuffer< PendingCommand, TX_QUEUE_SIZE > queue_
Definition pioneer_wt.h:73
void send_setting_(uint16_t attr, bool on)
Queues a command that sets one on/off attribute.
static constexpr int8_t POWER_UNCHANGED
Definition pioneer_wt.h:57
SUB_SWITCH(display) SUB_SWITCH(beeper) protected void control(const climate::ClimateCall &call) override
void handle_status_(const uint8_t *body, size_t len)
void handle_frame_(const uint8_t *frame, size_t len)
bool wanted_power_(bool skip_head) const
Power once the queued commands are applied; skip_head leaves out a head about to be dropped.
void pop_command_()
Removes the command at the head of the queue.
void queue_command_(uint8_t msg_class, const uint8_t *body, size_t len, int8_t power=POWER_UNCHANGED)
Queues a data frame that the AC must acknowledge; it is resent until it is.
void write_array_progmem(const uint8_t *data, size_t len)
Definition uart.h:31
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
bool read_frames(R &reader, F &&handle, uint32_t now, uint32_t &last_rx, uint32_t gap_ms=250)
Feed the waiting bytes to a LengthFrameReader; on a pass with none, gap_ms after last_rx,...
Definition uart.h:96
uint16_t flags
uint8_t type
@ CLIMATE_SUPPORTS_CURRENT_TEMPERATURE
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_ECO
Device is running an energy-saving preset.
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
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_COOL
The climate device is set to cool to reach the target temperature.
@ CLIMATE_MODE_HEAT_COOL
The climate device is set to heat/cool to reach the target temperature.
@ CLIMATE_MODE_OFF
The climate device is off.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
ESPHOME_LOG_TAG(TAG, "pioneer_wt")
uint8_t * put_attr(uint8_t *p, uint16_t id, uint8_t value)
Writes a one-byte attribute (2-byte ID, then the value) at p. Returns the new end.
bool checksum_valid(const uint8_t *frame, size_t len)
size_t build_frame(uint8_t *out, uint8_t flags, uint8_t type, uint8_t seq, uint8_t ack, uint8_t class_1, uint8_t class_2, const uint8_t *body, size_t len)
Builds a frame into out, which must hold HEADER_SIZE + 2 + len bytes. Returns the frame length.
const void size_t len
Definition hal.h:68
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1296
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition helpers.h:2110
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition helpers.h:2112
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint8_t * put_be32(uint8_t *dst, uint32_t value)
Definition helpers.h:1307
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t
std::array< uint8_t, MAX_BODY_SIZE > body
Definition pioneer_wt.h:65
uint16_t seq
uint8_t ack
spi_device_handle_t handle