Public Access
refactor(ups-hid): UPS HID component codebase use proper constants instead of hardcoded values
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
6 files changed
+395
-195
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#include "apc_hid_protocol.h"
|
||||
#include "hid_constants.h"
|
||||
#include "ups_constants.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include <cstring>
|
||||
@@ -35,7 +36,7 @@ static const uint8_t APC_REPORT_ID_SENSITIVITY = 0x35; // Input sensitivity (
|
||||
|
||||
// APC-specific date conversion (hex-as-decimal format)
|
||||
static std::string convert_apc_date(uint32_t apc_date) {
|
||||
if (apc_date == 0) return "Unknown";
|
||||
if (apc_date == 0) return status::UNKNOWN;
|
||||
|
||||
// APC uses hex-as-decimal format, e.g., 0x102202 = 10/22/02
|
||||
uint8_t month = (apc_date >> 16) & 0xFF;
|
||||
@@ -63,7 +64,7 @@ bool ApcHidProtocol::detect() {
|
||||
}
|
||||
|
||||
// Give device time to initialize after connection
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
vTaskDelay(pdMS_TO_TICKS(timing::USB_INITIALIZATION_DELAY_MS));
|
||||
|
||||
// Try key report IDs based on NUT analysis - these are the critical ones
|
||||
const uint8_t test_report_ids[] = {
|
||||
@@ -92,7 +93,7 @@ bool ApcHidProtocol::detect() {
|
||||
}
|
||||
|
||||
// Small delay between attempts
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
vTaskDelay(pdMS_TO_TICKS(timing::REPORT_RETRY_DELAY_MS));
|
||||
}
|
||||
|
||||
ESP_LOGD(APC_HID_TAG, "APC HID Protocol detection failed - no reports responded");
|
||||
@@ -351,17 +352,17 @@ void ApcHidProtocol::parse_status_report(const HidReport &report, UpsData &data)
|
||||
// Update power status based on AC presence and discharging
|
||||
if (ac_present && !discharging) {
|
||||
data.power.input_voltage = parent_->get_fallback_nominal_voltage(); // Use configured fallback voltage when AC present
|
||||
data.power.status = "Online";
|
||||
data.power.status = status::ONLINE;
|
||||
} else {
|
||||
data.power.input_voltage = NAN; // No AC input
|
||||
data.power.status = "On Battery";
|
||||
data.power.status = status::ON_BATTERY;
|
||||
}
|
||||
|
||||
// Update battery status
|
||||
if (charging) {
|
||||
data.battery.status = "Charging";
|
||||
data.battery.status = battery_status::CHARGING;
|
||||
} else if (discharging) {
|
||||
data.battery.status = "Discharging";
|
||||
data.battery.status = battery_status::DISCHARGING;
|
||||
} else {
|
||||
data.battery.status = "Not Charging";
|
||||
}
|
||||
@@ -388,7 +389,7 @@ void ApcHidProtocol::parse_status_report(const HidReport &report, UpsData &data)
|
||||
if (report.data.size() >= 4) {
|
||||
uint8_t shutdown_byte = report.data[3];
|
||||
if (shutdown_byte > 0) {
|
||||
data.battery.charge_low = 10.0f; // Indicate low battery threshold
|
||||
data.battery.charge_low = battery::LOW_THRESHOLD_PERCENT; // Indicate low battery threshold
|
||||
}
|
||||
}
|
||||
|
||||
@@ -461,7 +462,7 @@ void ApcHidProtocol::parse_voltage_report(const HidReport &report, UpsData &data
|
||||
float voltage_scaled;
|
||||
if (voltage_raw > 1000) {
|
||||
// Assume raw value needs scaling (e.g., 1367 → 136.7V)
|
||||
voltage_scaled = voltage_raw / 10.0f;
|
||||
voltage_scaled = voltage_raw / battery::VOLTAGE_SCALE_FACTOR;
|
||||
} else {
|
||||
voltage_scaled = static_cast<float>(voltage_raw);
|
||||
}
|
||||
@@ -613,24 +614,24 @@ void ApcHidProtocol::parse_present_status_report(const HidReport &report, UpsDat
|
||||
// Update power status based on AC presence and discharging
|
||||
if (ac_present && !discharging) {
|
||||
data.power.input_voltage = parent_->get_fallback_nominal_voltage(); // Use configured fallback voltage when AC present
|
||||
data.power.status = "Online";
|
||||
data.power.status = status::ONLINE;
|
||||
} else {
|
||||
data.power.input_voltage = NAN; // No AC input
|
||||
data.power.status = "On Battery";
|
||||
data.power.status = status::ON_BATTERY;
|
||||
}
|
||||
|
||||
// Update battery status
|
||||
if (charging) {
|
||||
data.battery.status = "Charging";
|
||||
data.battery.status = battery_status::CHARGING;
|
||||
} else if (discharging) {
|
||||
data.battery.status = "Discharging";
|
||||
data.battery.status = battery_status::DISCHARGING;
|
||||
} else {
|
||||
data.battery.status = "Normal";
|
||||
}
|
||||
|
||||
// Handle battery issues
|
||||
if (below_capacity || shutdown_imminent) {
|
||||
data.battery.charge_low = 10.0f; // Indicate low battery threshold
|
||||
data.battery.charge_low = battery::LOW_THRESHOLD_PERCENT; // Indicate low battery threshold
|
||||
if (shutdown_imminent) {
|
||||
data.battery.status += " - Shutdown Imminent";
|
||||
}
|
||||
@@ -1175,7 +1176,7 @@ void ApcHidProtocol::parse_battery_voltage_nominal_report(const HidReport &repor
|
||||
// Data format: [ID, volt_low, volt_high] - 16-bit little endian
|
||||
// CRITICAL FIX: Raw value 1200 needs to be scaled to 12.0V (divide by 100)
|
||||
uint16_t voltage_raw = report.data[1] | (report.data[2] << 8);
|
||||
data.battery.voltage_nominal = static_cast<float>(voltage_raw) / 100.0f;
|
||||
data.battery.voltage_nominal = static_cast<float>(voltage_raw) / battery::MAX_LEVEL_PERCENT;
|
||||
|
||||
ESP_LOGI(APC_HID_TAG, "APC Battery voltage nominal: %.1fV (raw: 0x%02X%02X = %d, scaled from %d)",
|
||||
data.battery.voltage_nominal, report.data[2], report.data[1], voltage_raw, voltage_raw);
|
||||
@@ -1193,20 +1194,20 @@ void ApcHidProtocol::parse_battery_voltage_actual_report(const HidReport &report
|
||||
uint16_t voltage_raw = report.data[1] | (report.data[2] << 8);
|
||||
|
||||
// Try different scaling factors to match NUT value of ~13.67V
|
||||
float voltage_candidate1 = static_cast<float>(voltage_raw) / 100.0f; // Same as nominal
|
||||
float voltage_candidate2 = static_cast<float>(voltage_raw) / 10.0f; // Less scaling
|
||||
float voltage_candidate1 = static_cast<float>(voltage_raw) / battery::MAX_LEVEL_PERCENT; // Same as nominal
|
||||
float voltage_candidate2 = static_cast<float>(voltage_raw) / battery::VOLTAGE_SCALE_FACTOR; // Less scaling
|
||||
float voltage_candidate3 = static_cast<float>(voltage_raw); // No scaling
|
||||
|
||||
// Choose the one closest to expected battery voltage range (10-15V)
|
||||
if (voltage_candidate1 >= 10.0f && voltage_candidate1 <= 20.0f) {
|
||||
if (voltage_candidate1 >= battery::VOLTAGE_SCALE_FACTOR && voltage_candidate1 <= 20.0f) {
|
||||
data.battery.voltage = voltage_candidate1;
|
||||
ESP_LOGI(APC_HID_TAG, "APC Battery voltage: %.2fV (raw: 0x%02X%02X = %d, scaled /100)",
|
||||
data.battery.voltage, report.data[2], report.data[1], voltage_raw);
|
||||
} else if (voltage_candidate2 >= 10.0f && voltage_candidate2 <= 20.0f) {
|
||||
} else if (voltage_candidate2 >= battery::VOLTAGE_SCALE_FACTOR && voltage_candidate2 <= 20.0f) {
|
||||
data.battery.voltage = voltage_candidate2;
|
||||
ESP_LOGI(APC_HID_TAG, "APC Battery voltage: %.2fV (raw: 0x%02X%02X = %d, scaled /10)",
|
||||
data.battery.voltage, report.data[2], report.data[1], voltage_raw);
|
||||
} else if (voltage_candidate3 >= 10.0f && voltage_candidate3 <= 20.0f) {
|
||||
} else if (voltage_candidate3 >= battery::VOLTAGE_SCALE_FACTOR && voltage_candidate3 <= 20.0f) {
|
||||
data.battery.voltage = voltage_candidate3;
|
||||
ESP_LOGI(APC_HID_TAG, "APC Battery voltage: %.2fV (raw: 0x%02X%02X = %d, no scaling)",
|
||||
data.battery.voltage, report.data[2], report.data[1], voltage_raw);
|
||||
@@ -1419,7 +1420,7 @@ void ApcHidProtocol::parse_battery_chemistry_report(const HidReport &report, Ups
|
||||
data.battery.type = "LiPoly";
|
||||
break;
|
||||
default:
|
||||
data.battery.type = "Unknown";
|
||||
data.battery.type = battery_status::UNKNOWN;
|
||||
ESP_LOGW(APC_HID_TAG, "Unknown APC battery chemistry value: %d", chemistry_raw);
|
||||
break;
|
||||
}
|
||||
@@ -1440,7 +1441,7 @@ bool ApcHidProtocol::beeper_enable() {
|
||||
uint8_t report_id = report_ids_to_try[i];
|
||||
ESP_LOGD(APC_HID_TAG, "Trying beeper enable with report ID 0x%02X", report_id);
|
||||
|
||||
uint8_t beeper_data[2] = {report_id, 0x02}; // Report ID, Value=2 (enabled)
|
||||
uint8_t beeper_data[2] = {report_id, beeper::CONTROL_ENABLE}; // Report ID, Value=2 (enabled)
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, report_id, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
@@ -1467,7 +1468,7 @@ bool ApcHidProtocol::beeper_disable() {
|
||||
uint8_t report_id = report_ids_to_try[i];
|
||||
ESP_LOGD(APC_HID_TAG, "Trying beeper disable with report ID 0x%02X", report_id);
|
||||
|
||||
uint8_t beeper_data[2] = {report_id, 0x01}; // Report ID, Value=1 (disabled)
|
||||
uint8_t beeper_data[2] = {report_id, beeper::CONTROL_DISABLE}; // Report ID, Value=1 (disabled)
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, report_id, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
@@ -1497,7 +1498,7 @@ bool ApcHidProtocol::beeper_mute() {
|
||||
uint8_t report_id = report_ids_to_try[i];
|
||||
ESP_LOGD(APC_HID_TAG, "Trying beeper mute (acknowledge alarms) with report ID 0x%02X", report_id);
|
||||
|
||||
uint8_t beeper_data[2] = {report_id, 0x03}; // Report ID, Value=3 (muted/acknowledged)
|
||||
uint8_t beeper_data[2] = {report_id, beeper::CONTROL_MUTE}; // Report ID, Value=3 (muted/acknowledged)
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, report_id, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
@@ -1522,7 +1523,7 @@ bool ApcHidProtocol::beeper_test() {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t original_state = (current_report.data.size() >= 2) ? current_report.data[1] : 0x02;
|
||||
uint8_t original_state = (current_report.data.size() >= 2) ? current_report.data[1] : beeper::CONTROL_ENABLE;
|
||||
ESP_LOGI(APC_HID_TAG, "Original beeper state: %d", original_state);
|
||||
|
||||
// For APC beeper test, we need to:
|
||||
@@ -1585,7 +1586,7 @@ bool ApcHidProtocol::start_battery_test_quick() {
|
||||
|
||||
// For supported models: Based on NUT debug logs, APC uses report ID 0x52 for battery test
|
||||
// Command value 1 = Quick test (based on NUT test_write_info struct)
|
||||
uint8_t test_data[2] = {0x52, 1};
|
||||
uint8_t test_data[2] = {0x52, test::COMMAND_QUICK};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x52, test_data, sizeof(test_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
@@ -1611,7 +1612,7 @@ bool ApcHidProtocol::start_battery_test_deep() {
|
||||
|
||||
// For supported models: Based on NUT debug logs, APC uses report ID 0x52 for battery test
|
||||
// Command value 2 = Deep test (based on NUT test_write_info struct)
|
||||
uint8_t test_data[2] = {0x52, 2};
|
||||
uint8_t test_data[2] = {0x52, test::COMMAND_DEEP};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x52, test_data, sizeof(test_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
@@ -1637,7 +1638,7 @@ bool ApcHidProtocol::stop_battery_test() {
|
||||
|
||||
// For supported models: Based on NUT debug logs, APC uses report ID 0x52 for battery test
|
||||
// Command value 3 = Abort test (based on NUT test_write_info struct)
|
||||
uint8_t test_data[2] = {0x52, 3};
|
||||
uint8_t test_data[2] = {0x52, test::COMMAND_ABORT};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x52, test_data, sizeof(test_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
@@ -1847,7 +1848,7 @@ float ApcHidProtocol::parse_frequency_from_report(const HidReport &report) {
|
||||
// Method 4: Scaled frequency (multiply by 0.1 for devices reporting in 0.1 Hz units)
|
||||
if (report.data.size() >= 3) {
|
||||
uint16_t freq_scaled = report.data[1] | (report.data[2] << 8);
|
||||
float freq_value = static_cast<float>(freq_scaled) / 10.0f;
|
||||
float freq_value = static_cast<float>(freq_scaled) / battery::VOLTAGE_SCALE_FACTOR;
|
||||
ESP_LOGV(APC_HID_TAG, "Method 4 - Testing scaled frequency: raw=%d, scaled=%.1f - Range check: %s",
|
||||
freq_scaled, freq_value,
|
||||
(freq_value >= FREQUENCY_MIN_VALID && freq_value <= FREQUENCY_MAX_VALID) ? "PASS" : "FAIL");
|
||||
@@ -1860,7 +1861,7 @@ float ApcHidProtocol::parse_frequency_from_report(const HidReport &report) {
|
||||
// Method 5: APC-specific frequency encoding - scale by 0.01 (some APC models use centihz)
|
||||
if (report.data.size() >= 3) {
|
||||
uint16_t freq_scaled = report.data[1] | (report.data[2] << 8);
|
||||
float freq_value = static_cast<float>(freq_scaled) / 100.0f;
|
||||
float freq_value = static_cast<float>(freq_scaled) / battery::MAX_LEVEL_PERCENT;
|
||||
ESP_LOGV(APC_HID_TAG, "Method 5 - Testing APC centihz frequency: raw=%d, scaled=%.2f - Range check: %s",
|
||||
freq_scaled, freq_value,
|
||||
(freq_value >= FREQUENCY_MIN_VALID && freq_value <= FREQUENCY_MAX_VALID) ? "PASS" : "FAIL");
|
||||
@@ -1873,7 +1874,7 @@ float ApcHidProtocol::parse_frequency_from_report(const HidReport &report) {
|
||||
// Method 6: Try big-endian centihz scaling (some APC devices)
|
||||
if (report.data.size() >= 3) {
|
||||
uint16_t freq_scaled = (report.data[1] << 8) | report.data[2];
|
||||
float freq_value = static_cast<float>(freq_scaled) / 100.0f;
|
||||
float freq_value = static_cast<float>(freq_scaled) / battery::MAX_LEVEL_PERCENT;
|
||||
ESP_LOGV(APC_HID_TAG, "Method 6 - Testing APC big-endian centihz: raw=%d, scaled=%.2f - Range check: %s",
|
||||
freq_scaled, freq_value,
|
||||
(freq_value >= FREQUENCY_MIN_VALID && freq_value <= FREQUENCY_MAX_VALID) ? "PASS" : "FAIL");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "button.h"
|
||||
#include "ups_constants.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -17,7 +18,7 @@ void UpsHidButton::dump_config() {
|
||||
|
||||
void UpsHidButton::press_action() {
|
||||
if (!parent_) {
|
||||
ESP_LOGE(BUTTON_TAG, "No UPS HID parent component set");
|
||||
ESP_LOGE(BUTTON_TAG, log_messages::NO_PARENT_COMPONENT);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -49,13 +50,13 @@ void UpsHidButton::press_action() {
|
||||
if (button_type_ == BUTTON_TYPE_BEEPER) {
|
||||
ESP_LOGI(BUTTON_TAG, "Executing beeper action: %s", beeper_action_.c_str());
|
||||
|
||||
if (beeper_action_ == "enable") {
|
||||
if (beeper_action_ == beeper::ACTION_ENABLE) {
|
||||
success = active_protocol->beeper_enable();
|
||||
} else if (beeper_action_ == "disable") {
|
||||
} else if (beeper_action_ == beeper::ACTION_DISABLE) {
|
||||
success = active_protocol->beeper_disable();
|
||||
} else if (beeper_action_ == "mute") {
|
||||
} else if (beeper_action_ == beeper::ACTION_MUTE) {
|
||||
success = active_protocol->beeper_mute();
|
||||
} else if (beeper_action_ == "test") {
|
||||
} else if (beeper_action_ == beeper::ACTION_TEST) {
|
||||
success = active_protocol->beeper_test();
|
||||
} else {
|
||||
ESP_LOGE(BUTTON_TAG, "Unknown beeper action: %s", beeper_action_.c_str());
|
||||
@@ -71,15 +72,15 @@ void UpsHidButton::press_action() {
|
||||
else if (button_type_ == BUTTON_TYPE_TEST) {
|
||||
ESP_LOGI(BUTTON_TAG, "Executing test action: %s", test_action_.c_str());
|
||||
|
||||
if (test_action_ == "battery_quick") {
|
||||
if (test_action_ == test::ACTION_BATTERY_QUICK) {
|
||||
success = active_protocol->start_battery_test_quick();
|
||||
} else if (test_action_ == "battery_deep") {
|
||||
} else if (test_action_ == test::ACTION_BATTERY_DEEP) {
|
||||
success = active_protocol->start_battery_test_deep();
|
||||
} else if (test_action_ == "battery_stop") {
|
||||
} else if (test_action_ == test::ACTION_BATTERY_STOP) {
|
||||
success = active_protocol->stop_battery_test();
|
||||
} else if (test_action_ == "ups_test") {
|
||||
} else if (test_action_ == test::ACTION_UPS_TEST) {
|
||||
success = active_protocol->start_ups_test();
|
||||
} else if (test_action_ == "ups_stop") {
|
||||
} else if (test_action_ == test::ACTION_UPS_STOP) {
|
||||
success = active_protocol->stop_ups_test();
|
||||
} else {
|
||||
ESP_LOGE(BUTTON_TAG, "Unknown test action: %s", test_action_.c_str());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "cyberpower_protocol.h"
|
||||
#include "ups_hid.h"
|
||||
#include "hid_constants.h"
|
||||
#include "ups_constants.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/portmacro.h"
|
||||
@@ -24,7 +25,7 @@ bool CyberPowerProtocol::detect() {
|
||||
}
|
||||
|
||||
// Give device time to initialize after connection (same as APC)
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
vTaskDelay(pdMS_TO_TICKS(timing::USB_INITIALIZATION_DELAY_MS));
|
||||
|
||||
// Test multiple report IDs that are known to work with CyberPower devices
|
||||
// Based on NUT debug logs
|
||||
@@ -54,7 +55,7 @@ bool CyberPowerProtocol::detect() {
|
||||
}
|
||||
|
||||
// Small delay between attempts
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
vTaskDelay(pdMS_TO_TICKS(timing::REPORT_RETRY_DELAY_MS));
|
||||
}
|
||||
|
||||
ESP_LOGD(CP_TAG, "CyberPower HID protocol not detected");
|
||||
@@ -252,7 +253,7 @@ bool CyberPowerProtocol::read_hid_report(uint8_t report_id, HidReport &report) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t buffer[64]; // Maximum HID report size
|
||||
uint8_t buffer[limits::MAX_HID_REPORT_SIZE]; // Maximum HID report size
|
||||
size_t buffer_len = sizeof(buffer);
|
||||
esp_err_t ret;
|
||||
|
||||
@@ -260,7 +261,7 @@ bool CyberPowerProtocol::read_hid_report(uint8_t report_id, HidReport &report) {
|
||||
ESP_LOGD(CP_TAG, "Attempting to read report 0x%02X from parent device", report_id);
|
||||
|
||||
// CyberPower devices primarily use Feature Reports (0x03) - based on NUT debug logs
|
||||
ret = parent_->hid_get_report(0x03, report_id, buffer, &buffer_len, parent_->get_protocol_timeout());
|
||||
ret = parent_->hid_get_report(HID_REPORT_TYPE_FEATURE, report_id, buffer, &buffer_len, parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK && buffer_len > 0) {
|
||||
report.report_id = report_id;
|
||||
report.data.assign(buffer, buffer + buffer_len);
|
||||
@@ -279,7 +280,7 @@ bool CyberPowerProtocol::read_hid_report(uint8_t report_id, HidReport &report) {
|
||||
|
||||
// Fallback: try Input Report (0x01) for real-time data
|
||||
buffer_len = sizeof(buffer);
|
||||
ret = parent_->hid_get_report(0x01, report_id, buffer, &buffer_len, parent_->get_protocol_timeout());
|
||||
ret = parent_->hid_get_report(HID_REPORT_TYPE_INPUT, report_id, buffer, &buffer_len, parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK && buffer_len > 0) {
|
||||
report.report_id = report_id;
|
||||
report.data.assign(buffer, buffer + buffer_len);
|
||||
@@ -307,7 +308,7 @@ void CyberPowerProtocol::parse_battery_runtime_report(const HidReport &report, U
|
||||
uint16_t runtime_seconds = report.data[2] | (report.data[3] << 8);
|
||||
|
||||
// Clamp battery to 100% like NUT does
|
||||
data.battery.level = static_cast<float>(battery_percentage > 100 ? 100 : battery_percentage);
|
||||
data.battery.level = static_cast<float>(battery_percentage > battery::MAX_LEVEL_PERCENT ? battery::MAX_LEVEL_PERCENT : battery_percentage);
|
||||
|
||||
// CRITICAL FIX: Convert runtime from seconds to minutes
|
||||
// CyberPower reports runtime in seconds, but ESPHome expects minutes
|
||||
@@ -335,7 +336,7 @@ void CyberPowerProtocol::parse_battery_voltage_report(const HidReport &report, U
|
||||
// Current raw value: 0xF0 (240) should become 24V
|
||||
// So scaling factor is 24/240 = 0.1 (divide by 10)
|
||||
uint8_t voltage_raw = report.data[1];
|
||||
data.battery.voltage = static_cast<float>(voltage_raw) / 10.0f; // Scale by 0.1
|
||||
data.battery.voltage = static_cast<float>(voltage_raw) / battery::VOLTAGE_SCALE_FACTOR; // Scale by 0.1
|
||||
|
||||
ESP_LOGD(CP_TAG, "Battery voltage: %.1fV (raw: 0x%02X = %d)",
|
||||
data.battery.voltage, voltage_raw, voltage_raw);
|
||||
@@ -361,28 +362,28 @@ void CyberPowerProtocol::parse_present_status_report(const HidReport &report, Up
|
||||
// Update power status based on AC presence
|
||||
if (ac_present && !discharging) {
|
||||
data.power.input_voltage = parent_->get_fallback_nominal_voltage(); // Use configured fallback voltage when AC present
|
||||
data.power.status = "Online";
|
||||
data.power.status = status::ONLINE;
|
||||
} else {
|
||||
data.power.input_voltage = NAN; // No AC input
|
||||
data.power.status = "On Battery";
|
||||
data.power.status = status::ON_BATTERY;
|
||||
}
|
||||
|
||||
// Set battery status based on charging/discharging state
|
||||
if (charging) {
|
||||
data.battery.status = "Charging";
|
||||
data.battery.status = battery_status::CHARGING;
|
||||
} else if (discharging || !ac_present) {
|
||||
data.battery.status = "Discharging";
|
||||
data.battery.status = battery_status::DISCHARGING;
|
||||
} else if (fully_charged) {
|
||||
data.battery.status = "Fully Charged";
|
||||
data.battery.status = battery_status::FULLY_CHARGED;
|
||||
} else {
|
||||
data.battery.status = "Normal";
|
||||
data.battery.status = battery_status::NORMAL;
|
||||
}
|
||||
|
||||
// Set low battery indicators
|
||||
if (low_battery || time_limit_expired) {
|
||||
data.battery.charge_low = 10.0f; // Indicate low battery threshold
|
||||
data.battery.charge_low = battery::LOW_THRESHOLD_PERCENT; // Indicate low battery threshold
|
||||
if (time_limit_expired) {
|
||||
data.battery.status += " - Time Limit Expired";
|
||||
data.battery.status += battery_status::TIME_LIMIT_EXPIRED_SUFFIX;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,7 +470,7 @@ void CyberPowerProtocol::parse_battery_voltage_nominal_report(const HidReport &r
|
||||
// NUT debug shows: Report 0x09, Value: 24 (ConfigVoltage)
|
||||
// Some CyberPower models report in decivolts (240 = 24.0V)
|
||||
uint8_t voltage_raw = report.data[1];
|
||||
data.battery.voltage_nominal = static_cast<float>(voltage_raw) / 10.0f;
|
||||
data.battery.voltage_nominal = static_cast<float>(voltage_raw) / battery::VOLTAGE_SCALE_FACTOR;
|
||||
|
||||
ESP_LOGD(CP_TAG, "Battery voltage nominal: %.0fV (raw: 0x%02X = %d)",
|
||||
data.battery.voltage_nominal, voltage_raw, voltage_raw);
|
||||
@@ -486,17 +487,17 @@ void CyberPowerProtocol::parse_beeper_status_report(const HidReport &report, Ups
|
||||
|
||||
// Map NUT values: 1=disabled, 2=enabled, 3=muted
|
||||
switch (beeper_raw) {
|
||||
case 1:
|
||||
data.config.beeper_status = "disabled";
|
||||
case beeper::CONTROL_DISABLE:
|
||||
data.config.beeper_status = beeper::ACTION_DISABLE;
|
||||
break;
|
||||
case 2:
|
||||
case beeper::CONTROL_ENABLE:
|
||||
data.config.beeper_status = "enabled";
|
||||
break;
|
||||
case 3:
|
||||
data.config.beeper_status = "muted";
|
||||
case beeper::CONTROL_MUTE:
|
||||
data.config.beeper_status = beeper::ACTION_MUTE;
|
||||
break;
|
||||
default:
|
||||
data.config.beeper_status = "unknown";
|
||||
data.config.beeper_status = sensitivity::UNKNOWN;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -548,8 +549,8 @@ void CyberPowerProtocol::parse_delay_shutdown_report(const HidReport &report, Up
|
||||
uint16_t delay_raw_unsigned = report.data[1] | (report.data[2] << 8);
|
||||
if (delay_raw_unsigned == 0xFFFF) {
|
||||
// When disabled, use NUT default for CyberPower (DEFAULT_OFFDELAY_CPS = 60)
|
||||
data.config.delay_shutdown = 60;
|
||||
ESP_LOGD(CP_TAG, "UPS delay shutdown: 60 seconds (default, raw: 0xFFFF)");
|
||||
data.config.delay_shutdown = defaults::CYBERPOWER_SHUTDOWN_DELAY_SEC;
|
||||
ESP_LOGD(CP_TAG, "UPS delay shutdown: %d seconds (default, raw: 0xFFFF)", defaults::CYBERPOWER_SHUTDOWN_DELAY_SEC);
|
||||
} else {
|
||||
int16_t delay_raw = static_cast<int16_t>(delay_raw_unsigned);
|
||||
data.config.delay_shutdown = delay_raw;
|
||||
@@ -568,8 +569,8 @@ void CyberPowerProtocol::parse_delay_start_report(const HidReport &report, UpsDa
|
||||
uint16_t delay_raw_unsigned = report.data[1] | (report.data[2] << 8);
|
||||
if (delay_raw_unsigned == 0xFFFF) {
|
||||
// When disabled, use NUT default for CyberPower (DEFAULT_ONDELAY_CPS = 120)
|
||||
data.config.delay_start = 120;
|
||||
ESP_LOGD(CP_TAG, "UPS delay start: 120 seconds (default, raw: 0xFFFF)");
|
||||
data.config.delay_start = defaults::CYBERPOWER_STARTUP_DELAY_SEC;
|
||||
ESP_LOGD(CP_TAG, "UPS delay start: %d seconds (default, raw: 0xFFFF)", defaults::CYBERPOWER_STARTUP_DELAY_SEC);
|
||||
} else {
|
||||
int16_t delay_raw = static_cast<int16_t>(delay_raw_unsigned);
|
||||
data.config.delay_start = delay_raw;
|
||||
@@ -603,15 +604,15 @@ void CyberPowerProtocol::parse_input_sensitivity_report(const HidReport &report,
|
||||
// DYNAMIC SENSITIVITY MAPPING: Handle known CyberPower values with intelligent fallbacks
|
||||
switch (sensitivity_raw) {
|
||||
case 0:
|
||||
data.config.input_sensitivity = "high";
|
||||
data.config.input_sensitivity = sensitivity::HIGH;
|
||||
ESP_LOGI(CP_TAG, "CyberPower input sensitivity: high (raw: %d)", sensitivity_raw);
|
||||
break;
|
||||
case 1:
|
||||
data.config.input_sensitivity = "normal";
|
||||
data.config.input_sensitivity = sensitivity::NORMAL;
|
||||
ESP_LOGI(CP_TAG, "CyberPower input sensitivity: normal (raw: %d)", sensitivity_raw);
|
||||
break;
|
||||
case 2:
|
||||
data.config.input_sensitivity = "low";
|
||||
data.config.input_sensitivity = sensitivity::LOW;
|
||||
ESP_LOGI(CP_TAG, "CyberPower input sensitivity: low (raw: %d)", sensitivity_raw);
|
||||
break;
|
||||
default:
|
||||
@@ -629,9 +630,9 @@ void CyberPowerProtocol::parse_input_sensitivity_report(const HidReport &report,
|
||||
if (alt_value <= 2) {
|
||||
sensitivity_raw = alt_value;
|
||||
switch (sensitivity_raw) {
|
||||
case 0: data.config.input_sensitivity = "high"; break;
|
||||
case 1: data.config.input_sensitivity = "normal"; break;
|
||||
case 2: data.config.input_sensitivity = "low"; break;
|
||||
case 0: data.config.input_sensitivity = sensitivity::HIGH; break;
|
||||
case 1: data.config.input_sensitivity = sensitivity::NORMAL; break;
|
||||
case 2: data.config.input_sensitivity = sensitivity::LOW; break;
|
||||
}
|
||||
ESP_LOGI(CP_TAG, "CyberPower input sensitivity (alt parsing): %s (raw: %d)",
|
||||
data.config.input_sensitivity.c_str(), sensitivity_raw);
|
||||
@@ -640,15 +641,15 @@ void CyberPowerProtocol::parse_input_sensitivity_report(const HidReport &report,
|
||||
}
|
||||
|
||||
// Fallback for problematic values
|
||||
data.config.input_sensitivity = "normal";
|
||||
data.config.input_sensitivity = sensitivity::NORMAL;
|
||||
ESP_LOGW(CP_TAG, "Using default 'normal' sensitivity due to unexpected value: %d", sensitivity_raw);
|
||||
} else {
|
||||
// Values 3-99 - extend mapping for future CyberPower models
|
||||
if (sensitivity_raw == 3) {
|
||||
data.config.input_sensitivity = "auto";
|
||||
data.config.input_sensitivity = sensitivity::AUTO;
|
||||
ESP_LOGI(CP_TAG, "CyberPower input sensitivity: auto (raw: %d)", sensitivity_raw);
|
||||
} else {
|
||||
data.config.input_sensitivity = "unknown";
|
||||
data.config.input_sensitivity = sensitivity::UNKNOWN;
|
||||
ESP_LOGW(CP_TAG, "Unknown CyberPower sensitivity value: %d - please report this for future support",
|
||||
sensitivity_raw);
|
||||
}
|
||||
@@ -840,7 +841,7 @@ void CyberPowerProtocol::read_missing_dynamic_values(UpsData &data) {
|
||||
// NUT shows: ups.timer.shutdown: -60, ups.timer.start: -60
|
||||
data.test.timer_shutdown = -data.config.delay_shutdown; // Negative indicates no active countdown
|
||||
data.test.timer_start = -data.config.delay_start; // Negative indicates no active countdown
|
||||
data.test.timer_reboot = -10; // CyberPower doesn't have separate reboot timer, use default
|
||||
data.test.timer_reboot = defaults::REBOOT_TIMER_DEFAULT; // CyberPower doesn't have separate reboot timer, use default
|
||||
|
||||
ESP_LOGD(CP_TAG, "Completed reading CyberPower missing dynamic values");
|
||||
}
|
||||
@@ -916,7 +917,7 @@ void CyberPowerProtocol::parse_battery_chemistry_report(const HidReport &report,
|
||||
data.battery.type = "LiPoly";
|
||||
break;
|
||||
default:
|
||||
data.battery.type = "Unknown";
|
||||
data.battery.type = battery_status::UNKNOWN;
|
||||
ESP_LOGW(CP_TAG, "Unknown CyberPower battery chemistry value: %d", chemistry_raw);
|
||||
break;
|
||||
}
|
||||
@@ -965,9 +966,9 @@ bool CyberPowerProtocol::beeper_enable() {
|
||||
// NUT shows: "UPS.PowerSummary.AudibleAlarmControl, Type: Feature, ReportID: 0x0c"
|
||||
ESP_LOGD(CP_TAG, "Trying beeper enable with report ID 0x%02X", BEEPER_STATUS_REPORT_ID);
|
||||
|
||||
uint8_t beeper_data[2] = {BEEPER_STATUS_REPORT_ID, 0x02}; // Report ID, Value=2 (enabled)
|
||||
uint8_t beeper_data[2] = {BEEPER_STATUS_REPORT_ID, beeper::CONTROL_ENABLE}; // Report ID, Value=2 (enabled)
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "CyberPower beeper enabled successfully with report ID 0x%02X", BEEPER_STATUS_REPORT_ID);
|
||||
return true;
|
||||
@@ -983,9 +984,9 @@ bool CyberPowerProtocol::beeper_disable() {
|
||||
// CYBERPOWER DEVICE SPECIFIC: From NUT debug, device uses report ID 0x0c
|
||||
ESP_LOGD(CP_TAG, "Trying beeper disable with report ID 0x%02X", BEEPER_STATUS_REPORT_ID);
|
||||
|
||||
uint8_t beeper_data[2] = {BEEPER_STATUS_REPORT_ID, 0x01}; // Report ID, Value=1 (disabled)
|
||||
uint8_t beeper_data[2] = {BEEPER_STATUS_REPORT_ID, beeper::CONTROL_DISABLE}; // Report ID, Value=1 (disabled)
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "CyberPower beeper disabled successfully with report ID 0x%02X", BEEPER_STATUS_REPORT_ID);
|
||||
return true;
|
||||
@@ -1002,9 +1003,9 @@ bool CyberPowerProtocol::beeper_mute() {
|
||||
// - Acknowledges and silences current active alarms
|
||||
// - Beeper may still sound for new critical events
|
||||
// - Different from DISABLE (1) which turns off beeper completely
|
||||
uint8_t beeper_data[2] = {0x0c, 0x03}; // Report ID, Value=3 (muted/acknowledged)
|
||||
uint8_t beeper_data[2] = {BEEPER_STATUS_REPORT_ID, beeper::CONTROL_MUTE}; // Report ID, Value=3 (muted/acknowledged)
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data), parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "CyberPower beeper muted (current alarms acknowledged) successfully");
|
||||
return true;
|
||||
@@ -1055,8 +1056,8 @@ bool CyberPowerProtocol::beeper_test() {
|
||||
|
||||
// Step 4: Restore original beeper state
|
||||
ESP_LOGI(CP_TAG, "Step 4: Restoring original beeper state: %d", original_state);
|
||||
uint8_t restore_data[2] = {0x0c, original_state};
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, restore_data, sizeof(restore_data), parent_->get_protocol_timeout());
|
||||
uint8_t restore_data[2] = {BEEPER_STATUS_REPORT_ID, original_state};
|
||||
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, BEEPER_STATUS_REPORT_ID, restore_data, sizeof(restore_data), parent_->get_protocol_timeout());
|
||||
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "CyberPower beeper test sequence completed successfully");
|
||||
@@ -1073,9 +1074,9 @@ bool CyberPowerProtocol::start_battery_test_quick() {
|
||||
|
||||
// CyberPower uses UPS.Output.Test path, HID report ID 0x14
|
||||
// Quick test command value is 1 (from NUT test_write_info)
|
||||
uint8_t test_data[2] = {0x14, 1}; // Report ID 0x14, value 1 = Quick test
|
||||
uint8_t test_data[2] = {TEST_RESULT_REPORT_ID, test::COMMAND_QUICK}; // Report ID 0x14, value 1 = Quick test
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x14, test_data, 2, parent_->get_protocol_timeout());
|
||||
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, TEST_RESULT_REPORT_ID, test_data, 2, parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "Quick battery test command sent successfully");
|
||||
return true;
|
||||
@@ -1089,9 +1090,9 @@ bool CyberPowerProtocol::start_battery_test_deep() {
|
||||
ESP_LOGI(CP_TAG, "Initiating deep battery test");
|
||||
|
||||
// Deep test command value is 2 (from NUT test_write_info)
|
||||
uint8_t test_data[2] = {0x14, 2}; // Report ID 0x14, value 2 = Deep test
|
||||
uint8_t test_data[2] = {TEST_RESULT_REPORT_ID, test::COMMAND_DEEP}; // Report ID 0x14, value 2 = Deep test
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x14, test_data, 2, parent_->get_protocol_timeout());
|
||||
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, TEST_RESULT_REPORT_ID, test_data, 2, parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "Deep battery test command sent successfully");
|
||||
return true;
|
||||
@@ -1105,9 +1106,9 @@ bool CyberPowerProtocol::stop_battery_test() {
|
||||
ESP_LOGI(CP_TAG, "Stopping battery test");
|
||||
|
||||
// Abort test command value is 3 (from NUT test_write_info)
|
||||
uint8_t test_data[2] = {0x14, 3}; // Report ID 0x14, value 3 = Abort test
|
||||
uint8_t test_data[2] = {TEST_RESULT_REPORT_ID, test::COMMAND_ABORT}; // Report ID 0x14, value 3 = Abort test
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x14, test_data, 2, parent_->get_protocol_timeout());
|
||||
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, TEST_RESULT_REPORT_ID, test_data, 2, parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "Battery test stop command sent successfully");
|
||||
return true;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "generic_hid_protocol.h"
|
||||
#include "ups_hid.h"
|
||||
#include "hid_constants.h"
|
||||
#include "ups_constants.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
@@ -55,7 +56,7 @@ bool GenericHidProtocol::detect() {
|
||||
}
|
||||
|
||||
// Try common report IDs to detect HID Power Device
|
||||
uint8_t buffer[8];
|
||||
uint8_t buffer[limits::MIN_HID_REPORT_SIZE];
|
||||
size_t buffer_len;
|
||||
|
||||
for (uint8_t report_id : COMMON_REPORT_IDS) {
|
||||
@@ -92,7 +93,7 @@ bool GenericHidProtocol::detect() {
|
||||
}
|
||||
|
||||
// Small delay to avoid overwhelming the device
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
vTaskDelay(pdMS_TO_TICKS(timing::EXTENDED_DISCOVERY_DELAY_MS));
|
||||
}
|
||||
|
||||
ESP_LOGD(GEN_TAG, "No standard HID Power Device reports found");
|
||||
@@ -135,7 +136,7 @@ bool GenericHidProtocol::read_data(UpsData &data) {
|
||||
ESP_LOGV(GEN_TAG, "Reading Generic HID UPS data...");
|
||||
|
||||
bool success = false;
|
||||
uint8_t buffer[64];
|
||||
uint8_t buffer[limits::MAX_HID_REPORT_SIZE];
|
||||
size_t buffer_len;
|
||||
|
||||
// Try to read known report types in priority order
|
||||
@@ -211,7 +212,7 @@ bool GenericHidProtocol::read_data(UpsData &data) {
|
||||
|
||||
// Set generic manufacturer/model if not already set
|
||||
if (data.device.manufacturer.empty()) {
|
||||
data.device.manufacturer = "Generic";
|
||||
data.device.manufacturer = protocol::GENERIC;
|
||||
}
|
||||
if (data.device.model.empty()) {
|
||||
uint16_t vid = parent_->get_vendor_id();
|
||||
@@ -224,7 +225,7 @@ bool GenericHidProtocol::read_data(UpsData &data) {
|
||||
// Ensure we have at least basic power status
|
||||
if (success && data.power.status.empty()) {
|
||||
// If we got data but no power status, assume online
|
||||
data.power.status = "Online";
|
||||
data.power.status = status::ONLINE;
|
||||
data.power.input_voltage = parent_->get_fallback_nominal_voltage(); // Use configured fallback voltage
|
||||
}
|
||||
|
||||
@@ -237,7 +238,7 @@ bool GenericHidProtocol::read_data(UpsData &data) {
|
||||
void GenericHidProtocol::enumerate_reports() {
|
||||
ESP_LOGD(GEN_TAG, "Enumerating HID reports...");
|
||||
|
||||
uint8_t buffer[64];
|
||||
uint8_t buffer[limits::MAX_HID_REPORT_SIZE];
|
||||
size_t buffer_len;
|
||||
int discovered_count = 0;
|
||||
|
||||
@@ -277,11 +278,11 @@ void GenericHidProtocol::enumerate_reports() {
|
||||
ESP_LOGV(GEN_TAG, "Found Feature report 0x%02X (%zu bytes)", id, buffer_len);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(5));
|
||||
vTaskDelay(pdMS_TO_TICKS(timing::REPORT_DISCOVERY_DELAY_MS));
|
||||
}
|
||||
|
||||
// If we found enough reports, skip extended search
|
||||
if (discovered_count >= 3) {
|
||||
if (discovered_count >= limits::MAX_DISCOVERY_ATTEMPTS) {
|
||||
ESP_LOGD(GEN_TAG, "Found %d reports, skipping extended search", discovered_count);
|
||||
return;
|
||||
}
|
||||
@@ -296,7 +297,7 @@ void GenericHidProtocol::enumerate_reports() {
|
||||
}
|
||||
|
||||
// Limit total discovery time
|
||||
if (discovered_count >= 10) {
|
||||
if (discovered_count >= limits::MAX_EXTENDED_DISCOVERY_ATTEMPTS) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -309,7 +310,7 @@ void GenericHidProtocol::enumerate_reports() {
|
||||
ESP_LOGV(GEN_TAG, "Found Input report 0x%02X (%zu bytes)", id, buffer_len);
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(5));
|
||||
vTaskDelay(pdMS_TO_TICKS(timing::REPORT_DISCOVERY_DELAY_MS));
|
||||
}
|
||||
|
||||
ESP_LOGD(GEN_TAG, "Enumeration complete: found %d reports", discovered_count);
|
||||
@@ -318,7 +319,7 @@ void GenericHidProtocol::enumerate_reports() {
|
||||
bool GenericHidProtocol::read_report(uint8_t report_id, uint8_t* buffer, size_t& buffer_len) {
|
||||
// Try Input report first if available (real-time data)
|
||||
if (available_input_reports_.count(report_id)) {
|
||||
buffer_len = 64;
|
||||
buffer_len = limits::MAX_HID_REPORT_SIZE;
|
||||
esp_err_t ret = parent_->hid_get_report(HID_REPORT_TYPE_INPUT, report_id, buffer, &buffer_len, parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK && buffer_len > 0) {
|
||||
ESP_LOGV(GEN_TAG, "Read Input report 0x%02X: %zu bytes", report_id, buffer_len);
|
||||
@@ -328,7 +329,7 @@ bool GenericHidProtocol::read_report(uint8_t report_id, uint8_t* buffer, size_t&
|
||||
|
||||
// Try Feature report (static/configuration data)
|
||||
if (available_feature_reports_.count(report_id)) {
|
||||
buffer_len = 64;
|
||||
buffer_len = limits::MAX_HID_REPORT_SIZE;
|
||||
esp_err_t ret = parent_->hid_get_report(HID_REPORT_TYPE_FEATURE, report_id, buffer, &buffer_len, parent_->get_protocol_timeout());
|
||||
if (ret == ESP_OK && buffer_len > 0) {
|
||||
ESP_LOGV(GEN_TAG, "Read Feature report 0x%02X: %zu bytes", report_id, buffer_len);
|
||||
@@ -349,9 +350,9 @@ void GenericHidProtocol::parse_power_summary(uint8_t* data, size_t len, UpsData&
|
||||
if (battery <= 100) {
|
||||
ups_data.battery.level = static_cast<float>(battery);
|
||||
ESP_LOGD(GEN_TAG, "Power summary: Battery %d%%", battery);
|
||||
} else if (battery <= 200 && battery > 100) {
|
||||
} else if (battery <= battery::ALTERNATIVE_PERCENTAGE_SCALE && battery > 100) {
|
||||
// Some devices use 0-200 scale
|
||||
ups_data.battery.level = static_cast<float>(battery) / 2.0f;
|
||||
ups_data.battery.level = static_cast<float>(battery) / (battery::ALTERNATIVE_PERCENTAGE_SCALE / battery::MAX_LEVEL_PERCENT);
|
||||
ESP_LOGD(GEN_TAG, "Power summary: Battery %.1f%% (scaled from %d)", ups_data.battery.level, battery);
|
||||
}
|
||||
}
|
||||
@@ -375,21 +376,21 @@ void GenericHidProtocol::parse_battery_status(uint8_t* data, size_t len, UpsData
|
||||
if (status != 0xFF && status != 0x00) { // Valid status
|
||||
// Update power status
|
||||
if (status & 0x01) {
|
||||
ups_data.power.status = "Online";
|
||||
ups_data.power.status = status::ONLINE;
|
||||
ups_data.power.input_voltage = parent_->get_fallback_nominal_voltage(); // Use configured fallback voltage
|
||||
}
|
||||
if (status & 0x02) {
|
||||
ups_data.power.status = "On Battery";
|
||||
ups_data.power.status = status::ON_BATTERY;
|
||||
ups_data.power.input_voltage = NAN;
|
||||
}
|
||||
|
||||
// Update battery status
|
||||
if (status & 0x08) {
|
||||
ups_data.battery.status = "Charging";
|
||||
ups_data.battery.status = battery_status::CHARGING;
|
||||
}
|
||||
|
||||
if (status & 0x04) {
|
||||
ups_data.battery.charge_low = 10.0f; // Low battery
|
||||
ups_data.battery.charge_low = battery::LOW_THRESHOLD_PERCENT; // Low battery
|
||||
}
|
||||
|
||||
if (status & 0x10) {
|
||||
@@ -419,21 +420,21 @@ void GenericHidProtocol::parse_present_status(uint8_t* data, size_t len, UpsData
|
||||
|
||||
// Standard HID Power Device status bits - map to data structures
|
||||
if (status & 0x01) {
|
||||
ups_data.battery.status = "Charging";
|
||||
ups_data.battery.status = battery_status::CHARGING;
|
||||
}
|
||||
|
||||
if (status & 0x02) {
|
||||
ups_data.power.status = "On Battery";
|
||||
ups_data.power.status = status::ON_BATTERY;
|
||||
ups_data.power.input_voltage = NAN;
|
||||
}
|
||||
|
||||
if (status & 0x04) {
|
||||
ups_data.power.status = "Online";
|
||||
ups_data.power.status = status::ONLINE;
|
||||
ups_data.power.input_voltage = parent_->get_fallback_nominal_voltage(); // Use configured fallback voltage
|
||||
}
|
||||
|
||||
if (status & 0x08) {
|
||||
ups_data.battery.charge_low = 10.0f; // Low battery
|
||||
ups_data.battery.charge_low = battery::LOW_THRESHOLD_PERCENT; // Low battery
|
||||
}
|
||||
|
||||
if (status & 0x10) {
|
||||
@@ -461,11 +462,11 @@ void GenericHidProtocol::parse_general_status(uint8_t* data, size_t len, UpsData
|
||||
|
||||
// Different vendors use different bit patterns, try common ones
|
||||
if (byte1 & 0x01) {
|
||||
ups_data.power.status = "Online";
|
||||
ups_data.power.status = status::ONLINE;
|
||||
ups_data.power.input_voltage = parent_->get_fallback_nominal_voltage(); // Use configured fallback voltage
|
||||
}
|
||||
if (byte1 & 0x10) {
|
||||
ups_data.power.status = "On Battery";
|
||||
ups_data.power.status = status::ON_BATTERY;
|
||||
ups_data.power.input_voltage = NAN;
|
||||
}
|
||||
|
||||
@@ -485,7 +486,7 @@ void GenericHidProtocol::parse_voltage(uint8_t* data, size_t len, UpsData& ups_d
|
||||
}
|
||||
|
||||
// Sanity check
|
||||
if (voltage >= 80.0f && voltage <= 300.0f) {
|
||||
if (voltage >= voltage::MIN_VALID_VOLTAGE && voltage <= voltage::MAX_VALID_VOLTAGE) {
|
||||
if (is_input) {
|
||||
ups_data.power.input_voltage = voltage;
|
||||
ESP_LOGD(GEN_TAG, "Input voltage: %.1fV", voltage);
|
||||
@@ -503,9 +504,9 @@ void GenericHidProtocol::parse_load(uint8_t* data, size_t len, UpsData& ups_data
|
||||
if (load <= 100) {
|
||||
ups_data.power.load_percent = static_cast<float>(load);
|
||||
ESP_LOGD(GEN_TAG, "Load: %d%%", load);
|
||||
} else if (load <= 200) {
|
||||
} else if (load <= battery::ALTERNATIVE_PERCENTAGE_SCALE) {
|
||||
// Some devices use 0-200 scale
|
||||
ups_data.power.load_percent = static_cast<float>(load) / 2.0f;
|
||||
ups_data.power.load_percent = static_cast<float>(load) / (battery::ALTERNATIVE_PERCENTAGE_SCALE / battery::MAX_LEVEL_PERCENT);
|
||||
ESP_LOGD(GEN_TAG, "Load: %.1f%% (scaled from %d)", ups_data.power.load_percent, load);
|
||||
}
|
||||
}
|
||||
@@ -538,7 +539,7 @@ bool GenericHidProtocol::parse_unknown_report(uint8_t* data, size_t len, UpsData
|
||||
// Try direct and scaled
|
||||
if (voltage > 1000) voltage /= 10.0f;
|
||||
|
||||
if (voltage >= 80.0f && voltage <= 300.0f) {
|
||||
if (voltage >= voltage::MIN_VALID_VOLTAGE && voltage <= voltage::MAX_VALID_VOLTAGE) {
|
||||
if (std::isnan(ups_data.power.input_voltage)) {
|
||||
ups_data.power.input_voltage = voltage;
|
||||
ESP_LOGV(GEN_TAG, "Heuristic: Found possible voltage %.1fV at bytes %zu-%zu",
|
||||
@@ -564,20 +565,20 @@ void GenericHidProtocol::parse_input_sensitivity(uint8_t* data, size_t len, UpsD
|
||||
// Handle both APC-style (0x35) and CyberPower-style (0x1a) with intelligent fallbacks
|
||||
switch (sensitivity_raw) {
|
||||
case 0:
|
||||
ups_data.config.input_sensitivity = "high";
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): high (raw: %d)", style, sensitivity_raw);
|
||||
ups_data.config.input_sensitivity = sensitivity::HIGH;
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): %s (raw: %d)", style, sensitivity::HIGH, sensitivity_raw);
|
||||
break;
|
||||
case 1:
|
||||
ups_data.config.input_sensitivity = "normal";
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): normal (raw: %d)", style, sensitivity_raw);
|
||||
ups_data.config.input_sensitivity = sensitivity::NORMAL;
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): %s (raw: %d)", style, sensitivity::NORMAL, sensitivity_raw);
|
||||
break;
|
||||
case 2:
|
||||
ups_data.config.input_sensitivity = "low";
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): low (raw: %d)", style, sensitivity_raw);
|
||||
ups_data.config.input_sensitivity = sensitivity::LOW;
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): %s (raw: %d)", style, sensitivity::LOW, sensitivity_raw);
|
||||
break;
|
||||
case 3:
|
||||
ups_data.config.input_sensitivity = "auto";
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): auto (raw: %d)", style, sensitivity_raw);
|
||||
ups_data.config.input_sensitivity = sensitivity::AUTO;
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s): %s (raw: %d)", style, sensitivity::AUTO, sensitivity_raw);
|
||||
break;
|
||||
default:
|
||||
// GENERIC DYNAMIC HANDLING: For unknown values, be more permissive
|
||||
@@ -591,10 +592,10 @@ void GenericHidProtocol::parse_input_sensitivity(uint8_t* data, size_t len, UpsD
|
||||
uint8_t alt_value = data[i];
|
||||
if (alt_value <= 3) {
|
||||
switch (alt_value) {
|
||||
case 0: ups_data.config.input_sensitivity = "high"; break;
|
||||
case 1: ups_data.config.input_sensitivity = "normal"; break;
|
||||
case 2: ups_data.config.input_sensitivity = "low"; break;
|
||||
case 3: ups_data.config.input_sensitivity = "auto"; break;
|
||||
case 0: ups_data.config.input_sensitivity = sensitivity::HIGH; break;
|
||||
case 1: ups_data.config.input_sensitivity = sensitivity::NORMAL; break;
|
||||
case 2: ups_data.config.input_sensitivity = sensitivity::LOW; break;
|
||||
case 3: ups_data.config.input_sensitivity = sensitivity::AUTO; break;
|
||||
}
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s, alt byte[%zu]): %s (raw: %d)",
|
||||
style, i, ups_data.config.input_sensitivity.c_str(), alt_value);
|
||||
@@ -603,7 +604,7 @@ void GenericHidProtocol::parse_input_sensitivity(uint8_t* data, size_t len, UpsD
|
||||
}
|
||||
|
||||
// Fallback to reasonable default
|
||||
ups_data.config.input_sensitivity = "normal";
|
||||
ups_data.config.input_sensitivity = sensitivity::NORMAL;
|
||||
ESP_LOGW(GEN_TAG, "Using default 'normal' sensitivity (%s) due to unexpected value: %d",
|
||||
style, sensitivity_raw);
|
||||
} else {
|
||||
@@ -611,16 +612,16 @@ void GenericHidProtocol::parse_input_sensitivity(uint8_t* data, size_t len, UpsD
|
||||
if (sensitivity_raw <= 10) {
|
||||
// Map to nearest known value
|
||||
if (sensitivity_raw <= 3) {
|
||||
ups_data.config.input_sensitivity = "high";
|
||||
ups_data.config.input_sensitivity = sensitivity::HIGH;
|
||||
} else if (sensitivity_raw <= 6) {
|
||||
ups_data.config.input_sensitivity = "normal";
|
||||
ups_data.config.input_sensitivity = sensitivity::NORMAL;
|
||||
} else {
|
||||
ups_data.config.input_sensitivity = "low";
|
||||
ups_data.config.input_sensitivity = sensitivity::LOW;
|
||||
}
|
||||
ESP_LOGI(GEN_TAG, "Generic input sensitivity (%s, mapped): %s (raw: %d)",
|
||||
style, ups_data.config.input_sensitivity.c_str(), sensitivity_raw);
|
||||
} else {
|
||||
ups_data.config.input_sensitivity = "unknown";
|
||||
ups_data.config.input_sensitivity = sensitivity::UNKNOWN;
|
||||
ESP_LOGW(GEN_TAG, "Unknown generic sensitivity value (%s): %d", style, sensitivity_raw);
|
||||
}
|
||||
}
|
||||
@@ -638,7 +639,7 @@ bool GenericHidProtocol::start_battery_test_quick() {
|
||||
|
||||
for (size_t i = 0; i < sizeof(test_report_ids); i++) {
|
||||
uint8_t report_id = test_report_ids[i];
|
||||
uint8_t test_data[2] = {report_id, 1}; // Command value 1 = Quick test
|
||||
uint8_t test_data[2] = {report_id, test::COMMAND_QUICK}; // Command value 1 = Quick test
|
||||
|
||||
ESP_LOGD(GEN_TAG, "Trying quick battery test with report ID 0x%02X", report_id);
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, report_id, test_data, sizeof(test_data), parent_->get_protocol_timeout());
|
||||
@@ -663,7 +664,7 @@ bool GenericHidProtocol::start_battery_test_deep() {
|
||||
|
||||
for (size_t i = 0; i < sizeof(test_report_ids); i++) {
|
||||
uint8_t report_id = test_report_ids[i];
|
||||
uint8_t test_data[2] = {report_id, 2}; // Command value 2 = Deep test
|
||||
uint8_t test_data[2] = {report_id, test::COMMAND_DEEP}; // Command value 2 = Deep test
|
||||
|
||||
ESP_LOGD(GEN_TAG, "Trying deep battery test with report ID 0x%02X", report_id);
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, report_id, test_data, sizeof(test_data), parent_->get_protocol_timeout());
|
||||
@@ -688,7 +689,7 @@ bool GenericHidProtocol::stop_battery_test() {
|
||||
|
||||
for (size_t i = 0; i < sizeof(test_report_ids); i++) {
|
||||
uint8_t report_id = test_report_ids[i];
|
||||
uint8_t test_data[2] = {report_id, 3}; // Command value 3 = Abort test
|
||||
uint8_t test_data[2] = {report_id, test::COMMAND_ABORT}; // Command value 3 = Abort test
|
||||
|
||||
ESP_LOGD(GEN_TAG, "Trying battery test stop with report ID 0x%02X", report_id);
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, report_id, test_data, sizeof(test_data), parent_->get_protocol_timeout());
|
||||
@@ -773,7 +774,7 @@ void GenericHidProtocol::read_frequency_data(UpsData &data) {
|
||||
0x12, // Output voltage report (might contain frequency)
|
||||
};
|
||||
|
||||
uint8_t buffer[64];
|
||||
uint8_t buffer[limits::MAX_HID_REPORT_SIZE];
|
||||
size_t buffer_len;
|
||||
|
||||
for (uint8_t report_id : frequency_report_ids) {
|
||||
@@ -801,7 +802,7 @@ float GenericHidProtocol::parse_frequency_from_report(uint8_t* data, size_t len)
|
||||
// Method 1: Single byte at position 1 (simple frequency reports)
|
||||
if (len >= 2) {
|
||||
uint8_t freq_byte = data[1];
|
||||
if (freq_byte >= FREQUENCY_MIN_VALID && freq_byte <= FREQUENCY_MAX_VALID) {
|
||||
if (freq_byte >= static_cast<uint8_t>(FREQUENCY_MIN_VALID) && freq_byte <= static_cast<uint8_t>(FREQUENCY_MAX_VALID)) {
|
||||
ESP_LOGD(GEN_TAG, "Found frequency %d Hz (single byte)", freq_byte);
|
||||
return static_cast<float>(freq_byte);
|
||||
}
|
||||
@@ -810,7 +811,7 @@ float GenericHidProtocol::parse_frequency_from_report(uint8_t* data, size_t len)
|
||||
// Method 2: Check all byte positions for valid frequency values
|
||||
for (size_t i = 1; i < len; i++) {
|
||||
uint8_t freq_byte = data[i];
|
||||
if ((freq_byte == 50 || freq_byte == 60) && freq_byte <= FREQUENCY_MAX_VALID) {
|
||||
if ((freq_byte == 50 || freq_byte == 60) && freq_byte <= static_cast<uint8_t>(FREQUENCY_MAX_VALID)) {
|
||||
ESP_LOGD(GEN_TAG, "Found standard frequency %d Hz at byte %zu", freq_byte, i);
|
||||
return static_cast<float>(freq_byte);
|
||||
}
|
||||
@@ -819,7 +820,7 @@ float GenericHidProtocol::parse_frequency_from_report(uint8_t* data, size_t len)
|
||||
// Method 3: 16-bit little-endian values
|
||||
for (size_t i = 1; i <= len - 2; i++) {
|
||||
uint16_t freq_word = data[i] | (data[i + 1] << 8);
|
||||
if (freq_word >= FREQUENCY_MIN_VALID && freq_word <= FREQUENCY_MAX_VALID) {
|
||||
if (freq_word >= static_cast<uint16_t>(FREQUENCY_MIN_VALID) && freq_word <= static_cast<uint16_t>(FREQUENCY_MAX_VALID)) {
|
||||
ESP_LOGD(GEN_TAG, "Found frequency %d Hz (16-bit LE) at bytes %zu-%zu", freq_word, i, i + 1);
|
||||
return static_cast<float>(freq_word);
|
||||
}
|
||||
@@ -828,7 +829,7 @@ float GenericHidProtocol::parse_frequency_from_report(uint8_t* data, size_t len)
|
||||
// Method 4: 16-bit big-endian values
|
||||
for (size_t i = 1; i <= len - 2; i++) {
|
||||
uint16_t freq_word = (data[i] << 8) | data[i + 1];
|
||||
if (freq_word >= FREQUENCY_MIN_VALID && freq_word <= FREQUENCY_MAX_VALID) {
|
||||
if (freq_word >= static_cast<uint16_t>(FREQUENCY_MIN_VALID) && freq_word <= static_cast<uint16_t>(FREQUENCY_MAX_VALID)) {
|
||||
ESP_LOGD(GEN_TAG, "Found frequency %d Hz (16-bit BE) at bytes %zu-%zu", freq_word, i, i + 1);
|
||||
return static_cast<float>(freq_word);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace esphome {
|
||||
namespace ups_hid {
|
||||
|
||||
// ==================== Timing Constants ====================
|
||||
namespace timing {
|
||||
static constexpr uint32_t USB_INITIALIZATION_DELAY_MS = 100;
|
||||
static constexpr uint32_t REPORT_RETRY_DELAY_MS = 50;
|
||||
static constexpr uint32_t REPORT_DISCOVERY_DELAY_MS = 5;
|
||||
static constexpr uint32_t EXTENDED_DISCOVERY_DELAY_MS = 10;
|
||||
static constexpr uint32_t PROTOCOL_DETECTION_DELAY_MS = 100;
|
||||
|
||||
static constexpr uint32_t MIN_PROTOCOL_TIMEOUT_MS = 5000; // 5 seconds
|
||||
static constexpr uint32_t MAX_PROTOCOL_TIMEOUT_MS = 300000; // 5 minutes
|
||||
static constexpr uint32_t DEFAULT_HID_TIMEOUT_MS = 1000; // 1 second
|
||||
|
||||
static constexpr uint32_t ERROR_RATE_LIMIT_WINDOW_MS = 5000; // 5 seconds
|
||||
static constexpr uint32_t MAX_ERRORS_PER_WINDOW = 3;
|
||||
}
|
||||
|
||||
// ==================== Protocol Limits ====================
|
||||
namespace limits {
|
||||
static constexpr uint32_t MAX_CONSECUTIVE_FAILURES = 5;
|
||||
static constexpr uint32_t MAX_PROTOCOL_DETECTION_ATTEMPTS = 3;
|
||||
static constexpr uint32_t MAX_DISCOVERY_ATTEMPTS = 3;
|
||||
static constexpr uint32_t MAX_EXTENDED_DISCOVERY_ATTEMPTS = 10;
|
||||
|
||||
static constexpr size_t MAX_HID_REPORT_SIZE = 64;
|
||||
static constexpr size_t MIN_HID_REPORT_SIZE = 8;
|
||||
static constexpr size_t USB_STRING_DESCRIPTOR_MAX_LENGTH = 256;
|
||||
}
|
||||
|
||||
// ==================== Battery Constants ====================
|
||||
namespace battery {
|
||||
static constexpr float LOW_THRESHOLD_PERCENT = 10.0f;
|
||||
static constexpr float MAX_LEVEL_PERCENT = 100.0f;
|
||||
static constexpr float VOLTAGE_SCALE_FACTOR = 10.0f;
|
||||
static constexpr float ALTERNATIVE_PERCENTAGE_SCALE = 200.0f;
|
||||
}
|
||||
|
||||
// ==================== Voltage Constants ====================
|
||||
namespace voltage {
|
||||
static constexpr float MIN_VALID_VOLTAGE = 80.0f;
|
||||
static constexpr float MAX_VALID_VOLTAGE = 300.0f;
|
||||
static constexpr float EUROPEAN_STANDARD_VOLTAGE = 230.0f;
|
||||
static constexpr float AMERICAN_STANDARD_VOLTAGE = 120.0f;
|
||||
}
|
||||
|
||||
// ==================== Default Timer Values ====================
|
||||
namespace defaults {
|
||||
static constexpr int CYBERPOWER_SHUTDOWN_DELAY_SEC = 60;
|
||||
static constexpr int CYBERPOWER_STARTUP_DELAY_SEC = 120;
|
||||
static constexpr int REBOOT_TIMER_DEFAULT = -10;
|
||||
static constexpr uint16_t AUTO_DETECT_VENDOR_ID = 0;
|
||||
static constexpr uint16_t AUTO_DETECT_PRODUCT_ID = 0;
|
||||
}
|
||||
|
||||
// ==================== Status Strings ====================
|
||||
namespace status {
|
||||
static constexpr const char* ONLINE = "Online";
|
||||
static constexpr const char* ON_BATTERY = "On Battery";
|
||||
static constexpr const char* UNKNOWN = "Unknown";
|
||||
static constexpr const char* CONNECTED = "Connected";
|
||||
static constexpr const char* DISCONNECTED = "Disconnected";
|
||||
static constexpr const char* DETECTION_PENDING = "Detection pending";
|
||||
static constexpr const char* NONE = "None";
|
||||
static constexpr const char* YES = "YES";
|
||||
static constexpr const char* NO = "NO";
|
||||
}
|
||||
|
||||
// ==================== Battery Status Strings ====================
|
||||
namespace battery_status {
|
||||
static constexpr const char* CHARGING = "Charging";
|
||||
static constexpr const char* DISCHARGING = "Discharging";
|
||||
static constexpr const char* FULLY_CHARGED = "Fully Charged";
|
||||
static constexpr const char* NORMAL = "Normal";
|
||||
static constexpr const char* LOW = "Low";
|
||||
static constexpr const char* UNKNOWN = "Unknown";
|
||||
static constexpr const char* TIME_LIMIT_EXPIRED_SUFFIX = " - Time Limit Expired";
|
||||
}
|
||||
|
||||
// ==================== Beeper Actions ====================
|
||||
namespace beeper {
|
||||
static constexpr const char* ACTION_ENABLE = "enable";
|
||||
static constexpr const char* ACTION_DISABLE = "disable";
|
||||
static constexpr const char* ACTION_MUTE = "mute";
|
||||
static constexpr const char* ACTION_TEST = "test";
|
||||
|
||||
// Beeper control values
|
||||
static constexpr uint8_t CONTROL_DISABLE = 0x01;
|
||||
static constexpr uint8_t CONTROL_ENABLE = 0x02;
|
||||
static constexpr uint8_t CONTROL_MUTE = 0x03;
|
||||
}
|
||||
|
||||
// ==================== Test Actions ====================
|
||||
namespace test {
|
||||
static constexpr const char* ACTION_BATTERY_QUICK = "battery_quick";
|
||||
static constexpr const char* ACTION_BATTERY_DEEP = "battery_deep";
|
||||
static constexpr const char* ACTION_BATTERY_STOP = "battery_stop";
|
||||
static constexpr const char* ACTION_UPS_TEST = "ups_test";
|
||||
static constexpr const char* ACTION_UPS_STOP = "ups_stop";
|
||||
|
||||
// Test command values
|
||||
static constexpr uint8_t COMMAND_QUICK = 1;
|
||||
static constexpr uint8_t COMMAND_DEEP = 2;
|
||||
static constexpr uint8_t COMMAND_ABORT = 3;
|
||||
}
|
||||
|
||||
// ==================== Sensor Type Identifiers ====================
|
||||
namespace sensor_type {
|
||||
static constexpr const char* BATTERY_LEVEL = "battery_level";
|
||||
static constexpr const char* BATTERY_VOLTAGE = "battery_voltage";
|
||||
static constexpr const char* BATTERY_VOLTAGE_NOMINAL = "battery_voltage_nominal";
|
||||
static constexpr const char* RUNTIME = "runtime";
|
||||
static constexpr const char* INPUT_VOLTAGE = "input_voltage";
|
||||
static constexpr const char* INPUT_VOLTAGE_NOMINAL = "input_voltage_nominal";
|
||||
static constexpr const char* OUTPUT_VOLTAGE = "output_voltage";
|
||||
static constexpr const char* LOAD_PERCENT = "load_percent";
|
||||
static constexpr const char* FREQUENCY = "frequency";
|
||||
static constexpr const char* INPUT_TRANSFER_LOW = "input_transfer_low";
|
||||
static constexpr const char* INPUT_TRANSFER_HIGH = "input_transfer_high";
|
||||
static constexpr const char* BATTERY_RUNTIME_LOW = "battery_runtime_low";
|
||||
static constexpr const char* UPS_REALPOWER_NOMINAL = "ups_realpower_nominal";
|
||||
static constexpr const char* UPS_DELAY_SHUTDOWN = "ups_delay_shutdown";
|
||||
static constexpr const char* UPS_DELAY_START = "ups_delay_start";
|
||||
static constexpr const char* UPS_DELAY_REBOOT = "ups_delay_reboot";
|
||||
static constexpr const char* UPS_TIMER_REBOOT = "ups_timer_reboot";
|
||||
static constexpr const char* UPS_TIMER_SHUTDOWN = "ups_timer_shutdown";
|
||||
static constexpr const char* UPS_TIMER_START = "ups_timer_start";
|
||||
}
|
||||
|
||||
// ==================== Binary Sensor Type Identifiers ====================
|
||||
namespace binary_sensor_type {
|
||||
static constexpr const char* ONLINE = "online";
|
||||
static constexpr const char* ON_BATTERY = "on_battery";
|
||||
static constexpr const char* LOW_BATTERY = "low_battery";
|
||||
static constexpr const char* OVERLOAD = "overload";
|
||||
static constexpr const char* BUCK = "buck";
|
||||
static constexpr const char* BOOST = "boost";
|
||||
static constexpr const char* CHARGING = "charging";
|
||||
static constexpr const char* DISCHARGING = "discharging";
|
||||
static constexpr const char* BYPASS_ACTIVE = "bypass_active";
|
||||
}
|
||||
|
||||
// ==================== Text Sensor Type Identifiers ====================
|
||||
namespace text_sensor_type {
|
||||
static constexpr const char* MODEL = "model";
|
||||
static constexpr const char* MANUFACTURER = "manufacturer";
|
||||
static constexpr const char* SERIAL_NUMBER = "serial_number";
|
||||
static constexpr const char* FIRMWARE_VERSION = "firmware_version";
|
||||
static constexpr const char* BATTERY_STATUS = "battery_status";
|
||||
static constexpr const char* UPS_TEST_RESULT = "ups_test_result";
|
||||
static constexpr const char* UPS_BEEPER_STATUS = "ups_beeper_status";
|
||||
static constexpr const char* INPUT_SENSITIVITY = "input_sensitivity";
|
||||
static constexpr const char* STATUS = "status";
|
||||
static constexpr const char* PROTOCOL = "protocol";
|
||||
}
|
||||
|
||||
// ==================== Input Sensitivity Values ====================
|
||||
namespace sensitivity {
|
||||
static constexpr const char* HIGH = "high";
|
||||
static constexpr const char* NORMAL = "normal";
|
||||
static constexpr const char* LOW = "low";
|
||||
static constexpr const char* AUTO = "auto";
|
||||
static constexpr const char* UNKNOWN = "unknown";
|
||||
}
|
||||
|
||||
// ==================== Protocol Names ====================
|
||||
namespace protocol {
|
||||
static constexpr const char* APC_HID = "APC HID";
|
||||
static constexpr const char* CYBERPOWER = "CyberPower";
|
||||
static constexpr const char* GENERIC = "Generic";
|
||||
static constexpr const char* NONE = "None";
|
||||
}
|
||||
|
||||
// ==================== Log Messages ====================
|
||||
namespace log_messages {
|
||||
static constexpr const char* SETTING_UP = "Setting up UPS HID Component...";
|
||||
static constexpr const char* TRANSPORT_INIT_FAILED = "Failed to initialize transport";
|
||||
static constexpr const char* SETUP_COMPLETE = "UPS HID Component setup complete - waiting for USB device connection";
|
||||
static constexpr const char* WAITING_FOR_DEVICE = "USB transport not connected - waiting for device";
|
||||
static constexpr const char* ATTEMPTING_DETECTION = "USB device connected - attempting protocol detection";
|
||||
static constexpr const char* PROTOCOL_DETECTED = "UPS protocol detected and configured successfully";
|
||||
static constexpr const char* DETECTION_FAILED = "Failed to detect UPS protocol (attempt #%u)";
|
||||
static constexpr const char* TOO_MANY_FAILURES = "Too many consecutive protocol detection failures, marking component as failed";
|
||||
static constexpr const char* READ_FAILED = "Failed to read UPS data (failure #%u)";
|
||||
static constexpr const char* RESETTING_PROTOCOL = "Too many consecutive read failures - resetting protocol to retry detection";
|
||||
static constexpr const char* NO_PARENT_COMPONENT = "No UPS HID parent component set";
|
||||
}
|
||||
|
||||
} // namespace ups_hid
|
||||
} // namespace esphome
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "ups_hid.h"
|
||||
#include "ups_constants.h"
|
||||
#include "ups_vendors.h"
|
||||
#include "usb_transport_factory.h"
|
||||
#include "simulated_transport.h"
|
||||
@@ -18,37 +19,37 @@ namespace esphome {
|
||||
namespace ups_hid {
|
||||
|
||||
void UpsHidComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up UPS HID Component...");
|
||||
ESP_LOGCONFIG(TAG, log_messages::SETTING_UP);
|
||||
|
||||
if (!initialize_transport()) {
|
||||
ESP_LOGE(TAG, "Failed to initialize transport");
|
||||
ESP_LOGE(TAG, log_messages::TRANSPORT_INIT_FAILED);
|
||||
mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
// Protocol detection is deferred to update() method to handle asynchronous USB enumeration
|
||||
ESP_LOGCONFIG(TAG, "UPS HID Component setup complete - waiting for USB device connection");
|
||||
ESP_LOGCONFIG(TAG, log_messages::SETUP_COMPLETE);
|
||||
}
|
||||
|
||||
void UpsHidComponent::update() {
|
||||
if (!transport_ || !transport_->is_connected()) {
|
||||
// Device not connected yet - normal during startup or after disconnection
|
||||
ESP_LOGD(TAG, "USB transport not connected - waiting for device");
|
||||
ESP_LOGD(TAG, log_messages::WAITING_FOR_DEVICE);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if protocol detection is needed
|
||||
if (!active_protocol_) {
|
||||
ESP_LOGI(TAG, "USB device connected - attempting protocol detection");
|
||||
ESP_LOGI(TAG, log_messages::ATTEMPTING_DETECTION);
|
||||
if (detect_protocol()) {
|
||||
ESP_LOGI(TAG, "UPS protocol detected and configured successfully");
|
||||
ESP_LOGI(TAG, log_messages::PROTOCOL_DETECTED);
|
||||
consecutive_failures_ = 0;
|
||||
} else {
|
||||
consecutive_failures_++;
|
||||
ESP_LOGW(TAG, "Failed to detect UPS protocol (attempt #%u)", consecutive_failures_);
|
||||
ESP_LOGW(TAG, log_messages::DETECTION_FAILED, consecutive_failures_);
|
||||
|
||||
if (consecutive_failures_ > max_consecutive_failures_) {
|
||||
ESP_LOGE(TAG, "Too many consecutive protocol detection failures, marking component as failed");
|
||||
ESP_LOGE(TAG, log_messages::TOO_MANY_FAILURES);
|
||||
mark_failed();
|
||||
}
|
||||
return;
|
||||
@@ -62,10 +63,10 @@ void UpsHidComponent::update() {
|
||||
last_successful_read_ = millis();
|
||||
} else {
|
||||
consecutive_failures_++;
|
||||
ESP_LOGW(TAG, "Failed to read UPS data (failure #%u)", consecutive_failures_);
|
||||
ESP_LOGW(TAG, log_messages::READ_FAILED, consecutive_failures_);
|
||||
|
||||
if (consecutive_failures_ > max_consecutive_failures_) {
|
||||
ESP_LOGW(TAG, "Too many consecutive read failures - resetting protocol to retry detection");
|
||||
ESP_LOGW(TAG, log_messages::RESETTING_PROTOCOL);
|
||||
active_protocol_.reset(); // Force protocol re-detection on next update
|
||||
consecutive_failures_ = 0;
|
||||
}
|
||||
@@ -74,7 +75,7 @@ void UpsHidComponent::update() {
|
||||
|
||||
void UpsHidComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "UPS HID Component:");
|
||||
ESP_LOGCONFIG(TAG, " Simulation Mode: %s", simulation_mode_ ? "YES" : "NO");
|
||||
ESP_LOGCONFIG(TAG, " Simulation Mode: %s", simulation_mode_ ? status::YES : status::NO);
|
||||
|
||||
if (transport_ && transport_->is_connected()) {
|
||||
ESP_LOGCONFIG(TAG, " USB Vendor ID: 0x%04X", transport_->get_vendor_id());
|
||||
@@ -86,15 +87,15 @@ void UpsHidComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Update Interval: %u ms", get_update_interval());
|
||||
|
||||
if (transport_ && transport_->is_connected()) {
|
||||
ESP_LOGCONFIG(TAG, " Status: Connected");
|
||||
ESP_LOGCONFIG(TAG, " Status: %s", status::CONNECTED);
|
||||
if (active_protocol_) {
|
||||
ESP_LOGCONFIG(TAG, " Active Protocol: %s",
|
||||
active_protocol_->get_protocol_name().c_str());
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " Protocol Status: Detection pending");
|
||||
ESP_LOGCONFIG(TAG, " Protocol Status: %s", status::DETECTION_PENDING);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " Status: Disconnected");
|
||||
ESP_LOGCONFIG(TAG, " Status: %s", status::DISCONNECTED);
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(TAG, " Registered Sensors: %zu", sensors_.size());
|
||||
@@ -133,11 +134,11 @@ bool UpsHidComponent::is_connected() const {
|
||||
}
|
||||
|
||||
uint16_t UpsHidComponent::get_vendor_id() const {
|
||||
return transport_ ? transport_->get_vendor_id() : 0;
|
||||
return transport_ ? transport_->get_vendor_id() : defaults::AUTO_DETECT_VENDOR_ID;
|
||||
}
|
||||
|
||||
uint16_t UpsHidComponent::get_product_id() const {
|
||||
return transport_ ? transport_->get_product_id() : 0;
|
||||
return transport_ ? transport_->get_product_id() : defaults::AUTO_DETECT_PRODUCT_ID;
|
||||
}
|
||||
|
||||
// Core implementation methods
|
||||
@@ -256,43 +257,43 @@ void UpsHidComponent::update_sensors() {
|
||||
// Extract appropriate value based on sensor type
|
||||
float value = NAN;
|
||||
|
||||
if (type == "battery_level" && ups_data_.battery.is_valid()) {
|
||||
if (type == sensor_type::BATTERY_LEVEL && ups_data_.battery.is_valid()) {
|
||||
value = ups_data_.battery.level;
|
||||
} else if (type == "battery_voltage" && !std::isnan(ups_data_.battery.voltage)) {
|
||||
} else if (type == sensor_type::BATTERY_VOLTAGE && !std::isnan(ups_data_.battery.voltage)) {
|
||||
value = ups_data_.battery.voltage;
|
||||
} else if (type == "battery_voltage_nominal" && !std::isnan(ups_data_.battery.voltage_nominal)) {
|
||||
} else if (type == sensor_type::BATTERY_VOLTAGE_NOMINAL && !std::isnan(ups_data_.battery.voltage_nominal)) {
|
||||
value = ups_data_.battery.voltage_nominal;
|
||||
} else if (type == "runtime" && !std::isnan(ups_data_.battery.runtime_minutes)) {
|
||||
} else if (type == sensor_type::RUNTIME && !std::isnan(ups_data_.battery.runtime_minutes)) {
|
||||
value = ups_data_.battery.runtime_minutes;
|
||||
} else if (type == "input_voltage" && !std::isnan(ups_data_.power.input_voltage)) {
|
||||
} else if (type == sensor_type::INPUT_VOLTAGE && !std::isnan(ups_data_.power.input_voltage)) {
|
||||
value = ups_data_.power.input_voltage;
|
||||
} else if (type == "input_voltage_nominal" && !std::isnan(ups_data_.power.input_voltage_nominal)) {
|
||||
} else if (type == sensor_type::INPUT_VOLTAGE_NOMINAL && !std::isnan(ups_data_.power.input_voltage_nominal)) {
|
||||
value = ups_data_.power.input_voltage_nominal;
|
||||
} else if (type == "output_voltage" && !std::isnan(ups_data_.power.output_voltage)) {
|
||||
} else if (type == sensor_type::OUTPUT_VOLTAGE && !std::isnan(ups_data_.power.output_voltage)) {
|
||||
value = ups_data_.power.output_voltage;
|
||||
} else if (type == "load_percent" && !std::isnan(ups_data_.power.load_percent)) {
|
||||
} else if (type == sensor_type::LOAD_PERCENT && !std::isnan(ups_data_.power.load_percent)) {
|
||||
value = ups_data_.power.load_percent;
|
||||
} else if (type == "frequency" && !std::isnan(ups_data_.power.frequency)) {
|
||||
} else if (type == sensor_type::FREQUENCY && !std::isnan(ups_data_.power.frequency)) {
|
||||
value = ups_data_.power.frequency;
|
||||
} else if (type == "input_transfer_low" && !std::isnan(ups_data_.power.input_transfer_low)) {
|
||||
} else if (type == sensor_type::INPUT_TRANSFER_LOW && !std::isnan(ups_data_.power.input_transfer_low)) {
|
||||
value = ups_data_.power.input_transfer_low;
|
||||
} else if (type == "input_transfer_high" && !std::isnan(ups_data_.power.input_transfer_high)) {
|
||||
} else if (type == sensor_type::INPUT_TRANSFER_HIGH && !std::isnan(ups_data_.power.input_transfer_high)) {
|
||||
value = ups_data_.power.input_transfer_high;
|
||||
} else if (type == "battery_runtime_low" && !std::isnan(ups_data_.battery.runtime_low_threshold)) {
|
||||
value = ups_data_.battery.runtime_low_threshold;
|
||||
} else if (type == "ups_realpower_nominal" && !std::isnan(ups_data_.power.realpower_nominal)) {
|
||||
} else if (type == sensor_type::BATTERY_RUNTIME_LOW && !std::isnan(ups_data_.battery.runtime_low)) {
|
||||
value = ups_data_.battery.runtime_low;
|
||||
} else if (type == sensor_type::UPS_REALPOWER_NOMINAL && !std::isnan(ups_data_.power.realpower_nominal)) {
|
||||
value = ups_data_.power.realpower_nominal;
|
||||
} else if (type == "ups_delay_shutdown" && !std::isnan(ups_data_.config.delay_shutdown)) {
|
||||
} else if (type == sensor_type::UPS_DELAY_SHUTDOWN && !std::isnan(ups_data_.config.delay_shutdown)) {
|
||||
value = ups_data_.config.delay_shutdown;
|
||||
} else if (type == "ups_delay_start" && !std::isnan(ups_data_.config.delay_start)) {
|
||||
} else if (type == sensor_type::UPS_DELAY_START && !std::isnan(ups_data_.config.delay_start)) {
|
||||
value = ups_data_.config.delay_start;
|
||||
} else if (type == "ups_delay_reboot" && !std::isnan(ups_data_.config.delay_reboot)) {
|
||||
} else if (type == sensor_type::UPS_DELAY_REBOOT && !std::isnan(ups_data_.config.delay_reboot)) {
|
||||
value = ups_data_.config.delay_reboot;
|
||||
} else if (type == "ups_timer_reboot" && ups_data_.test.timer_reboot != -1) {
|
||||
} else if (type == sensor_type::UPS_TIMER_REBOOT && ups_data_.test.timer_reboot != -1) {
|
||||
value = ups_data_.test.timer_reboot;
|
||||
} else if (type == "ups_timer_shutdown" && ups_data_.test.timer_shutdown != -1) {
|
||||
} else if (type == sensor_type::UPS_TIMER_SHUTDOWN && ups_data_.test.timer_shutdown != -1) {
|
||||
value = ups_data_.test.timer_shutdown;
|
||||
} else if (type == "ups_timer_start" && ups_data_.test.timer_start != -1) {
|
||||
} else if (type == sensor_type::UPS_TIMER_START && ups_data_.test.timer_start != -1) {
|
||||
value = ups_data_.test.timer_start;
|
||||
}
|
||||
|
||||
@@ -308,11 +309,11 @@ void UpsHidComponent::update_sensors() {
|
||||
|
||||
bool state = false;
|
||||
|
||||
if (type == "online" && ups_data_.power.input_voltage_valid()) {
|
||||
if (type == binary_sensor_type::ONLINE && ups_data_.power.input_voltage_valid()) {
|
||||
state = true;
|
||||
} else if (type == "on_battery" && ups_data_.power.input_voltage_valid()) {
|
||||
} else if (type == binary_sensor_type::ON_BATTERY && ups_data_.power.input_voltage_valid()) {
|
||||
state = false; // Opposite of online
|
||||
} else if (type == "low_battery") {
|
||||
} else if (type == binary_sensor_type::LOW_BATTERY) {
|
||||
state = ups_data_.battery.is_low();
|
||||
}
|
||||
|
||||
@@ -326,25 +327,25 @@ void UpsHidComponent::update_sensors() {
|
||||
|
||||
std::string value = "";
|
||||
|
||||
if (type == "model" && !ups_data_.device.model.empty()) {
|
||||
if (type == text_sensor_type::MODEL && !ups_data_.device.model.empty()) {
|
||||
value = ups_data_.device.model;
|
||||
} else if (type == "manufacturer" && !ups_data_.device.manufacturer.empty()) {
|
||||
} else if (type == text_sensor_type::MANUFACTURER && !ups_data_.device.manufacturer.empty()) {
|
||||
value = ups_data_.device.manufacturer;
|
||||
} else if (type == "serial_number" && !ups_data_.device.serial_number.empty()) {
|
||||
} else if (type == text_sensor_type::SERIAL_NUMBER && !ups_data_.device.serial_number.empty()) {
|
||||
value = ups_data_.device.serial_number;
|
||||
} else if (type == "firmware_version" && !ups_data_.device.firmware_version.empty()) {
|
||||
} else if (type == text_sensor_type::FIRMWARE_VERSION && !ups_data_.device.firmware_version.empty()) {
|
||||
value = ups_data_.device.firmware_version;
|
||||
} else if (type == "battery_status" && !ups_data_.battery.status.empty()) {
|
||||
} else if (type == text_sensor_type::BATTERY_STATUS && !ups_data_.battery.status.empty()) {
|
||||
value = ups_data_.battery.status;
|
||||
} else if (type == "ups_test_result" && !ups_data_.test.ups_test_result.empty()) {
|
||||
} else if (type == text_sensor_type::UPS_TEST_RESULT && !ups_data_.test.ups_test_result.empty()) {
|
||||
value = ups_data_.test.ups_test_result;
|
||||
} else if (type == "ups_beeper_status" && !ups_data_.config.beeper_status.empty()) {
|
||||
} else if (type == text_sensor_type::UPS_BEEPER_STATUS && !ups_data_.config.beeper_status.empty()) {
|
||||
value = ups_data_.config.beeper_status;
|
||||
} else if (type == "input_sensitivity" && !ups_data_.config.input_sensitivity.empty()) {
|
||||
} else if (type == text_sensor_type::INPUT_SENSITIVITY && !ups_data_.config.input_sensitivity.empty()) {
|
||||
value = ups_data_.config.input_sensitivity;
|
||||
} else if (type == "status" && !ups_data_.power.status.empty()) {
|
||||
} else if (type == text_sensor_type::STATUS && !ups_data_.power.status.empty()) {
|
||||
value = ups_data_.power.status;
|
||||
} else if (type == "protocol") {
|
||||
} else if (type == text_sensor_type::PROTOCOL) {
|
||||
value = get_protocol_name();
|
||||
}
|
||||
|
||||
@@ -452,7 +453,7 @@ std::string UpsHidComponent::get_protocol_name() const {
|
||||
if (active_protocol_) {
|
||||
return active_protocol_->get_protocol_name();
|
||||
}
|
||||
return "None";
|
||||
return protocol::NONE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user