Corrected and updated the code based on review

This commit is contained in:
optimusprimespace 2024-10-07 13:37:57 -04:00
parent 489d5952ef
commit e1954ab5ab
3 changed files with 21 additions and 24 deletions

View file

@ -76,14 +76,17 @@ void HDC2010Component::update() {
delayMicroseconds(1000); // 1ms delay after triggering the sample
float temp = read_temp();
float humidity = read_humidity();
if (this->temperature_sensor_ != nullptr) {
float temp = this->read_temp();
this->temperature_->publish_state(temp);
ESP_LOGD(TAG, "Got temperature=%.1f°C", temp);
}
this->temperature_->publish_state(temp);
this->humidity_->publish_state(humidity);
ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temp, humidity);
this->status_clear_warning();
if (this->humidity_sensor_ != nullptr) {
float humidity = this->read_humidity();
this->humidity_->publish_state(humidity);
ESP_LOGD(TAG, "Got humidity=%.1f%%", humidity);
}
}
float HDC2010Component::read_temp() {

View file

@ -9,8 +9,9 @@ namespace hdc2010 {
class HDC2010Component : public PollingComponent, public i2c::I2CDevice {
public:
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
void set_temperature_sensor(sensor::Sensor *temperature) { this->temperature_sensor_ = temperature; }
void set_humidity_sensor(sensor::Sensor *humidity) { this->humidity_sensor_ = humidity; }
/// Setup the sensor and check for connection.
void setup() override;
@ -25,8 +26,8 @@ class HDC2010Component : public PollingComponent, public i2c::I2CDevice {
float get_setup_priority() const override;
protected:
sensor::Sensor *temperature_{nullptr};
sensor::Sensor *humidity_{nullptr};
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *humidity_sensor_{nullptr};
uint16_t heater_temperature_{100};
uint16_t heater_duration_{30};
};

View file

@ -10,7 +10,6 @@ from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_PERCENT,
# CONF_HEATER,
)
DEPENDENCIES = ["i2c"]
@ -48,17 +47,11 @@ async def to_code(config):
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
if CONF_TEMPERATURE in config:
sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
if temperature_config := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature_config)
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(sens))
if humidity_config := config.get(CONF_HUMIDITY):
sens = await sensor.new_sensor(humidity_config)
cg.add(var.set_humidity_sensor(sens))
# if CONF_HEATER in config:
# conf = config[CONF_HEATER]
# if not conf:
# cg.add(var.set_heater(0, 0))
# else:
# cg.add(var.set_heater(conf[CONF_TEMPERATURE], conf[CONF_DURATION]))