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