Update protocol_goldenmate.cpp

This commit is contained in:
rwalker777 authored and GitHub committed 2026-05-02 13:15:00 -05:00
1 parent d5d81ab445
commit 1b63f30b4b
1 file changed
+63 -162
+63 -162
View File
@@ -13,7 +13,7 @@ static const char *const GM_TAG = "ups_hid.goldenmate";
REGISTER_UPS_PROTOCOL_FOR_VENDOR( REGISTER_UPS_PROTOCOL_FOR_VENDOR(
0x075D, GoldenMateProtocol_075D, 0x075D, GoldenMateProtocol_075D,
[](UpsHidComponent *p) { return std::make_unique<GoldenMateProtocol>(p); }, [](UpsHidComponent *p) { return std::make_unique<GoldenMateProtocol>(p); },
"GoldenMate UPS", "GoldenMate UPS Pro", 200); "GoldenMate", "GoldenMate UPS Pro", 200);
bool GoldenMateProtocol::read_feature_report(uint8_t report_id, HidReport &report) { bool GoldenMateProtocol::read_feature_report(uint8_t report_id, HidReport &report) {
if (!parent_->is_device_connected()) { if (!parent_->is_device_connected()) {
@@ -35,32 +35,22 @@ bool GoldenMateProtocol::read_feature_report(uint8_t report_id, HidReport &repor
} }
bool GoldenMateProtocol::detect() { bool GoldenMateProtocol::detect() {
ESP_LOGD(GM_TAG, "=== START RAW HEX DUMP FOR 0x075D ==="); ESP_LOGD(GM_TAG, "Detecting GoldenMate BMS for 0x075D...");
// The specific report IDs we saw your UPS answering to // Simply check if Report 0x0C exists and is long enough
uint8_t reports_to_check[] = {0x01, 0x06, 0x0C, 0x16, 0x30, 0x31}; HidReport megatec;
if (!read_feature_report(REPORT_ID_MEGATEC, megatec)) {
for (uint8_t r_id : reports_to_check) { ESP_LOGW(GM_TAG, "Failed to read Report 0x0C");
HidReport report; return false;
if (read_feature_report(r_id, report)) {
std::string hex_dump = "";
char buf[5];
// Loop through every single byte and convert to hex
for (size_t i = 0; i < report.data.size(); i++) {
snprintf(buf, sizeof(buf), "%02X ", report.data[i]);
hex_dump += buf;
}
ESP_LOGD(GM_TAG, "Report 0x%02X (%zu bytes): %s", r_id, report.data.size(), hex_dump.c_str());
} else {
ESP_LOGD(GM_TAG, "Report 0x%02X: Failed to read", r_id);
}
} }
ESP_LOGD(GM_TAG, "=== END RAW HEX DUMP ==="); if (megatec.data.size() < 62) {
ESP_LOGW(GM_TAG, "Report 0x0C too short: %zu bytes", megatec.data.size());
return false;
}
// Return false so ESPHome doesn't crash trying to parse it yet. ESP_LOGI(GM_TAG, "GoldenMate BMS protocol successfully detected!");
// It will safely fall back to the Generic HID parser after printing our data. return true;
return false;
} }
bool GoldenMateProtocol::initialize() { bool GoldenMateProtocol::initialize() {
@@ -71,18 +61,19 @@ bool GoldenMateProtocol::initialize() {
data.device.usb_vendor_id = parent_->get_vendor_id(); data.device.usb_vendor_id = parent_->get_vendor_id();
data.device.usb_product_id = parent_->get_product_id(); data.device.usb_product_id = parent_->get_product_id();
// Hardcode manufacturer
data.device.manufacturer = "GoldenMate";
// Try to get model from descriptor 2 (usually "Smart-Battery"), fallback to "UPS Pro"
std::string str; std::string str;
if (parent_->get_string_descriptor(1, str) == ESP_OK) { if (parent_->get_string_descriptor(2, str) == ESP_OK && !str.empty()) {
data.device.manufacturer = str;
} else {
data.device.manufacturer = "GoldenMate";
}
if (parent_->get_string_descriptor(2, str) == ESP_OK) {
data.device.model = str; data.device.model = str;
} else {
data.device.model = "UPS Pro";
} }
if (parent_->get_string_descriptor(3, str) == ESP_OK) { // Get Serial Number from descriptor 3
if (parent_->get_string_descriptor(3, str) == ESP_OK && !str.empty()) {
data.device.serial_number = str; data.device.serial_number = str;
} }
@@ -93,6 +84,7 @@ bool GoldenMateProtocol::initialize() {
data.device.manufacturer.c_str(), data.device.manufacturer.c_str(),
data.device.model.c_str(), data.device.model.c_str(),
data.device.serial_number.c_str()); data.device.serial_number.c_str());
return true; return true;
} }
@@ -114,7 +106,7 @@ bool GoldenMateProtocol::parse_binary_status(const HidReport &report, UpsData &d
const uint8_t *d = report.data.data(); const uint8_t *d = report.data.data();
// Battery level (byte 11) // Battery level (byte 11) - fallback if Report 0x0C fails
uint8_t battery_pct = d[11]; uint8_t battery_pct = d[11];
if (battery_pct <= 100) { if (battery_pct <= 100) {
data.battery.level = static_cast<float>(battery_pct); data.battery.level = static_cast<float>(battery_pct);
@@ -126,23 +118,6 @@ bool GoldenMateProtocol::parse_binary_status(const HidReport &report, UpsData &d
data.battery.runtime_minutes = static_cast<float>(runtime_seconds) / 60.0f; data.battery.runtime_minutes = static_cast<float>(runtime_seconds) / 60.0f;
} }
// Nominal input voltage (byte 16, static config value)
uint8_t nominal_voltage = d[16];
if (nominal_voltage > 50 && nominal_voltage < 250) {
data.power.input_voltage_nominal = static_cast<float>(nominal_voltage);
}
// Temperature (byte 20, BMS internal)
uint8_t temp = d[20];
if (temp > 0 && temp < 100) {
// Store as battery temperature via status string
// (no dedicated temperature field in UpsData)
}
ESP_LOGD(GM_TAG, "Binary: battery=%d%% runtime=%ds(%0.1fmin) nominal=%dV temp=%d°C",
battery_pct, runtime_seconds, data.battery.runtime_minutes,
nominal_voltage, temp);
return true; return true;
} }
@@ -152,21 +127,6 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
} }
const uint8_t *d = report.data.data(); const uint8_t *d = report.data.data();
// Extract the packed ASCII string from bytes 30-61 (32 chars)
// Format: LLLLFFFFOOOOBBBBFFFVVVTTTSSSSSSSS
// Load(4) TransferLow(4) TransferHigh(4) Batt(3) Freq(3) BattV(3) Temp(3) Status(8) = 32
// Verify we have ASCII digits
int digit_count = 0;
for (int i = 30; i < 62; i++) {
if (d[i] >= '0' && d[i] <= '9') digit_count++;
}
if (digit_count < 20) {
ESP_LOGD(GM_TAG, "Megatec string not ready (only %d digits)", digit_count);
return false;
}
std::string packed; std::string packed;
for (int i = 30; i < 62; i++) { for (int i = 30; i < 62; i++) {
if (d[i] >= '0' && d[i] <= '9') { if (d[i] >= '0' && d[i] <= '9') {
@@ -175,92 +135,50 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
} }
if (packed.size() < 30) { if (packed.size() < 30) {
ESP_LOGD(GM_TAG, "Packed Megatec string too short: %zu chars", packed.size()); ESP_LOGD(GM_TAG, "Packed BMS string too short: %zu chars", packed.size());
return false; return false;
} }
ESP_LOGD(GM_TAG, "Megatec packed: %s", packed.c_str()); // Field 1: Current (4 chars) -> e.g. "1491" = 14.91 A
float current = std::atof(packed.substr(0, 4).c_str()) / 100.0f;
data.power.load_percent = current; // Mapped to load_percent
// Parse fields from packed string // Field 2: Voltage (4 chars) -> e.g. "1400" = 14.00 V
// Field 1: Load % × 10 (4 chars) float voltage = std::atof(packed.substr(4, 4).c_str()) / 100.0f;
// Note: at no/very low load, this field reads 9999 (999.9%) which is data.battery.voltage = voltage;
// the battery % leaking into this field. Only trust values <= 100%.
std::string load_str = packed.substr(0, 4);
float load_raw = std::atof(load_str.c_str()) / 10.0f;
if (load_raw >= 0.0f && load_raw <= 100.0f) {
data.power.load_percent = load_raw;
} else {
// At no load, field 1 shows battery % — use 0
data.power.load_percent = 0.0f;
}
// Field 2: Low voltage transfer × 10 (4 chars) // Field 3: Power (4 chars) -> e.g. "2084" = 208.4 W
std::string transfer_low_str = packed.substr(4, 4); float power = std::atof(packed.substr(8, 4).c_str()) / 10.0f;
float transfer_low = std::atof(transfer_low_str.c_str()) / 10.0f; data.power.input_voltage = power; // Mapped to input_voltage
if (transfer_low > 0.0f && transfer_low < 300.0f) {
data.power.input_transfer_low = transfer_low;
}
// Field 3: High voltage transfer × 10 (4 chars) // Field 4: SOC % (3 chars) -> e.g. "100" = 100%
std::string transfer_high_str = packed.substr(8, 4); float soc = std::atof(packed.substr(12, 3).c_str());
float transfer_high = std::atof(transfer_high_str.c_str()) / 10.0f; data.battery.level = soc;
if (transfer_high > 0.0f && transfer_high < 300.0f) {
data.power.input_transfer_high = transfer_high;
}
// Field 4: Battery % (3 chars) // Field 5: Capacity Ah (3 chars) -> e.g. "599" = 59.9 Ah
std::string batt_str = packed.substr(12, 3); float capacity = std::atof(packed.substr(15, 3).c_str()) / 10.0f;
float batt_pct = std::atof(batt_str.c_str()); data.power.frequency = capacity; // Mapped to frequency
// Use Report 0x01 battery value preferentially (already set in parse_binary_status)
// Field 5: Frequency × 10 (3 chars) // Field 6: Cycles (3 chars) -> e.g. "205" = 205 cycles
std::string freq_str = packed.substr(15, 3); float cycles = std::atof(packed.substr(18, 3).c_str());
float freq = std::atof(freq_str.c_str()) / 10.0f; data.power.output_voltage = cycles; // Mapped to output_voltage
if (freq >= 45.0f && freq <= 65.0f) {
data.power.frequency = freq;
}
// Field 6: Battery voltage × 100 (3 chars) // Field 7: Temperature (3 chars) -> e.g. "350" = 35.0 °C
std::string battv_str = packed.substr(18, 3); float temp = std::atof(packed.substr(21, 3).c_str()) / 10.0f;
float batt_voltage = std::atof(battv_str.c_str()) / 100.0f; data.power.input_transfer_low = temp; // Mapped to input_transfer_low
if (batt_voltage > 0.0f && batt_voltage < 60.0f) {
data.battery.voltage = batt_voltage;
}
// Field 7: Temperature × 10 (3 chars)
std::string temp_str = packed.substr(21, 3);
float temp = std::atof(temp_str.c_str()) / 10.0f;
// No dedicated temperature field in UpsData, but log it
// Field 8: Status bits (8 chars)
std::string status_str;
if (packed.size() >= 30) {
status_str = packed.substr(24, 8);
}
// Parse status bits
bool on_battery = false;
if (status_str.size() >= 7) {
// Bit 6 (index 1 from left in "01000000") = on battery
on_battery = (status_str[1] == '1');
}
// Set power status // Set power status
if (on_battery) { data.power.status = "Online";
data.power.status = "On Battery"; if (soc >= 100.0f) {
data.battery.status = "Discharging";
} else {
data.power.status = "Online";
if (batt_pct >= 99.0f) {
data.battery.status = "Full"; data.battery.status = "Full";
} else { } else if (current > 0.0f) {
data.battery.status = "Charging"; data.battery.status = "Charging";
} } else {
data.battery.status = "Discharging";
} }
ESP_LOGD(GM_TAG, "Megatec: load=%.1f%% freq=%.1fHz battV=%.2fV temp=%.1f°C status=%s on_battery=%s", ESP_LOGI(GM_TAG, "Decoded BMS Data: %.2fV | %.2fA | %.1fW | %.0f%% | %.1fAh | %.0f Cycles | %.1fC",
load_raw, freq, batt_voltage, temp, voltage, current, power, soc, capacity, cycles, temp);
status_str.c_str(), on_battery ? "YES" : "no");
return true; return true;
} }
@@ -268,7 +186,7 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
bool GoldenMateProtocol::read_data(UpsData &data) { bool GoldenMateProtocol::read_data(UpsData &data) {
bool success = false; bool success = false;
// Read binary status from Report 0x01 // Read binary status from Report 0x01 (Grabs the runtime estimate)
HidReport status_report; HidReport status_report;
if (read_feature_report(REPORT_ID_STATUS, status_report)) { if (read_feature_report(REPORT_ID_STATUS, status_report)) {
if (parse_binary_status(status_report, data)) { if (parse_binary_status(status_report, data)) {
@@ -276,7 +194,7 @@ bool GoldenMateProtocol::read_data(UpsData &data) {
} }
} }
// Read Megatec ASCII data from Report 0x0C // Read BMS ASCII data from Report 0x0C (Grabs voltage, current, power, etc.)
HidReport megatec_report; HidReport megatec_report;
if (read_feature_report(REPORT_ID_MEGATEC, megatec_report)) { if (read_feature_report(REPORT_ID_MEGATEC, megatec_report)) {
if (parse_megatec_string(megatec_report, data)) { if (parse_megatec_string(megatec_report, data)) {
@@ -284,34 +202,17 @@ bool GoldenMateProtocol::read_data(UpsData &data) {
} }
} }
// Set device info // Set device info to ensure it populates sensors
data.device.usb_vendor_id = parent_->get_vendor_id(); data.device.manufacturer = "GoldenMate";
data.device.usb_product_id = parent_->get_product_id();
std::string str; std::string str;
if (data.device.manufacturer.empty()) { if (parent_->get_string_descriptor(2, str) == ESP_OK && !str.empty()) {
if (parent_->get_string_descriptor(1, str) == ESP_OK) { data.device.model = str;
data.device.manufacturer = str; } else {
} else { data.device.model = "UPS Pro";
data.device.manufacturer = "GoldenMate";
}
} }
if (data.device.model.empty()) { if (parent_->get_string_descriptor(3, str) == ESP_OK && !str.empty()) {
if (parent_->get_string_descriptor(2, str) == ESP_OK) { data.device.serial_number = str;
data.device.model = str;
} else {
data.device.model = "Smart-Battery";
}
} }
if (data.device.serial_number.empty()) {
if (parent_->get_string_descriptor(3, str) == ESP_OK) {
data.device.serial_number = str;
}
}
// Set apparent power rating (1000VA for the GoldenMate 1000VA Pro)
// The packed Megatec bytes 18-19 in Report 0x01 = 1320, possibly VA rating
data.power.apparent_power_nominal = 1000.0f;
return success; return success;
} }