1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 00:31:58 +00:00

pipsolar_teleinfo

This commit is contained in:
J. Nick Koston
2026-01-17 14:35:33 -10:00
parent e82cc8bbc5
commit 70fb561ce1
9 changed files with 19 additions and 21 deletions

View File

@@ -13,7 +13,7 @@ void PipsolarOutput::write_state(float state) {
if (std::find(this->possible_values_.begin(), this->possible_values_.end(), state) != this->possible_values_.end()) { if (std::find(this->possible_values_.begin(), this->possible_values_.end(), state) != this->possible_values_.end()) {
ESP_LOGD(TAG, "Will write: %s out of value %f / %02.0f", tmp, state, state); ESP_LOGD(TAG, "Will write: %s out of value %f / %02.0f", tmp, state, state);
this->parent_->queue_command(std::string(tmp)); this->parent_->queue_command(tmp);
} else { } else {
ESP_LOGD(TAG, "Will not write: %s as it is not in list of allowed values", tmp); ESP_LOGD(TAG, "Will not write: %s as it is not in list of allowed values", tmp);
} }

View File

@@ -45,20 +45,20 @@ void Pipsolar::loop() {
} else { } else {
ESP_LOGD(TAG, "command not successful"); ESP_LOGD(TAG, "command not successful");
} }
this->command_queue_[this->command_queue_position_] = std::string(""); this->command_queue_[this->command_queue_position_].clear();
this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH; this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH;
this->state_ = STATE_IDLE; this->state_ = STATE_IDLE;
} else { } else {
// crc failed // crc failed
// no log message necessary, check_incoming_crc_() logs // no log message necessary, check_incoming_crc_() logs
this->command_queue_[this->command_queue_position_] = std::string(""); this->command_queue_[this->command_queue_position_].clear();
this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH; this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH;
this->state_ = STATE_IDLE; this->state_ = STATE_IDLE;
} }
} else { } else {
ESP_LOGD(TAG, "command %s response length not OK: with length %zu", ESP_LOGD(TAG, "command %s response length not OK: with length %zu",
this->command_queue_[this->command_queue_position_].c_str(), this->read_pos_); this->command_queue_[this->command_queue_position_].c_str(), this->read_pos_);
this->command_queue_[this->command_queue_position_] = std::string(""); this->command_queue_[this->command_queue_position_].clear();
this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH; this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH;
this->state_ = STATE_IDLE; this->state_ = STATE_IDLE;
} }
@@ -127,7 +127,7 @@ void Pipsolar::loop() {
const char *command = this->command_queue_[this->command_queue_position_].c_str(); const char *command = this->command_queue_[this->command_queue_position_].c_str();
this->command_start_millis_ = millis(); this->command_start_millis_ = millis();
ESP_LOGD(TAG, "command %s timeout", command); ESP_LOGD(TAG, "command %s timeout", command);
this->command_queue_[this->command_queue_position_] = std::string(""); this->command_queue_[this->command_queue_position_].clear();
this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH; this->command_queue_position_ = (command_queue_position_ + 1) % COMMAND_QUEUE_LENGTH;
this->state_ = STATE_IDLE; this->state_ = STATE_IDLE;
return; return;
@@ -722,7 +722,7 @@ void Pipsolar::publish_binary_sensor_(esphome::optional<bool> b, binary_sensor::
} }
} }
esphome::optional<bool> Pipsolar::get_bit_(std::string bits, uint8_t bit_pos) { esphome::optional<bool> Pipsolar::get_bit_(const std::string &bits, uint8_t bit_pos) {
if (bit_pos >= bits.length()) { if (bit_pos >= bits.length()) {
return {}; return {};
} }

View File

@@ -224,7 +224,7 @@ class Pipsolar : public uart::UARTDevice, public PollingComponent {
void publish_binary_sensor_(esphome::optional<bool> b, binary_sensor::BinarySensor *sensor); void publish_binary_sensor_(esphome::optional<bool> b, binary_sensor::BinarySensor *sensor);
esphome::optional<bool> get_bit_(std::string bits, uint8_t bit_pos); esphome::optional<bool> get_bit_(const std::string &bits, uint8_t bit_pos);
std::string command_queue_[COMMAND_QUEUE_LENGTH]; std::string command_queue_[COMMAND_QUEUE_LENGTH];
uint8_t command_queue_position_ = 0; uint8_t command_queue_position_ = 0;

View File

@@ -4,8 +4,7 @@ namespace esphome {
namespace teleinfo { namespace teleinfo {
static const char *const TAG = "teleinfo_sensor"; static const char *const TAG = "teleinfo_sensor";
TeleInfoSensor::TeleInfoSensor(const char *tag) { this->tag = std::string(tag); } void TeleInfoSensor::publish_val(const char *val) {
void TeleInfoSensor::publish_val(const std::string &val) {
auto newval = parse_number<float>(val).value_or(0.0f); auto newval = parse_number<float>(val).value_or(0.0f);
publish_state(newval); publish_state(newval);
} }

View File

@@ -7,8 +7,8 @@ namespace teleinfo {
class TeleInfoSensor : public TeleInfoListener, public sensor::Sensor, public Component { class TeleInfoSensor : public TeleInfoListener, public sensor::Sensor, public Component {
public: public:
TeleInfoSensor(const char *tag); TeleInfoSensor(const char *tag) { this->tag = tag; }
void publish_val(const std::string &val) override; void publish_val(const char *val) override;
void dump_config() override; void dump_config() override;
}; };

View File

@@ -172,15 +172,15 @@ void TeleInfo::loop() {
/* Advance buf_finger to end of group */ /* Advance buf_finger to end of group */
buf_finger += field_len + 1 + 1 + 1; buf_finger += field_len + 1 + 1 + 1;
publish_value_(std::string(tag_), std::string(val_)); publish_value_(tag_, val_);
} }
state_ = OFF; state_ = OFF;
break; break;
} }
} }
void TeleInfo::publish_value_(const std::string &tag, const std::string &val) { void TeleInfo::publish_value_(const char *tag, const char *val) {
for (auto *element : teleinfo_listeners_) { for (auto *element : teleinfo_listeners_) {
if (tag != element->tag) if (strcmp(tag, element->tag) != 0)
continue; continue;
element->publish_val(val); element->publish_val(val);
} }

View File

@@ -18,8 +18,8 @@ static const uint16_t MAX_TIMESTAMP_SIZE = 14;
class TeleInfoListener { class TeleInfoListener {
public: public:
std::string tag; const char *tag{nullptr};
virtual void publish_val(const std::string &val){}; virtual void publish_val(const char *val){};
}; };
class TeleInfo : public PollingComponent, public uart::UARTDevice { class TeleInfo : public PollingComponent, public uart::UARTDevice {
public: public:
@@ -48,7 +48,7 @@ class TeleInfo : public PollingComponent, public uart::UARTDevice {
} state_{OFF}; } state_{OFF};
bool read_chars_until_(bool drop, uint8_t c); bool read_chars_until_(bool drop, uint8_t c);
bool check_crc_(const char *grp, const char *grp_end); bool check_crc_(const char *grp, const char *grp_end);
void publish_value_(const std::string &tag, const std::string &val); void publish_value_(const char *tag, const char *val);
}; };
} // namespace teleinfo } // namespace teleinfo
} // namespace esphome } // namespace esphome

View File

@@ -4,8 +4,7 @@ namespace esphome {
namespace teleinfo { namespace teleinfo {
static const char *const TAG = "teleinfo_text_sensor"; static const char *const TAG = "teleinfo_text_sensor";
TeleInfoTextSensor::TeleInfoTextSensor(const char *tag) { this->tag = std::string(tag); } void TeleInfoTextSensor::publish_val(const char *val) { publish_state(val); }
void TeleInfoTextSensor::publish_val(const std::string &val) { publish_state(val); }
void TeleInfoTextSensor::dump_config() { LOG_TEXT_SENSOR(" ", "Teleinfo Text Sensor", this); } void TeleInfoTextSensor::dump_config() { LOG_TEXT_SENSOR(" ", "Teleinfo Text Sensor", this); }
} // namespace teleinfo } // namespace teleinfo
} // namespace esphome } // namespace esphome

View File

@@ -5,8 +5,8 @@ namespace esphome {
namespace teleinfo { namespace teleinfo {
class TeleInfoTextSensor : public TeleInfoListener, public text_sensor::TextSensor, public Component { class TeleInfoTextSensor : public TeleInfoListener, public text_sensor::TextSensor, public Component {
public: public:
TeleInfoTextSensor(const char *tag); TeleInfoTextSensor(const char *tag) { this->tag = tag; }
void publish_val(const std::string &val) override; void publish_val(const char *val) override;
void dump_config() override; void dump_config() override;
}; };
} // namespace teleinfo } // namespace teleinfo