9static constexpr uint32_t REBOOT_INTERVAL_ID = 0;
11#define HYDREON_RGXX_COMMA ,
12static const char *
const PROTOCOL_NAMES[] = {HYDREON_RGXX_PROTOCOL_LIST(, HYDREON_RGXX_COMMA)};
13static const char *
const IGNORE_STRINGS[] = {HYDREON_RGXX_IGNORE_LIST(, HYDREON_RGXX_COMMA)};
15void HydreonRGxxComponent::dump_config() {
16 ESP_LOGCONFIG(TAG,
"hydreon_rgxx:");
23 ESP_LOGCONFIG(TAG,
" Model: RG15");
25 ESP_LOGCONFIG(TAG,
" Resolution: high");
27 ESP_LOGCONFIG(TAG,
" Resolution: low");
30 LOG_UPDATE_INTERVAL(
this);
33#define HYDREON_RGXX_LOG_SENSOR(s) \
34 if (this->sensors_[i++] != nullptr) { \
35 LOG_SENSOR(" ", #s, this->sensors_[i - 1]); \
37 HYDREON_RGXX_PROTOCOL_LIST(HYDREON_RGXX_LOG_SENSOR, );
40void HydreonRGxxComponent::setup() {
49 int ret = NUM_SENSORS;
50 for (
int i = 0; i < NUM_SENSORS; i++) {
62void HydreonRGxxComponent::update() {
65 for (
int i = 0; i < NUM_SENSORS; i++) {
70 ESP_LOGW(TAG,
"Missing %s", PROTOCOL_NAMES[i]);
77 ESP_LOGE(TAG,
"asking sensor to reboot");
78 for (
auto &sensor : this->
sensors_) {
79 PUBLISH_STATE(sensor, NAN);
88#ifdef USE_BINARY_SENSOR
89 PUBLISH_STATE(this->too_cold_binary_sensor_, this->
too_cold_);
90 PUBLISH_STATE(this->lens_bad_binary_sensor_, this->
lens_bad_);
91 PUBLISH_STATE(this->em_sat_binary_sensor_, this->
em_sat_);
100void HydreonRGxxComponent::loop() {
105 if (line !=
nullptr) {
129 ESP_LOGW(TAG,
"hydreon_rgxx failed to boot %d times", -this->
boot_count_);
134 ESP_LOGE(TAG,
"hydreon_rgxx can't boot, giving up");
135 for (
auto &sensor : this->
sensors_) {
136 PUBLISH_STATE(sensor, NAN);
144 ESP_LOGV(TAG,
"Read from serial: %s", line.
c_str());
146 if (line[0] ==
';') {
147 ESP_LOGI(TAG,
"Comment: %s", line.
c_str());
151 ESP_LOGD(TAG,
"Received empty line");
154 if (line.
size() == 1) {
156 ESP_LOGD(TAG,
"Received ack: %s", line.
c_str());
159 if (line.
find(
"LensBad") != std::string::npos) {
160 ESP_LOGW(TAG,
"Received LensBad!");
163 if (line.
find(
"EmSat") != std::string::npos) {
164 ESP_LOGW(TAG,
"Received EmSat!");
175 ESP_LOGI(TAG,
"Boot detected: %s", line.
c_str());
199 if (majend == std::string::npos || endversion == std::string::npos || majend > endversion) {
200 ESP_LOGW(TAG,
"invalid version string: %s", line.
c_str());
203 int major = line.
size() > 3 ? strtol(line.
c_str() + 3,
nullptr, 10) : 0;
204 int minor = strtol(line.
c_str() + (majend == std::string::npos ? 0 : majend + 1),
nullptr, 10);
206 if (major > 10 || minor >= 1000 || minor < 0 || major < 0) {
207 ESP_LOGW(TAG,
"invalid version: %s", line.
c_str());
210 ESP_LOGI(TAG,
"detected sw version %i", this->
sw_version_);
213 bool is_data_line =
false;
214 for (
int i = 0; i < NUM_SENSORS; i++) {
219 if (n == std::string::npos) {
224 if (n == line.
find(
't', n)) {
228 n = line.
find(
'F', n);
229 if (n == std::string::npos) {
234 n += strlen(PROTOCOL_NAMES[i]);
238 float data = strtof(line.
c_str() + n,
nullptr);
240 ESP_LOGD(TAG,
"Received %s: %f", PROTOCOL_NAMES[i], this->
sensors_[i]->get_raw_state());
245 this->
too_cold_ |= tc != std::string::npos;
247 ESP_LOGD(TAG,
"Received TooCold");
253 for (
const auto *ignore : IGNORE_STRINGS) {
255 ESP_LOGI(TAG,
"Ignoring %s", line.
c_str());
259 ESP_LOGI(TAG,
"Got unknown line: %s", line.
c_str());
void mark_failed()
Mark this component as failed.
bool cancel_interval(const char *name)
Cancel an interval function.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
char * feed(uint8_t c)
Feed one byte; returns the completed line (empty lines included), valid until the next call,...
void log_if_overflowed(const char *tag) const
Log the shared "line dropped" warning when the last feed() overflowed.
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
size_type find(const StringRef &s, size_type pos=0) const
Find first occurrence of a substring, returns std::string::npos if not found.
bool starts_with(const StringRef &prefix) const
True if the view begins with the given prefix (std::string::starts_with-like)
constexpr bool empty() const
constexpr size_type size() const
std::allocator_traits< allocator_type >::size_type size_type
int16_t no_response_count_
int num_sensors_missing_()
void process_line_(StringRef line)
line is NUL terminated
bool request_temperature_
LineReader< 80 > line_reader_
void schedule_reboot_()
Communication with the sensor is asynchronous.
RG15Resolution resolution_
sensor::Sensor * sensors_[NUM_SENSORS]
void publish_state(float state)
Publish a new state to the front-end.
void discard_available()
Read and drop every byte waiting in the receive buffer.
void write_str(const char *str)
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
ESPHOME_LOG_TAG(TAG, "hydreon_rgxx.sensor")