Skip to content

Commit c36c9ee

Browse files
author
Kevin Hübner
committed
forgot formatting again... :p
1 parent 072454c commit c36c9ee

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

components/soyosource_virtual_meter/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ def validate_min_max(config):
9191
): cv.int_range(min=1, max=5400),
9292
cv.Optional(CONF_ZERO_OUTPUT_ON_MIN_POWER_DEMAND, default=True): cv.boolean,
9393
cv.Optional(
94-
CONF_MAX_POWER_SENSOR_LATENCY_MS, default=DEFAULT_MAX_POWER_SENSOR_LATENCY_MS
95-
): cv.int_range(min=DEFAULT_MIN_MAX_POWER_SENSOR_LATENCY_MS, max=DEFAULT_MAX_MAX_POWER_SENSOR_LATENCY_MS),
94+
CONF_MAX_POWER_SENSOR_LATENCY_MS,
95+
default=DEFAULT_MAX_POWER_SENSOR_LATENCY_MS,
96+
): cv.int_range(
97+
min=DEFAULT_MIN_MAX_POWER_SENSOR_LATENCY_MS,
98+
max=DEFAULT_MAX_MAX_POWER_SENSOR_LATENCY_MS,
99+
),
96100
}
97101
)
98102
.extend(soyosource_modbus.soyosource_modbus_device_schema(0x24))
@@ -124,7 +128,11 @@ async def to_code(config):
124128
)
125129
)
126130
cg.add(var.set_power_demand_calculation(config[CONF_POWER_DEMAND_CALCULATION]))
127-
cg.add(var.set_power_demand_compensation_timeout_ms(config[CONF_MAX_POWER_SENSOR_LATENCY_MS]))
131+
cg.add(
132+
var.set_power_demand_compensation_timeout_ms(
133+
config[CONF_MAX_POWER_SENSOR_LATENCY_MS]
134+
)
135+
)
128136

129137
if CONF_OPERATION_STATUS_ID in config:
130138
operation_status_sensor = await cg.get_variable(

components/soyosource_virtual_meter/soyosource_virtual_meter.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void SoyosourceVirtualMeter::setup() {
2121

2222
this->last_power_demand_received_ = millis();
2323

24-
//if update intervall is set to never, trigger the update() method from here
24+
// if update intervall is set to never, trigger the update() method from here
2525
if (this->get_update_interval() == -1) {
2626
this->update();
2727
}
@@ -112,7 +112,7 @@ int16_t SoyosourceVirtualMeter::calculate_power_demand_negative_measurements_(in
112112
// -700 -690 10 500 -200 0
113113
int16_t importing_now = consumption - this->buffer_;
114114

115-
reset_power_demand_compensation(importing_now);
115+
reset_power_demand_compensation_(importing_now);
116116

117117
int16_t power_demand = importing_now + last_power_demand - this->power_demand_compensation_;
118118

@@ -124,18 +124,20 @@ int16_t SoyosourceVirtualMeter::calculate_power_demand_negative_measurements_(in
124124
power_demand = (this->zero_output_on_min_power_demand_) ? 0 : this->min_power_demand_;
125125
}
126126

127-
ESP_LOGD(TAG, "'%s': updated power_demand_compensation_ from %d", this->get_modbus_name(), this->power_demand_compensation_);
128-
//only reduction of compensation but keep old demand:
127+
ESP_LOGD(TAG, "'%s': updated power_demand_compensation_ from %d", this->get_modbus_name(),
128+
this->power_demand_compensation_);
129+
// only reduction of compensation but keep old demand:
129130
if (this->power_demand_compensation_ != 0 && ((importing_now > 0 && power_demand < last_power_demand) ||
130-
(importing_now < 0 && power_demand > last_power_demand))) {
131+
(importing_now < 0 && power_demand > last_power_demand))) {
131132
this->power_demand_compensation_ += power_demand - last_power_demand;
132133
power_demand = last_power_demand;
133134
ESP_LOGD(TAG, "'%s': Oscillation prevention, keeping previous demand: %d; reducing compensation",
134135
this->get_modbus_name(), last_power_demand);
135136
} else {
136137
this->power_demand_compensation_ += power_demand - last_power_demand;
137138
}
138-
ESP_LOGD(TAG, "'%s': updated power_demand_compensation_ to %d", this->get_modbus_name(), this->power_demand_compensation_);
139+
ESP_LOGD(TAG, "'%s': updated power_demand_compensation_ to %d", this->get_modbus_name(),
140+
this->power_demand_compensation_);
139141

140142
return power_demand;
141143
}
@@ -222,13 +224,15 @@ void SoyosourceVirtualMeter::publish_state_(text_sensor::TextSensor *text_sensor
222224
text_sensor->publish_state(state);
223225
}
224226

225-
void SoyosourceVirtualMeter::reset_power_demand_compensation(int16_t importing_now) {
226-
//reset on zero crossing or timeout
227-
if (importing_now == 0 || (this->power_demand_compensation_ > 0 && importing_now < 0)
228-
|| (this->power_demand_compensation_ < 0 && importing_now > 0)
229-
|| this->power_demand_compensation_timestamp_ + this->power_demand_compensation_timeout_ms_ < millis()) {
227+
void SoyosourceVirtualMeter::reset_power_demand_compensation_(int16_t importing_now) {
228+
// reset on zero crossing or timeout
229+
if (importing_now == 0 || (this->power_demand_compensation_ > 0 && importing_now < 0) ||
230+
(this->power_demand_compensation_ < 0 && importing_now > 0) ||
231+
this->power_demand_compensation_timestamp_ + this->power_demand_compensation_timeout_ms_ < millis()) {
230232
ESP_LOGD(TAG, "'%s': reset power_demand_compensation_ to 0 %s", this->get_modbus_name(),
231-
this->power_demand_compensation_timestamp_ + this->power_demand_compensation_timeout_ms_ < millis() ? "after timout" : "after zero crossing");
233+
this->power_demand_compensation_timestamp_ + this->power_demand_compensation_timeout_ms_ < millis()
234+
? "after timout"
235+
: "after zero crossing");
232236
this->power_demand_compensation_timestamp_ = millis();
233237
this->power_demand_compensation_ = 0;
234238
}

components/soyosource_virtual_meter/soyosource_virtual_meter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class SoyosourceVirtualMeter : public PollingComponent, public soyosource_modbus
4747
void set_power_demand_divider_number(number::Number *power_demand_divider_number) {
4848
power_demand_divider_number_ = power_demand_divider_number;
4949
}
50-
void set_power_demand_compensation_timeout_ms(uint16_t power_demand_compensation_timeout_ms) { this->power_demand_compensation_timeout_ms_ = power_demand_compensation_timeout_ms; }
50+
void set_power_demand_compensation_timeout_ms(uint16_t power_demand_compensation_timeout_ms) {
51+
this->power_demand_compensation_timeout_ms_ = power_demand_compensation_timeout_ms;
52+
}
5153

5254
void set_manual_mode_switch(switch_::Switch *manual_mode_switch) { manual_mode_switch_ = manual_mode_switch; }
5355
void set_emergency_power_off_switch(switch_::Switch *emergency_power_off_switch) {
@@ -107,7 +109,7 @@ class SoyosourceVirtualMeter : public PollingComponent, public soyosource_modbus
107109
int16_t calculate_power_demand_negative_measurements_(int16_t consumption, uint16_t last_power_demand);
108110
int16_t calculate_power_demand_restart_on_crossing_zero_(int16_t consumption, uint16_t last_power_demand);
109111
int16_t calculate_power_demand_oem_(int16_t consumption);
110-
void reset_power_demand_compensation(int16_t importing_now);
112+
void reset_power_demand_compensation_(int16_t importing_now);
111113
};
112114

113115
} // namespace soyosource_virtual_meter

0 commit comments

Comments
 (0)