From 8ba7e37df10162c4a5213bbf76ca4cdadf5b4020 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Fri, 18 Feb 2022 16:13:27 +1300 Subject: [PATCH] Rewrite PMSX003 to be less repetitive --- esphome/components/pmsx003/pmsx003.cpp | 89 +++++++------------------- esphome/components/pmsx003/pmsx003.h | 62 ++++++++---------- esphome/components/pmsx003/sensor.py | 81 +++++++---------------- 3 files changed, 71 insertions(+), 161 deletions(-) diff --git a/esphome/components/pmsx003/pmsx003.cpp b/esphome/components/pmsx003/pmsx003.cpp index 5de94699f0..7a18556028 100644 --- a/esphome/components/pmsx003/pmsx003.cpp +++ b/esphome/components/pmsx003/pmsx003.cpp @@ -6,47 +6,6 @@ namespace pmsx003 { static const char *const TAG = "pmsx003"; -void PMSX003Component::set_pm_1_0_std_sensor(sensor::Sensor *pm_1_0_std_sensor) { - pm_1_0_std_sensor_ = pm_1_0_std_sensor; -} -void PMSX003Component::set_pm_2_5_std_sensor(sensor::Sensor *pm_2_5_std_sensor) { - pm_2_5_std_sensor_ = pm_2_5_std_sensor; -} -void PMSX003Component::set_pm_10_0_std_sensor(sensor::Sensor *pm_10_0_std_sensor) { - pm_10_0_std_sensor_ = pm_10_0_std_sensor; -} - -void PMSX003Component::set_pm_1_0_sensor(sensor::Sensor *pm_1_0_sensor) { pm_1_0_sensor_ = pm_1_0_sensor; } -void PMSX003Component::set_pm_2_5_sensor(sensor::Sensor *pm_2_5_sensor) { pm_2_5_sensor_ = pm_2_5_sensor; } -void PMSX003Component::set_pm_10_0_sensor(sensor::Sensor *pm_10_0_sensor) { pm_10_0_sensor_ = pm_10_0_sensor; } - -void PMSX003Component::set_pm_particles_03um_sensor(sensor::Sensor *pm_particles_03um_sensor) { - pm_particles_03um_sensor_ = pm_particles_03um_sensor; -} -void PMSX003Component::set_pm_particles_05um_sensor(sensor::Sensor *pm_particles_05um_sensor) { - pm_particles_05um_sensor_ = pm_particles_05um_sensor; -} -void PMSX003Component::set_pm_particles_10um_sensor(sensor::Sensor *pm_particles_10um_sensor) { - pm_particles_10um_sensor_ = pm_particles_10um_sensor; -} -void PMSX003Component::set_pm_particles_25um_sensor(sensor::Sensor *pm_particles_25um_sensor) { - pm_particles_25um_sensor_ = pm_particles_25um_sensor; -} -void PMSX003Component::set_pm_particles_50um_sensor(sensor::Sensor *pm_particles_50um_sensor) { - pm_particles_50um_sensor_ = pm_particles_50um_sensor; -} -void PMSX003Component::set_pm_particles_100um_sensor(sensor::Sensor *pm_particles_100um_sensor) { - pm_particles_100um_sensor_ = pm_particles_100um_sensor; -} - -void PMSX003Component::set_temperature_sensor(sensor::Sensor *temperature_sensor) { - temperature_sensor_ = temperature_sensor; -} -void PMSX003Component::set_humidity_sensor(sensor::Sensor *humidity_sensor) { humidity_sensor_ = humidity_sensor; } -void PMSX003Component::set_formaldehyde_sensor(sensor::Sensor *formaldehyde_sensor) { - formaldehyde_sensor_ = formaldehyde_sensor; -} - void PMSX003Component::loop() { const uint32_t now = millis(); if (now - this->last_transmission_ >= 500) { @@ -163,12 +122,12 @@ void PMSX003Component::parse_data_() { uint16_t pm_2_5_concentration = this->get_16_bit_uint_(12); uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); - uint16_t pm_particles_03um = this->get_16_bit_uint_(16); - uint16_t pm_particles_05um = this->get_16_bit_uint_(18); - uint16_t pm_particles_10um = this->get_16_bit_uint_(20); - uint16_t pm_particles_25um = this->get_16_bit_uint_(22); - uint16_t pm_particles_50um = this->get_16_bit_uint_(24); - uint16_t pm_particles_100um = this->get_16_bit_uint_(26); + uint16_t pm_0_3um = this->get_16_bit_uint_(16); + uint16_t pm_0_5um = this->get_16_bit_uint_(18); + uint16_t pm_1_0um = this->get_16_bit_uint_(20); + uint16_t pm_2_5um = this->get_16_bit_uint_(22); + uint16_t pm_5_0um = this->get_16_bit_uint_(24); + uint16_t pm_10_0um = this->get_16_bit_uint_(26); ESP_LOGD(TAG, "Got PM1.0 Concentration: %u µg/m^3, PM2.5 Concentration %u µg/m^3, PM10.0 Concentration: %u µg/m^3", @@ -188,18 +147,18 @@ void PMSX003Component::parse_data_() { if (this->pm_10_0_sensor_ != nullptr) this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); - if (this->pm_particles_03um_sensor_ != nullptr) - this->pm_particles_03um_sensor_->publish_state(pm_particles_03um); - if (this->pm_particles_05um_sensor_ != nullptr) - this->pm_particles_05um_sensor_->publish_state(pm_particles_05um); - if (this->pm_particles_10um_sensor_ != nullptr) - this->pm_particles_10um_sensor_->publish_state(pm_particles_10um); - if (this->pm_particles_25um_sensor_ != nullptr) - this->pm_particles_25um_sensor_->publish_state(pm_particles_25um); - if (this->pm_particles_50um_sensor_ != nullptr) - this->pm_particles_50um_sensor_->publish_state(pm_particles_50um); - if (this->pm_particles_100um_sensor_ != nullptr) - this->pm_particles_100um_sensor_->publish_state(pm_particles_100um); + if (this->pm_0_3um_sensor_ != nullptr) + this->pm_0_3um_sensor_->publish_state(pm_0_3um); + if (this->pm_0_5um_sensor_ != nullptr) + this->pm_0_5um_sensor_->publish_state(pm_0_5um); + if (this->pm_1_0um_sensor_ != nullptr) + this->pm_1_0um_sensor_->publish_state(pm_1_0um); + if (this->pm_2_5um_sensor_ != nullptr) + this->pm_2_5um_sensor_->publish_state(pm_2_5um); + if (this->pm_5_0um_sensor_ != nullptr) + this->pm_5_0um_sensor_->publish_state(pm_5_0um); + if (this->pm_10_0um_sensor_ != nullptr) + this->pm_10_0um_sensor_->publish_state(pm_10_0um); break; } case PMSX003_TYPE_5003T: { @@ -233,12 +192,12 @@ void PMSX003Component::dump_config() { LOG_SENSOR(" ", "PM2.5", this->pm_2_5_sensor_); LOG_SENSOR(" ", "PM10.0", this->pm_10_0_sensor_); - LOG_SENSOR(" ", "PM0.3um", this->pm_particles_03um_sensor_); - LOG_SENSOR(" ", "PM0.5um", this->pm_particles_05um_sensor_); - LOG_SENSOR(" ", "PM1.0um", this->pm_particles_10um_sensor_); - LOG_SENSOR(" ", "PM2.5um", this->pm_particles_25um_sensor_); - LOG_SENSOR(" ", "PM5.0um", this->pm_particles_50um_sensor_); - LOG_SENSOR(" ", "PM10.0um", this->pm_particles_100um_sensor_); + LOG_SENSOR(" ", "PM0.3um", this->pm_0_3um_sensor_); + LOG_SENSOR(" ", "PM0.5um", this->pm_0_5um_sensor_); + LOG_SENSOR(" ", "PM1.0um", this->pm_1_0um_sensor_); + LOG_SENSOR(" ", "PM2.5um", this->pm_2_5um_sensor_); + LOG_SENSOR(" ", "PM5.0um", this->pm_5_0um_sensor_); + LOG_SENSOR(" ", "PM10.0um", this->pm_10_0um_sensor_); LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); LOG_SENSOR(" ", "Humidity", this->humidity_sensor_); diff --git a/esphome/components/pmsx003/pmsx003.h b/esphome/components/pmsx003/pmsx003.h index fd6364c70c..612c508b5a 100644 --- a/esphome/components/pmsx003/pmsx003.h +++ b/esphome/components/pmsx003/pmsx003.h @@ -14,6 +14,13 @@ enum PMSX003Type { PMSX003_TYPE_5003S, }; +#define PMSX003_SENSOR(key) \ + protected: \ + sensor::Sensor *key##_; \ +\ + public: \ + void set_##key##_sensor(sensor::Sensor *sensor) { this->key##_ = sensor; } + class PMSX003Component : public uart::UARTDevice, public Component { public: PMSX003Component() = default; @@ -23,24 +30,27 @@ class PMSX003Component : public uart::UARTDevice, public Component { void set_type(PMSX003Type type) { type_ = type; } - void set_pm_1_0_std_sensor(sensor::Sensor *pm_1_0_std_sensor); - void set_pm_2_5_std_sensor(sensor::Sensor *pm_2_5_std_sensor); - void set_pm_10_0_std_sensor(sensor::Sensor *pm_10_0_std_sensor); + // "Standard Particle" + PMSX003_SENSOR(pm_1_0_std) + PMSX003_SENSOR(pm_2_5_std) + PMSX003_SENSOR(pm_10_0_std) - void set_pm_1_0_sensor(sensor::Sensor *pm_1_0_sensor); - void set_pm_2_5_sensor(sensor::Sensor *pm_2_5_sensor); - void set_pm_10_0_sensor(sensor::Sensor *pm_10_0_sensor); + // "Under Atmospheric Pressure" + PMSX003_SENSOR(pm_1_0) + PMSX003_SENSOR(pm_2_5) + PMSX003_SENSOR(pm_10_0) - void set_pm_particles_03um_sensor(sensor::Sensor *pm_particles_03um_sensor); - void set_pm_particles_05um_sensor(sensor::Sensor *pm_particles_05um_sensor); - void set_pm_particles_10um_sensor(sensor::Sensor *pm_particles_10um_sensor); - void set_pm_particles_25um_sensor(sensor::Sensor *pm_particles_25um_sensor); - void set_pm_particles_50um_sensor(sensor::Sensor *pm_particles_50um_sensor); - void set_pm_particles_100um_sensor(sensor::Sensor *pm_particles_100um_sensor); + // Particle counts by size + PMSX003_SENSOR(pm_0_3um) + PMSX003_SENSOR(pm_0_5um) + PMSX003_SENSOR(pm_1_0um) + PMSX003_SENSOR(pm_2_5um) + PMSX003_SENSOR(pm_5_0um) + PMSX003_SENSOR(pm_10_0um) - void set_temperature_sensor(sensor::Sensor *temperature_sensor); - void set_humidity_sensor(sensor::Sensor *humidity_sensor); - void set_formaldehyde_sensor(sensor::Sensor *formaldehyde_sensor); + PMSX003_SENSOR(temperature) + PMSX003_SENSOR(humidity) + PMSX003_SENSOR(formaldehyde) protected: optional check_byte_(); @@ -51,28 +61,6 @@ class PMSX003Component : public uart::UARTDevice, public Component { uint8_t data_index_{0}; uint32_t last_transmission_{0}; PMSX003Type type_; - - // "Standard Particle" - sensor::Sensor *pm_1_0_std_sensor_{nullptr}; - sensor::Sensor *pm_2_5_std_sensor_{nullptr}; - sensor::Sensor *pm_10_0_std_sensor_{nullptr}; - - // "Under Atmospheric Pressure" - sensor::Sensor *pm_1_0_sensor_{nullptr}; - sensor::Sensor *pm_2_5_sensor_{nullptr}; - sensor::Sensor *pm_10_0_sensor_{nullptr}; - - // Particle counts by size - sensor::Sensor *pm_particles_03um_sensor_{nullptr}; - sensor::Sensor *pm_particles_05um_sensor_{nullptr}; - sensor::Sensor *pm_particles_10um_sensor_{nullptr}; - sensor::Sensor *pm_particles_25um_sensor_{nullptr}; - sensor::Sensor *pm_particles_50um_sensor_{nullptr}; - sensor::Sensor *pm_particles_100um_sensor_{nullptr}; - - sensor::Sensor *temperature_sensor_{nullptr}; - sensor::Sensor *humidity_sensor_{nullptr}; - sensor::Sensor *formaldehyde_sensor_{nullptr}; }; } // namespace pmsx003 diff --git a/esphome/components/pmsx003/sensor.py b/esphome/components/pmsx003/sensor.py index b731e48e31..68f9e1cb14 100644 --- a/esphome/components/pmsx003/sensor.py +++ b/esphome/components/pmsx003/sensor.py @@ -68,6 +68,24 @@ def validate_pmsx003_sensors(value): return value +SENSORS = [ + CONF_PM_1_0_STD, + CONF_PM_2_5_STD, + CONF_PM_10_0_STD, + CONF_PM_1_0, + CONF_PM_2_5, + CONF_PM_10_0, + CONF_PM_0_3UM, + CONF_PM_0_5UM, + CONF_PM_1_0UM, + CONF_PM_2_5UM, + CONF_PM_5_0UM, + CONF_PM_10_0UM, + CONF_TEMPERATURE, + CONF_HUMIDITY, + CONF_FORMALDEHYDE, +] + CONFIG_SCHEMA = ( cv.Schema( { @@ -171,62 +189,7 @@ async def to_code(config): cg.add(var.set_type(config[CONF_TYPE])) - if CONF_PM_1_0_STD in config: - sens = await sensor.new_sensor(config[CONF_PM_1_0_STD]) - cg.add(var.set_pm_1_0_std_sensor(sens)) - - if CONF_PM_2_5_STD in config: - sens = await sensor.new_sensor(config[CONF_PM_2_5_STD]) - cg.add(var.set_pm_2_5_std_sensor(sens)) - - if CONF_PM_10_0_STD in config: - sens = await sensor.new_sensor(config[CONF_PM_10_0_STD]) - cg.add(var.set_pm_10_0_std_sensor(sens)) - - if CONF_PM_1_0 in config: - sens = await sensor.new_sensor(config[CONF_PM_1_0]) - cg.add(var.set_pm_1_0_sensor(sens)) - - if CONF_PM_2_5 in config: - sens = await sensor.new_sensor(config[CONF_PM_2_5]) - cg.add(var.set_pm_2_5_sensor(sens)) - - if CONF_PM_10_0 in config: - sens = await sensor.new_sensor(config[CONF_PM_10_0]) - cg.add(var.set_pm_10_0_sensor(sens)) - - if CONF_PM_0_3UM in config: - sens = await sensor.new_sensor(config[CONF_PM_0_3UM]) - cg.add(var.set_pm_particles_03um_sensor(sens)) - - if CONF_PM_0_5UM in config: - sens = await sensor.new_sensor(config[CONF_PM_0_5UM]) - cg.add(var.set_pm_particles_05um_sensor(sens)) - - if CONF_PM_1_0UM in config: - sens = await sensor.new_sensor(config[CONF_PM_1_0UM]) - cg.add(var.set_pm_particles_10um_sensor(sens)) - - if CONF_PM_2_5UM in config: - sens = await sensor.new_sensor(config[CONF_PM_2_5UM]) - cg.add(var.set_pm_particles_25um_sensor(sens)) - - if CONF_PM_5_0UM in config: - sens = await sensor.new_sensor(config[CONF_PM_5_0UM]) - cg.add(var.set_pm_particles_50um_sensor(sens)) - - if CONF_PM_10_0UM in config: - sens = await sensor.new_sensor(config[CONF_PM_10_0UM]) - cg.add(var.set_pm_particles_100um_sensor(sens)) - - if CONF_TEMPERATURE in config: - sens = await sensor.new_sensor(config[CONF_TEMPERATURE]) - cg.add(var.set_temperature_sensor(sens)) - - if CONF_HUMIDITY in config: - sens = await sensor.new_sensor(config[CONF_HUMIDITY]) - cg.add(var.set_humidity_sensor(sens)) - - if CONF_FORMALDEHYDE in config: - sens = await sensor.new_sensor(config[CONF_FORMALDEHYDE]) - cg.add(var.set_formaldehyde_sensor(sens)) + for key in SENSORS: + if key in config: + sens = await sensor.new_sensor(config[key]) + cg.add(getattr(var, f"set_{key}_sensor")(sens))