Added Htu21d model option (#6511)

Co-authored-by: Remus <remus@intelNuc.local>
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
MRemy2 2024-04-12 02:28:59 +03:00 committed by GitHub
parent 55433463d7
commit 39947a1634
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 38 additions and 4 deletions

View file

@ -76,12 +76,27 @@ void HTU21DComponent::update() {
float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f;
int8_t heater_level = this->get_heater_level();
ESP_LOGD(TAG, "Got Humidity=%.1f%% Heater Level=%d", humidity, heater_level);
ESP_LOGD(TAG, "Got Humidity=%.1f%%", humidity);
if (this->humidity_ != nullptr)
this->humidity_->publish_state(humidity);
int8_t heater_level;
// HTU21D does have a heater module but does not have heater level
// Setting heater level to 1 in case the heater is ON
if (this->sensor_model_ == HTU21D_SENSOR_MODEL_HTU21D) {
if (this->is_heater_enabled()) {
heater_level = 1;
} else {
heater_level = 0;
}
} else {
heater_level = this->get_heater_level();
}
ESP_LOGD(TAG, "Heater Level=%d", heater_level);
if (this->heater_ != nullptr)
this->heater_->publish_state(heater_level);
this->status_clear_warning();

View file

@ -8,6 +8,8 @@
namespace esphome {
namespace htu21d {
enum HTU21DSensorModels { HTU21D_SENSOR_MODEL_HTU21D = 0, HTU21D_SENSOR_MODEL_SI7021, HTU21D_SENSOR_MODEL_SHT21 };
class HTU21DComponent : public PollingComponent, public i2c::I2CDevice {
public:
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
@ -17,6 +19,7 @@ class HTU21DComponent : public PollingComponent, public i2c::I2CDevice {
/// Setup (reset) the sensor and check connection.
void setup() override;
void dump_config() override;
void set_sensor_model(HTU21DSensorModels sensor_model) { sensor_model_ = sensor_model; }
/// Update the sensor values (temperature+humidity).
void update() override;
@ -31,6 +34,7 @@ class HTU21DComponent : public PollingComponent, public i2c::I2CDevice {
sensor::Sensor *temperature_{nullptr};
sensor::Sensor *humidity_{nullptr};
sensor::Sensor *heater_{nullptr};
HTU21DSensorModels sensor_model_{HTU21D_SENSOR_MODEL_HTU21D};
};
template<typename... Ts> class SetHeaterLevelAction : public Action<Ts...>, public Parented<HTU21DComponent> {

View file

@ -5,6 +5,7 @@ from esphome import automation
from esphome.const import (
CONF_HUMIDITY,
CONF_ID,
CONF_MODEL,
CONF_TEMPERATURE,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE,
@ -23,10 +24,15 @@ htu21d_ns = cg.esphome_ns.namespace("htu21d")
HTU21DComponent = htu21d_ns.class_(
"HTU21DComponent", cg.PollingComponent, i2c.I2CDevice
)
SetHeaterLevelAction = htu21d_ns.class_("SetHeaterLevelAction", automation.Action)
SetHeaterAction = htu21d_ns.class_("SetHeaterAction", automation.Action)
HTU21DSensorModels = htu21d_ns.enum("HTU21DSensorModels")
MODELS = {
"HTU21D": HTU21DSensorModels.HTU21D_SENSOR_MODEL_HTU21D,
"SI7021": HTU21DSensorModels.HTU21D_SENSOR_MODEL_SI7021,
"SHT21": HTU21DSensorModels.HTU21D_SENSOR_MODEL_SHT21,
}
CONFIG_SCHEMA = (
cv.Schema(
@ -49,6 +55,7 @@ CONFIG_SCHEMA = (
accuracy_decimals=1,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_MODEL, default="HTU21D"): cv.enum(MODELS, upper=True),
}
)
.extend(cv.polling_component_schema("60s"))
@ -73,6 +80,8 @@ async def to_code(config):
sens = await sensor.new_sensor(config[CONF_HEATER])
cg.add(var.set_heater(sens))
cg.add(var.set_sensor_model(config[CONF_MODEL]))
@automation.register_action(
"htu21d.set_heater_level",

View file

@ -5,6 +5,7 @@ i2c:
sensor:
- platform: htu21d
model: htu21d
temperature:
name: Temperature
humidity:

View file

@ -5,6 +5,7 @@ i2c:
sensor:
- platform: htu21d
model: htu21d
temperature:
name: Temperature
humidity:

View file

@ -5,6 +5,7 @@ i2c:
sensor:
- platform: htu21d
model: htu21d
temperature:
name: Temperature
humidity:

View file

@ -5,6 +5,7 @@ i2c:
sensor:
- platform: htu21d
model: htu21d
temperature:
name: Temperature
humidity:

View file

@ -5,6 +5,7 @@ i2c:
sensor:
- platform: htu21d
model: htu21d
temperature:
name: Temperature
humidity:

View file

@ -5,6 +5,7 @@ i2c:
sensor:
- platform: htu21d
model: htu21d
temperature:
name: Temperature
humidity: