Check DHT sensor exists before publishing (#850)

Fixes https://github.com/esphome/issues/issues/841
This commit is contained in:
Otto Winter 2019-11-12 19:04:11 +01:00
parent 74878276fc
commit 7c0d777173
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E

View file

@ -47,8 +47,10 @@ void DHT::update() {
if (error) { if (error) {
ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity); ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
this->temperature_sensor_->publish_state(temperature); if (this->temperature_sensor_ != nullptr)
this->humidity_sensor_->publish_state(humidity); this->temperature_sensor_->publish_state(temperature);
if (this->humidity_sensor_ != nullptr)
this->humidity_sensor_->publish_state(humidity);
this->status_clear_warning(); this->status_clear_warning();
} else { } else {
const char *str = ""; const char *str = "";
@ -56,8 +58,10 @@ void DHT::update() {
str = " and consider manually specifying the DHT model using the model option"; str = " and consider manually specifying the DHT model using the model option";
} }
ESP_LOGW(TAG, "Invalid readings! Please check your wiring (pull-up resistor, pin number)%s.", str); ESP_LOGW(TAG, "Invalid readings! Please check your wiring (pull-up resistor, pin number)%s.", str);
this->temperature_sensor_->publish_state(NAN); if (this->temperature_sensor_ != nullptr)
this->humidity_sensor_->publish_state(NAN); this->temperature_sensor_->publish_state(NAN);
if (this->humidity_sensor_ != nullptr)
this->humidity_sensor_->publish_state(NAN);
this->status_set_warning(); this->status_set_warning();
} }
} }