ESPHome 2026.12.0-dev
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", LOG_STR_ARG(ModelStrings::get_log_str(this->model_)));
29}
30
32 if (index > VANE_V_LOWEST) {
33 ESP_LOGW(TAG, "Vane position %u out of range", static_cast<unsigned>(index));
34 return;
35 }
36 // A vane position takes the unit out of frost protection, as a swing change does
38 // The select only offers 0 to 5
39 if (index == 0) {
41 } else {
42 this->vane_position_ = index;
44 }
45 this->transmit_state();
46 this->publish_state();
47}
48
51 return VANE_V_AUTO;
52 if (this->vane_position_ != 0)
53 return this->vane_position_;
54 // Blow down to heat from the bottom of the room, and up to cool from the top
55 return this->mode == climate::CLIMATE_MODE_HEAT ? VANE_V_LOWEST : VANE_V_HIGHEST;
56}
57
59#ifdef USE_SELECT
60 if (this->vane_select_ == nullptr)
61 return;
62 const uint8_t vane = this->vane_v_();
63 const size_t index = vane == VANE_V_AUTO ? 0 : vane;
64 if (this->vane_select_->active_index().value_or(SIZE_MAX) != index)
65 this->vane_select_->publish_state(index);
66#endif
67}
68
70 // The base class only knows about the regular fan modes
71 if (call.has_custom_fan_mode()) {
73 } else if (call.get_fan_mode().has_value()) {
75 }
76
77 // Frost protection is a mode of its own on the remote. Changing anything else takes the unit out of it
78 const bool in_maintenance = this->preset.value_or(climate::CLIMATE_PRESET_NONE) == climate::CLIMATE_PRESET_AWAY;
79 if (call.get_preset().has_value()) {
80 // Choosing frost protection turns the unit on. transmit_state() sets the rest
81 if (*call.get_preset() == climate::CLIMATE_PRESET_AWAY)
83 } else if (in_maintenance &&
84 (call.get_mode().has_value() || call.get_target_temperature().has_value() ||
85 call.get_fan_mode().has_value() || call.has_custom_fan_mode() || call.get_swing_mode().has_value())) {
87 }
89}
90
92 if (this->has_custom_fan_mode()) {
93 const auto custom_fan_mode = this->get_custom_fan_mode();
94 if (custom_fan_mode == FAN_MODE_MINIMUM)
95 return FAN_SPEED_MIN;
96 if (custom_fan_mode == FAN_MODE_MAXIMUM)
97 return FAN_SPEED_MAX;
98 }
99 switch (this->fan_mode.value_or(climate::CLIMATE_FAN_AUTO)) {
101 return FAN_SPEED_LOW;
103 return FAN_SPEED_MEDIUM;
105 return FAN_SPEED_HIGH;
106 default:
107 return FAN_SPEED_AUTO;
108 }
109}
110
112 // Frost protection is sent as the remote does, whatever else a call combined with the preset set, and the entity
113 // shows what is sent
114 const bool maintenance = this->mode != climate::CLIMATE_MODE_OFF &&
116 if (maintenance) {
118 this->set_custom_fan_mode_(FAN_MODE_MAXIMUM);
120 }
121
122 FrameParams params;
123 params.model = this->model_;
124
125 bool power = true;
126 switch (this->mode) {
128 params.mode = Mode::AUTO;
129 break;
131 params.mode = Mode::DRY;
132 break;
134 params.mode = Mode::COOL;
135 break;
137 params.mode = Mode::HEAT;
138 break;
140 params.mode = Mode::FAN;
141 break;
143 default:
144 power = false;
145 params.mode = this->last_mode_;
146 break;
147 }
148 if (power)
149 this->last_mode_ = params.mode;
150
151 if (this->model_ == Model::CKP) {
152 // No declarative power on CKP. The power bit toggles the unit, so only set it when the state changes
153 params.power = (power != this->ckp_is_on_);
154 this->ckp_is_on_ = power;
155 } else {
156 params.power = power;
157 }
158
159 params.temperature = maintenance
160 ? TEMP_MAINTENANCE
161 : static_cast<uint8_t>(lroundf(clamp<float>(this->target_temperature, TEMP_MIN, TEMP_MAX)));
162 params.fan_speed = this->fan_speed_();
163 params.vane_v = this->vane_v_();
164
165 uint8_t state[STATE_LENGTH];
166 encode_frame(params, state);
167 memcpy(this->echo_, state + SECTION1_LENGTH, SECTION2_LENGTH);
168 this->echo_sent_ms_ = millis();
169
170 auto transmit = this->transmitter_->transmit();
171 auto *data = transmit.get_data();
172 data->set_carrier_frequency(CARRIER_FREQUENCY);
173 // Two headers (mark + space) and footer marks, 16 timings per byte, and the gap
174 data->reserve(2 * 3 + STATE_LENGTH * 16 + 1);
175
176 data->item(HEADER_MARK, HEADER_SPACE);
177 data->item_bytes<BIT_TIMINGS>(state, SECTION1_LENGTH);
178 data->item(BIT_MARK, SECTION_GAP);
179 data->item(HEADER_MARK, HEADER_SPACE);
180 data->item_bytes<BIT_TIMINGS>(state + SECTION1_LENGTH, SECTION2_LENGTH);
181 data->mark(BIT_MARK);
182
183 transmit.perform();
184 this->sync_vane_select_();
185}
186
188 uint8_t state[STATE_LENGTH];
189 if (!read_state(data, state))
190 return false;
191
192 FrameParams params;
193 params.model = this->model_;
194 if (!decode_frame(state, params)) {
195 ESP_LOGD(TAG, "Received frame is not a valid Panasonic state");
196 return false;
197 }
198
199 // A receiver near the transmitter hears our own frame. Applying it would flip the CKP power toggle back and store
200 // the vane position derived from the mode as one the user chose.
201 if (millis() - this->echo_sent_ms_ < ECHO_WINDOW_MS &&
202 memcmp(state + SECTION1_LENGTH, this->echo_, SECTION2_LENGTH) == 0) {
203 ESP_LOGD(TAG, "Ignoring the echo of our own frame");
204 this->echo_sent_ms_ -= ECHO_WINDOW_MS; // only the first echo
205 return true;
206 }
207
208 this->apply_received_(params);
209 return true;
210}
211
213 bool power = params.power;
214 if (this->model_ == Model::CKP) {
215 // The power bit is only a toggle
216 if (params.power)
217 this->ckp_is_on_ = !this->ckp_is_on_;
218 power = this->ckp_is_on_;
219 }
220
221 this->last_mode_ = params.mode;
222 if (!power) {
224 } else {
225 switch (params.mode) {
226 case Mode::DRY:
228 break;
229 case Mode::COOL:
231 break;
232 case Mode::HEAT:
234 break;
235 case Mode::FAN:
237 break;
238 case Mode::AUTO:
239 default:
241 break;
242 }
243 }
244
245 // 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
246 const bool maintenance = params.temperature < TEMP_MIN;
248 // Fan only mode always sends a fixed temperature, so it says nothing about the set-point either
249 if (params.mode != Mode::FAN && !maintenance)
250 this->target_temperature = params.temperature;
251
252 switch (params.fan_speed) {
253 case FAN_SPEED_MIN:
254 this->set_custom_fan_mode_(FAN_MODE_MINIMUM);
255 break;
256 case FAN_SPEED_LOW:
258 break;
259 case FAN_SPEED_MEDIUM:
261 break;
262 case FAN_SPEED_HIGH:
264 break;
265 case FAN_SPEED_MAX:
266 this->set_custom_fan_mode_(FAN_MODE_MAXIMUM);
267 break;
268 case FAN_SPEED_AUTO:
269 default:
271 break;
272 }
273
274 // Any other vane value is not one the remote sends, so it leaves the vane as it was
275 if (params.vane_v == VANE_V_AUTO) {
277 } else if (params.vane_v >= VANE_V_HIGHEST && params.vane_v <= VANE_V_LOWEST) {
279 this->vane_position_ = params.vane_v;
280 } else {
281 ESP_LOGV(TAG, "Ignoring vane value %u", params.vane_v);
282 }
283 this->sync_vane_select_();
284
285 this->publish_state();
286}
287
288} // 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