ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
internal_temperature_zephyr.cpp
Go to the documentation of this file.
1#if defined(USE_ZEPHYR) && defined(USE_NRF52)
2
3#include "esphome/core/log.h"
5
6#include <zephyr/device.h>
7#include <zephyr/drivers/sensor.h>
8
10
11static const char *const TAG = "internal_temperature.zephyr";
12
13static const struct device *const DIE_TEMPERATURE_SENSOR = DEVICE_DT_GET_ONE(nordic_nrf_temp);
14
16 struct sensor_value value;
17 int result = sensor_sample_fetch(DIE_TEMPERATURE_SENSOR);
18 if (result != 0) {
19 ESP_LOGE(TAG, "Failed to fetch nRF52 die temperature sample (%d)", result);
20 if (!this->has_state()) {
21 this->publish_state(NAN);
22 }
23 return;
24 }
25
26 result = sensor_channel_get(DIE_TEMPERATURE_SENSOR, SENSOR_CHAN_DIE_TEMP, &value);
27 if (result != 0) {
28 ESP_LOGE(TAG, "Failed to get nRF52 die temperature (%d)", result);
29 if (!this->has_state()) {
30 this->publish_state(NAN);
31 }
32 return;
33 }
34
35 const float temperature = value.val1 + (value.val2 / 1000000.0f);
36 if (std::isfinite(temperature)) {
37 this->publish_state(temperature);
38 } else {
39 ESP_LOGD(TAG, "Ignoring invalid nRF52 temperature (value=%.1f)", temperature);
40 if (!this->has_state()) {
41 this->publish_state(NAN);
42 }
43 }
44}
45
47 if (!device_is_ready(DIE_TEMPERATURE_SENSOR)) {
48 ESP_LOGE(TAG, "nRF52 die temperature sensor device %s not ready", DIE_TEMPERATURE_SENSOR->name);
49 this->mark_failed();
50 return;
51 }
52}
53
54} // namespace esphome::internal_temperature
55
56#endif // USE_ZEPHYR && USE_NRF52
void mark_failed()
Mark this component as failed.
bool has_state() const
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t temperature
Definition sun_gtil2.cpp:12