mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	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:
		| @@ -76,12 +76,27 @@ void HTU21DComponent::update() { | |||||||
|  |  | ||||||
|       float humidity = (float(raw_humidity & 0xFFFC)) * 125.0f / 65536.0f - 6.0f; |       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%%", humidity); | ||||||
|  |  | ||||||
|       ESP_LOGD(TAG, "Got Humidity=%.1f%% Heater Level=%d", humidity, heater_level); |  | ||||||
|  |  | ||||||
|       if (this->humidity_ != nullptr) |       if (this->humidity_ != nullptr) | ||||||
|         this->humidity_->publish_state(humidity); |         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) |       if (this->heater_ != nullptr) | ||||||
|         this->heater_->publish_state(heater_level); |         this->heater_->publish_state(heater_level); | ||||||
|       this->status_clear_warning(); |       this->status_clear_warning(); | ||||||
|   | |||||||
| @@ -8,6 +8,8 @@ | |||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace htu21d { | namespace htu21d { | ||||||
|  |  | ||||||
|  | enum HTU21DSensorModels { HTU21D_SENSOR_MODEL_HTU21D = 0, HTU21D_SENSOR_MODEL_SI7021, HTU21D_SENSOR_MODEL_SHT21 }; | ||||||
|  |  | ||||||
| class HTU21DComponent : public PollingComponent, public i2c::I2CDevice { | class HTU21DComponent : public PollingComponent, public i2c::I2CDevice { | ||||||
|  public: |  public: | ||||||
|   void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; } |   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. |   /// Setup (reset) the sensor and check connection. | ||||||
|   void setup() override; |   void setup() override; | ||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|  |   void set_sensor_model(HTU21DSensorModels sensor_model) { sensor_model_ = sensor_model; } | ||||||
|   /// Update the sensor values (temperature+humidity). |   /// Update the sensor values (temperature+humidity). | ||||||
|   void update() override; |   void update() override; | ||||||
|  |  | ||||||
| @@ -31,6 +34,7 @@ class HTU21DComponent : public PollingComponent, public i2c::I2CDevice { | |||||||
|   sensor::Sensor *temperature_{nullptr}; |   sensor::Sensor *temperature_{nullptr}; | ||||||
|   sensor::Sensor *humidity_{nullptr}; |   sensor::Sensor *humidity_{nullptr}; | ||||||
|   sensor::Sensor *heater_{nullptr}; |   sensor::Sensor *heater_{nullptr}; | ||||||
|  |   HTU21DSensorModels sensor_model_{HTU21D_SENSOR_MODEL_HTU21D}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| template<typename... Ts> class SetHeaterLevelAction : public Action<Ts...>, public Parented<HTU21DComponent> { | template<typename... Ts> class SetHeaterLevelAction : public Action<Ts...>, public Parented<HTU21DComponent> { | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ from esphome import automation | |||||||
| from esphome.const import ( | from esphome.const import ( | ||||||
|     CONF_HUMIDITY, |     CONF_HUMIDITY, | ||||||
|     CONF_ID, |     CONF_ID, | ||||||
|  |     CONF_MODEL, | ||||||
|     CONF_TEMPERATURE, |     CONF_TEMPERATURE, | ||||||
|     DEVICE_CLASS_HUMIDITY, |     DEVICE_CLASS_HUMIDITY, | ||||||
|     DEVICE_CLASS_TEMPERATURE, |     DEVICE_CLASS_TEMPERATURE, | ||||||
| @@ -23,10 +24,15 @@ htu21d_ns = cg.esphome_ns.namespace("htu21d") | |||||||
| HTU21DComponent = htu21d_ns.class_( | HTU21DComponent = htu21d_ns.class_( | ||||||
|     "HTU21DComponent", cg.PollingComponent, i2c.I2CDevice |     "HTU21DComponent", cg.PollingComponent, i2c.I2CDevice | ||||||
| ) | ) | ||||||
|  |  | ||||||
| SetHeaterLevelAction = htu21d_ns.class_("SetHeaterLevelAction", automation.Action) | SetHeaterLevelAction = htu21d_ns.class_("SetHeaterLevelAction", automation.Action) | ||||||
| SetHeaterAction = htu21d_ns.class_("SetHeaterAction", 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 = ( | CONFIG_SCHEMA = ( | ||||||
|     cv.Schema( |     cv.Schema( | ||||||
| @@ -49,6 +55,7 @@ CONFIG_SCHEMA = ( | |||||||
|                 accuracy_decimals=1, |                 accuracy_decimals=1, | ||||||
|                 state_class=STATE_CLASS_MEASUREMENT, |                 state_class=STATE_CLASS_MEASUREMENT, | ||||||
|             ), |             ), | ||||||
|  |             cv.Optional(CONF_MODEL, default="HTU21D"): cv.enum(MODELS, upper=True), | ||||||
|         } |         } | ||||||
|     ) |     ) | ||||||
|     .extend(cv.polling_component_schema("60s")) |     .extend(cv.polling_component_schema("60s")) | ||||||
| @@ -73,6 +80,8 @@ async def to_code(config): | |||||||
|         sens = await sensor.new_sensor(config[CONF_HEATER]) |         sens = await sensor.new_sensor(config[CONF_HEATER]) | ||||||
|         cg.add(var.set_heater(sens)) |         cg.add(var.set_heater(sens)) | ||||||
|  |  | ||||||
|  |     cg.add(var.set_sensor_model(config[CONF_MODEL])) | ||||||
|  |  | ||||||
|  |  | ||||||
| @automation.register_action( | @automation.register_action( | ||||||
|     "htu21d.set_heater_level", |     "htu21d.set_heater_level", | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ i2c: | |||||||
|  |  | ||||||
| sensor: | sensor: | ||||||
|   - platform: htu21d |   - platform: htu21d | ||||||
|  |     model: htu21d | ||||||
|     temperature: |     temperature: | ||||||
|       name: Temperature |       name: Temperature | ||||||
|     humidity: |     humidity: | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ i2c: | |||||||
|  |  | ||||||
| sensor: | sensor: | ||||||
|   - platform: htu21d |   - platform: htu21d | ||||||
|  |     model: htu21d | ||||||
|     temperature: |     temperature: | ||||||
|       name: Temperature |       name: Temperature | ||||||
|     humidity: |     humidity: | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ i2c: | |||||||
|  |  | ||||||
| sensor: | sensor: | ||||||
|   - platform: htu21d |   - platform: htu21d | ||||||
|  |     model: htu21d | ||||||
|     temperature: |     temperature: | ||||||
|       name: Temperature |       name: Temperature | ||||||
|     humidity: |     humidity: | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ i2c: | |||||||
|  |  | ||||||
| sensor: | sensor: | ||||||
|   - platform: htu21d |   - platform: htu21d | ||||||
|  |     model: htu21d | ||||||
|     temperature: |     temperature: | ||||||
|       name: Temperature |       name: Temperature | ||||||
|     humidity: |     humidity: | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ i2c: | |||||||
|  |  | ||||||
| sensor: | sensor: | ||||||
|   - platform: htu21d |   - platform: htu21d | ||||||
|  |     model: htu21d | ||||||
|     temperature: |     temperature: | ||||||
|       name: Temperature |       name: Temperature | ||||||
|     humidity: |     humidity: | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ i2c: | |||||||
|  |  | ||||||
| sensor: | sensor: | ||||||
|   - platform: htu21d |   - platform: htu21d | ||||||
|  |     model: htu21d | ||||||
|     temperature: |     temperature: | ||||||
|       name: Temperature |       name: Temperature | ||||||
|     humidity: |     humidity: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user