ESPHome  2024.11.0
pipsolar.h
Go to the documentation of this file.
1 #pragma once
2 
10 
11 namespace esphome {
12 namespace pipsolar {
13 
22 };
24  uint8_t *command;
25  uint8_t length = 0;
26  uint8_t errors;
28 };
29 
30 #define PIPSOLAR_VALUED_ENTITY_(type, name, polling_command, value_type) \
31  protected: \
32  value_type value_##name##_; \
33  PIPSOLAR_ENTITY_(type, name, polling_command)
34 
35 #define PIPSOLAR_ENTITY_(type, name, polling_command) \
36  protected: \
37  type *name##_{}; /* NOLINT */ \
38 \
39  public: \
40  void set_##name(type *name) { /* NOLINT */ \
41  this->name##_ = name; \
42  this->add_polling_command_(#polling_command, POLLING_##polling_command); \
43  }
44 
45 #define PIPSOLAR_SENSOR(name, polling_command, value_type) \
46  PIPSOLAR_VALUED_ENTITY_(sensor::Sensor, name, polling_command, value_type)
47 #define PIPSOLAR_SWITCH(name, polling_command) PIPSOLAR_ENTITY_(switch_::Switch, name, polling_command)
48 #define PIPSOLAR_BINARY_SENSOR(name, polling_command, value_type) \
49  PIPSOLAR_VALUED_ENTITY_(binary_sensor::BinarySensor, name, polling_command, value_type)
50 #define PIPSOLAR_VALUED_TEXT_SENSOR(name, polling_command, value_type) \
51  PIPSOLAR_VALUED_ENTITY_(text_sensor::TextSensor, name, polling_command, value_type)
52 #define PIPSOLAR_TEXT_SENSOR(name, polling_command) PIPSOLAR_ENTITY_(text_sensor::TextSensor, name, polling_command)
53 
54 class Pipsolar : public uart::UARTDevice, public PollingComponent {
55  // QPIGS values
56  PIPSOLAR_SENSOR(grid_voltage, QPIGS, float)
57  PIPSOLAR_SENSOR(grid_frequency, QPIGS, float)
58  PIPSOLAR_SENSOR(ac_output_voltage, QPIGS, float)
59  PIPSOLAR_SENSOR(ac_output_frequency, QPIGS, float)
60  PIPSOLAR_SENSOR(ac_output_apparent_power, QPIGS, int)
61  PIPSOLAR_SENSOR(ac_output_active_power, QPIGS, int)
62  PIPSOLAR_SENSOR(output_load_percent, QPIGS, int)
63  PIPSOLAR_SENSOR(bus_voltage, QPIGS, int)
64  PIPSOLAR_SENSOR(battery_voltage, QPIGS, float)
65  PIPSOLAR_SENSOR(battery_charging_current, QPIGS, int)
66  PIPSOLAR_SENSOR(battery_capacity_percent, QPIGS, int)
67  PIPSOLAR_SENSOR(inverter_heat_sink_temperature, QPIGS, int)
68  PIPSOLAR_SENSOR(pv_input_current_for_battery, QPIGS, float)
69  PIPSOLAR_SENSOR(pv_input_voltage, QPIGS, float)
70  PIPSOLAR_SENSOR(battery_voltage_scc, QPIGS, float)
71  PIPSOLAR_SENSOR(battery_discharge_current, QPIGS, int)
72  PIPSOLAR_BINARY_SENSOR(add_sbu_priority_version, QPIGS, int)
73  PIPSOLAR_BINARY_SENSOR(configuration_status, QPIGS, int)
74  PIPSOLAR_BINARY_SENSOR(scc_firmware_version, QPIGS, int)
75  PIPSOLAR_BINARY_SENSOR(load_status, QPIGS, int)
76  PIPSOLAR_BINARY_SENSOR(battery_voltage_to_steady_while_charging, QPIGS, int)
77  PIPSOLAR_BINARY_SENSOR(charging_status, QPIGS, int)
78  PIPSOLAR_BINARY_SENSOR(scc_charging_status, QPIGS, int)
79  PIPSOLAR_BINARY_SENSOR(ac_charging_status, QPIGS, int)
80  PIPSOLAR_SENSOR(battery_voltage_offset_for_fans_on, QPIGS, int) //.1 scale
81  PIPSOLAR_SENSOR(eeprom_version, QPIGS, int)
82  PIPSOLAR_SENSOR(pv_charging_power, QPIGS, int)
83  PIPSOLAR_BINARY_SENSOR(charging_to_floating_mode, QPIGS, int)
84  PIPSOLAR_BINARY_SENSOR(switch_on, QPIGS, int)
85  PIPSOLAR_BINARY_SENSOR(dustproof_installed, QPIGS, int)
86 
87  // QPIRI values
88  PIPSOLAR_SENSOR(grid_rating_voltage, QPIRI, float)
89  PIPSOLAR_SENSOR(grid_rating_current, QPIRI, float)
90  PIPSOLAR_SENSOR(ac_output_rating_voltage, QPIRI, float)
91  PIPSOLAR_SENSOR(ac_output_rating_frequency, QPIRI, float)
92  PIPSOLAR_SENSOR(ac_output_rating_current, QPIRI, float)
93  PIPSOLAR_SENSOR(ac_output_rating_apparent_power, QPIRI, int)
94  PIPSOLAR_SENSOR(ac_output_rating_active_power, QPIRI, int)
95  PIPSOLAR_SENSOR(battery_rating_voltage, QPIRI, float)
96  PIPSOLAR_SENSOR(battery_recharge_voltage, QPIRI, float)
97  PIPSOLAR_SENSOR(battery_under_voltage, QPIRI, float)
98  PIPSOLAR_SENSOR(battery_bulk_voltage, QPIRI, float)
99  PIPSOLAR_SENSOR(battery_float_voltage, QPIRI, float)
100  PIPSOLAR_SENSOR(battery_type, QPIRI, int)
101  PIPSOLAR_SENSOR(current_max_ac_charging_current, QPIRI, int)
102  PIPSOLAR_SENSOR(current_max_charging_current, QPIRI, int)
103  PIPSOLAR_SENSOR(input_voltage_range, QPIRI, int)
104  PIPSOLAR_SENSOR(output_source_priority, QPIRI, int)
105  PIPSOLAR_SENSOR(charger_source_priority, QPIRI, int)
106  PIPSOLAR_SENSOR(parallel_max_num, QPIRI, int)
107  PIPSOLAR_SENSOR(machine_type, QPIRI, int)
108  PIPSOLAR_SENSOR(topology, QPIRI, int)
109  PIPSOLAR_SENSOR(output_mode, QPIRI, int)
110  PIPSOLAR_SENSOR(battery_redischarge_voltage, QPIRI, float)
111  PIPSOLAR_SENSOR(pv_ok_condition_for_parallel, QPIRI, int)
112  PIPSOLAR_SENSOR(pv_power_balance, QPIRI, int)
113 
114  // QMOD values
115  PIPSOLAR_VALUED_TEXT_SENSOR(device_mode, QMOD, char)
116 
117  // QFLAG values
118  PIPSOLAR_BINARY_SENSOR(silence_buzzer_open_buzzer, QFLAG, int)
119  PIPSOLAR_BINARY_SENSOR(overload_bypass_function, QFLAG, int)
120  PIPSOLAR_BINARY_SENSOR(lcd_escape_to_default, QFLAG, int)
121  PIPSOLAR_BINARY_SENSOR(overload_restart_function, QFLAG, int)
122  PIPSOLAR_BINARY_SENSOR(over_temperature_restart_function, QFLAG, int)
123  PIPSOLAR_BINARY_SENSOR(backlight_on, QFLAG, int)
124  PIPSOLAR_BINARY_SENSOR(alarm_on_when_primary_source_interrupt, QFLAG, int)
125  PIPSOLAR_BINARY_SENSOR(fault_code_record, QFLAG, int)
126  PIPSOLAR_BINARY_SENSOR(power_saving, QFLAG, int)
127 
128  // QPIWS values
129  PIPSOLAR_BINARY_SENSOR(warnings_present, QPIWS, bool)
130  PIPSOLAR_BINARY_SENSOR(faults_present, QPIWS, bool)
131  PIPSOLAR_BINARY_SENSOR(warning_power_loss, QPIWS, bool)
132  PIPSOLAR_BINARY_SENSOR(fault_inverter_fault, QPIWS, bool)
133  PIPSOLAR_BINARY_SENSOR(fault_bus_over, QPIWS, bool)
134  PIPSOLAR_BINARY_SENSOR(fault_bus_under, QPIWS, bool)
135  PIPSOLAR_BINARY_SENSOR(fault_bus_soft_fail, QPIWS, bool)
136  PIPSOLAR_BINARY_SENSOR(warning_line_fail, QPIWS, bool)
137  PIPSOLAR_BINARY_SENSOR(fault_opvshort, QPIWS, bool)
138  PIPSOLAR_BINARY_SENSOR(fault_inverter_voltage_too_low, QPIWS, bool)
139  PIPSOLAR_BINARY_SENSOR(fault_inverter_voltage_too_high, QPIWS, bool)
140  PIPSOLAR_BINARY_SENSOR(warning_over_temperature, QPIWS, bool)
141  PIPSOLAR_BINARY_SENSOR(warning_fan_lock, QPIWS, bool)
142  PIPSOLAR_BINARY_SENSOR(warning_battery_voltage_high, QPIWS, bool)
143  PIPSOLAR_BINARY_SENSOR(warning_battery_low_alarm, QPIWS, bool)
144  PIPSOLAR_BINARY_SENSOR(warning_battery_under_shutdown, QPIWS, bool)
145  PIPSOLAR_BINARY_SENSOR(warning_battery_derating, QPIWS, bool)
146  PIPSOLAR_BINARY_SENSOR(warning_over_load, QPIWS, bool)
147  PIPSOLAR_BINARY_SENSOR(warning_eeprom_failed, QPIWS, bool)
148  PIPSOLAR_BINARY_SENSOR(fault_inverter_over_current, QPIWS, bool)
149  PIPSOLAR_BINARY_SENSOR(fault_inverter_soft_failed, QPIWS, bool)
150  PIPSOLAR_BINARY_SENSOR(fault_self_test_failed, QPIWS, bool)
151  PIPSOLAR_BINARY_SENSOR(fault_op_dc_voltage_over, QPIWS, bool)
152  PIPSOLAR_BINARY_SENSOR(fault_battery_open, QPIWS, bool)
153  PIPSOLAR_BINARY_SENSOR(fault_current_sensor_failed, QPIWS, bool)
154  PIPSOLAR_BINARY_SENSOR(fault_battery_short, QPIWS, bool)
155  PIPSOLAR_BINARY_SENSOR(warning_power_limit, QPIWS, bool)
156  PIPSOLAR_BINARY_SENSOR(warning_pv_voltage_high, QPIWS, bool)
157  PIPSOLAR_BINARY_SENSOR(fault_mppt_overload, QPIWS, bool)
158  PIPSOLAR_BINARY_SENSOR(warning_mppt_overload, QPIWS, bool)
159  PIPSOLAR_BINARY_SENSOR(warning_battery_too_low_to_charge, QPIWS, bool)
160  PIPSOLAR_BINARY_SENSOR(fault_dc_dc_over_current, QPIWS, bool)
161  PIPSOLAR_BINARY_SENSOR(fault_code, QPIWS, int)
162  PIPSOLAR_BINARY_SENSOR(warnung_low_pv_energy, QPIWS, bool)
163  PIPSOLAR_BINARY_SENSOR(warning_high_ac_input_during_bus_soft_start, QPIWS, bool)
164  PIPSOLAR_BINARY_SENSOR(warning_battery_equalization, QPIWS, bool)
165 
166  PIPSOLAR_TEXT_SENSOR(last_qpigs, QPIGS)
167  PIPSOLAR_TEXT_SENSOR(last_qpiri, QPIRI)
168  PIPSOLAR_TEXT_SENSOR(last_qmod, QMOD)
169  PIPSOLAR_TEXT_SENSOR(last_qflag, QFLAG)
170  PIPSOLAR_TEXT_SENSOR(last_qpiws, QPIWS)
171  PIPSOLAR_TEXT_SENSOR(last_qt, QT)
172  PIPSOLAR_TEXT_SENSOR(last_qmn, QMN)
173 
174  PIPSOLAR_SWITCH(output_source_priority_utility_switch, QPIRI)
175  PIPSOLAR_SWITCH(output_source_priority_solar_switch, QPIRI)
176  PIPSOLAR_SWITCH(output_source_priority_battery_switch, QPIRI)
177  PIPSOLAR_SWITCH(output_source_priority_hybrid_switch, QPIRI)
178  PIPSOLAR_SWITCH(input_voltage_range_switch, QPIRI)
179  PIPSOLAR_SWITCH(pv_ok_condition_for_parallel_switch, QPIRI)
180  PIPSOLAR_SWITCH(pv_power_balance_switch, QPIRI)
181 
182  void switch_command(const std::string &command);
183  void setup() override;
184  void loop() override;
185  void dump_config() override;
186  void update() override;
187 
188  protected:
189  static const size_t PIPSOLAR_READ_BUFFER_LENGTH = 110; // maximum supported answer length
190  static const size_t COMMAND_QUEUE_LENGTH = 10;
191  static const size_t COMMAND_TIMEOUT = 5000;
192  uint32_t last_poll_ = 0;
193  void add_polling_command_(const char *command, ENUMPollingCommand polling_command);
194  void empty_uart_buffer_();
195  uint8_t check_incoming_crc_();
196  uint8_t check_incoming_length_(uint8_t length);
197  uint16_t pipsolar_crc_(uint8_t *msg, uint8_t len);
198  uint8_t send_next_command_();
199  void send_next_poll_();
200  void queue_command_(const char *command, uint8_t length);
201  std::string command_queue_[COMMAND_QUEUE_LENGTH];
202  uint8_t command_queue_position_ = 0;
203  uint8_t read_buffer_[PIPSOLAR_READ_BUFFER_LENGTH];
204  size_t read_pos_{0};
205 
206  uint32_t command_start_millis_ = 0;
207  uint8_t state_;
208  enum State {
210  STATE_POLL = 1,
211  STATE_COMMAND = 2,
212  STATE_POLL_COMPLETE = 3,
213  STATE_COMMAND_COMPLETE = 4,
214  STATE_POLL_CHECKED = 5,
215  STATE_POLL_DECODED = 6,
216  };
217 
218  uint8_t last_polling_command_ = 0;
219  PollingCommand used_polling_commands_[15];
220 };
221 
222 } // namespace pipsolar
223 } // namespace esphome
void setup()
void loop()
ENUMPollingCommand identifier
Definition: pipsolar.h:27
This class simplifies creating components that periodically check a state.
Definition: component.h:283
std::string size_t len
Definition: helpers.h:293
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7