ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
panasonic_ir.cpp
Go to the documentation of this file.
1#include "panasonic_ir.h"
2
3#include <cmath>
4
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
8
9namespace esphome::panasonic_ir {
10
11ESPHOME_LOG_TAG(TAG, "panasonic_ir.climate");
12
13// How long after sending a frame an identical one is taken for its echo
14static constexpr uint32_t ECHO_WINDOW_MS = 1000;
15
16// In the order of the Model enum
17PROGMEM_STRING_TABLE(ModelStrings, "DKE", "JKE", "LKE", "NKE", "CKP", "RKR", "PKE");
18
20 climate_ir::ClimateIR::setup();
21 // The restored mode is the best guess of whether the unit is on
23 this->sync_vane_select_();
24}
25
27 climate_ir::ClimateIR::dump_config();
28 ESP_LOGCONFIG(TAG, " Model: %s",
29 LOG_STR_ARG(ModelStrings::get_log_str(static_cast<uint8_t>(this->model_), ModelStrings::LAST_INDEX)));
30}
31
33 if (index > VANE_V_LOWEST) {
34 ESP_LOGW(TAG, "Vane position %u out of range", static_cast<unsigned>(index));
35 return;
36 }
37 // A vane position takes the unit out of frost protection, as a swing change does
39 // The select only offers 0 to 5
40 if (index == 0) {
42 } else {
43 this->vane_position_ = index;
45 }
46 this->transmit_state();
47 this->publish_state();
48}
49
52 return VANE_V_AUTO;
53 if (this->vane_position_ != 0)
54 return this->vane_position_;
55 // Blow down to heat from the bottom of the room, and up to cool from the top
56 return this->mode == climate::CLIMATE_MODE_HEAT ? VANE_V_LOWEST : VANE_V_HIGHEST;
57}
58
60#ifdef USE_SELECT
61 if (this->vane_select_ == nullptr)
62 return;
63 const uint8_t vane = this->vane_v_();
64 const size_t index = vane == VANE_V_AUTO ? 0 : vane;
65 if (this->vane_select_->active_index().value_or(SIZE_MAX) != index)
66 this->vane_select_->publish_state(index);
67#endif
68}
69
71 // The base class only knows about the regular fan modes
72 if (call.has_custom_fan_mode()) {
74 } else if (call.get_fan_mode().has_value()) {
76 }
77
78 // Frost protection is a mode of its own on the remote. Changing anything else takes the unit out of it
79 const bool in_maintenance = this->preset.value_or(climate::CLIMATE_PRESET_NONE) == climate::CLIMATE_PRESET_AWAY;
80 if (call.get_preset().has_value()) {
81 // Choosing frost protection turns the unit on. transmit_state() sets the rest
82 if (*call.get_preset() == climate::CLIMATE_PRESET_AWAY)
84 } else if (in_maintenance &&
85 (call.get_mode().has_value() || call.get_target_temperature().has_value() ||
86 call.get_fan_mode().has_value() || call.has_custom_fan_mode() || call.get_swing_mode().has_value())) {
88 }
90}
91
93 if (this->has_custom_fan_mode()) {
94 const auto custom_fan_mode = this->get_custom_fan_mode();
95 if (custom_fan_mode == FAN_MODE_MINIMUM)
96 return FAN_SPEED_MIN;
97 if (custom_fan_mode == FAN_MODE_MAXIMUM)
98 return FAN_SPEED_MAX;
99 }
100 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO)) {
102 return FAN_SPEED_LOW;
104 return FAN_SPEED_MEDIUM;
106 return FAN_SPEED_HIGH;
107 default:
108 return FAN_SPEED_AUTO;
109 }
110}
111
113 // Frost protection is sent as the remote does, whatever else a call combined with the preset set, and the entity
114 // shows what is sent
115 const bool maintenance = this->mode != climate::CLIMATE_MODE_OFF &&
117 if (maintenance) {
119 this->set_custom_fan_mode_(FAN_MODE_MAXIMUM);
121 }
122
123 FrameParams params;
124 params.model = this->model_;
125
126 bool power = true;
127 switch (this->mode) {
129 params.mode = Mode::AUTO;
130 break;
132 params.mode = Mode::DRY;
133 break;
135 params.mode = Mode::COOL;
136 break;
138 params.mode = Mode::HEAT;
139 break;
141 params.mode = Mode::FAN;
142 break;
144 default:
145 power = false;
146 params.mode = this->last_mode_;
147 break;
148 }
149 if (power)
150 this->last_mode_ = params.mode;
151
152 if (this->model_ == Model::CKP) {
153 // No declarative power on CKP. The power bit toggles the unit, so only set it when the state changes
154 params.power = (power != this->ckp_is_on_);
155 this->ckp_is_on_ = power;
156 } else {
157 params.power = power;
158 }
159
160 params.temperature = maintenance
161 ? TEMP_MAINTENANCE
162 : static_cast<uint8_t>(lroundf(clamp<float>(this->target_temperature, TEMP_MIN, TEMP_MAX)));
163 params.fan_speed = this->fan_speed_();
164 params.vane_v = this->vane_v_();
165
166 uint8_t state[STATE_LENGTH];
167 encode_frame(params, state);
168 memcpy(this->echo_, state + SECTION1_LENGTH, SECTION2_LENGTH);
169 this->echo_sent_ms_ = millis();
170
171 auto transmit = this->transmitter_->transmit();
172 auto *data = transmit.get_data();
173 data->set_carrier_frequency(CARRIER_FREQUENCY);
174 // Two headers (mark + space) and footer marks, 16 timings per byte, and the gap
175 data->reserve(2 * 3 + STATE_LENGTH * 16 + 1);
176
177 data->item(HEADER_MARK, HEADER_SPACE);
178 data->item_bytes<BIT_TIMINGS>(state, SECTION1_LENGTH);
179 data->item(BIT_MARK, SECTION_GAP);
180 data->item(HEADER_MARK, HEADER_SPACE);
181 data->item_bytes<BIT_TIMINGS>(state + SECTION1_LENGTH, SECTION2_LENGTH);
182 data->mark(BIT_MARK);
183
184 transmit.perform();
185 this->sync_vane_select_();
186}
187
189 uint8_t state[STATE_LENGTH];
190 if (!read_state(data, state))
191 return false;
192
193 FrameParams params;
194 params.model = this->model_;
195 if (!decode_frame(state, params)) {
196 ESP_LOGD(TAG, "Received frame is not a valid Panasonic state");
197 return false;
198 }
199
200 // A receiver near the transmitter hears our own frame. Applying it would flip the CKP power toggle back and store
201 // the vane position derived from the mode as one the user chose.
202 if (millis() - this->echo_sent_ms_ < ECHO_WINDOW_MS &&
203 memcmp(state + SECTION1_LENGTH, this->echo_, SECTION2_LENGTH) == 0) {
204 ESP_LOGD(TAG, "Ignoring the echo of our own frame");
205 this->echo_sent_ms_ -= ECHO_WINDOW_MS; // only the first echo
206 return true;
207 }
208
209 this->apply_received_(params);
210 return true;
211}
212
214 bool power = params.power;
215 if (this->model_ == Model::CKP) {
216 // The power bit is only a toggle
217 if (params.power)
218 this->ckp_is_on_ = !this->ckp_is_on_;
219 power = this->ckp_is_on_;
220 }
221
222 this->last_mode_ = params.mode;
223 if (!power) {
225 } else {
226 switch (params.mode) {
227 case Mode::DRY:
229 break;
230 case Mode::COOL:
232 break;
233 case Mode::HEAT:
235 break;
236 case Mode::FAN:
238 break;
239 case Mode::AUTO:
240 default:
242 break;
243 }
244 }
245
246 // The frost protection frame holds 8 C or 10 C, which is not a set-point of the normal range. Keep the one we had
247 const bool maintenance = params.temperature < TEMP_MIN;
249 // Fan only mode always sends a fixed temperature, so it says nothing about the set-point either
250 if (params.mode != Mode::FAN && !maintenance)
251 this->target_temperature = params.temperature;
252
253 switch (params.fan_speed) {
254 case FAN_SPEED_MIN:
255 this->set_custom_fan_mode_(FAN_MODE_MINIMUM);
256 break;
257 case FAN_SPEED_LOW:
259 break;
260 case FAN_SPEED_MEDIUM:
262 break;
263 case FAN_SPEED_HIGH:
265 break;
266 case FAN_SPEED_MAX:
267 this->set_custom_fan_mode_(FAN_MODE_MAXIMUM);
268 break;
269 case FAN_SPEED_AUTO:
270 default:
272 break;
273 }
274
275 // Any other vane value is not one the remote sends, so it leaves the vane as it was
276 if (params.vane_v == VANE_V_AUTO) {
278 } else if (params.vane_v >= VANE_V_HIGHEST && params.vane_v <= VANE_V_LOWEST) {
280 this->vane_position_ = params.vane_v;
281 } else {
282 ESP_LOGV(TAG, "Ignoring vane value %u", params.vane_v);
283 }
284 this->sync_vane_select_();
285
286 this->publish_state();
287}
288
289} // namespace esphome::panasonic_ir
This class is used to encode all control actions on a climate device.
Definition climate.h:31
const optional< ClimatePreset > & get_preset() const
Definition climate.cpp:318
StringRef get_custom_fan_mode() const
Definition climate.h:124
ClimateMode mode
The active mode of the climate device.
Definition climate.h:317
optional< ClimateFanMode > fan_mode
The active fan mode of the climate device.
Definition climate.h:311
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
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
void clear_custom_fan_mode_()
Clear custom fan mode.
Definition climate.cpp:672
virtual void control(const ClimateCall &call)=0
Control the climate device, this is a virtual method that each climate integration must implement.
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition climate.cpp:432
bool has_custom_fan_mode() const
Check if a custom fan mode is currently active.
Definition climate.h:285
optional< ClimatePreset > preset
The active preset of the climate device.
Definition climate.h:314
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
StringRef get_custom_fan_mode() const
Get the active custom fan mode (read-only access). Returns StringRef.
Definition climate.h:326
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
void apply_received_(const FrameParams &params)
bool on_receive(remote_base::RemoteReceiveData data) override
Handle received IR buffer.
void set_vane_position(size_t index)
Set the vertical vane from the select: 0 is auto (swing vertical), 1 (highest) to 5 (lowest) are fixe...
SUB_SELECT(vane) protected void transmit_state() override
Transmit via IR the state of this climate controller.
uint8_t custom_fan_mode
Definition climate.h:4
bool state
Definition fan.h:2
@ CLIMATE_PRESET_NONE
No preset is active.
@ CLIMATE_PRESET_AWAY
Device is in away preset.
@ CLIMATE_SWING_OFF
The swing mode is set to Off.
@ CLIMATE_SWING_VERTICAL
The fan mode is set to Vertical.
@ 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_MEDIUM
The fan mode is set to Medium.
@ CLIMATE_FAN_AUTO
The fan mode is set to Auto.
@ CLIMATE_FAN_LOW
The fan mode is set to Low.
@ CLIMATE_FAN_HIGH
The fan mode is set to High.
void encode_frame(const FrameParams &p, uint8_t *state)
Encodes p as is: the temperature is not clamped, except that fan only mode always sends 27 C.
bool decode_frame(const uint8_t *state, FrameParams &p)
Decode a state. Returns false if the state doesn't look like a valid Panasonic A/C state.
ESPHOME_LOG_TAG(TAG, "panasonic_ir.climate")
PROGMEM_STRING_TABLE(ModelStrings, "DKE", "JKE", "LKE", "NKE", "CKP", "RKR", "PKE")
bool read_state(remote_base::RemoteReceiveData &data, uint8_t *state)
Reads a state from a received capture.
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t