1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00

The code has been corrected according to the recommendations.

This commit is contained in:
Andrew J.Swan 2025-02-21 17:30:47 +02:00
parent f4eaac33f9
commit ec333215f6
3 changed files with 52 additions and 25 deletions

View File

@ -6,7 +6,7 @@ namespace pm2005 {
static const char *const TAG = "pm2005"; static const char *const TAG = "pm2005";
#ifdef TYPE_2005 #ifdef PM2005_USE_TYPE_2005
static const uint8_t SITUATION_VALUE_INDEX = 3; static const uint8_t SITUATION_VALUE_INDEX = 3;
static const uint8_t PM_1_0_VALUE_INDEX = 4; static const uint8_t PM_1_0_VALUE_INDEX = 4;
static const uint8_t PM_2_5_VALUE_INDEX = 6; static const uint8_t PM_2_5_VALUE_INDEX = 6;
@ -20,6 +20,36 @@ static const uint8_t PM_10_0_VALUE_INDEX = 7;
static const uint8_t MEASURING_VALUE_INDEX = 9; static const uint8_t MEASURING_VALUE_INDEX = 9;
#endif #endif
// Converts a sensor situation to a human readable string
static const LogString *pm2005_get_situation_string(int status) {
switch (status) {
case 1:
return LOG_STR("Close.");
case 2:
return LOG_STR("Malfunction.");
case 3:
return LOG_STR("Under detecting.");
case 0x80:
return LOG_STR("Detecting completed.");
default:
return LOG_STR("Invalid");
}
}
// Converts a sensor measuring mode to a human readable string
static const LogString *pm2005_get_measuring_mode_string(int status) {
switch (status) {
case 2:
return LOG_STR("Single measuring mode.");
case 3:
return LOG_STR("Continuous measuring mode.");
case 5:
return LOG_STR("Dynamic measuring mode.");
default:
return LOG_STR("Unknown");
}
}
void PM2005Component::update() { void PM2005Component::update() {
if (this->read(data_buffer_, 12) != i2c::ERROR_OK) { if (this->read(data_buffer_, 12) != i2c::ERROR_OK) {
ESP_LOGW(TAG, "Read result failed"); ESP_LOGW(TAG, "Read result failed");
@ -27,17 +57,17 @@ void PM2005Component::update() {
return; return;
} }
if (sensor_situation_ != data_buffer_[SITUATION_VALUE_INDEX]) { if (this->sensor_situation_ != data_buffer_[SITUATION_VALUE_INDEX]) {
sensor_situation_ = data_buffer_[SITUATION_VALUE_INDEX]; this->sensor_situation_ = data_buffer_[SITUATION_VALUE_INDEX];
if (sensor_situation_ == 1) { if (this->sensor_situation_ == 1)
ESP_LOGD(TAG, "Sensor situation: Close."); ESP_LOGD(TAG, "Sensor situation: %s.", LOG_STR_ARG(pm2005_get_situation_string(this->sensor_situation_)));
} else if (sensor_situation_ == 2) { else if (this->sensor_situation_ == 2) {
ESP_LOGD(TAG, "Sensor situation: Malfunction."); ESP_LOGD(TAG, "Sensor situation: %s.", LOG_STR_ARG(pm2005_get_situation_string(this->sensor_situation_)));
this->status_set_warning(); this->status_set_warning();
} else if (sensor_situation_ == 3) { } else if (this->sensor_situation_ == 3)
ESP_LOGD(TAG, "Sensor situation: Under detecting."); ESP_LOGD(TAG, "Sensor situation: %s.", LOG_STR_ARG(pm2005_get_situation_string(this->sensor_situation_)));
} else if (sensor_situation_ == 0x80) { else if (this->sensor_situation_ == 0x80) {
ESP_LOGD(TAG, "Sensor situation: Detecting completed."); ESP_LOGD(TAG, "Sensor situation: %s.", LOG_STR_ARG(pm2005_get_situation_string(this->sensor_situation_)));
if (this->pm_1_0_sensor_ != nullptr) { if (this->pm_1_0_sensor_ != nullptr) {
int16_t pm1 = get_sensor_value_(data_buffer_, PM_1_0_VALUE_INDEX); int16_t pm1 = get_sensor_value_(data_buffer_, PM_1_0_VALUE_INDEX);
@ -58,12 +88,9 @@ void PM2005Component::update() {
} }
uint16_t sensor_measuring_mode = get_sensor_value_(data_buffer_, MEASURING_VALUE_INDEX); uint16_t sensor_measuring_mode = get_sensor_value_(data_buffer_, MEASURING_VALUE_INDEX);
if (sensor_measuring_mode == 2) { if (sensor_measuring_mode >= 2 && sensor_measuring_mode <= 5) {
ESP_LOGD(TAG, "The measuring mode of sensor: Single measuring mode."); ESP_LOGD(TAG, "The measuring mode of sensor: %s.",
} else if (sensor_measuring_mode == 3) { LOG_STR_ARG(pm2005_get_measuring_mode_string(sensor_measuring_mode)));
ESP_LOGD(TAG, "The measuring mode of sensor: Continuous measuring mode.");
} else if (sensor_measuring_mode == 5) {
ESP_LOGD(TAG, "The measuring mode of sensor: Dynamic measuring mode.");
} }
this->status_clear_warning(); this->status_clear_warning();
@ -78,10 +105,10 @@ uint16_t PM2005Component::get_sensor_value_(const uint8_t *data, uint8_t i) {
void PM2005Component::dump_config() { void PM2005Component::dump_config() {
ESP_LOGCONFIG(TAG, "PM2005:"); ESP_LOGCONFIG(TAG, "PM2005:");
#ifdef TYPE_2005 #ifdef PM2005_USE_TYPE_2005
ESP_LOGCONFIG(TAG, "Type: PM2005"); ESP_LOGCONFIG(TAG, " Type: PM2005");
#else #else
ESP_LOGCONFIG(TAG, "Type: PM2105"); ESP_LOGCONFIG(TAG, " Type: PM2105");
#endif #endif
LOG_I2C_DEVICE(this); LOG_I2C_DEVICE(this);

View File

@ -13,9 +13,9 @@ class PM2005Component : public PollingComponent, public i2c::I2CDevice {
PM2005Component() = default; PM2005Component() = default;
void set_pm_1_0_sensor(sensor::Sensor *pm_1_0_sensor) { pm_1_0_sensor_ = pm_1_0_sensor; } void set_pm_1_0_sensor(sensor::Sensor *pm_1_0_sensor) { this->pm_1_0_sensor_ = pm_1_0_sensor; }
void set_pm_2_5_sensor(sensor::Sensor *pm_2_5_sensor) { pm_2_5_sensor_ = pm_2_5_sensor; } void set_pm_2_5_sensor(sensor::Sensor *pm_2_5_sensor) { this->pm_2_5_sensor_ = pm_2_5_sensor; }
void set_pm_10_0_sensor(sensor::Sensor *pm_10_0_sensor) { pm_10_0_sensor_ = pm_10_0_sensor; } void set_pm_10_0_sensor(sensor::Sensor *pm_10_0_sensor) { this->pm_10_0_sensor_ = pm_10_0_sensor; }
void dump_config() override; void dump_config() override;
void update() override; void update() override;

View File

@ -75,9 +75,9 @@ async def to_code(config) -> None:
await i2c.register_i2c_device(var, config) await i2c.register_i2c_device(var, config)
if config[CONF_TYPE] == TYPE_2005: if config[CONF_TYPE] == TYPE_2005:
cg.add_define("TYPE_2005") cg.add_define("PM2005_USE_TYPE_2005")
else: else:
cg.add_define("TYPE_2105") cg.add_define("PM2005_USE_TYPE_2105")
if CONF_PM_1_0 in config: if CONF_PM_1_0 in config:
sens = await sensor.new_sensor(config[CONF_PM_1_0]) sens = await sensor.new_sensor(config[CONF_PM_1_0])