Fix SM300D2 sensor component routines so they correctly read the sensor output (#2159)

This commit is contained in:
James Braid 2021-09-13 02:23:59 -07:00 committed by GitHub
parent 4e308f551c
commit c6109024aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -72,13 +72,13 @@ CONFIG_SCHEMA = cv.All(
), ),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS, unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=0, accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE, device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),
cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT, unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0, accuracy_decimals=1,
device_class=DEVICE_CLASS_HUMIDITY, device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
), ),

View File

@ -9,10 +9,12 @@ static const uint8_t SM300D2_RESPONSE_LENGTH = 17;
void SM300D2Sensor::update() { void SM300D2Sensor::update() {
uint8_t response[SM300D2_RESPONSE_LENGTH]; uint8_t response[SM300D2_RESPONSE_LENGTH];
uint8_t peeked;
while (this->available() > 0 && this->peek_byte(&peeked) && peeked != 0x3C)
this->read();
flush();
bool read_success = read_array(response, SM300D2_RESPONSE_LENGTH); bool read_success = read_array(response, SM300D2_RESPONSE_LENGTH);
flush();
if (!read_success) { if (!read_success) {
ESP_LOGW(TAG, "Reading data from SM300D2 failed!"); ESP_LOGW(TAG, "Reading data from SM300D2 failed!");
@ -63,7 +65,7 @@ void SM300D2Sensor::update() {
if (this->pm_2_5_sensor_ != nullptr) if (this->pm_2_5_sensor_ != nullptr)
this->pm_2_5_sensor_->publish_state(pm_2_5); this->pm_2_5_sensor_->publish_state(pm_2_5);
ESP_LOGD(TAG, "Received pm_10_0: %u µg/m³", pm_10_0); ESP_LOGD(TAG, "Received PM10: %u µg/m³", pm_10_0);
if (this->pm_10_0_sensor_ != nullptr) if (this->pm_10_0_sensor_ != nullptr)
this->pm_10_0_sensor_->publish_state(pm_10_0); this->pm_10_0_sensor_->publish_state(pm_10_0);