ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
pipsolar.cpp
Go to the documentation of this file.
1#include "pipsolar.h"
3#include "esphome/core/log.h"
4
5namespace esphome::pipsolar {
6
7ESPHOME_LOG_TAG(TAG, "pipsolar");
8
9// value_or(NAN) on an integer optional converts NaN to int, which is undefined
10static float int_or_nan(optional<int32_t> value) { return value.has_value() ? static_cast<float>(*value) : NAN; }
11
12void Pipsolar::setup() {
13 this->state_ = STATE_IDLE;
14 this->command_start_millis_ = 0;
15}
16
17void Pipsolar::loop() {
18 // Read message
19 if (this->state_ == STATE_IDLE) {
20 this->discard_available();
21
22 if (this->send_next_command_()) {
23 // command sent
24 return;
25 }
26
27 if (this->send_next_poll_()) {
28 // poll sent
29 return;
30 }
31
32 return;
33 }
34 if (this->state_ == STATE_COMMAND_COMPLETE) {
35 if (this->check_incoming_length_(4)) {
36 if (this->check_incoming_crc_()) {
37 // crc ok
38 if (this->read_buffer_[1] == 'A' && this->read_buffer_[2] == 'C' && this->read_buffer_[3] == 'K') {
39 ESP_LOGD(TAG, "command successful");
40 } else {
41 ESP_LOGD(TAG, "command not successful");
42 }
43 this->command_queue_[this->command_queue_position_] = std::string("");
45 this->state_ = STATE_IDLE;
46 } else {
47 // crc failed
48 // no log message necessary, check_incoming_crc_() logs
49 this->command_queue_[this->command_queue_position_] = std::string("");
51 this->state_ = STATE_IDLE;
52 }
53 } else {
54 ESP_LOGD(TAG, "command %s response length not OK: with length %zu",
55 this->command_queue_[this->command_queue_position_].c_str(), this->read_pos_);
56 this->command_queue_[this->command_queue_position_] = std::string("");
58 this->state_ = STATE_IDLE;
59 }
60 }
61
62 if (this->state_ == STATE_POLL_CHECKED) {
63 ESP_LOGD(TAG, "poll %s decode", this->enabled_polling_commands_[this->last_polling_command_].command);
65 (const char *) this->read_buffer_);
66 this->state_ = STATE_IDLE;
67 return;
68 }
69
70 if (this->state_ == STATE_POLL_COMPLETE) {
71 if (this->check_incoming_crc_()) {
72 if (this->read_buffer_[0] == '(' && this->read_buffer_[1] == 'N' && this->read_buffer_[2] == 'A' &&
73 this->read_buffer_[3] == 'K') {
74 ESP_LOGD(TAG, "poll %s NACK", this->enabled_polling_commands_[this->last_polling_command_].command);
76 this->state_ = STATE_IDLE;
77 return;
78 }
79 // crc ok
82 return;
83 } else {
84 // crc failed
85 // no log message necessary, check_incoming_crc_() logs
87 this->state_ = STATE_IDLE;
88 }
89 }
90
91 if (this->state_ == STATE_COMMAND || this->state_ == STATE_POLL) {
92 this->read_available([this](uint8_t byte) {
93 // make sure data and null terminator fit in buffer
94 if (this->read_pos_ >= PIPSOLAR_READ_BUFFER_LENGTH - 1) {
95 this->read_pos_ = 0;
96 this->discard_available();
97 ESP_LOGW(TAG, "response data too long, discarding.");
98 return false;
99 }
100 this->read_buffer_[this->read_pos_] = byte;
101 this->read_pos_++;
102
103 // end of answer
104 if (byte == 0x0D) {
105 this->read_buffer_[this->read_pos_] = 0;
106 this->discard_available();
107 if (this->state_ == STATE_POLL) {
109 }
110 if (this->state_ == STATE_COMMAND) {
112 }
113 return false;
114 }
115 return true;
116 });
117 }
118 if (this->state_ == STATE_COMMAND) {
120 // command timeout
121 const char *command = this->command_queue_[this->command_queue_position_].c_str();
123 ESP_LOGD(TAG, "command %s timeout", command);
124 this->command_queue_[this->command_queue_position_] = std::string("");
126 this->state_ = STATE_IDLE;
127 return;
128 }
129 }
130 if (this->state_ == STATE_POLL) {
132 // command timeout
133 ESP_LOGD(TAG, "poll %s timeout", this->enabled_polling_commands_[this->last_polling_command_].command);
135 this->state_ = STATE_IDLE;
136 }
137 }
138}
139
141 if (this->read_pos_ >= 3 && this->read_pos_ - 3 == length) {
142 return 1;
143 }
144 return 0;
145}
146
148 if (this->read_pos_ < 3)
149 return 0;
150 uint16_t crc16;
152 if (((uint8_t) ((crc16) >> 8)) == read_buffer_[read_pos_ - 3] &&
153 ((uint8_t) ((crc16) & 0xff)) == read_buffer_[read_pos_ - 2]) {
154 ESP_LOGD(TAG, "CRC OK");
155 read_buffer_[read_pos_ - 1] = 0;
156 read_buffer_[read_pos_ - 2] = 0;
157 read_buffer_[read_pos_ - 3] = 0;
158 return 1;
159 }
160 ESP_LOGD(TAG, "CRC NOK expected: %X %X but got: %X %X", ((uint8_t) ((crc16) >> 8)), ((uint8_t) ((crc16) & 0xff)),
162 return 0;
163}
164
165// send next command from queue
167 uint16_t crc16;
168 if (!this->command_queue_[this->command_queue_position_].empty()) {
169 const char *command = this->command_queue_[this->command_queue_position_].c_str();
170 uint8_t byte_command[16];
171 size_t length = this->command_queue_[this->command_queue_position_].length();
172 if (length > sizeof(byte_command)) {
173 ESP_LOGE(TAG, "Command too long: %zu", length);
174 this->command_queue_[this->command_queue_position_].clear();
175 return false;
176 }
177 for (size_t i = 0; i < length; i++) {
178 byte_command[i] = (uint8_t) this->command_queue_[this->command_queue_position_].at(i);
179 }
180 this->state_ = STATE_COMMAND;
182 this->discard_available();
183 this->read_pos_ = 0;
184 crc16 = this->pipsolar_crc_(byte_command, length);
185 this->write_str(command);
186 // checksum
187 this->write(((uint8_t) ((crc16) >> 8))); // highbyte
188 this->write(((uint8_t) ((crc16) & 0xff))); // lowbyte
189 // end Byte
190 this->write(0x0D);
191 ESP_LOGD(TAG, "Sending command from queue: %s with length %d", command, length);
192 return true;
193 }
194 return false;
195}
196
198 uint16_t crc16;
199 for (uint8_t i = 0; i < POLLING_COMMANDS_MAX; i++) {
200 this->last_polling_command_ = (this->last_polling_command_ + 1) % POLLING_COMMANDS_MAX;
202 // not enabled
203 continue;
204 }
205 if (!this->enabled_polling_commands_[this->last_polling_command_].needs_update) {
206 // no update requested
207 continue;
208 }
209 this->state_ = STATE_POLL;
211 this->discard_available();
212 this->read_pos_ = 0;
213 crc16 = this->pipsolar_crc_(this->enabled_polling_commands_[this->last_polling_command_].command,
217 // checksum
218 this->write(((uint8_t) ((crc16) >> 8))); // highbyte
219 this->write(((uint8_t) ((crc16) & 0xff))); // lowbyte
220 // end Byte
221 this->write(0x0D);
222 ESP_LOGD(TAG, "Sending polling command: %s with length %d",
225 return true;
226 }
227 return false;
228}
229
230void Pipsolar::write_switch(bool state, const char *on, const char *off) {
231 const char *command = state ? on : off;
232 if (command != nullptr)
233 this->queue_command(command);
234}
235
236void Pipsolar::queue_command(const char *command) {
237 uint8_t next_position = command_queue_position_;
238 for (uint8_t i = 0; i < COMMAND_QUEUE_LENGTH; i++) {
239 uint8_t testposition = (next_position + i) % COMMAND_QUEUE_LENGTH;
240 if (command_queue_[testposition].empty()) {
241 command_queue_[testposition] = command;
242 ESP_LOGD(TAG, "Command queued successfully: %s at position %d", command, testposition);
243 return;
244 }
245 }
246 ESP_LOGD(TAG, "Command queue full dropping command: %s", command);
247}
248
250 switch (polling_command) {
251 case POLLING_QPIRI:
253 break;
254 case POLLING_QPIGS:
256 break;
257 case POLLING_QMOD:
259 break;
260 case POLLING_QFLAG:
262 break;
263 case POLLING_QPIWS:
265 break;
266 case POLLING_QT:
268 break;
269 case POLLING_QMN:
271 break;
272 case POLLING_QET:
273 handle_single_int_(message, this->last_qet_, this->total_pv_generated_energy_);
274 break;
275 case POLLING_QLT:
276 handle_single_int_(message, this->last_qlt_, this->total_output_load_energy_);
277 break;
278 default:
279 break;
280 }
281}
283 // handlers are designed in a way that an empty message sets all sensors to unknown
284 this->handle_poll_response_(polling_command, "");
285}
286
288 PUBLISH_STATE(this->last_qpiri_, message);
289
290 size_t pos = 0;
291 this->skip_start_(message, &pos);
292
293 this->read_float_sensor_(message, &pos, this->grid_rating_voltage_);
294 this->read_float_sensor_(message, &pos, this->grid_rating_current_);
295 this->read_float_sensor_(message, &pos, this->ac_output_rating_voltage_);
296 this->read_float_sensor_(message, &pos, this->ac_output_rating_frequency_);
297 this->read_float_sensor_(message, &pos, this->ac_output_rating_current_);
298
299 this->read_int_sensor_(message, &pos, this->ac_output_rating_apparent_power_);
300 this->read_int_sensor_(message, &pos, this->ac_output_rating_active_power_);
301
302 this->read_float_sensor_(message, &pos, this->battery_rating_voltage_);
303 this->read_float_sensor_(message, &pos, this->battery_recharge_voltage_);
304 this->read_float_sensor_(message, &pos, this->battery_under_voltage_);
305 this->read_float_sensor_(message, &pos, this->battery_bulk_voltage_);
306 this->read_float_sensor_(message, &pos, this->battery_float_voltage_);
307
308 this->read_int_sensor_(message, &pos, this->battery_type_);
309 this->read_int_sensor_(message, &pos, this->current_max_ac_charging_current_);
310 this->read_int_sensor_(message, &pos, this->current_max_charging_current_);
311
312 esphome::optional<int> input_voltage_range = parse_number<int32_t>(this->read_field_(message, &pos));
313 esphome::optional<int> output_source_priority = parse_number<int32_t>(this->read_field_(message, &pos));
314
315 this->read_int_sensor_(message, &pos, this->charger_source_priority_);
316 this->read_int_sensor_(message, &pos, this->parallel_max_num_);
317 this->read_int_sensor_(message, &pos, this->machine_type_);
318 this->read_int_sensor_(message, &pos, this->topology_);
319 this->read_int_sensor_(message, &pos, this->output_mode_);
320
321 this->read_float_sensor_(message, &pos, this->battery_redischarge_voltage_);
322
323 esphome::optional<int> pv_ok_condition_for_parallel = parse_number<int32_t>(this->read_field_(message, &pos));
324 esphome::optional<int> pv_power_balance = parse_number<int32_t>(this->read_field_(message, &pos));
325
326 PUBLISH_STATE(this->input_voltage_range_, int_or_nan(input_voltage_range));
327 // special for input voltage range switch
328 if (this->input_voltage_range_switch_ && input_voltage_range.has_value()) {
329 this->input_voltage_range_switch_->publish_state(input_voltage_range.value() == 1);
330 }
331
332 PUBLISH_STATE(this->output_source_priority_, int_or_nan(output_source_priority));
333 // special for output source priority switches
334 if (this->output_source_priority_utility_switch_ && output_source_priority.has_value()) {
335 this->output_source_priority_utility_switch_->publish_state(output_source_priority.value() == 0);
336 }
337 if (this->output_source_priority_solar_switch_ && output_source_priority.has_value()) {
338 this->output_source_priority_solar_switch_->publish_state(output_source_priority.value() == 1);
339 }
340 if (this->output_source_priority_battery_switch_ && output_source_priority.has_value()) {
341 this->output_source_priority_battery_switch_->publish_state(output_source_priority.value() == 2);
342 }
343 if (this->output_source_priority_hybrid_switch_ && output_source_priority.has_value()) {
344 this->output_source_priority_hybrid_switch_->publish_state(output_source_priority.value() == 3);
345 }
346
347 PUBLISH_STATE(this->pv_ok_condition_for_parallel_, int_or_nan(pv_ok_condition_for_parallel));
348 // special for pv ok condition switch
349 if (this->pv_ok_condition_for_parallel_switch_ && pv_ok_condition_for_parallel.has_value()) {
350 this->pv_ok_condition_for_parallel_switch_->publish_state(pv_ok_condition_for_parallel.value() == 1);
351 }
352
353 PUBLISH_STATE(this->pv_power_balance_, int_or_nan(pv_power_balance));
354 // special for power balance switch
355 if (this->pv_power_balance_switch_ && pv_power_balance.has_value()) {
356 this->pv_power_balance_switch_->publish_state(pv_power_balance.value() == 1);
357 }
358}
359
361 PUBLISH_STATE(this->last_qpigs_, message);
362
363 size_t pos = 0;
364 this->skip_start_(message, &pos);
365
366 this->read_float_sensor_(message, &pos, this->grid_voltage_);
367 this->read_float_sensor_(message, &pos, this->grid_frequency_);
368 this->read_float_sensor_(message, &pos, this->ac_output_voltage_);
369 this->read_float_sensor_(message, &pos, this->ac_output_frequency_);
370
371 this->read_int_sensor_(message, &pos, this->ac_output_apparent_power_);
372 this->read_int_sensor_(message, &pos, this->ac_output_active_power_);
373 this->read_int_sensor_(message, &pos, this->output_load_percent_);
374 this->read_int_sensor_(message, &pos, this->bus_voltage_);
375
376 this->read_float_sensor_(message, &pos, this->battery_voltage_);
377
378 this->read_int_sensor_(message, &pos, this->battery_charging_current_);
379 this->read_int_sensor_(message, &pos, this->battery_capacity_percent_);
380 this->read_int_sensor_(message, &pos, this->inverter_heat_sink_temperature_);
381
382 this->read_float_sensor_(message, &pos, this->pv_input_current_for_battery_);
383 this->read_float_sensor_(message, &pos, this->pv_input_voltage_);
384 this->read_float_sensor_(message, &pos, this->battery_voltage_scc_);
385
386 this->read_int_sensor_(message, &pos, this->battery_discharge_current_);
387
388 std::string device_status_1 = this->read_field_(message, &pos);
389 this->publish_binary_sensor_(this->get_bit_(device_status_1, 0), this->add_sbu_priority_version_);
390 this->publish_binary_sensor_(this->get_bit_(device_status_1, 1), this->configuration_status_);
391 this->publish_binary_sensor_(this->get_bit_(device_status_1, 2), this->scc_firmware_version_);
392 this->publish_binary_sensor_(this->get_bit_(device_status_1, 3), this->load_status_);
393 this->publish_binary_sensor_(this->get_bit_(device_status_1, 4), this->battery_voltage_to_steady_while_charging_);
394 this->publish_binary_sensor_(this->get_bit_(device_status_1, 5), this->charging_status_);
395 this->publish_binary_sensor_(this->get_bit_(device_status_1, 6), this->scc_charging_status_);
396 this->publish_binary_sensor_(this->get_bit_(device_status_1, 7), this->ac_charging_status_);
397
398 esphome::optional<int> battery_voltage_offset_for_fans_on = parse_number<int32_t>(this->read_field_(message, &pos));
399 PUBLISH_STATE(this->battery_voltage_offset_for_fans_on_, int_or_nan(battery_voltage_offset_for_fans_on) / 10.0f);
400 this->read_int_sensor_(message, &pos, this->eeprom_version_);
401 this->read_int_sensor_(message, &pos, this->pv_charging_power_);
402
403 std::string device_status_2 = this->read_field_(message, &pos);
404 this->publish_binary_sensor_(this->get_bit_(device_status_2, 0), this->charging_to_floating_mode_);
405 this->publish_binary_sensor_(this->get_bit_(device_status_2, 1), this->switch_on_);
406 this->publish_binary_sensor_(this->get_bit_(device_status_2, 2), this->dustproof_installed_);
407}
408
410 PUBLISH_STATE(this->last_qmod_, message);
411 // QMOD response is "(M" where M is the device-mode character. Bail out if the
412 // message is shorter than 2 chars (e.g. empty error response from
413 // handle_poll_error_) — reading message[1] would otherwise be out of bounds.
414 if (message[0] == '\0' || message[1] == '\0')
415 return;
416 PUBLISH_STATE(this->device_mode_, &message[1], 1);
417}
418
420 // result like:"(EbkuvxzDajy"
421 // get through all char: ignore first "(" Enable flag on 'E', Disable on 'D') else set the corresponding value
422 PUBLISH_STATE(this->last_qflag_, message);
423
424 QFLAGValues values = QFLAGValues();
425 bool enabled = true;
426 for (size_t i = 1; i < strlen(message); i++) {
427 switch (message[i]) {
428 case 'E':
429 enabled = true;
430 break;
431 case 'D':
432 enabled = false;
433 break;
434 case 'a':
435 values.silence_buzzer_open_buzzer = enabled;
436 break;
437 case 'b':
438 values.overload_bypass_function = enabled;
439 break;
440 case 'k':
441 values.lcd_escape_to_default = enabled;
442 break;
443 case 'u':
444 values.overload_restart_function = enabled;
445 break;
446 case 'v':
447 values.over_temperature_restart_function = enabled;
448 break;
449 case 'x':
450 values.backlight_on = enabled;
451 break;
452 case 'y':
454 break;
455 case 'z':
456 values.fault_code_record = enabled;
457 break;
458 case 'j':
459 values.power_saving = enabled;
460 break;
461 }
462 }
463
464 this->publish_binary_sensor_(values.silence_buzzer_open_buzzer, this->silence_buzzer_open_buzzer_);
465 this->publish_binary_sensor_(values.overload_bypass_function, this->overload_bypass_function_);
466 this->publish_binary_sensor_(values.lcd_escape_to_default, this->lcd_escape_to_default_);
467 this->publish_binary_sensor_(values.overload_restart_function, this->overload_restart_function_);
468 this->publish_binary_sensor_(values.over_temperature_restart_function, this->over_temperature_restart_function_);
469 this->publish_binary_sensor_(values.backlight_on, this->backlight_on_);
471 this->alarm_on_when_primary_source_interrupt_);
472 this->publish_binary_sensor_(values.fault_code_record, this->fault_code_record_);
473 this->publish_binary_sensor_(values.power_saving, this->power_saving_);
474}
475
477 // '(00000000000000000000000000000000'
478 // iterate over all available flag (as not all models have all flags, but at least in the same order)
479 PUBLISH_STATE(this->last_qpiws_, message);
480
481 size_t pos = 0;
482 this->skip_start_(message, &pos);
483 std::string flags = this->read_field_(message, &pos);
484
485 esphome::optional<bool> enabled;
486 bool value_warnings_present = false;
487 bool value_faults_present = false;
488
489 for (size_t i = 0; i < 36; i++) {
490 if (i == 31 || i == 32) {
491 // special case for fault code
492 continue;
493 }
494 enabled = this->get_bit_(flags, i);
495 switch (i) {
496 case 0:
497 this->publish_binary_sensor_(enabled, this->warning_power_loss_);
498 value_warnings_present |= enabled.value_or(false);
499 break;
500 case 1:
501 this->publish_binary_sensor_(enabled, this->fault_inverter_fault_);
502 value_faults_present |= enabled.value_or(false);
503 break;
504 case 2:
505 this->publish_binary_sensor_(enabled, this->fault_bus_over_);
506 value_faults_present |= enabled.value_or(false);
507 break;
508 case 3:
509 this->publish_binary_sensor_(enabled, this->fault_bus_under_);
510 value_faults_present |= enabled.value_or(false);
511 break;
512 case 4:
513 this->publish_binary_sensor_(enabled, this->fault_bus_soft_fail_);
514 value_faults_present |= enabled.value_or(false);
515 break;
516 case 5:
517 this->publish_binary_sensor_(enabled, this->warning_line_fail_);
518 value_warnings_present |= enabled.value_or(false);
519 break;
520 case 6:
521 this->publish_binary_sensor_(enabled, this->fault_opvshort_);
522 value_faults_present |= enabled.value_or(false);
523 break;
524 case 7:
525 this->publish_binary_sensor_(enabled, this->fault_inverter_voltage_too_low_);
526 value_faults_present |= enabled.value_or(false);
527 break;
528 case 8:
529 this->publish_binary_sensor_(enabled, this->fault_inverter_voltage_too_high_);
530 value_faults_present |= enabled.value_or(false);
531 break;
532 case 9:
533 this->publish_binary_sensor_(enabled, this->warning_over_temperature_);
534 value_warnings_present |= enabled.value_or(false);
535 break;
536 case 10:
537 this->publish_binary_sensor_(enabled, this->warning_fan_lock_);
538 value_warnings_present |= enabled.value_or(false);
539 break;
540 case 11:
541 this->publish_binary_sensor_(enabled, this->warning_battery_voltage_high_);
542 value_warnings_present |= enabled.value_or(false);
543 break;
544 case 12:
545 this->publish_binary_sensor_(enabled, this->warning_battery_low_alarm_);
546 value_warnings_present |= enabled.value_or(false);
547 break;
548 case 14:
549 this->publish_binary_sensor_(enabled, this->warning_battery_under_shutdown_);
550 value_warnings_present |= enabled.value_or(false);
551 break;
552 case 15:
553 this->publish_binary_sensor_(enabled, this->warning_battery_derating_);
554 value_warnings_present |= enabled.value_or(false);
555 break;
556 case 16:
557 this->publish_binary_sensor_(enabled, this->warning_over_load_);
558 value_warnings_present |= enabled.value_or(false);
559 break;
560 case 17:
561 this->publish_binary_sensor_(enabled, this->warning_eeprom_failed_);
562 value_warnings_present |= enabled.value_or(false);
563 break;
564 case 18:
565 this->publish_binary_sensor_(enabled, this->fault_inverter_over_current_);
566 value_faults_present |= enabled.value_or(false);
567 break;
568 case 19:
569 this->publish_binary_sensor_(enabled, this->fault_inverter_soft_failed_);
570 value_faults_present |= enabled.value_or(false);
571 break;
572 case 20:
573 this->publish_binary_sensor_(enabled, this->fault_self_test_failed_);
574 value_faults_present |= enabled.value_or(false);
575 break;
576 case 21:
577 this->publish_binary_sensor_(enabled, this->fault_op_dc_voltage_over_);
578 value_faults_present |= enabled.value_or(false);
579 break;
580 case 22:
581 this->publish_binary_sensor_(enabled, this->fault_battery_open_);
582 value_faults_present |= enabled.value_or(false);
583 break;
584 case 23:
585 this->publish_binary_sensor_(enabled, this->fault_current_sensor_failed_);
586 value_faults_present |= enabled.value_or(false);
587 break;
588 case 24:
589 this->publish_binary_sensor_(enabled, this->fault_battery_short_);
590 value_faults_present |= enabled.value_or(false);
591 break;
592 case 25:
593 this->publish_binary_sensor_(enabled, this->warning_power_limit_);
594 value_warnings_present |= enabled.value_or(false);
595 break;
596 case 26:
597 this->publish_binary_sensor_(enabled, this->warning_pv_voltage_high_);
598 value_warnings_present |= enabled.value_or(false);
599 break;
600 case 27:
601 this->publish_binary_sensor_(enabled, this->fault_mppt_overload_);
602 value_faults_present |= enabled.value_or(false);
603 break;
604 case 28:
605 this->publish_binary_sensor_(enabled, this->warning_mppt_overload_);
606 value_warnings_present |= enabled.value_or(false);
607 break;
608 case 29:
609 this->publish_binary_sensor_(enabled, this->warning_battery_too_low_to_charge_);
610 value_warnings_present |= enabled.value_or(false);
611 break;
612 case 30:
613 this->publish_binary_sensor_(enabled, this->fault_dc_dc_over_current_);
614 value_faults_present |= enabled.value_or(false);
615 break;
616 case 33:
617 this->publish_binary_sensor_(enabled, this->warning_low_pv_energy_);
618 value_warnings_present |= enabled.value_or(false);
619 break;
620 case 34:
621 this->publish_binary_sensor_(enabled, this->warning_high_ac_input_during_bus_soft_start_);
622 value_warnings_present |= enabled.value_or(false);
623 break;
624 case 35:
625 this->publish_binary_sensor_(enabled, this->warning_battery_equalization_);
626 value_warnings_present |= enabled.value_or(false);
627 break;
628 default:
629 break;
630 }
631 }
632
633 this->publish_binary_sensor_(value_warnings_present, this->warnings_present_);
634 this->publish_binary_sensor_(value_faults_present, this->faults_present_);
635
636 if (this->fault_code_) {
637 optional<bool> fault;
638 if (flags.length() >= 33) {
639 if (auto code = parse_number<int>(std::string(flags, 31, 2)))
640 fault = *code != 0;
641 }
642 this->publish_binary_sensor_(fault, this->fault_code_);
643 }
644}
645
646void Pipsolar::handle_qt_(const char *message) { PUBLISH_STATE(this->last_qt_, message); }
647
648void Pipsolar::handle_qmn_(const char *message) { PUBLISH_STATE(this->last_qmn_, message); }
649
650// QET and QLT each answer with one integer
652 PUBLISH_STATE(raw, message);
653
654 size_t pos = 0;
655 this->skip_start_(message, &pos);
656 this->read_int_sensor_(message, &pos, value);
657}
658
659void Pipsolar::skip_start_(const char *message, size_t *pos) {
660 if (message[*pos] == '(') {
661 (*pos)++;
662 }
663}
664void Pipsolar::skip_field_(const char *message, size_t *pos) {
665 // find delimiter or end of string
666 while (message[*pos] != '\0' && message[*pos] != ' ') {
667 (*pos)++;
668 }
669 if (message[*pos] != '\0') {
670 // skip delimiter after this field if there is one
671 (*pos)++;
672 }
673}
674std::string Pipsolar::read_field_(const char *message, size_t *pos) {
675 size_t begin = *pos;
676 // find delimiter or end of string
677 while (message[*pos] != '\0' && message[*pos] != ' ') {
678 (*pos)++;
679 }
680 if (*pos == begin) {
681 return "";
682 }
683
684 std::string field(message, begin, *pos - begin);
685
686 if (message[*pos] != '\0') {
687 // skip delimiter after this field if there is one
688 (*pos)++;
689 }
690
691 return field;
692}
693
694void Pipsolar::read_float_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor) {
695 if (sensor != nullptr) {
696 std::string field = this->read_field_(message, pos);
697 sensor->publish_state(parse_number<float>(field).value_or(NAN));
698 } else {
699 this->skip_field_(message, pos);
700 }
701}
702void Pipsolar::read_int_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor) {
703 if (sensor != nullptr) {
704 std::string field = this->read_field_(message, pos);
705 sensor->publish_state(int_or_nan(parse_number<int32_t>(field)));
706 } else {
707 this->skip_field_(message, pos);
708 }
709}
710
711void Pipsolar::publish_binary_sensor_(esphome::optional<bool> b, binary_sensor::BinarySensor *sensor) {
712 if (sensor) {
713 if (b.has_value()) {
714 sensor->publish_state(b.value());
715 } else {
716 sensor->invalidate_state();
717 }
718 }
719}
720
721esphome::optional<bool> Pipsolar::get_bit_(std::string bits, uint8_t bit_pos) {
722 if (bit_pos >= bits.length()) {
723 return {};
724 }
725 return bits[bit_pos] == '1';
726}
727
728void Pipsolar::dump_config() {
729 ESP_LOGCONFIG(TAG, "Pipsolar enabled polling commands:");
730 for (auto &enabled_polling_command : this->enabled_polling_commands_) {
731 if (enabled_polling_command.length != 0) {
732 ESP_LOGCONFIG(TAG, "%s", enabled_polling_command.command);
733 }
734 }
735}
736void Pipsolar::update() {
737 for (auto &enabled_polling_command : this->enabled_polling_commands_) {
738 if (enabled_polling_command.length != 0) {
739 enabled_polling_command.needs_update = true;
740 }
741 }
742}
743
744void Pipsolar::add_polling_command_(const char *command, ENUMPollingCommand polling_command) {
745 for (auto &enabled_polling_command : this->enabled_polling_commands_) {
746 if (enabled_polling_command.length == strlen(command)) {
747 uint8_t len = strlen(command);
748 if (memcmp(enabled_polling_command.command, command, len) == 0) {
749 return;
750 }
751 }
752 if (enabled_polling_command.length == 0) {
753 size_t length = strlen(command);
754
755 enabled_polling_command.command = new uint8_t[length + 1]; // NOLINT(cppcoreguidelines-owning-memory)
756 for (size_t i = 0; i < length + 1; i++) {
757 enabled_polling_command.command[i] = (uint8_t) command[i];
758 }
759 enabled_polling_command.errors = 0;
760 enabled_polling_command.identifier = polling_command;
761 enabled_polling_command.length = length;
762 enabled_polling_command.needs_update = true;
763 return;
764 }
765 }
766}
767
768uint16_t Pipsolar::pipsolar_crc_(uint8_t *msg, uint8_t len) {
769 uint16_t crc = crc16be(msg, len);
770 uint8_t crc_low = crc & 0xff;
771 uint8_t crc_high = crc >> 8;
772 if (crc_low == 0x28 || crc_low == 0x0d || crc_low == 0x0a)
773 crc_low++;
774 if (crc_high == 0x28 || crc_high == 0x0d || crc_high == 0x0a)
775 crc_high++;
776 crc = (crc_high << 8) | crc_low;
777 return crc;
778}
779
780} // namespace esphome::pipsolar
uint8_t raw[35]
Definition bl0939.h:0
void invalidate_state()
Clear the state — sets has_state() to false and fires callbacks with nullopt.
Base class for all binary_sensor-type classes.
void publish_state(bool new_state)
Publish a new state to the front-end.
static const size_t COMMAND_QUEUE_LENGTH
Definition pipsolar.h:208
void handle_qmod_(const char *message)
Definition pipsolar.cpp:409
uint8_t read_buffer_[PIPSOLAR_READ_BUFFER_LENGTH]
Definition pipsolar.h:243
uint16_t pipsolar_crc_(uint8_t *msg, uint8_t len)
Definition pipsolar.cpp:768
void handle_poll_error_(ENUMPollingCommand polling_command)
Definition pipsolar.cpp:282
void handle_qpiws_(const char *message)
Definition pipsolar.cpp:476
esphome::optional< bool > get_bit_(std::string bits, uint8_t bit_pos)
Definition pipsolar.cpp:721
void handle_qt_(const char *message)
Definition pipsolar.cpp:646
uint8_t check_incoming_length_(uint8_t length)
Definition pipsolar.cpp:140
void handle_qpigs_(const char *message)
Definition pipsolar.cpp:360
void handle_poll_response_(ENUMPollingCommand polling_command, const char *message)
Definition pipsolar.cpp:249
static const size_t PIPSOLAR_READ_BUFFER_LENGTH
Definition pipsolar.h:207
std::string read_field_(const char *message, size_t *pos)
Definition pipsolar.cpp:674
std::string command_queue_[COMMAND_QUEUE_LENGTH]
Definition pipsolar.h:241
void read_int_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor)
Definition pipsolar.cpp:702
void read_float_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor)
Definition pipsolar.cpp:694
void handle_qflag_(const char *message)
Definition pipsolar.cpp:419
void skip_field_(const char *message, size_t *pos)
Definition pipsolar.cpp:664
void handle_qpiri_(const char *message)
Definition pipsolar.cpp:287
PollingCommand enabled_polling_commands_[POLLING_COMMANDS_MAX]
Definition pipsolar.h:258
void publish_binary_sensor_(esphome::optional< bool > b, binary_sensor::BinarySensor *sensor)
Definition pipsolar.cpp:711
static const size_t POLLING_COMMANDS_MAX
Definition pipsolar.h:210
void skip_start_(const char *message, size_t *pos)
Definition pipsolar.cpp:659
void handle_single_int_(const char *message, text_sensor::TextSensor *raw, sensor::Sensor *value)
Definition pipsolar.cpp:651
static const size_t COMMAND_TIMEOUT
Definition pipsolar.h:209
void add_polling_command_(const char *command, ENUMPollingCommand polling_command)
Definition pipsolar.cpp:744
void handle_qmn_(const char *message)
Definition pipsolar.cpp:648
Base-class for all sensors.
Definition sensor.h:48
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
void write_str(const char *str)
Definition uart.h:37
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
size_t write(uint8_t data)
Definition uart.h:105
const LogString * message
Definition component.cpp:35
uint16_t flags
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "pipsolar.output")
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
Definition helpers.cpp:95
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1430
size_t size_t pos
Definition helpers.h:1333
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Definition helpers.cpp:135
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
esphome::optional< bool > overload_bypass_function
Definition pipsolar.h:35
esphome::optional< bool > alarm_on_when_primary_source_interrupt
Definition pipsolar.h:40
esphome::optional< bool > backlight_on
Definition pipsolar.h:39
esphome::optional< bool > lcd_escape_to_default
Definition pipsolar.h:36
esphome::optional< bool > fault_code_record
Definition pipsolar.h:41
esphome::optional< bool > over_temperature_restart_function
Definition pipsolar.h:38
esphome::optional< bool > silence_buzzer_open_buzzer
Definition pipsolar.h:34
esphome::optional< bool > overload_restart_function
Definition pipsolar.h:37
esphome::optional< bool > power_saving
Definition pipsolar.h:42
uint16_t length
Definition tt21100.cpp:0