Code review fixes

This commit is contained in:
Chris Feenstra
2023-11-06 22:40:12 +00:00
parent 0277c0731f
commit b1fb9eb9b1
3 changed files with 32 additions and 118 deletions
@@ -44,29 +44,9 @@ void KamstrupKMPComponent::dump_config() {
LOG_SENSOR(" ", "Volume", this->volume_sensor_);
}
if (this->custom1_sensor_ != nullptr) {
LOG_SENSOR(" ", "Custom Sensor 1", this->custom1_sensor_);
ESP_LOGCONFIG(TAG, " Command: 0x%04X", this->custom1_command_);
}
if (this->custom2_sensor_ != nullptr) {
LOG_SENSOR(" ", "Custom Sensor 2", this->custom2_sensor_);
ESP_LOGCONFIG(TAG, " Command: 0x%04X", this->custom2_command_);
}
if (this->custom3_sensor_ != nullptr) {
LOG_SENSOR(" ", "Custom Sensor 3", this->custom3_sensor_);
ESP_LOGCONFIG(TAG, " Command: 0x%04X", this->custom3_command_);
}
if (this->custom4_sensor_ != nullptr) {
LOG_SENSOR(" ", "Custom Sensor 4", this->custom4_sensor_);
ESP_LOGCONFIG(TAG, " Command: 0x%04X", this->custom4_command_);
}
if (this->custom5_sensor_ != nullptr) {
LOG_SENSOR(" ", "Custom Sensor 5", this->custom5_sensor_);
ESP_LOGCONFIG(TAG, " Command: 0x%04X", this->custom5_command_);
for (int i = 0; i < this->custom_sensors_.size(); i++) {
LOG_SENSOR(" ", "Custom Sensor", this->custom_sensors_[i]);
ESP_LOGCONFIG(TAG, " Command: 0x%04X", this->custom_commands_[i]);
}
this->check_uart_settings(1200, 2, uart::UART_CONFIG_PARITY_NONE, 8);
@@ -103,24 +83,8 @@ void KamstrupKMPComponent::update() {
this->send_command_(CMD_VOLUME);
}
if (this->custom1_sensor_ != nullptr) {
this->send_command_(this->custom1_command_);
}
if (this->custom2_sensor_ != nullptr) {
this->send_command_(this->custom2_command_);
}
if (this->custom3_sensor_ != nullptr) {
this->send_command_(this->custom3_command_);
}
if (this->custom4_sensor_ != nullptr) {
this->send_command_(this->custom4_command_);
}
if (this->custom5_sensor_ != nullptr) {
this->send_command_(this->custom5_command_);
for (int i = 0; i < this->custom_commands_.size(); i++) {
this->send_command_(this->custom_commands_[i]);
}
}
@@ -318,20 +282,10 @@ void KamstrupKMPComponent::set_sensor_value_(uint16_t command, float value, uint
}
// Custom sensors
if (command == this->custom1_command_ && this->custom1_sensor_ != nullptr) {
this->custom1_sensor_->publish_state(value);
}
if (command == this->custom2_command_ && this->custom2_sensor_ != nullptr) {
this->custom2_sensor_->publish_state(value);
}
if (command == this->custom3_command_ && this->custom3_sensor_ != nullptr) {
this->custom3_sensor_->publish_state(value);
}
if (command == this->custom4_command_ && this->custom4_sensor_ != nullptr) {
this->custom4_sensor_->publish_state(value);
}
if (command == this->custom5_command_ && this->custom5_sensor_ != nullptr) {
this->custom5_sensor_->publish_state(value);
for (int i = 0; i < this->custom_commands_.size(); i++) {
if (command == this->custom_commands_[i]) {
this->custom_sensors_[i]->publish_state(value);
}
}
ESP_LOGD(TAG, "Received value for command 0x%04X: %.3f [%s]", command, value, unit);
+7 -21
View File
@@ -82,20 +82,14 @@ class KamstrupKMPComponent : public PollingComponent, public uart::UARTDevice {
void set_temp_diff_sensor(sensor::Sensor *sensor) { temp_diff_sensor_ = sensor; }
void set_flow_sensor(sensor::Sensor *sensor) { flow_sensor_ = sensor; }
void set_volume_sensor(sensor::Sensor *sensor) { volume_sensor_ = sensor; }
void set_custom1_sensor(sensor::Sensor *sensor) { custom1_sensor_ = sensor; }
void set_custom2_sensor(sensor::Sensor *sensor) { custom2_sensor_ = sensor; }
void set_custom3_sensor(sensor::Sensor *sensor) { custom3_sensor_ = sensor; }
void set_custom4_sensor(sensor::Sensor *sensor) { custom4_sensor_ = sensor; }
void set_custom5_sensor(sensor::Sensor *sensor) { custom5_sensor_ = sensor; }
void set_custom1_command(uint16_t command) { custom1_command_ = command; }
void set_custom2_command(uint16_t command) { custom2_command_ = command; }
void set_custom3_command(uint16_t command) { custom3_command_ = command; }
void set_custom4_command(uint16_t command) { custom4_command_ = command; }
void set_custom5_command(uint16_t command) { custom5_command_ = command; }
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
void update() override;
void set_custom_sensor(int i, sensor::Sensor *sensor, uint16_t command) {
this->custom_sensors_.insert(this->custom_sensors_.begin() + i, sensor);
this->custom_commands_.insert(this->custom_commands_.begin() + i, command);
}
protected:
// Sensors
@@ -106,18 +100,10 @@ class KamstrupKMPComponent : public PollingComponent, public uart::UARTDevice {
sensor::Sensor *temp_diff_sensor_{nullptr};
sensor::Sensor *flow_sensor_{nullptr};
sensor::Sensor *volume_sensor_{nullptr};
sensor::Sensor *custom1_sensor_{nullptr};
sensor::Sensor *custom2_sensor_{nullptr};
sensor::Sensor *custom3_sensor_{nullptr};
sensor::Sensor *custom4_sensor_{nullptr};
sensor::Sensor *custom5_sensor_{nullptr};
// Custom sensor commands
uint16_t custom1_command_{0};
uint16_t custom2_command_{0};
uint16_t custom3_command_{0};
uint16_t custom4_command_{0};
uint16_t custom5_command_{0};
// Custom sensors and commands
std::vector<sensor::Sensor *> custom_sensors_;
std::vector<uint16_t> custom_commands_;
// Methods
+16 -42
View File
@@ -33,11 +33,7 @@ CONF_TEMP1 = "temp1"
CONF_TEMP2 = "temp2"
CONF_TEMP_DIFF = "temp_diff"
CONF_FLOW = "flow"
CONF_CUSTOM_1 = "custom1"
CONF_CUSTOM_2 = "custom2"
CONF_CUSTOM_3 = "custom3"
CONF_CUSTOM_4 = "custom4"
CONF_CUSTOM_5 = "custom5"
CONF_CUSTOM = "custom"
UNIT_GIGA_JOULE = "GJ"
UNIT_LITRE_PER_HOUR = "l/h"
@@ -89,36 +85,14 @@ CONFIG_SCHEMA = (
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_CUBIC_METER,
),
cv.Optional(CONF_CUSTOM_1): sensor.sensor_schema(
accuracy_decimals=1,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_EMPTY,
).extend({cv.Required(CONF_COMMAND): cv.hex_uint16_t}),
cv.Optional(CONF_CUSTOM_2): sensor.sensor_schema(
accuracy_decimals=1,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_EMPTY,
).extend({cv.Required(CONF_COMMAND): cv.hex_uint16_t}),
cv.Optional(CONF_CUSTOM_3): sensor.sensor_schema(
accuracy_decimals=1,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_EMPTY,
).extend({cv.Required(CONF_COMMAND): cv.hex_uint16_t}),
cv.Optional(CONF_CUSTOM_4): sensor.sensor_schema(
accuracy_decimals=1,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_EMPTY,
).extend({cv.Required(CONF_COMMAND): cv.hex_uint16_t}),
cv.Optional(CONF_CUSTOM_5): sensor.sensor_schema(
accuracy_decimals=1,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_EMPTY,
).extend({cv.Required(CONF_COMMAND): cv.hex_uint16_t}),
cv.Optional(CONF_CUSTOM): cv.ensure_list(
sensor.sensor_schema(
accuracy_decimals=1,
device_class=DEVICE_CLASS_EMPTY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_EMPTY,
).extend({cv.Required(CONF_COMMAND): cv.hex_uint16_t})
),
}
)
.extend(cv.polling_component_schema("60s"))
@@ -135,6 +109,7 @@ async def to_code(config):
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
# Standard sensors
for key in [
CONF_HEAT_ENERGY,
CONF_POWER,
@@ -143,16 +118,15 @@ async def to_code(config):
CONF_TEMP_DIFF,
CONF_FLOW,
CONF_VOLUME,
CONF_CUSTOM_1,
CONF_CUSTOM_2,
CONF_CUSTOM_3,
CONF_CUSTOM_4,
CONF_CUSTOM_5,
]:
if key not in config:
continue
conf = config[key]
sens = await sensor.new_sensor(conf)
cg.add(getattr(var, f"set_{key}_sensor")(sens))
if CONF_COMMAND in conf:
cg.add(getattr(var, f"set_{key}_command")(conf[CONF_COMMAND]))
# Custom sensors
if CONF_CUSTOM in config:
for i, conf in enumerate(config[CONF_CUSTOM]):
sens = await sensor.new_sensor(conf)
cg.add(getattr(var, "set_custom_sensor")(i, sens, conf[CONF_COMMAND]))