Add TC74 temperature sensor (#7460)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Seth Girvan 2024-10-15 20:24:37 -07:00 committed by GitHub
parent de943908bd
commit fb002ac3b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 178 additions and 0 deletions

View file

@ -403,6 +403,7 @@ esphome/components/sun/* @OttoWinter
esphome/components/sun_gtil2/* @Mat931
esphome/components/switch/* @esphome/core
esphome/components/t6615/* @tylermenezes
esphome/components/tc74/* @sethgirvan
esphome/components/tca9548a/* @andreashergert1984
esphome/components/tca9555/* @mobrembski
esphome/components/tcl112/* @glmnet

View file

@ -0,0 +1 @@
CODEOWNERS = ["@sethgirvan"]

View file

@ -0,0 +1,32 @@
import esphome.codegen as cg
from esphome.components import i2c, sensor
import esphome.config_validation as cv
from esphome.const import (
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
)
CODEOWNERS = ["@sethgirvan"]
DEPENDENCIES = ["i2c"]
tc74_ns = cg.esphome_ns.namespace("tc74")
TC74Component = tc74_ns.class_("TC74Component", cg.PollingComponent, i2c.I2CDevice)
CONFIG_SCHEMA = (
sensor.sensor_schema(
TC74Component,
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x48))
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)

View file

@ -0,0 +1,68 @@
// Based on the TC74 datasheet https://ww1.microchip.com/downloads/en/DeviceDoc/21462D.pdf
#include "tc74.h"
#include "esphome/core/log.h"
namespace esphome {
namespace tc74 {
static const char *const TAG = "tc74";
static const uint8_t TC74_REGISTER_TEMPERATURE = 0x00;
static const uint8_t TC74_REGISTER_CONFIGURATION = 0x01;
static const uint8_t TC74_DATA_READY_MASK = 0x40;
// It is possible the "Data Ready" bit will not be set if the TC74 has not been powered on for at least 250ms, so it not
// being set does not constitute a failure.
void TC74Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up TC74...");
uint8_t config_reg;
if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
this->mark_failed();
return;
}
this->data_ready_ = config_reg & TC74_DATA_READY_MASK;
}
void TC74Component::update() { this->read_temperature_(); }
void TC74Component::dump_config() {
LOG_SENSOR("", "TC74", this);
LOG_I2C_DEVICE(this);
if (this->is_failed()) {
ESP_LOGE(TAG, "Connection with TC74 failed!");
}
LOG_UPDATE_INTERVAL(this);
}
void TC74Component::read_temperature_() {
if (!this->data_ready_) {
uint8_t config_reg;
if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
this->status_set_warning();
return;
}
if (config_reg & TC74_DATA_READY_MASK) {
this->data_ready_ = true;
} else {
ESP_LOGD(TAG, "TC74 not ready");
return;
}
}
uint8_t temperature_reg;
if (this->read_register(TC74_REGISTER_TEMPERATURE, &temperature_reg, 1) != i2c::ERROR_OK) {
this->status_set_warning();
return;
}
ESP_LOGD(TAG, "Got Temperature=%d °C", temperature_reg);
this->publish_state(temperature_reg);
this->status_clear_warning();
}
float TC74Component::get_setup_priority() const { return setup_priority::DATA; }
} // namespace tc74
} // namespace esphome

View file

@ -0,0 +1,28 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace tc74 {
class TC74Component : public PollingComponent, public i2c::I2CDevice, public sensor::Sensor {
public:
/// Setup the sensor and check connection.
void setup() override;
void dump_config() override;
/// Update the sensor value (temperature).
void update() override;
float get_setup_priority() const override;
protected:
/// Internal method to read the temperature from the component after it has been scheduled.
void read_temperature_();
bool data_ready_ = false;
};
} // namespace tc74
} // namespace esphome

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 16
sda: 17
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 16
sda: 17
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature

View file

@ -0,0 +1,8 @@
i2c:
- id: i2c_tc74
scl: 5
sda: 4
sensor:
- platform: tc74
name: TC74 Temperature