From 64f29e49c8b65413ffca9a345c82406e0c49da5e Mon Sep 17 00:00:00 2001 From: bullshit Date: Mon, 18 Aug 2025 01:37:31 +0200 Subject: [PATCH] refactor(ups-hid): battery status parsing in cyberpower protocol Signed-off-by: bullshit --- components/ups_hid/cyberpower_protocol.cpp | 29 ++++++++++++++-------- components/ups_hid/ups_hid.cpp | 18 +++++++++++--- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/components/ups_hid/cyberpower_protocol.cpp b/components/ups_hid/cyberpower_protocol.cpp index 0d220c0..0d0d2a3 100644 --- a/components/ups_hid/cyberpower_protocol.cpp +++ b/components/ups_hid/cyberpower_protocol.cpp @@ -343,11 +343,23 @@ void CyberPowerProtocol::parse_present_status_report(const HidReport &report, Up data.status_flags = status_flags; - ESP_LOGD(CP_TAG, "Status: AC:%s Charging:%s OnBatt:%s LowBatt:%s", + // Set battery status based on charging/discharging state (not FullChargeCapacity) + if (charging) { + data.battery_status = "charging"; + } else if (discharging || !ac_present) { + data.battery_status = "discharging"; + } else if (fully_charged) { + data.battery_status = "fully charged"; + } else { + data.battery_status = "normal"; + } + + ESP_LOGD(CP_TAG, "Status: AC:%s Charging:%s OnBatt:%s LowBatt:%s BattStatus:\"%s\"", ac_present ? "Yes" : "No", charging ? "Yes" : "No", (status_flags & UPS_STATUS_ON_BATTERY) ? "Yes" : "No", - (status_flags & UPS_STATUS_LOW_BATTERY) ? "Yes" : "No"); + (status_flags & UPS_STATUS_LOW_BATTERY) ? "Yes" : "No", + data.battery_status.c_str()); } void CyberPowerProtocol::parse_input_voltage_report(const HidReport &report, UpsData &data) { @@ -786,7 +798,7 @@ void CyberPowerProtocol::read_missing_dynamic_values(UpsData &data) { // 4. Set static/derived values based on NUT behavior data.ups_test_result = "No test initiated"; // Default test result - // NOTE: battery_status is now properly read from FullChargeCapacity in parse_battery_capacity_report + // NOTE: battery_status is now properly set based on charging state in parse_present_status_report // 5. Timer values represent active countdown (negative when no countdown active) // NUT shows: ups.timer.shutdown: -60, ups.timer.start: -60 @@ -829,15 +841,12 @@ void CyberPowerProtocol::parse_battery_capacity_limits_report(const HidReport &r data.battery_charge_low, remaining_limit); } - // Extract full charge capacity (offset 40 = byte 6) - this is used for battery.status + // Extract full charge capacity (offset 40 = byte 6) - this is NOT used for battery.status + // FullChargeCapacity represents maximum capacity (100%), not current status if (report.data.size() > 6) { uint8_t full_charge_capacity = report.data[6]; // Offset 40 bits = byte 5 + 1 - // Format as "XX%" like NUT cps_battstatus function does - char battery_status_buf[8]; - snprintf(battery_status_buf, sizeof(battery_status_buf), "%.0f%%", static_cast(full_charge_capacity)); - data.battery_status = std::string(battery_status_buf); - ESP_LOGI(CP_TAG, "CyberPower Battery status (FullChargeCapacity): %s (raw: %d)", - data.battery_status.c_str(), full_charge_capacity); + ESP_LOGD(CP_TAG, "CyberPower FullChargeCapacity: %d%% (always 100%% for healthy battery)", full_charge_capacity); + // Note: battery_status is now set from charging state in parse_present_status_report } } diff --git a/components/ups_hid/ups_hid.cpp b/components/ups_hid/ups_hid.cpp index 78d0c6a..3fbea92 100644 --- a/components/ups_hid/ups_hid.cpp +++ b/components/ups_hid/ups_hid.cpp @@ -1475,11 +1475,23 @@ namespace esphome esp_err_t UpsHidComponent::hid_get_report(uint8_t report_type, uint8_t report_id, uint8_t* data, size_t* data_len) { - if (!device_connected_ || !usb_device_.dev_hdl || !data || !data_len || *data_len == 0) { - ESP_LOGE(TAG, "HID GET_REPORT: Invalid parameters or device not connected"); + if (!device_connected_) + { + ESP_LOGE(TAG, "HID GET_REPORT: Device not connected (device_connected_=false)"); return ESP_ERR_INVALID_ARG; } - + if (!usb_device_.dev_hdl) + { + ESP_LOGE(TAG, "HID GET_REPORT: No device handle (usb_device_.dev_hdl=NULL)"); + return ESP_ERR_INVALID_ARG; + } + if (!data || !data_len || *data_len == 0) + { + ESP_LOGE(TAG, "HID GET_REPORT: Invalid parameters (data=%p, data_len=%p, *data_len=%zu)", + data, data_len, data_len ? *data_len : 0); + return ESP_ERR_INVALID_ARG; + } + ESP_LOGD(TAG, "HID GET_REPORT: type=0x%02X, id=0x%02X, max_len=%zu", report_type, report_id, *data_len); // Use specific buffer sizes based on working NUT implementation