Compare commits

...
Author SHA1 Message Date
J. Nick Koston ff3ba05f2f [hdc302x] Declare child sensors with SUB_SENSOR 2026-10-28 09:07:10 -10:00
3 changed files with 7 additions and 10 deletions

No files matched your search

+3 -3
View File
@@ -43,7 +43,7 @@ void HDC302XComponent::dump_config() {
this->heater_active_ ? LOG_STR_LITERAL("active") : LOG_STR_LITERAL("inactive"));
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "Temperature", this->temp_sensor_);
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
}
@@ -137,11 +137,11 @@ void HDC302XComponent::read_data_() {
if (!this->status_comm_result(crc_ok, LOG_STR("Read data: invalid CRC")))
return;
if (this->temp_sensor_ != nullptr) {
if (this->temperature_sensor_ != nullptr) {
uint16_t raw_t = encode_uint16(buf[0], buf[1]);
// Calculate temperature in Celsius per datasheet section 7.3.3.
float temp = -45 + 175 * (float(raw_t) / 65535.0f);
this->temp_sensor_->publish_state(temp);
this->temperature_sensor_->publish_state(temp);
}
if (this->humidity_sensor_ != nullptr) {
+3 -6
View File
@@ -20,6 +20,9 @@ enum HDC302XPowerMode : uint8_t {
https://www.ti.com/lit/ds/symlink/hdc3020.pdf
*/
class HDC302XComponent final : public PollingComponent, public i2c::I2CDevice {
SUB_SENSOR(temperature)
SUB_SENSOR(humidity)
public:
void setup() override;
void dump_config() override;
@@ -28,15 +31,9 @@ class HDC302XComponent final : public PollingComponent, public i2c::I2CDevice {
void start_heater(uint16_t power, uint32_t duration_ms);
void stop_heater();
void set_temp_sensor(sensor::Sensor *temp_sensor) { this->temp_sensor_ = temp_sensor; }
void set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
void set_power_mode(HDC302XPowerMode power_mode) { this->power_mode_ = power_mode; }
protected:
sensor::Sensor *temp_sensor_{nullptr};
sensor::Sensor *humidity_sensor_{nullptr};
HDC302XPowerMode power_mode_{HDC302XPowerMode::HIGH_ACCURACY};
bool heater_active_{false};
+1 -1
View File
@@ -61,7 +61,7 @@ async def to_code(config: ConfigType) -> None:
var = await i2c.new_i2c_component(config)
sensors = sensor.sub_sensors(config)
await sensors(CONF_TEMPERATURE, var.set_temp_sensor)
await sensors(CONF_TEMPERATURE, var.set_temperature_sensor)
await sensors(CONF_HUMIDITY, var.set_humidity_sensor)
cg.add(var.set_power_mode(config[CONF_POWER_MODE]))