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
@@ -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
|
||||
Reference in new issue
Block a user