mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 23:21:54 +00:00 
			
		
		
		
	Rewrite PMSX003 to be less repetitive
This commit is contained in:
		| @@ -6,47 +6,6 @@ namespace pmsx003 { | |||||||
|  |  | ||||||
| static const char *const TAG = "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() { | void PMSX003Component::loop() { | ||||||
|   const uint32_t now = millis(); |   const uint32_t now = millis(); | ||||||
|   if (now - this->last_transmission_ >= 500) { |   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_2_5_concentration = this->get_16_bit_uint_(12); | ||||||
|       uint16_t pm_10_0_concentration = this->get_16_bit_uint_(14); |       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_0_3um = this->get_16_bit_uint_(16); | ||||||
|       uint16_t pm_particles_05um = this->get_16_bit_uint_(18); |       uint16_t pm_0_5um = this->get_16_bit_uint_(18); | ||||||
|       uint16_t pm_particles_10um = this->get_16_bit_uint_(20); |       uint16_t pm_1_0um = this->get_16_bit_uint_(20); | ||||||
|       uint16_t pm_particles_25um = this->get_16_bit_uint_(22); |       uint16_t pm_2_5um = this->get_16_bit_uint_(22); | ||||||
|       uint16_t pm_particles_50um = this->get_16_bit_uint_(24); |       uint16_t pm_5_0um = this->get_16_bit_uint_(24); | ||||||
|       uint16_t pm_particles_100um = this->get_16_bit_uint_(26); |       uint16_t pm_10_0um = this->get_16_bit_uint_(26); | ||||||
|  |  | ||||||
|       ESP_LOGD(TAG, |       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", |                "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) |       if (this->pm_10_0_sensor_ != nullptr) | ||||||
|         this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); |         this->pm_10_0_sensor_->publish_state(pm_10_0_concentration); | ||||||
|  |  | ||||||
|       if (this->pm_particles_03um_sensor_ != nullptr) |       if (this->pm_0_3um_sensor_ != nullptr) | ||||||
|         this->pm_particles_03um_sensor_->publish_state(pm_particles_03um); |         this->pm_0_3um_sensor_->publish_state(pm_0_3um); | ||||||
|       if (this->pm_particles_05um_sensor_ != nullptr) |       if (this->pm_0_5um_sensor_ != nullptr) | ||||||
|         this->pm_particles_05um_sensor_->publish_state(pm_particles_05um); |         this->pm_0_5um_sensor_->publish_state(pm_0_5um); | ||||||
|       if (this->pm_particles_10um_sensor_ != nullptr) |       if (this->pm_1_0um_sensor_ != nullptr) | ||||||
|         this->pm_particles_10um_sensor_->publish_state(pm_particles_10um); |         this->pm_1_0um_sensor_->publish_state(pm_1_0um); | ||||||
|       if (this->pm_particles_25um_sensor_ != nullptr) |       if (this->pm_2_5um_sensor_ != nullptr) | ||||||
|         this->pm_particles_25um_sensor_->publish_state(pm_particles_25um); |         this->pm_2_5um_sensor_->publish_state(pm_2_5um); | ||||||
|       if (this->pm_particles_50um_sensor_ != nullptr) |       if (this->pm_5_0um_sensor_ != nullptr) | ||||||
|         this->pm_particles_50um_sensor_->publish_state(pm_particles_50um); |         this->pm_5_0um_sensor_->publish_state(pm_5_0um); | ||||||
|       if (this->pm_particles_100um_sensor_ != nullptr) |       if (this->pm_10_0um_sensor_ != nullptr) | ||||||
|         this->pm_particles_100um_sensor_->publish_state(pm_particles_100um); |         this->pm_10_0um_sensor_->publish_state(pm_10_0um); | ||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     case PMSX003_TYPE_5003T: { |     case PMSX003_TYPE_5003T: { | ||||||
| @@ -233,12 +192,12 @@ void PMSX003Component::dump_config() { | |||||||
|   LOG_SENSOR("  ", "PM2.5", this->pm_2_5_sensor_); |   LOG_SENSOR("  ", "PM2.5", this->pm_2_5_sensor_); | ||||||
|   LOG_SENSOR("  ", "PM10.0", this->pm_10_0_sensor_); |   LOG_SENSOR("  ", "PM10.0", this->pm_10_0_sensor_); | ||||||
|  |  | ||||||
|   LOG_SENSOR("  ", "PM0.3um", this->pm_particles_03um_sensor_); |   LOG_SENSOR("  ", "PM0.3um", this->pm_0_3um_sensor_); | ||||||
|   LOG_SENSOR("  ", "PM0.5um", this->pm_particles_05um_sensor_); |   LOG_SENSOR("  ", "PM0.5um", this->pm_0_5um_sensor_); | ||||||
|   LOG_SENSOR("  ", "PM1.0um", this->pm_particles_10um_sensor_); |   LOG_SENSOR("  ", "PM1.0um", this->pm_1_0um_sensor_); | ||||||
|   LOG_SENSOR("  ", "PM2.5um", this->pm_particles_25um_sensor_); |   LOG_SENSOR("  ", "PM2.5um", this->pm_2_5um_sensor_); | ||||||
|   LOG_SENSOR("  ", "PM5.0um", this->pm_particles_50um_sensor_); |   LOG_SENSOR("  ", "PM5.0um", this->pm_5_0um_sensor_); | ||||||
|   LOG_SENSOR("  ", "PM10.0um", this->pm_particles_100um_sensor_); |   LOG_SENSOR("  ", "PM10.0um", this->pm_10_0um_sensor_); | ||||||
|  |  | ||||||
|   LOG_SENSOR("  ", "Temperature", this->temperature_sensor_); |   LOG_SENSOR("  ", "Temperature", this->temperature_sensor_); | ||||||
|   LOG_SENSOR("  ", "Humidity", this->humidity_sensor_); |   LOG_SENSOR("  ", "Humidity", this->humidity_sensor_); | ||||||
|   | |||||||
| @@ -14,6 +14,13 @@ enum PMSX003Type { | |||||||
|   PMSX003_TYPE_5003S, |   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 { | class PMSX003Component : public uart::UARTDevice, public Component { | ||||||
|  public: |  public: | ||||||
|   PMSX003Component() = default; |   PMSX003Component() = default; | ||||||
| @@ -23,24 +30,27 @@ class PMSX003Component : public uart::UARTDevice, public Component { | |||||||
|  |  | ||||||
|   void set_type(PMSX003Type type) { type_ = type; } |   void set_type(PMSX003Type type) { type_ = type; } | ||||||
|  |  | ||||||
|   void set_pm_1_0_std_sensor(sensor::Sensor *pm_1_0_std_sensor); |   // "Standard Particle" | ||||||
|   void set_pm_2_5_std_sensor(sensor::Sensor *pm_2_5_std_sensor); |   PMSX003_SENSOR(pm_1_0_std) | ||||||
|   void set_pm_10_0_std_sensor(sensor::Sensor *pm_10_0_std_sensor); |   PMSX003_SENSOR(pm_2_5_std) | ||||||
|  |   PMSX003_SENSOR(pm_10_0_std) | ||||||
|  |  | ||||||
|   void set_pm_1_0_sensor(sensor::Sensor *pm_1_0_sensor); |   // "Under Atmospheric Pressure" | ||||||
|   void set_pm_2_5_sensor(sensor::Sensor *pm_2_5_sensor); |   PMSX003_SENSOR(pm_1_0) | ||||||
|   void set_pm_10_0_sensor(sensor::Sensor *pm_10_0_sensor); |   PMSX003_SENSOR(pm_2_5) | ||||||
|  |   PMSX003_SENSOR(pm_10_0) | ||||||
|  |  | ||||||
|   void set_pm_particles_03um_sensor(sensor::Sensor *pm_particles_03um_sensor); |   // Particle counts by size | ||||||
|   void set_pm_particles_05um_sensor(sensor::Sensor *pm_particles_05um_sensor); |   PMSX003_SENSOR(pm_0_3um) | ||||||
|   void set_pm_particles_10um_sensor(sensor::Sensor *pm_particles_10um_sensor); |   PMSX003_SENSOR(pm_0_5um) | ||||||
|   void set_pm_particles_25um_sensor(sensor::Sensor *pm_particles_25um_sensor); |   PMSX003_SENSOR(pm_1_0um) | ||||||
|   void set_pm_particles_50um_sensor(sensor::Sensor *pm_particles_50um_sensor); |   PMSX003_SENSOR(pm_2_5um) | ||||||
|   void set_pm_particles_100um_sensor(sensor::Sensor *pm_particles_100um_sensor); |   PMSX003_SENSOR(pm_5_0um) | ||||||
|  |   PMSX003_SENSOR(pm_10_0um) | ||||||
|  |  | ||||||
|   void set_temperature_sensor(sensor::Sensor *temperature_sensor); |   PMSX003_SENSOR(temperature) | ||||||
|   void set_humidity_sensor(sensor::Sensor *humidity_sensor); |   PMSX003_SENSOR(humidity) | ||||||
|   void set_formaldehyde_sensor(sensor::Sensor *formaldehyde_sensor); |   PMSX003_SENSOR(formaldehyde) | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   optional<bool> check_byte_(); |   optional<bool> check_byte_(); | ||||||
| @@ -51,28 +61,6 @@ class PMSX003Component : public uart::UARTDevice, public Component { | |||||||
|   uint8_t data_index_{0}; |   uint8_t data_index_{0}; | ||||||
|   uint32_t last_transmission_{0}; |   uint32_t last_transmission_{0}; | ||||||
|   PMSX003Type type_; |   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 | }  // namespace pmsx003 | ||||||
|   | |||||||
| @@ -68,6 +68,24 @@ def validate_pmsx003_sensors(value): | |||||||
|     return 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 = ( | CONFIG_SCHEMA = ( | ||||||
|     cv.Schema( |     cv.Schema( | ||||||
|         { |         { | ||||||
| @@ -171,62 +189,7 @@ async def to_code(config): | |||||||
|  |  | ||||||
|     cg.add(var.set_type(config[CONF_TYPE])) |     cg.add(var.set_type(config[CONF_TYPE])) | ||||||
|  |  | ||||||
|     if CONF_PM_1_0_STD in config: |     for key in SENSORS: | ||||||
|         sens = await sensor.new_sensor(config[CONF_PM_1_0_STD]) |         if key in config: | ||||||
|         cg.add(var.set_pm_1_0_std_sensor(sens)) |             sens = await sensor.new_sensor(config[key]) | ||||||
|  |             cg.add(getattr(var, f"set_{key}_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)) |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user