8static constexpr uint8_t NUMBER_OF_READ_RETRIES = 5;
9static constexpr uint8_t NUMBER_OF_RESET_RETRIES = 30;
10static constexpr uint32_t RESET_INTERVAL_ID = 0;
11static constexpr uint32_t RESET_INTERVAL_MS = 1000;
13void GDK101Component::update() {
40 this->
set_interval(RESET_INTERVAL_ID, RESET_INTERVAL_MS, [
this]() {
49 this->
mark_failed(LOG_STR(
"Reset failed after retries"));
57 uint8_t data[2] = {0};
69 this->
mark_failed(LOG_STR(
"Failed to read firmware version"));
78void GDK101Component::dump_config() {
79 ESP_LOGCONFIG(TAG,
"GDK101:");
82 LOG_SENSOR(
" ",
"Average Radaition Dose per 1 minute", this->rad_1m_sensor_);
83 LOG_SENSOR(
" ",
"Average Radaition Dose per 10 minutes", this->rad_10m_sensor_);
84 LOG_SENSOR(
" ",
"Status", this->status_sensor_);
85 LOG_SENSOR(
" ",
"Measurement Duration", this->measurement_duration_sensor_);
88#ifdef USE_BINARY_SENSOR
89 LOG_BINARY_SENSOR(
" ",
"Vibration Status", this->vibration_binary_sensor_);
93 LOG_TEXT_SENSOR(
" ",
"Firmware Version", this->fw_version_text_sensor_);
98 uint8_t retry = NUMBER_OF_READ_RETRIES;
100 while (!
status && retry) {
117 if (this->rad_1m_sensor_ !=
nullptr) {
118 if (!this->
read_bytes(GDK101_REG_READ_1MIN_AVG, data, 2)) {
119 ESP_LOGE(TAG,
"Updating GDK101 failed!");
123 const float dose = data[0] + (data[1] / 100.0f);
125 this->rad_1m_sensor_->publish_state(dose);
133 if (this->rad_10m_sensor_ !=
nullptr) {
134 if (!this->
read_bytes(GDK101_REG_READ_10MIN_AVG, data, 2)) {
135 ESP_LOGE(TAG,
"Updating GDK101 failed!");
139 const float dose = data[0] + (data[1] / 100.0f);
141 this->rad_10m_sensor_->publish_state(dose);
148 if (!this->
read_bytes(GDK101_REG_READ_STATUS, data, 2)) {
149 ESP_LOGE(TAG,
"Updating GDK101 failed!");
154 if (this->status_sensor_ !=
nullptr) {
155 this->status_sensor_->publish_state(data[0]);
159#ifdef USE_BINARY_SENSOR
160 if (this->vibration_binary_sensor_ !=
nullptr) {
161 this->vibration_binary_sensor_->publish_state(data[1]);
169#ifdef USE_TEXT_SENSOR
170 if (this->fw_version_text_sensor_ !=
nullptr) {
171 if (!this->
read_bytes(GDK101_REG_READ_FIRMWARE, data, 2)) {
172 ESP_LOGE(TAG,
"Updating GDK101 failed!");
178 snprintf(buf,
sizeof(buf),
"%d.%d", data[0], data[1]);
179 this->fw_version_text_sensor_->publish_state(buf);
187 if (this->measurement_duration_sensor_ !=
nullptr) {
188 if (!this->
read_bytes(GDK101_REG_READ_MEASURING_TIME, data, 2)) {
189 ESP_LOGE(TAG,
"Updating GDK101 failed!");
193 const float meas_time = (data[0] * 60) + data[1];
195 this->measurement_duration_sensor_->publish_state(meas_time);
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
void status_set_warning()
bool cancel_interval(const char *name)
Cancel an interval function.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
void status_clear_warning()
bool try_reset_()
Attempt to reset the sensor and read firmware version. Returns true on success or hard failure.
bool read_status_(uint8_t *data)
bool read_bytes_with_retry_(uint8_t a_register, uint8_t *data, uint8_t len)
bool read_dose_1m_(uint8_t *data)
bool read_dose_10m_(uint8_t *data)
bool read_fw_version_(uint8_t *data)
bool reset_sensor_(uint8_t *data)
uint8_t reset_retries_remaining_
bool read_measurement_duration_(uint8_t *data)
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
ESPHOME_LOG_TAG(TAG, "gdk101")
void HOT delay(uint32_t ms)