ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pipsolar.h
Go to the documentation of this file.
1#pragma once
2
11
12namespace esphome::pipsolar {
13
32
34 esphome::optional<bool> silence_buzzer_open_buzzer;
35 esphome::optional<bool> overload_bypass_function;
36 esphome::optional<bool> lcd_escape_to_default;
37 esphome::optional<bool> overload_restart_function;
38 esphome::optional<bool> over_temperature_restart_function;
39 esphome::optional<bool> backlight_on;
41 esphome::optional<bool> fault_code_record;
42 esphome::optional<bool> power_saving;
43};
44
45#define PIPSOLAR_ENTITY_(type, name, polling_command) \
46 protected: \
47 type *name##_{}; /* NOLINT */ \
48\
49 public: \
50 void set_##name(type *name) { /* NOLINT */ \
51 this->name##_ = name; \
52 this->add_polling_command_(#polling_command, POLLING_##polling_command); \
53 }
54
55#define PIPSOLAR_SENSOR(name, polling_command) PIPSOLAR_ENTITY_(sensor::Sensor, name, polling_command)
56#define PIPSOLAR_SWITCH(name, polling_command) PIPSOLAR_ENTITY_(switch_::Switch, name, polling_command)
57#define PIPSOLAR_BINARY_SENSOR(name, polling_command) \
58 PIPSOLAR_ENTITY_(binary_sensor::BinarySensor, name, polling_command)
59#define PIPSOLAR_TEXT_SENSOR(name, polling_command) PIPSOLAR_ENTITY_(text_sensor::TextSensor, name, polling_command)
60
61class Pipsolar final : public uart::UARTDevice, public PollingComponent {
62 // QPIGS values
63 PIPSOLAR_SENSOR(grid_voltage, QPIGS)
64 PIPSOLAR_SENSOR(grid_frequency, QPIGS)
65 PIPSOLAR_SENSOR(ac_output_voltage, QPIGS)
66 PIPSOLAR_SENSOR(ac_output_frequency, QPIGS)
67 PIPSOLAR_SENSOR(ac_output_apparent_power, QPIGS)
68 PIPSOLAR_SENSOR(ac_output_active_power, QPIGS)
69 PIPSOLAR_SENSOR(output_load_percent, QPIGS)
70 PIPSOLAR_SENSOR(bus_voltage, QPIGS)
71 PIPSOLAR_SENSOR(battery_voltage, QPIGS)
72 PIPSOLAR_SENSOR(battery_charging_current, QPIGS)
73 PIPSOLAR_SENSOR(battery_capacity_percent, QPIGS)
74 PIPSOLAR_SENSOR(inverter_heat_sink_temperature, QPIGS)
75 PIPSOLAR_SENSOR(pv_input_current_for_battery, QPIGS)
76 PIPSOLAR_SENSOR(pv_input_voltage, QPIGS)
77 PIPSOLAR_SENSOR(battery_voltage_scc, QPIGS)
78 PIPSOLAR_SENSOR(battery_discharge_current, QPIGS)
79 PIPSOLAR_BINARY_SENSOR(add_sbu_priority_version, QPIGS)
80 PIPSOLAR_BINARY_SENSOR(configuration_status, QPIGS)
81 PIPSOLAR_BINARY_SENSOR(scc_firmware_version, QPIGS)
82 PIPSOLAR_BINARY_SENSOR(load_status, QPIGS)
83 PIPSOLAR_BINARY_SENSOR(battery_voltage_to_steady_while_charging, QPIGS)
84 PIPSOLAR_BINARY_SENSOR(charging_status, QPIGS)
85 PIPSOLAR_BINARY_SENSOR(scc_charging_status, QPIGS)
86 PIPSOLAR_BINARY_SENSOR(ac_charging_status, QPIGS)
87 PIPSOLAR_SENSOR(battery_voltage_offset_for_fans_on, QPIGS) //.1 scale
88 PIPSOLAR_SENSOR(eeprom_version, QPIGS)
89 PIPSOLAR_SENSOR(pv_charging_power, QPIGS)
90 PIPSOLAR_BINARY_SENSOR(charging_to_floating_mode, QPIGS)
91 PIPSOLAR_BINARY_SENSOR(switch_on, QPIGS)
92 PIPSOLAR_BINARY_SENSOR(dustproof_installed, QPIGS)
93
94 // QPIRI values
95 PIPSOLAR_SENSOR(grid_rating_voltage, QPIRI)
96 PIPSOLAR_SENSOR(grid_rating_current, QPIRI)
97 PIPSOLAR_SENSOR(ac_output_rating_voltage, QPIRI)
98 PIPSOLAR_SENSOR(ac_output_rating_frequency, QPIRI)
99 PIPSOLAR_SENSOR(ac_output_rating_current, QPIRI)
100 PIPSOLAR_SENSOR(ac_output_rating_apparent_power, QPIRI)
101 PIPSOLAR_SENSOR(ac_output_rating_active_power, QPIRI)
102 PIPSOLAR_SENSOR(battery_rating_voltage, QPIRI)
103 PIPSOLAR_SENSOR(battery_recharge_voltage, QPIRI)
104 PIPSOLAR_SENSOR(battery_under_voltage, QPIRI)
105 PIPSOLAR_SENSOR(battery_bulk_voltage, QPIRI)
106 PIPSOLAR_SENSOR(battery_float_voltage, QPIRI)
107 PIPSOLAR_SENSOR(battery_type, QPIRI)
108 PIPSOLAR_SENSOR(current_max_ac_charging_current, QPIRI)
109 PIPSOLAR_SENSOR(current_max_charging_current, QPIRI)
110 PIPSOLAR_SENSOR(input_voltage_range, QPIRI)
111 PIPSOLAR_SENSOR(output_source_priority, QPIRI)
112 PIPSOLAR_SENSOR(charger_source_priority, QPIRI)
113 PIPSOLAR_SENSOR(parallel_max_num, QPIRI)
114 PIPSOLAR_SENSOR(machine_type, QPIRI)
115 PIPSOLAR_SENSOR(topology, QPIRI)
116 PIPSOLAR_SENSOR(output_mode, QPIRI)
117 PIPSOLAR_SENSOR(battery_redischarge_voltage, QPIRI)
118 PIPSOLAR_SENSOR(pv_ok_condition_for_parallel, QPIRI)
119 PIPSOLAR_SENSOR(pv_power_balance, QPIRI)
120
121 // QET value
122 PIPSOLAR_SENSOR(total_pv_generated_energy, QET)
123
124 // QLT value
125 PIPSOLAR_SENSOR(total_output_load_energy, QLT)
126
127 // QMOD values
128 PIPSOLAR_TEXT_SENSOR(device_mode, QMOD)
129
130 // QFLAG values
131 PIPSOLAR_BINARY_SENSOR(silence_buzzer_open_buzzer, QFLAG)
132 PIPSOLAR_BINARY_SENSOR(overload_bypass_function, QFLAG)
133 PIPSOLAR_BINARY_SENSOR(lcd_escape_to_default, QFLAG)
134 PIPSOLAR_BINARY_SENSOR(overload_restart_function, QFLAG)
135 PIPSOLAR_BINARY_SENSOR(over_temperature_restart_function, QFLAG)
136 PIPSOLAR_BINARY_SENSOR(backlight_on, QFLAG)
137 PIPSOLAR_BINARY_SENSOR(alarm_on_when_primary_source_interrupt, QFLAG)
138 PIPSOLAR_BINARY_SENSOR(fault_code_record, QFLAG)
139 PIPSOLAR_BINARY_SENSOR(power_saving, QFLAG)
140
141 // QPIWS values
142 PIPSOLAR_BINARY_SENSOR(warnings_present, QPIWS)
143 PIPSOLAR_BINARY_SENSOR(faults_present, QPIWS)
144 PIPSOLAR_BINARY_SENSOR(warning_power_loss, QPIWS)
145 PIPSOLAR_BINARY_SENSOR(fault_inverter_fault, QPIWS)
146 PIPSOLAR_BINARY_SENSOR(fault_bus_over, QPIWS)
147 PIPSOLAR_BINARY_SENSOR(fault_bus_under, QPIWS)
148 PIPSOLAR_BINARY_SENSOR(fault_bus_soft_fail, QPIWS)
149 PIPSOLAR_BINARY_SENSOR(warning_line_fail, QPIWS)
150 PIPSOLAR_BINARY_SENSOR(fault_opvshort, QPIWS)
151 PIPSOLAR_BINARY_SENSOR(fault_inverter_voltage_too_low, QPIWS)
152 PIPSOLAR_BINARY_SENSOR(fault_inverter_voltage_too_high, QPIWS)
153 PIPSOLAR_BINARY_SENSOR(warning_over_temperature, QPIWS)
154 PIPSOLAR_BINARY_SENSOR(warning_fan_lock, QPIWS)
155 PIPSOLAR_BINARY_SENSOR(warning_battery_voltage_high, QPIWS)
156 PIPSOLAR_BINARY_SENSOR(warning_battery_low_alarm, QPIWS)
157 PIPSOLAR_BINARY_SENSOR(warning_battery_under_shutdown, QPIWS)
158 PIPSOLAR_BINARY_SENSOR(warning_battery_derating, QPIWS)
159 PIPSOLAR_BINARY_SENSOR(warning_over_load, QPIWS)
160 PIPSOLAR_BINARY_SENSOR(warning_eeprom_failed, QPIWS)
161 PIPSOLAR_BINARY_SENSOR(fault_inverter_over_current, QPIWS)
162 PIPSOLAR_BINARY_SENSOR(fault_inverter_soft_failed, QPIWS)
163 PIPSOLAR_BINARY_SENSOR(fault_self_test_failed, QPIWS)
164 PIPSOLAR_BINARY_SENSOR(fault_op_dc_voltage_over, QPIWS)
165 PIPSOLAR_BINARY_SENSOR(fault_battery_open, QPIWS)
166 PIPSOLAR_BINARY_SENSOR(fault_current_sensor_failed, QPIWS)
167 PIPSOLAR_BINARY_SENSOR(fault_battery_short, QPIWS)
168 PIPSOLAR_BINARY_SENSOR(warning_power_limit, QPIWS)
169 PIPSOLAR_BINARY_SENSOR(warning_pv_voltage_high, QPIWS)
170 PIPSOLAR_BINARY_SENSOR(fault_mppt_overload, QPIWS)
171 PIPSOLAR_BINARY_SENSOR(warning_mppt_overload, QPIWS)
172 PIPSOLAR_BINARY_SENSOR(warning_battery_too_low_to_charge, QPIWS)
173 PIPSOLAR_BINARY_SENSOR(fault_dc_dc_over_current, QPIWS)
174 PIPSOLAR_BINARY_SENSOR(fault_code, QPIWS)
175 PIPSOLAR_BINARY_SENSOR(warning_low_pv_energy, QPIWS)
176 PIPSOLAR_BINARY_SENSOR(warning_high_ac_input_during_bus_soft_start, QPIWS)
177 PIPSOLAR_BINARY_SENSOR(warning_battery_equalization, QPIWS)
178
179 PIPSOLAR_TEXT_SENSOR(last_qpigs, QPIGS)
180 PIPSOLAR_TEXT_SENSOR(last_qpiri, QPIRI)
181 PIPSOLAR_TEXT_SENSOR(last_qmod, QMOD)
182 PIPSOLAR_TEXT_SENSOR(last_qflag, QFLAG)
183 PIPSOLAR_TEXT_SENSOR(last_qpiws, QPIWS)
184 PIPSOLAR_TEXT_SENSOR(last_qt, QT)
185 PIPSOLAR_TEXT_SENSOR(last_qmn, QMN)
186 PIPSOLAR_TEXT_SENSOR(last_qet, QET)
187 PIPSOLAR_TEXT_SENSOR(last_qlt, QLT)
188
189 PIPSOLAR_SWITCH(output_source_priority_utility_switch, QPIRI)
190 PIPSOLAR_SWITCH(output_source_priority_solar_switch, QPIRI)
191 PIPSOLAR_SWITCH(output_source_priority_battery_switch, QPIRI)
192 PIPSOLAR_SWITCH(output_source_priority_hybrid_switch, QPIRI)
193 PIPSOLAR_SWITCH(input_voltage_range_switch, QPIRI)
194 PIPSOLAR_SWITCH(pv_ok_condition_for_parallel_switch, QPIRI)
195 PIPSOLAR_SWITCH(pv_power_balance_switch, QPIRI)
196
197 void queue_command(const std::string &command);
198 void setup() override;
199 void loop() override;
200 void dump_config() override;
201 void update() override;
202
203 protected:
204 static const size_t PIPSOLAR_READ_BUFFER_LENGTH = 128; // maximum supported answer length
205 static const size_t COMMAND_QUEUE_LENGTH = 10;
206 static const size_t COMMAND_TIMEOUT = 5000;
207 static const size_t POLLING_COMMANDS_MAX = 15;
208 void add_polling_command_(const char *command, ENUMPollingCommand polling_command);
209 uint8_t check_incoming_crc_();
210 uint8_t check_incoming_length_(uint8_t length);
211 uint16_t pipsolar_crc_(uint8_t *msg, uint8_t len);
212 bool send_next_command_();
213 bool send_next_poll_();
214
215 void handle_poll_response_(ENUMPollingCommand polling_command, const char *message);
216 void handle_poll_error_(ENUMPollingCommand polling_command);
217 // these handlers are designed in a way that an empty message sets all sensors to unknown
218 void handle_qpiri_(const char *message);
219 void handle_qpigs_(const char *message);
220 void handle_qmod_(const char *message);
221 void handle_qflag_(const char *message);
222 void handle_qpiws_(const char *message);
223 void handle_qt_(const char *message);
224 void handle_qmn_(const char *message);
226
227 void skip_start_(const char *message, size_t *pos);
228 void skip_field_(const char *message, size_t *pos);
229 std::string read_field_(const char *message, size_t *pos);
230
231 void read_float_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor);
232 void read_int_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor);
233
234 void publish_binary_sensor_(esphome::optional<bool> b, binary_sensor::BinarySensor *sensor);
235
236 esphome::optional<bool> get_bit_(std::string bits, uint8_t bit_pos);
237
241 size_t read_pos_{0};
242
244 uint8_t state_;
253
256};
257
258} // namespace esphome::pipsolar
uint8_t raw[35]
Definition bl0939.h:0
This class simplifies creating components that periodically check a state.
Definition component.h:519
Base class for all binary_sensor-type classes.
static const size_t COMMAND_QUEUE_LENGTH
Definition pipsolar.h:205
void handle_qmod_(const char *message)
Definition pipsolar.cpp:430
uint8_t read_buffer_[PIPSOLAR_READ_BUFFER_LENGTH]
Definition pipsolar.h:240
uint16_t pipsolar_crc_(uint8_t *msg, uint8_t len)
Definition pipsolar.cpp:810
void handle_poll_error_(ENUMPollingCommand polling_command)
Definition pipsolar.cpp:289
void handle_qpiws_(const char *message)
Definition pipsolar.cpp:505
esphome::optional< bool > get_bit_(std::string bits, uint8_t bit_pos)
Definition pipsolar.cpp:763
void handle_qt_(const char *message)
Definition pipsolar.cpp:677
uint8_t check_incoming_length_(uint8_t length)
Definition pipsolar.cpp:153
void handle_qpigs_(const char *message)
Definition pipsolar.cpp:377
void handle_poll_response_(ENUMPollingCommand polling_command, const char *message)
Definition pipsolar.cpp:256
static const size_t PIPSOLAR_READ_BUFFER_LENGTH
Definition pipsolar.h:204
std::string read_field_(const char *message, size_t *pos)
Definition pipsolar.cpp:715
std::string command_queue_[COMMAND_QUEUE_LENGTH]
Definition pipsolar.h:238
void read_int_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor)
Definition pipsolar.cpp:743
void read_float_sensor_(const char *message, size_t *pos, sensor::Sensor *sensor)
Definition pipsolar.cpp:735
void handle_qflag_(const char *message)
Definition pipsolar.cpp:446
void skip_field_(const char *message, size_t *pos)
Definition pipsolar.cpp:705
void handle_qpiri_(const char *message)
Definition pipsolar.cpp:294
PollingCommand enabled_polling_commands_[POLLING_COMMANDS_MAX]
Definition pipsolar.h:255
void publish_binary_sensor_(esphome::optional< bool > b, binary_sensor::BinarySensor *sensor)
Definition pipsolar.cpp:753
static const size_t POLLING_COMMANDS_MAX
Definition pipsolar.h:207
void skip_start_(const char *message, size_t *pos)
Definition pipsolar.cpp:700
void handle_single_int_(const char *message, text_sensor::TextSensor *raw, sensor::Sensor *value)
Definition pipsolar.cpp:690
static const size_t COMMAND_TIMEOUT
Definition pipsolar.h:206
void add_polling_command_(const char *command, ENUMPollingCommand polling_command)
Definition pipsolar.cpp:786
void handle_qmn_(const char *message)
Definition pipsolar.cpp:683
Base-class for all sensors.
Definition sensor.h:47
const LogString * message
Definition component.cpp:35
const void size_t len
Definition hal.h:68
size_t size_t pos
Definition helpers.h:1255
static void uint32_t
ENUMPollingCommand identifier
Definition pipsolar.h:29
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