VictronMPPT-ESPHOME/components/victron/victron.cpp

561 lines
15 KiB
C++
Raw Normal View History

2021-05-30 10:28:38 +00:00
#include "victron.h"
#include "esphome/core/log.h"
2021-10-04 17:31:50 +00:00
#include <algorithm> // std::min
2021-05-30 10:28:38 +00:00
namespace esphome {
namespace victron {
2021-08-31 09:58:04 +00:00
static const char *const TAG = "victron";
2021-05-30 10:28:38 +00:00
void VictronComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Victron:");
LOG_SENSOR(" ", "Max Power Yesterday", max_power_yesterday_sensor_);
LOG_SENSOR(" ", "Max Power Today", max_power_today_sensor_);
LOG_SENSOR(" ", "Yield Total", yield_total_sensor_);
LOG_SENSOR(" ", "Yield Yesterday", yield_yesterday_sensor_);
LOG_SENSOR(" ", "Yield Today", yield_today_sensor_);
LOG_SENSOR(" ", "Panel Voltage", panel_voltage_sensor_);
LOG_SENSOR(" ", "Panel Power", panel_power_sensor_);
LOG_SENSOR(" ", "Battery Voltage", battery_voltage_sensor_);
LOG_SENSOR(" ", "Battery Current", battery_current_sensor_);
2021-09-21 20:42:27 +00:00
LOG_SENSOR(" ", "AC Out Voltage", ac_out_voltage_sensor_);
LOG_SENSOR(" ", "AC Out Current", ac_out_current_sensor_);
2021-05-30 15:32:46 +00:00
LOG_SENSOR(" ", "Load Current", load_current_sensor_);
2021-05-30 10:28:38 +00:00
LOG_SENSOR(" ", "Day Number", day_number_sensor_);
2021-09-01 09:56:00 +00:00
LOG_SENSOR(" ", "Charging Mode ID", charging_mode_id_sensor_);
2021-05-30 10:28:38 +00:00
LOG_SENSOR(" ", "Error Code", error_code_sensor_);
2021-09-21 20:42:27 +00:00
LOG_SENSOR(" ", "Warning Code", warning_code_sensor_);
2021-09-01 09:56:00 +00:00
LOG_SENSOR(" ", "Tracking Mode ID", tracking_mode_id_sensor_);
2021-09-21 20:42:27 +00:00
LOG_SENSOR(" ", "Device Mode ID", device_mode_id_sensor_);
2021-09-01 09:56:00 +00:00
LOG_TEXT_SENSOR(" ", "Charging Mode", charging_mode_text_sensor_);
2021-05-30 10:28:38 +00:00
LOG_TEXT_SENSOR(" ", "Error Text", error_text_sensor_);
2021-09-21 20:42:27 +00:00
LOG_TEXT_SENSOR(" ", "Warning Text", warning_text_sensor_);
2021-09-01 09:56:00 +00:00
LOG_TEXT_SENSOR(" ", "Tracking Mode", tracking_mode_text_sensor_);
2021-09-21 20:42:27 +00:00
LOG_TEXT_SENSOR(" ", "Device Mode", device_mode_text_sensor_);
2021-09-01 09:56:00 +00:00
LOG_TEXT_SENSOR(" ", "Firmware Version", firmware_version_text_sensor_);
LOG_TEXT_SENSOR(" ", "Device Type", device_type_text_sensor_);
LOG_BINARY_SENSOR(" ", "Load state", load_state_binary_sensor_);
LOG_BINARY_SENSOR(" ", "Relay state", relay_state_binary_sensor_);
2021-05-30 10:28:38 +00:00
check_uart_settings(19200);
}
void VictronComponent::loop() {
const uint32_t now = millis();
if ((state_ > 0) && (now - last_transmission_ >= 200)) {
// last transmission too long ago. Reset RX index.
ESP_LOGW(TAG, "Last transmission too long ago.");
state_ = 0;
}
if (!available())
return;
last_transmission_ = now;
while (available()) {
uint8_t c;
read_byte(&c);
if (state_ == 0) {
if ((c == '\r') || (c == '\n'))
continue;
label_.clear();
value_.clear();
state_ = 1;
}
if (state_ == 1) {
if (c == '\t')
state_ = 2;
else
label_.push_back(c);
continue;
}
if (state_ == 2) {
if (label_ == "Checksum") {
state_ = 0;
2022-05-29 13:00:38 +00:00
// The checksum is used as end of frame indicator
if (now - this->last_publish_ >= this->throttle_) {
this->last_publish_ = now;
this->publishing_ = true;
} else {
this->publishing_ = false;
}
2021-05-30 10:28:38 +00:00
continue;
}
if ((c == '\r') || (c == '\n')) {
2022-05-29 13:00:38 +00:00
if (this->publishing_) {
handle_value_();
}
2021-05-30 10:28:38 +00:00
state_ = 0;
} else {
value_.push_back(c);
}
}
}
}
2021-10-22 13:32:46 +00:00
static const std::string charging_mode_text(int value) {
2021-05-30 10:28:38 +00:00
switch (value) {
2021-08-29 07:01:00 +00:00
case 0:
2021-10-22 13:32:46 +00:00
return "Off";
2021-08-29 07:01:00 +00:00
case 2:
2021-10-22 13:32:46 +00:00
return "Fault";
2021-08-29 07:01:00 +00:00
case 3:
2021-10-22 13:32:46 +00:00
return "Bulk";
2021-08-29 07:01:00 +00:00
case 4:
2021-10-22 13:32:46 +00:00
return "Absorption";
2021-08-29 07:01:00 +00:00
case 5:
2021-10-22 13:32:46 +00:00
return "Float";
2021-08-29 07:01:00 +00:00
case 7:
2021-10-22 13:32:46 +00:00
return "Equalize (manual)";
2021-09-21 20:42:27 +00:00
case 9:
2021-10-22 13:32:46 +00:00
return "Inverting";
2021-08-29 07:01:00 +00:00
case 245:
2021-10-22 13:32:46 +00:00
return "Starting-up";
2021-08-29 07:01:00 +00:00
case 247:
2021-10-22 13:32:46 +00:00
return "Auto equalize / Recondition";
2021-08-29 07:01:00 +00:00
case 252:
2021-10-22 13:32:46 +00:00
return "External control";
2021-08-29 07:01:00 +00:00
default:
2021-10-22 13:32:46 +00:00
return "Unknown";
2021-05-30 10:28:38 +00:00
}
}
2021-10-22 13:32:46 +00:00
static const std::string error_code_text(int value) {
2021-05-30 10:28:38 +00:00
switch (value) {
2021-08-29 07:01:00 +00:00
case 0:
2021-10-22 13:32:46 +00:00
return "No error";
2021-08-29 07:01:00 +00:00
case 2:
2021-10-22 13:32:46 +00:00
return "Battery voltage too high";
2021-08-29 07:01:00 +00:00
case 17:
2021-10-22 13:32:46 +00:00
return "Charger temperature too high";
2021-08-29 07:01:00 +00:00
case 18:
2021-10-22 13:32:46 +00:00
return "Charger over current";
2021-08-29 07:01:00 +00:00
case 19:
2021-10-22 13:32:46 +00:00
return "Charger current reversed";
2021-08-29 07:01:00 +00:00
case 20:
2021-10-22 13:32:46 +00:00
return "Bulk time limit exceeded";
2021-08-29 07:01:00 +00:00
case 21:
2021-10-22 13:32:46 +00:00
return "Current sensor issue";
2021-08-29 07:01:00 +00:00
case 26:
2021-10-22 13:32:46 +00:00
return "Terminals overheated";
2021-08-29 07:01:00 +00:00
case 28:
2021-10-22 13:32:46 +00:00
return "Converter issue";
2021-08-29 07:01:00 +00:00
case 33:
2021-10-22 13:32:46 +00:00
return "Input voltage too high (solar panel)";
2021-08-29 07:01:00 +00:00
case 34:
2021-10-22 13:32:46 +00:00
return "Input current too high (solar panel)";
2021-08-29 07:01:00 +00:00
case 38:
2021-10-22 13:32:46 +00:00
return "Input shutdown (excessive battery voltage)";
2021-08-29 07:01:00 +00:00
case 39:
2021-10-22 13:32:46 +00:00
return "Input shutdown (due to current flow during off mode)";
2021-08-29 07:01:00 +00:00
case 65:
2021-10-22 13:32:46 +00:00
return "Lost communication with one of devices";
2021-08-29 07:01:00 +00:00
case 66:
2021-10-22 13:32:46 +00:00
return "Synchronised charging device configuration issue";
2021-08-29 07:01:00 +00:00
case 67:
2021-10-22 13:32:46 +00:00
return "BMS connection lost";
2021-08-29 07:01:00 +00:00
case 68:
2021-10-22 13:32:46 +00:00
return "Network misconfigured";
2021-08-29 07:01:00 +00:00
case 116:
2021-10-22 13:32:46 +00:00
return "Factory calibration data lost";
2021-08-29 07:01:00 +00:00
case 117:
2021-10-22 13:32:46 +00:00
return "Invalid/incompatible firmware";
2021-08-29 07:01:00 +00:00
case 119:
2021-10-22 13:32:46 +00:00
return "User settings invalid";
2021-08-29 07:01:00 +00:00
default:
2021-10-22 13:32:46 +00:00
return "Unknown";
2021-05-30 10:28:38 +00:00
}
}
2021-10-22 13:32:46 +00:00
static const std::string warning_code_text(int value) {
2021-09-21 20:42:27 +00:00
switch (value) {
case 0:
2021-10-22 13:32:46 +00:00
return "No warning";
2021-09-21 20:42:27 +00:00
case 1:
2021-10-22 13:32:46 +00:00
return "Low Voltage";
2021-09-21 20:42:27 +00:00
case 2:
2021-10-22 13:32:46 +00:00
return "High Voltage";
2021-09-21 20:42:27 +00:00
case 4:
2021-10-22 13:32:46 +00:00
return "Low SOC";
2021-09-21 20:42:27 +00:00
case 8:
2021-10-22 13:32:46 +00:00
return "Low Starter Voltage";
2021-09-21 20:42:27 +00:00
case 16:
2021-10-22 13:32:46 +00:00
return "High Starter Voltage";
2021-09-21 20:42:27 +00:00
case 32:
2021-10-22 13:32:46 +00:00
return "Low Temperature";
2021-09-21 20:42:27 +00:00
case 64:
2021-10-22 13:32:46 +00:00
return "High Temperature";
2021-09-21 20:42:27 +00:00
case 128:
2021-10-22 13:32:46 +00:00
return "Mid Voltage";
2021-09-21 20:42:27 +00:00
case 256:
2021-10-22 13:32:46 +00:00
return "Overload";
2021-09-21 20:42:27 +00:00
case 512:
2021-10-22 13:32:46 +00:00
return "DC-ripple";
2021-09-21 20:42:27 +00:00
case 1024:
2021-10-22 13:32:46 +00:00
return "Low V AC out";
2021-09-21 20:42:27 +00:00
case 2048:
2021-10-22 13:32:46 +00:00
return "High V AC out";
2021-09-21 20:42:27 +00:00
default:
2021-10-22 13:32:46 +00:00
return "Multiple warnings";
2021-09-21 20:42:27 +00:00
}
}
2021-05-30 10:28:38 +00:00
2021-09-01 09:56:00 +00:00
static const std::string tracking_mode_text(int value) {
2021-05-30 10:28:38 +00:00
switch (value) {
2021-08-29 07:01:00 +00:00
case 0:
return "Off";
case 1:
return "Limited";
case 2:
return "Active";
default:
return "Unknown";
2021-05-30 10:28:38 +00:00
}
}
2021-09-21 20:42:27 +00:00
static const std::string device_mode_text(int value) {
switch (value) {
case 0:
return "Off";
case 2:
return "On";
case 4:
return "Off";
case 5:
return "Eco";
default:
return "Unknown";
}
}
2021-10-22 13:32:46 +00:00
static const std::string device_type_text(int value) {
2021-05-30 10:28:38 +00:00
switch (value) {
2021-08-29 07:01:00 +00:00
case 0x203:
2021-10-22 13:32:46 +00:00
return "BMV-700";
2021-08-29 07:01:00 +00:00
case 0x204:
2021-10-22 13:32:46 +00:00
return "BMV-702";
2021-08-29 07:01:00 +00:00
case 0x205:
2021-10-22 13:32:46 +00:00
return "BMV-700H";
2021-08-29 07:01:00 +00:00
case 0xA389:
2021-10-22 13:32:46 +00:00
return "SmartShunt";
2021-08-29 07:01:00 +00:00
case 0xA381:
2021-10-22 13:32:46 +00:00
return "BMV-712 Smart";
2021-08-29 07:01:00 +00:00
case 0xA04C:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 75/10";
2021-08-29 07:01:00 +00:00
case 0x300:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 70/15";
2021-08-29 07:01:00 +00:00
case 0xA042:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 75/15";
2021-08-29 07:01:00 +00:00
case 0xA043:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 100/15";
2021-08-29 07:01:00 +00:00
case 0xA044:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 100/30 rev1";
2021-08-29 07:01:00 +00:00
case 0xA04A:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 100/30 rev2";
2021-08-29 07:01:00 +00:00
case 0xA041:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 150/35 rev1";
2021-08-29 07:01:00 +00:00
case 0xA04B:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 150/35 rev2";
2021-08-29 07:01:00 +00:00
case 0xA04D:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 150/45";
2021-08-29 07:01:00 +00:00
case 0xA040:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 75/50";
2021-08-29 07:01:00 +00:00
case 0xA045:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 100/50 rev1";
2021-08-29 07:01:00 +00:00
case 0xA049:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 100/50 rev2";
2021-08-29 07:01:00 +00:00
case 0xA04E:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 150/60";
2021-08-29 07:01:00 +00:00
case 0xA046:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 150/70";
2021-08-29 07:01:00 +00:00
case 0xA04F:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 150/85";
2021-08-29 07:01:00 +00:00
case 0xA047:
2021-10-22 13:32:46 +00:00
return "BlueSolar MPPT 150/100";
2021-08-29 07:01:00 +00:00
case 0xA050:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 250/100";
2021-08-29 07:01:00 +00:00
case 0xA051:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/100";
2021-08-29 07:01:00 +00:00
case 0xA052:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/85";
2021-08-29 07:01:00 +00:00
case 0xA053:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 75/15";
2021-08-29 07:01:00 +00:00
case 0xA054:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 75/10";
2021-08-29 07:01:00 +00:00
case 0xA055:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 100/15";
2021-08-29 07:01:00 +00:00
case 0xA056:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 100/30";
2021-08-29 07:01:00 +00:00
case 0xA057:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 100/50";
2021-08-29 07:01:00 +00:00
case 0xA058:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/35";
2021-08-29 07:01:00 +00:00
case 0xA059:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/100 rev2";
2021-08-29 07:01:00 +00:00
case 0xA05A:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/85 rev2";
2021-08-29 07:01:00 +00:00
case 0xA05B:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 250/70";
2021-08-29 07:01:00 +00:00
case 0xA05C:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 250/85";
2021-08-29 07:01:00 +00:00
case 0xA05D:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 250/60";
2021-08-29 07:01:00 +00:00
case 0xA05E:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 250/45";
2021-08-29 07:01:00 +00:00
case 0xA05F:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 100/20";
2021-08-29 07:01:00 +00:00
case 0xA060:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 100/20 48V";
2021-08-29 07:01:00 +00:00
case 0xA061:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/45";
2021-08-29 07:01:00 +00:00
case 0xA062:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/60";
2021-08-29 07:01:00 +00:00
case 0xA063:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 150/70";
2021-08-29 07:01:00 +00:00
case 0xA064:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 250/85 rev2";
2021-08-29 07:01:00 +00:00
case 0xA065:
2021-10-22 13:32:46 +00:00
return "SmartSolar MPPT 250/100 rev2";
2021-08-29 07:01:00 +00:00
case 0xA201:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 250VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA202:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 250VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA204:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 250VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA211:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 375VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA212:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 375VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA214:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 375VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA221:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 500VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA222:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 500VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA224:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 500VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA231:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 250VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA232:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 250VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA234:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 250VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA239:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 250VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA23A:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 250VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA23C:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 250VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA241:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 375VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA242:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 375VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA244:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 375VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA249:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 375VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA24A:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 375VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA24C:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 375VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA251:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 500VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA252:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 500VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA254:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 500VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA259:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 500VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA25A:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 500VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA25C:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 500VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA261:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 800VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA262:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 800VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA264:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 800VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA269:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 800VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA26A:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 800VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA26C:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 800VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA271:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 1200VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA272:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 1200VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA274:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 1200VA 230V";
2021-08-29 07:01:00 +00:00
case 0xA279:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 12V 1200VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA27A:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 24V 1200VA 120V";
2021-08-29 07:01:00 +00:00
case 0xA27C:
2021-10-22 13:32:46 +00:00
return "Phoenix Inverter 48V 1200VA 120V";
2021-08-29 07:01:00 +00:00
default:
2021-10-22 13:32:46 +00:00
return "Unknown";
2021-05-30 10:28:38 +00:00
}
}
void VictronComponent::handle_value_() {
int value;
if (label_ == "H23") {
2022-05-29 14:16:47 +00:00
this->publish_state_(max_power_yesterday_sensor_, atoi(value_.c_str())); // NOLINT(cert-err34-c)
return;
}
if (label_ == "H21") {
this->publish_state_(max_power_today_sensor_, atoi(value_.c_str())); // NOLINT(cert-err34-c)
return;
}
if (label_ == "H19") {
this->publish_state_(yield_total_sensor_, atoi(value_.c_str()) * 10); // NOLINT(cert-err34-c)
return;
}
if (label_ == "H22") {
this->publish_state_(yield_yesterday_sensor_, atoi(value_.c_str()) * 10); // NOLINT(cert-err34-c)
return;
}
if (label_ == "H20") {
this->publish_state_(yield_today_sensor_, atoi(value_.c_str()) * 10); // NOLINT(cert-err34-c)
return;
}
if (label_ == "VPV") {
this->publish_state_(panel_voltage_sensor_, atoi(value_.c_str()) / 1000.0); // NOLINT(cert-err34-c)
return;
}
if (label_ == "PPV") {
this->publish_state_(panel_power_sensor_, atoi(value_.c_str())); // NOLINT(cert-err34-c)
return;
}
if (label_ == "V") {
this->publish_state_(battery_voltage_sensor_, atoi(value_.c_str()) / 1000.0); // NOLINT(cert-err34-c)
return;
}
if (label_ == "I") {
this->publish_state_(battery_current_sensor_, atoi(value_.c_str()) / 1000.0); // NOLINT(cert-err34-c)
return;
}
if (label_ == "AC_OUT_V") {
this->publish_state_(ac_out_voltage_sensor_, atoi(value_.c_str()) / 100.0); // NOLINT(cert-err34-c)
return;
}
if (label_ == "AC_OUT_I") {
this->publish_state_(ac_out_current_sensor_, std::max(0.0, atoi(value_.c_str()) / 10.0)); // NOLINT(cert-err34-c)
return;
}
if (label_ == "IL") {
this->publish_state_(load_current_sensor_, atoi(value_.c_str()) / 1000.0); // NOLINT(cert-err34-c)
return;
}
if (label_ == "HSDS") {
this->publish_state_(day_number_sensor_, atoi(value_.c_str())); // NOLINT(cert-err34-c)
return;
}
if (label_ == "CS") {
2021-08-31 09:58:04 +00:00
value = atoi(value_.c_str()); // NOLINT(cert-err34-c)
2022-05-29 14:16:47 +00:00
this->publish_state_(charging_mode_id_sensor_, (float) value);
this->publish_state_(charging_mode_text_sensor_, charging_mode_text(value));
return;
}
if (label_ == "ERR") {
2021-08-31 09:58:04 +00:00
value = atoi(value_.c_str()); // NOLINT(cert-err34-c)
2022-05-29 14:16:47 +00:00
this->publish_state_(error_code_sensor_, value);
this->publish_state_(error_text_sensor_, error_code_text(value));
return;
}
if (label_ == "WARN") {
2021-09-21 20:42:27 +00:00
value = atoi(value_.c_str()); // NOLINT(cert-err34-c)
2022-05-29 14:16:47 +00:00
this->publish_state_(warning_code_sensor_, value);
this->publish_state_(warning_text_sensor_, warning_code_text(value));
return;
}
if (label_ == "MPPT") {
2021-08-31 09:58:04 +00:00
value = atoi(value_.c_str()); // NOLINT(cert-err34-c)
2022-05-29 14:16:47 +00:00
this->publish_state_(tracking_mode_id_sensor_, (float) value);
this->publish_state_(tracking_mode_text_sensor_, tracking_mode_text(value));
return;
}
if (label_ == "MODE") {
2021-09-21 20:42:27 +00:00
value = atoi(value_.c_str()); // NOLINT(cert-err34-c)
2022-05-29 14:16:47 +00:00
this->publish_state_(device_mode_id_sensor_, (float) value);
this->publish_state_(device_mode_text_sensor_, device_mode_text(value));
return;
}
if (label_ == "FW") {
this->publish_state_once_(firmware_version_text_sensor_, value_.insert(value_.size() - 2, "."));
return;
2021-05-30 10:28:38 +00:00
}
2022-05-29 14:16:47 +00:00
if (label_ == "PID") {
this->publish_state_once_(device_type_text_sensor_, device_type_text(strtol(value_.c_str(), nullptr, 0)));
return;
}
if (label_ == "LOAD") {
this->publish_state_(load_state_binary_sensor_, value_ == "ON");
return;
}
if (label_ == "RELAY") {
this->publish_state_(relay_state_binary_sensor_, value_ == "ON");
return;
}
2022-05-29 14:40:10 +00:00
ESP_LOGD(TAG, "Unhandled property: %s %s", label_.c_str(), value_.c_str());
2022-05-29 14:16:47 +00:00
}
void VictronComponent::publish_state_(binary_sensor::BinarySensor *binary_sensor, const bool &state) {
if (binary_sensor == nullptr)
return;
binary_sensor->publish_state(state);
}
void VictronComponent::publish_state_(sensor::Sensor *sensor, float value) {
if (sensor == nullptr)
return;
sensor->publish_state(value);
}
void VictronComponent::publish_state_(text_sensor::TextSensor *text_sensor, const std::string &state) {
if (text_sensor == nullptr)
return;
text_sensor->publish_state(state);
}
void VictronComponent::publish_state_once_(text_sensor::TextSensor *text_sensor, const std::string &state) {
if (text_sensor == nullptr)
return;
if (text_sensor->has_state())
return;
text_sensor->publish_state(state);
2021-05-30 10:28:38 +00:00
}
} // namespace victron
} // namespace esphome