ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
hyt271.cpp
Go to the documentation of this file.
1#include "hyt271.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::hyt271 {
6
7static const char *const TAG = "hyt271";
8
9static const uint8_t HYT271_ADDRESS = 0x28;
10
12 ESP_LOGCONFIG(TAG, "HYT271:");
13 LOG_I2C_DEVICE(this);
14 LOG_UPDATE_INTERVAL(this);
15 LOG_SENSOR(" ", "Temperature", this->temperature_);
16 LOG_SENSOR(" ", "Humidity", this->humidity_);
17}
19 uint8_t raw_data[4] = {0, 0, 0, 0};
20
21 if (this->write(&raw_data[0], 0) != i2c::ERROR_OK) {
22 this->status_set_warning();
23 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
24 return;
25 }
26 this->set_timeout("wait_convert", 50, [this]() {
27 uint8_t raw_data[4];
28 if (this->read(raw_data, 4) != i2c::ERROR_OK) {
29 this->status_set_warning();
30 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
31 return;
32 }
33 uint16_t raw_temperature = ((raw_data[2] << 8) | raw_data[3]) >> 2;
34 uint16_t raw_humidity = ((raw_data[0] & 0x3F) << 8) | raw_data[1];
35
36 float temperature = ((float(raw_temperature)) * (165.0f / 16383.0f)) - 40.0f;
37 float humidity = (float(raw_humidity)) * (100.0f / 16383.0f);
38
39 ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
40
41 if (this->temperature_ != nullptr)
42 this->temperature_->publish_state(temperature);
43 if (this->humidity_ != nullptr)
44 this->humidity_->publish_state(humidity);
46 });
47}
48} // namespace esphome::hyt271
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void status_clear_warning()
Definition component.h:289
void update() override
Update the sensor values (temperature+humidity).
Definition hyt271.cpp:18
sensor::Sensor * temperature_
Definition hyt271.h:19
sensor::Sensor * humidity_
Definition hyt271.h:20
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
uint16_t temperature
Definition sun_gtil2.cpp:12