refactor(ups-hid): battery status parsing in cyberpower protocol

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-08-18 01:37:31 +02:00
1 parent 2d26f2d54e
commit 64f29e49c8
2 files changed
+33 -12

No files matched your search

+19 -10
View File
@@ -343,11 +343,23 @@ void CyberPowerProtocol::parse_present_status_report(const HidReport &report, Up
data.status_flags = status_flags; 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", ac_present ? "Yes" : "No",
charging ? "Yes" : "No", charging ? "Yes" : "No",
(status_flags & UPS_STATUS_ON_BATTERY) ? "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) { 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 // 4. Set static/derived values based on NUT behavior
data.ups_test_result = "No test initiated"; // Default test result 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) // 5. Timer values represent active countdown (negative when no countdown active)
// NUT shows: ups.timer.shutdown: -60, ups.timer.start: -60 // 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); 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) { if (report.data.size() > 6) {
uint8_t full_charge_capacity = report.data[6]; // Offset 40 bits = byte 5 + 1 uint8_t full_charge_capacity = report.data[6]; // Offset 40 bits = byte 5 + 1
// Format as "XX%" like NUT cps_battstatus function does ESP_LOGD(CP_TAG, "CyberPower FullChargeCapacity: %d%% (always 100%% for healthy battery)", full_charge_capacity);
char battery_status_buf[8]; // Note: battery_status is now set from charging state in parse_present_status_report
snprintf(battery_status_buf, sizeof(battery_status_buf), "%.0f%%", static_cast<float>(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);
} }
} }
+14 -2
View File
@@ -1475,8 +1475,20 @@ namespace esphome
esp_err_t UpsHidComponent::hid_get_report(uint8_t report_type, uint8_t report_id, uint8_t* data, size_t* data_len) 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) { if (!device_connected_)
ESP_LOGE(TAG, "HID GET_REPORT: Invalid parameters or device not 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; return ESP_ERR_INVALID_ARG;
} }