Add internal_temperature component (#4330)

* Add cpu_temperature component

* Add tests

* Fix formatting

* Possible fix for "sensor not shown in HomeAssistant"

* Rename component to internal_temperature

* Update esphome/components/internal_temperature/internal_temperature.cpp

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>

* Update esphome/components/internal_temperature/internal_temperature.cpp

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>

* Update esphome/components/internal_temperature/internal_temperature.cpp

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>

* Update internal_temperature.h

* Remove unique_id

* Update ESP32 variant detection

---------

Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
Mat931 2023-02-20 02:47:37 +00:00 committed by GitHub
parent f68d577986
commit ed801f7a27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 0 deletions

View file

@ -113,6 +113,7 @@ esphome/components/ina260/* @MrEditor97
esphome/components/inkbird_ibsth1_mini/* @fkirill
esphome/components/inkplate6/* @jesserockz
esphome/components/integration/* @OttoWinter
esphome/components/internal_temperature/* @Mat931
esphome/components/interval/* @esphome/core
esphome/components/json/* @OttoWinter
esphome/components/kalman_combinator/* @Cat-Ion

View file

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

View file

@ -0,0 +1,58 @@
#include "internal_temperature.h"
#include "esphome/core/log.h"
#ifdef USE_ESP32
#if defined(USE_ESP32_VARIANT_ESP32)
// there is no official API available on the original ESP32
extern "C" {
uint8_t temprature_sens_read();
}
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
#include "driver/temp_sensor.h"
#endif // USE_ESP32_VARIANT
#endif // USE_ESP32
#ifdef USE_RP2040
#include "Arduino.h"
#endif // USE_RP2040
namespace esphome {
namespace internal_temperature {
static const char *const TAG = "internal_temperature";
void InternalTemperatureSensor::update() {
float temperature = NAN;
bool success = false;
#ifdef USE_ESP32
#if defined(USE_ESP32_VARIANT_ESP32)
uint8_t raw = temprature_sens_read();
ESP_LOGV(TAG, "Raw temperature value: %d", raw);
temperature = (raw - 32) / 1.8f;
success = (raw != 128);
#elif defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
temp_sensor_config_t tsens = TSENS_CONFIG_DEFAULT();
temp_sensor_set_config(tsens);
temp_sensor_start();
esp_err_t result = temp_sensor_read_celsius(&temperature);
temp_sensor_stop();
success = (result == ESP_OK);
#endif // USE_ESP32_VARIANT
#endif // USE_ESP32
#ifdef USE_RP2040
temperature = analogReadTemp();
success = (temperature != 0.0f);
#endif // USE_RP2040
if (success && std::isfinite(temperature)) {
this->publish_state(temperature);
} else {
ESP_LOGD(TAG, "Ignoring invalid temperature (success=%d, value=%.1f)", success, temperature);
if (!this->has_state()) {
this->publish_state(NAN);
}
}
}
void InternalTemperatureSensor::dump_config() { LOG_SENSOR("", "Internal Temperature Sensor", this); }
} // namespace internal_temperature
} // namespace esphome

View file

@ -0,0 +1,17 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace internal_temperature {
class InternalTemperatureSensor : public sensor::Sensor, public PollingComponent {
public:
void dump_config() override;
void update() override;
};
} // namespace internal_temperature
} // namespace esphome

View file

@ -0,0 +1,31 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
DEVICE_CLASS_TEMPERATURE,
ENTITY_CATEGORY_DIAGNOSTIC,
)
internal_temperature_ns = cg.esphome_ns.namespace("internal_temperature")
InternalTemperatureSensor = internal_temperature_ns.class_(
"InternalTemperatureSensor", sensor.Sensor, cg.PollingComponent
)
CONFIG_SCHEMA = cv.All(
sensor.sensor_schema(
InternalTemperatureSensor,
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
).extend(cv.polling_component_schema("60s")),
cv.only_on(["esp32", "rp2040"]),
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)

View file

@ -324,6 +324,8 @@ mcp23s17:
deviceaddress: 1
sensor:
- platform: internal_temperature
name: "Internal Temperature"
- platform: ble_client
type: characteristic
ble_client_id: ble_foo

View file

@ -373,6 +373,8 @@ select:
"Three": 3
sensor:
- platform: internal_temperature
name: "Internal Temperature"
- platform: selec_meter
total_active_energy:
name: SelecEM2M Total Active Energy

View file

@ -37,3 +37,7 @@ switch:
- platform: output
output: pin_4
id: pin_4_switch
sensor:
- platform: internal_temperature
name: "Internal Temperature"