Public Access
feat(ups-hid): timer delay commands
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
19 files changed
+1008
-40
No files matched your search
@@ -116,7 +116,6 @@ components/
|
|||||||
└── ...
|
└── ...
|
||||||
|
|
||||||
tools/
|
tools/
|
||||||
├── generate_vendor_list.py # Vendor ID synchronization tool
|
|
||||||
├── scan-usb.sh # USB device scanning utility
|
├── scan-usb.sh # USB device scanning utility
|
||||||
└── README.md # Tools documentation
|
└── README.md # Tools documentation
|
||||||
|
|
||||||
@@ -126,8 +125,6 @@ tools/
|
|||||||
|
|
||||||
### Development Tools
|
### Development Tools
|
||||||
|
|
||||||
- **Generate Vendor List**: `python3 tools/generate_vendor_list.py`
|
|
||||||
- Synchronizes vendor IDs between C++ and Python code
|
|
||||||
- **Scan USB Devices**: `bash tools/scan-usb.sh`
|
- **Scan USB Devices**: `bash tools/scan-usb.sh`
|
||||||
- Lists connected ESP32, UPS, and serial devices
|
- Lists connected ESP32, UPS, and serial devices
|
||||||
- **VSCode Tasks**: Integrated development tasks via Command Palette
|
- **VSCode Tasks**: Integrated development tasks via Command Palette
|
||||||
|
|||||||
@@ -2004,6 +2004,178 @@ void ApcHidProtocol::detect_nominal_power_rating(const std::string& model_name,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ApcHidProtocol::read_timer_data(UpsData &data) {
|
||||||
|
ESP_LOGD(APC_HID_TAG, "Reading APC timer countdown data");
|
||||||
|
|
||||||
|
HidReport delay_shutdown_report;
|
||||||
|
HidReport delay_reboot_report;
|
||||||
|
bool success = false;
|
||||||
|
|
||||||
|
// Read delay shutdown report to get the RAW timer value
|
||||||
|
if (read_hid_report(APC_REPORT_ID_DELAY_SHUTDOWN, delay_shutdown_report)) {
|
||||||
|
// Parse the delay configuration (this updates data.config.delay_shutdown)
|
||||||
|
parse_ups_delay_shutdown_report(delay_shutdown_report, data);
|
||||||
|
|
||||||
|
// Now read the RAW HID value to determine actual timer state
|
||||||
|
// From NUT analysis: HID value -1 means timer inactive, positive values mean countdown active
|
||||||
|
if (delay_shutdown_report.data.size() >= 3) {
|
||||||
|
int16_t raw_timer_value = static_cast<int16_t>(
|
||||||
|
delay_shutdown_report.data[1] | (delay_shutdown_report.data[2] << 8)
|
||||||
|
);
|
||||||
|
|
||||||
|
ESP_LOGD(APC_HID_TAG, "Raw timer shutdown HID value: %d", raw_timer_value);
|
||||||
|
|
||||||
|
// Follow NUT convention: negative = inactive, positive = active countdown
|
||||||
|
if (raw_timer_value == -1) {
|
||||||
|
// Timer is inactive (normal operation)
|
||||||
|
data.test.timer_shutdown = -1;
|
||||||
|
ESP_LOGV(APC_HID_TAG, "Timer shutdown inactive (normal operation)");
|
||||||
|
} else if (raw_timer_value > 0) {
|
||||||
|
// Timer is actively counting down
|
||||||
|
data.test.timer_shutdown = raw_timer_value;
|
||||||
|
ESP_LOGI(APC_HID_TAG, "Timer shutdown ACTIVE countdown: %d seconds", raw_timer_value);
|
||||||
|
} else {
|
||||||
|
// Other negative values - treat as inactive but preserve the value
|
||||||
|
data.test.timer_shutdown = raw_timer_value;
|
||||||
|
ESP_LOGV(APC_HID_TAG, "Timer shutdown inactive: %d", raw_timer_value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback if report is too short
|
||||||
|
data.test.timer_shutdown = -1;
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Timer shutdown report too short, assuming inactive");
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read delay reboot report to get the RAW timer value
|
||||||
|
if (read_hid_report(APC_REPORT_ID_DELAY_REBOOT, delay_reboot_report)) {
|
||||||
|
// Parse the delay configuration (this updates data.config.delay_reboot)
|
||||||
|
parse_ups_delay_reboot_report(delay_reboot_report, data);
|
||||||
|
|
||||||
|
// Read the RAW HID value for reboot timer
|
||||||
|
if (delay_reboot_report.data.size() >= 2) {
|
||||||
|
uint8_t raw_reboot_value = delay_reboot_report.data[1];
|
||||||
|
|
||||||
|
ESP_LOGD(APC_HID_TAG, "Raw timer reboot HID value: %d", raw_reboot_value);
|
||||||
|
|
||||||
|
// APC reboot timer: 0 typically means no reboot delay (immediate)
|
||||||
|
if (raw_reboot_value == 0) {
|
||||||
|
data.test.timer_reboot = -1; // Inactive (or immediate reboot)
|
||||||
|
ESP_LOGV(APC_HID_TAG, "Timer reboot inactive (immediate/no delay)");
|
||||||
|
} else {
|
||||||
|
// Positive value indicates active reboot countdown
|
||||||
|
data.test.timer_reboot = static_cast<int>(raw_reboot_value);
|
||||||
|
ESP_LOGD(APC_HID_TAG, "Timer reboot countdown: %d seconds", raw_reboot_value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
data.test.timer_reboot = -1;
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Timer reboot report too short, assuming inactive");
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// APC doesn't have a separate "start" timer - use reboot timer value
|
||||||
|
data.test.timer_start = data.test.timer_reboot;
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
ESP_LOGD(APC_HID_TAG, "APC timer data updated - shutdown: %d, start: %d, reboot: %d",
|
||||||
|
data.test.timer_shutdown, data.test.timer_start, data.test.timer_reboot);
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool ApcHidProtocol::set_shutdown_delay(int seconds) {
|
||||||
|
ESP_LOGI(APC_HID_TAG, "Setting shutdown delay to %d seconds", seconds);
|
||||||
|
|
||||||
|
// Validate range (0-600 seconds = 0-10 minutes for APC)
|
||||||
|
if (seconds < 0 || seconds > 600) {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Shutdown delay %d seconds out of range (0-600)", seconds);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if device supports SET_REPORT operations
|
||||||
|
if (!parent_->is_connected()) {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Cannot set shutdown delay - device not connected");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For APC Back-UPS ES 700G (INPUT-ONLY device), we need special handling
|
||||||
|
// These devices don't have OUT endpoints and may not support SET_REPORT
|
||||||
|
// We'll attempt control transfer anyway as it might work on some models
|
||||||
|
|
||||||
|
// Prepare HID SET_REPORT data for shutdown delay
|
||||||
|
// APC uses report 0x41 for delay before shutdown
|
||||||
|
// Format: Report ID 0x41, 2 bytes little-endian seconds value
|
||||||
|
uint8_t delay_data[2];
|
||||||
|
delay_data[0] = seconds & 0xFF; // Low byte
|
||||||
|
delay_data[1] = (seconds >> 8) & 0xFF; // High byte
|
||||||
|
|
||||||
|
ESP_LOGD(APC_HID_TAG, "Writing shutdown delay: Report 0x%02X, Value: %d (0x%02X 0x%02X)",
|
||||||
|
APC_REPORT_ID_DELAY_SHUTDOWN, seconds, delay_data[0], delay_data[1]);
|
||||||
|
|
||||||
|
// Attempt SET_REPORT via control transfer (works even on INPUT-ONLY devices sometimes)
|
||||||
|
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, APC_REPORT_ID_DELAY_SHUTDOWN,
|
||||||
|
delay_data, 2, parent_->get_protocol_timeout());
|
||||||
|
|
||||||
|
if (ret == ESP_OK) {
|
||||||
|
ESP_LOGI(APC_HID_TAG, "Shutdown delay set successfully to %d seconds", seconds);
|
||||||
|
return true;
|
||||||
|
} else if (ret == ESP_ERR_NOT_SUPPORTED) {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Device does not support delay configuration (INPUT-ONLY device)");
|
||||||
|
ESP_LOGI(APC_HID_TAG, "Note: APC Back-UPS ES 700G is INPUT-ONLY and cannot be configured via USB");
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Failed to set shutdown delay: %s", esp_err_to_name(ret));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ApcHidProtocol::set_start_delay(int seconds) {
|
||||||
|
ESP_LOGI(APC_HID_TAG, "Setting start/reboot delay to %d seconds", seconds);
|
||||||
|
|
||||||
|
// Validate range (0-600 seconds = 0-10 minutes for APC)
|
||||||
|
if (seconds < 0 || seconds > 600) {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Start delay %d seconds out of range (0-600)", seconds);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if device supports SET_REPORT operations
|
||||||
|
if (!parent_->is_connected()) {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Cannot set start delay - device not connected");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// APC uses report 0x40 for reboot/start delay
|
||||||
|
// Format: Report ID 0x40, 1 byte seconds value (limited to 255 seconds)
|
||||||
|
uint8_t delay_data[1];
|
||||||
|
delay_data[0] = std::min(seconds, 255); // Limit to 255 for single byte
|
||||||
|
|
||||||
|
ESP_LOGD(APC_HID_TAG, "Writing start/reboot delay: Report 0x%02X, Value: %d (0x%02X)",
|
||||||
|
APC_REPORT_ID_DELAY_REBOOT, delay_data[0], delay_data[0]);
|
||||||
|
|
||||||
|
// Attempt SET_REPORT via control transfer
|
||||||
|
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, APC_REPORT_ID_DELAY_REBOOT,
|
||||||
|
delay_data, 1, parent_->get_protocol_timeout());
|
||||||
|
|
||||||
|
if (ret == ESP_OK) {
|
||||||
|
ESP_LOGI(APC_HID_TAG, "Start/reboot delay set successfully to %d seconds", delay_data[0]);
|
||||||
|
return true;
|
||||||
|
} else if (ret == ESP_ERR_NOT_SUPPORTED) {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Device does not support delay configuration (INPUT-ONLY device)");
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(APC_HID_TAG, "Failed to set start/reboot delay: %s", esp_err_to_name(ret));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ApcHidProtocol::set_reboot_delay(int seconds) {
|
||||||
|
// For APC, reboot delay is the same as start delay (report 0x40)
|
||||||
|
return set_start_delay(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ups_hid
|
} // namespace ups_hid
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,14 @@ public:
|
|||||||
bool stop_battery_test() override;
|
bool stop_battery_test() override;
|
||||||
bool start_ups_test() override;
|
bool start_ups_test() override;
|
||||||
bool stop_ups_test() override;
|
bool stop_ups_test() override;
|
||||||
|
|
||||||
|
// Timer polling for real-time countdown
|
||||||
|
bool read_timer_data(UpsData &data) override;
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool set_shutdown_delay(int seconds) override;
|
||||||
|
bool set_start_delay(int seconds) override;
|
||||||
|
bool set_reboot_delay(int seconds) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct HidReport {
|
struct HidReport {
|
||||||
|
|||||||
@@ -1314,6 +1314,153 @@ void CyberPowerProtocol::parse_manufacturing_date_report(const HidReport &report
|
|||||||
report.report_id, report.data.size());
|
report.report_id, report.data.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CyberPowerProtocol::read_timer_data(UpsData &data) {
|
||||||
|
ESP_LOGD(CP_TAG, "Reading CyberPower timer countdown data");
|
||||||
|
|
||||||
|
HidReport delay_shutdown_report;
|
||||||
|
HidReport delay_start_report;
|
||||||
|
bool success = false;
|
||||||
|
|
||||||
|
// Read delay shutdown report (timer countdown data)
|
||||||
|
if (read_hid_report(DELAY_SHUTDOWN_REPORT_ID, delay_shutdown_report)) {
|
||||||
|
// Parse the delay configuration (this updates data.config.delay_shutdown)
|
||||||
|
parse_delay_shutdown_report(delay_shutdown_report, data);
|
||||||
|
|
||||||
|
// For CyberPower, follow NUT behavior: timer shows negative of delay configuration during normal operation
|
||||||
|
// From CP_NUT_debug.md: ups.delay.shutdown: 60, ups.timer.shutdown: -60 (normal operation)
|
||||||
|
// During actual shutdown, the timer would show positive countdown values
|
||||||
|
|
||||||
|
// In normal operation, CyberPower timers should be negative of the configured delay
|
||||||
|
if (data.config.delay_shutdown > 0) {
|
||||||
|
// Normal operation - timer is inactive, show negative of configuration value
|
||||||
|
data.test.timer_shutdown = -data.config.delay_shutdown;
|
||||||
|
ESP_LOGV(CP_TAG, "Timer shutdown inactive: %d (config: %d)",
|
||||||
|
data.test.timer_shutdown, data.config.delay_shutdown);
|
||||||
|
} else {
|
||||||
|
// Use default if no configuration available
|
||||||
|
data.test.timer_shutdown = -defaults::CYBERPOWER_SHUTDOWN_DELAY_SEC;
|
||||||
|
ESP_LOGV(CP_TAG, "Timer shutdown inactive (default): %d", data.test.timer_shutdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: During actual UPS shutdown, we would need to detect the active countdown state
|
||||||
|
// This would require monitoring for changing values or specific status indicators
|
||||||
|
// For now, we follow NUT's normal operation behavior
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read delay start report (timer countdown data)
|
||||||
|
if (read_hid_report(DELAY_START_REPORT_ID, delay_start_report)) {
|
||||||
|
// Parse the delay configuration (this updates data.config.delay_start)
|
||||||
|
parse_delay_start_report(delay_start_report, data);
|
||||||
|
|
||||||
|
// Follow same pattern as shutdown timer
|
||||||
|
if (data.config.delay_start > 0) {
|
||||||
|
// Normal operation - timer is inactive, show negative of configuration value
|
||||||
|
data.test.timer_start = -data.config.delay_start;
|
||||||
|
ESP_LOGV(CP_TAG, "Timer start inactive: %d (config: %d)",
|
||||||
|
data.test.timer_start, data.config.delay_start);
|
||||||
|
} else {
|
||||||
|
// Use default if no configuration available
|
||||||
|
data.test.timer_start = -defaults::CYBERPOWER_STARTUP_DELAY_SEC;
|
||||||
|
ESP_LOGV(CP_TAG, "Timer start inactive (default): %d", data.test.timer_start);
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CyberPower typically doesn't have a separate reboot timer, use shutdown timer
|
||||||
|
data.test.timer_reboot = data.test.timer_shutdown;
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
ESP_LOGD(CP_TAG, "CyberPower timer data updated - shutdown: %d, start: %d, reboot: %d",
|
||||||
|
data.test.timer_shutdown, data.test.timer_start, data.test.timer_reboot);
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool CyberPowerProtocol::set_shutdown_delay(int seconds) {
|
||||||
|
ESP_LOGI(CP_TAG, "Setting shutdown delay to %d seconds", seconds);
|
||||||
|
|
||||||
|
// Validate range (0-7200 seconds = 0-2 hours)
|
||||||
|
if (seconds < 0 || seconds > 7200) {
|
||||||
|
ESP_LOGW(CP_TAG, "Shutdown delay %d seconds out of range (0-7200)", seconds);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare HID SET_REPORT data for shutdown delay
|
||||||
|
// Format: Report ID 0x15, 2 bytes little-endian seconds value
|
||||||
|
uint8_t delay_data[3];
|
||||||
|
delay_data[0] = DELAY_SHUTDOWN_REPORT_ID;
|
||||||
|
delay_data[1] = seconds & 0xFF; // Low byte
|
||||||
|
delay_data[2] = (seconds >> 8) & 0xFF; // High byte
|
||||||
|
|
||||||
|
ESP_LOGD(CP_TAG, "Writing shutdown delay: Report 0x%02X, Value: %d (0x%02X 0x%02X)",
|
||||||
|
DELAY_SHUTDOWN_REPORT_ID, seconds, delay_data[1], delay_data[2]);
|
||||||
|
|
||||||
|
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, DELAY_SHUTDOWN_REPORT_ID,
|
||||||
|
delay_data + 1, 2, parent_->get_protocol_timeout());
|
||||||
|
|
||||||
|
if (ret == ESP_OK) {
|
||||||
|
ESP_LOGI(CP_TAG, "Shutdown delay set successfully to %d seconds", seconds);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(CP_TAG, "Failed to set shutdown delay: %s", esp_err_to_name(ret));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CyberPowerProtocol::set_start_delay(int seconds) {
|
||||||
|
ESP_LOGI(CP_TAG, "Setting start delay to %d seconds", seconds);
|
||||||
|
|
||||||
|
// Validate range (0-7200 seconds = 0-2 hours)
|
||||||
|
if (seconds < 0 || seconds > 7200) {
|
||||||
|
ESP_LOGW(CP_TAG, "Start delay %d seconds out of range (0-7200)", seconds);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare HID SET_REPORT data for start delay
|
||||||
|
// Format: Report ID 0x16, 2 bytes little-endian seconds value
|
||||||
|
uint8_t delay_data[3];
|
||||||
|
delay_data[0] = DELAY_START_REPORT_ID;
|
||||||
|
delay_data[1] = seconds & 0xFF; // Low byte
|
||||||
|
delay_data[2] = (seconds >> 8) & 0xFF; // High byte
|
||||||
|
|
||||||
|
ESP_LOGD(CP_TAG, "Writing start delay: Report 0x%02X, Value: %d (0x%02X 0x%02X)",
|
||||||
|
DELAY_START_REPORT_ID, seconds, delay_data[1], delay_data[2]);
|
||||||
|
|
||||||
|
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, DELAY_START_REPORT_ID,
|
||||||
|
delay_data + 1, 2, parent_->get_protocol_timeout());
|
||||||
|
|
||||||
|
if (ret == ESP_OK) {
|
||||||
|
ESP_LOGI(CP_TAG, "Start delay set successfully to %d seconds", seconds);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(CP_TAG, "Failed to set start delay: %s", esp_err_to_name(ret));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CyberPowerProtocol::set_reboot_delay(int seconds) {
|
||||||
|
ESP_LOGI(CP_TAG, "Setting reboot delay to %d seconds", seconds);
|
||||||
|
|
||||||
|
// CyberPower typically uses shutdown delay for reboot timing
|
||||||
|
// But some models may have a separate reboot delay report
|
||||||
|
// For now, we'll set both shutdown and start delays for reboot
|
||||||
|
|
||||||
|
bool shutdown_ok = set_shutdown_delay(seconds);
|
||||||
|
bool start_ok = set_start_delay(seconds);
|
||||||
|
|
||||||
|
if (shutdown_ok && start_ok) {
|
||||||
|
ESP_LOGI(CP_TAG, "Reboot delay set successfully to %d seconds", seconds);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(CP_TAG, "Failed to set reboot delay completely");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<UpsProtocolBase> create_cyberpower_protocol(UpsHidComponent* parent) {
|
std::unique_ptr<UpsProtocolBase> create_cyberpower_protocol(UpsHidComponent* parent) {
|
||||||
return std::make_unique<CyberPowerProtocol>(parent);
|
return std::make_unique<CyberPowerProtocol>(parent);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,14 @@ class CyberPowerProtocol : public UpsProtocolBase {
|
|||||||
bool stop_battery_test() override;
|
bool stop_battery_test() override;
|
||||||
bool start_ups_test() override;
|
bool start_ups_test() override;
|
||||||
bool stop_ups_test() override;
|
bool stop_ups_test() override;
|
||||||
|
|
||||||
|
// Timer polling for real-time countdown
|
||||||
|
bool read_timer_data(UpsData &data) override;
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool set_shutdown_delay(int seconds) override;
|
||||||
|
bool set_start_delay(int seconds) override;
|
||||||
|
bool set_reboot_delay(int seconds) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Report ID constants (based on NUT debug logs)
|
// Report ID constants (based on NUT debug logs)
|
||||||
|
|||||||
@@ -858,6 +858,108 @@ float GenericHidProtocol::parse_frequency_from_report(uint8_t* data, size_t len)
|
|||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool GenericHidProtocol::set_shutdown_delay(int seconds) {
|
||||||
|
ESP_LOGI(TAG, "Setting shutdown delay to %d seconds (Generic HID)", seconds);
|
||||||
|
|
||||||
|
// Validate range (0-7200 seconds = 0-2 hours)
|
||||||
|
if (seconds < 0 || seconds > 7200) {
|
||||||
|
ESP_LOGW(TAG, "Shutdown delay %d seconds out of range (0-7200)", seconds);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try common report IDs for delay configuration
|
||||||
|
// Different vendors use different report IDs, so we'll try the most common ones
|
||||||
|
const std::vector<uint8_t> delay_report_ids = {
|
||||||
|
0x15, // CyberPower uses this
|
||||||
|
0x41, // APC uses this
|
||||||
|
0x57, // Some other vendors
|
||||||
|
HID_USAGE_POW_DELAY_BEFORE_SHUTDOWN, // Standard HID usage
|
||||||
|
};
|
||||||
|
|
||||||
|
bool success = false;
|
||||||
|
for (uint8_t report_id : delay_report_ids) {
|
||||||
|
uint8_t delay_data[2];
|
||||||
|
delay_data[0] = seconds & 0xFF; // Low byte
|
||||||
|
delay_data[1] = (seconds >> 8) & 0xFF; // High byte
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, "Trying shutdown delay on report 0x%02X: %d seconds", report_id, seconds);
|
||||||
|
|
||||||
|
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, report_id,
|
||||||
|
delay_data, 2, parent_->get_protocol_timeout());
|
||||||
|
|
||||||
|
if (ret == ESP_OK) {
|
||||||
|
ESP_LOGI(TAG, "Shutdown delay set successfully via report 0x%02X", report_id);
|
||||||
|
success = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
ESP_LOGW(TAG, "Failed to set shutdown delay - no compatible report found");
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GenericHidProtocol::set_start_delay(int seconds) {
|
||||||
|
ESP_LOGI(TAG, "Setting start delay to %d seconds (Generic HID)", seconds);
|
||||||
|
|
||||||
|
// Validate range (0-7200 seconds = 0-2 hours)
|
||||||
|
if (seconds < 0 || seconds > 7200) {
|
||||||
|
ESP_LOGW(TAG, "Start delay %d seconds out of range (0-7200)", seconds);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try common report IDs for start delay configuration
|
||||||
|
const std::vector<uint8_t> delay_report_ids = {
|
||||||
|
0x16, // CyberPower uses this
|
||||||
|
0x40, // APC might use this
|
||||||
|
0x58, // Some other vendors
|
||||||
|
HID_USAGE_POW_DELAY_BEFORE_STARTUP, // Standard HID usage
|
||||||
|
};
|
||||||
|
|
||||||
|
bool success = false;
|
||||||
|
for (uint8_t report_id : delay_report_ids) {
|
||||||
|
uint8_t delay_data[2];
|
||||||
|
delay_data[0] = seconds & 0xFF; // Low byte
|
||||||
|
delay_data[1] = (seconds >> 8) & 0xFF; // High byte
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, "Trying start delay on report 0x%02X: %d seconds", report_id, seconds);
|
||||||
|
|
||||||
|
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, report_id,
|
||||||
|
delay_data, 2, parent_->get_protocol_timeout());
|
||||||
|
|
||||||
|
if (ret == ESP_OK) {
|
||||||
|
ESP_LOGI(TAG, "Start delay set successfully via report 0x%02X", report_id);
|
||||||
|
success = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
ESP_LOGW(TAG, "Failed to set start delay - no compatible report found");
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GenericHidProtocol::set_reboot_delay(int seconds) {
|
||||||
|
ESP_LOGI(TAG, "Setting reboot delay to %d seconds (Generic HID)", seconds);
|
||||||
|
|
||||||
|
// For generic HID, reboot typically involves setting both shutdown and start delays
|
||||||
|
bool shutdown_ok = set_shutdown_delay(seconds);
|
||||||
|
bool start_ok = set_start_delay(seconds);
|
||||||
|
|
||||||
|
if (shutdown_ok || start_ok) {
|
||||||
|
ESP_LOGI(TAG, "Reboot delay partially set to %d seconds", seconds);
|
||||||
|
return true; // Return true if at least one succeeded
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(TAG, "Failed to set any reboot delay");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ups_hid
|
} // namespace ups_hid
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ public:
|
|||||||
bool detect() override;
|
bool detect() override;
|
||||||
bool initialize() override;
|
bool initialize() override;
|
||||||
bool read_data(UpsData &data) override;
|
bool read_data(UpsData &data) override;
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool set_shutdown_delay(int seconds) override;
|
||||||
|
bool set_start_delay(int seconds) override;
|
||||||
|
bool set_reboot_delay(int seconds) override;
|
||||||
|
|
||||||
// Report discovery and enumeration
|
// Report discovery and enumeration
|
||||||
void enumerate_reports();
|
void enumerate_reports();
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
#include "number.h"
|
||||||
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace ups_hid {
|
||||||
|
|
||||||
|
static const char *const TAG_NUMBER = "ups_hid.number";
|
||||||
|
|
||||||
|
void UpsDelayNumber::setup() {
|
||||||
|
ESP_LOGD(TAG_NUMBER, "Setting up UPS delay number '%s' for %s",
|
||||||
|
this->get_name().c_str(), this->delay_type_to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpsDelayNumber::dump_config() {
|
||||||
|
LOG_NUMBER("", "UPS Delay Number", this);
|
||||||
|
ESP_LOGCONFIG(TAG_NUMBER, " Type: %s", this->delay_type_to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpsDelayNumber::control(float value) {
|
||||||
|
ESP_LOGI(TAG_NUMBER, "Setting %s delay to %.0f seconds", this->delay_type_to_string(), value);
|
||||||
|
|
||||||
|
if (this->parent_ == nullptr) {
|
||||||
|
ESP_LOGW(TAG_NUMBER, "Parent UPS HID component not set");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call parent to set the delay value via USB HID
|
||||||
|
bool success = false;
|
||||||
|
switch (this->delay_type_) {
|
||||||
|
case DELAY_SHUTDOWN:
|
||||||
|
success = this->parent_->set_shutdown_delay(static_cast<int>(value));
|
||||||
|
break;
|
||||||
|
case DELAY_START:
|
||||||
|
success = this->parent_->set_start_delay(static_cast<int>(value));
|
||||||
|
break;
|
||||||
|
case DELAY_REBOOT:
|
||||||
|
success = this->parent_->set_reboot_delay(static_cast<int>(value));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
// Update displayed value if write succeeded
|
||||||
|
this->publish_state(value);
|
||||||
|
ESP_LOGI(TAG_NUMBER, "%s delay set successfully to %.0f seconds",
|
||||||
|
this->delay_type_to_string(), value);
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(TAG_NUMBER, "Failed to set %s delay", this->delay_type_to_string());
|
||||||
|
// Optionally refresh from device to show actual value
|
||||||
|
this->parent_->request_delay_refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpsDelayNumber::update_value(float value) {
|
||||||
|
if (!std::isnan(value) && this->state != value) {
|
||||||
|
this->publish_state(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *UpsDelayNumber::delay_type_to_string() const {
|
||||||
|
switch (this->delay_type_) {
|
||||||
|
case DELAY_SHUTDOWN:
|
||||||
|
return "shutdown";
|
||||||
|
case DELAY_START:
|
||||||
|
return "start";
|
||||||
|
case DELAY_REBOOT:
|
||||||
|
return "reboot";
|
||||||
|
default:
|
||||||
|
return "unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ups_hid
|
||||||
|
} // namespace esphome
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esphome/core/component.h"
|
||||||
|
#include "esphome/components/number/number.h"
|
||||||
|
#include "ups_hid.h"
|
||||||
|
|
||||||
|
namespace esphome {
|
||||||
|
namespace ups_hid {
|
||||||
|
|
||||||
|
enum DelayType {
|
||||||
|
DELAY_SHUTDOWN,
|
||||||
|
DELAY_START,
|
||||||
|
DELAY_REBOOT,
|
||||||
|
};
|
||||||
|
|
||||||
|
class UpsDelayNumber : public number::Number, public Component {
|
||||||
|
public:
|
||||||
|
void setup() override;
|
||||||
|
void dump_config() override;
|
||||||
|
|
||||||
|
void set_parent(UpsHidComponent *parent) { this->parent_ = parent; }
|
||||||
|
void set_delay_type(DelayType type) { this->delay_type_ = type; }
|
||||||
|
|
||||||
|
// Called when user changes the number value
|
||||||
|
void control(float value) override;
|
||||||
|
|
||||||
|
// Update the displayed value from UPS data
|
||||||
|
void update_value(float value);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
UpsHidComponent *parent_{nullptr};
|
||||||
|
DelayType delay_type_;
|
||||||
|
|
||||||
|
const char *delay_type_to_string() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace ups_hid
|
||||||
|
} // namespace esphome
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
"""ESPHome Number Platform for UPS HID Component"""
|
||||||
|
|
||||||
|
import esphome.codegen as cg
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.components import number
|
||||||
|
from esphome.const import (
|
||||||
|
CONF_ID,
|
||||||
|
DEVICE_CLASS_DURATION,
|
||||||
|
UNIT_SECOND,
|
||||||
|
ENTITY_CATEGORY_CONFIG,
|
||||||
|
)
|
||||||
|
from . import ups_hid_ns, UpsHidComponent, CONF_UPS_HID_ID
|
||||||
|
|
||||||
|
DEPENDENCIES = ["ups_hid"]
|
||||||
|
|
||||||
|
UpsDelayNumber = ups_hid_ns.class_("UpsDelayNumber", number.Number, cg.Component)
|
||||||
|
|
||||||
|
CONF_DELAY_TYPE = "delay_type"
|
||||||
|
|
||||||
|
DelayType = ups_hid_ns.enum("DelayType")
|
||||||
|
DELAY_TYPES = {
|
||||||
|
"shutdown": DelayType.DELAY_SHUTDOWN,
|
||||||
|
"start": DelayType.DELAY_START,
|
||||||
|
"reboot": DelayType.DELAY_REBOOT,
|
||||||
|
}
|
||||||
|
|
||||||
|
CONFIG_SCHEMA = number.number_schema(
|
||||||
|
UpsDelayNumber,
|
||||||
|
unit_of_measurement=UNIT_SECOND,
|
||||||
|
device_class=DEVICE_CLASS_DURATION,
|
||||||
|
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||||
|
).extend(
|
||||||
|
{
|
||||||
|
cv.GenerateID(CONF_UPS_HID_ID): cv.use_id(UpsHidComponent),
|
||||||
|
cv.Required(CONF_DELAY_TYPE): cv.enum(DELAY_TYPES, lower=True),
|
||||||
|
cv.Optional("min_value", default=0): cv.float_range(min=0, max=3600),
|
||||||
|
cv.Optional("max_value", default=600): cv.float_range(min=0, max=7200),
|
||||||
|
cv.Optional("step", default=10): cv.positive_float,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
async def to_code(config):
|
||||||
|
var = await number.new_number(
|
||||||
|
config,
|
||||||
|
min_value=config["min_value"],
|
||||||
|
max_value=config["max_value"],
|
||||||
|
step=config["step"],
|
||||||
|
)
|
||||||
|
|
||||||
|
ups_hid = await cg.get_variable(config[CONF_UPS_HID_ID])
|
||||||
|
cg.add(var.set_parent(ups_hid))
|
||||||
|
cg.add(var.set_delay_type(config[CONF_DELAY_TYPE]))
|
||||||
|
cg.add(ups_hid.register_delay_number(var))
|
||||||
@@ -7,7 +7,8 @@
|
|||||||
#endif
|
#endif
|
||||||
#include "protocol_factory.h"
|
#include "protocol_factory.h"
|
||||||
#include "apc_hid_protocol.h"
|
#include "apc_hid_protocol.h"
|
||||||
#include "cyberpower_protocol.h"
|
#include "cyberpower_protocol.h"
|
||||||
|
#include "number.h"
|
||||||
#include "generic_hid_protocol.h"
|
#include "generic_hid_protocol.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include "esphome/core/application.h"
|
#include "esphome/core/application.h"
|
||||||
@@ -60,6 +61,9 @@ void UpsHidComponent::update() {
|
|||||||
update_sensors();
|
update_sensors();
|
||||||
consecutive_failures_ = 0;
|
consecutive_failures_ = 0;
|
||||||
last_successful_read_ = millis();
|
last_successful_read_ = millis();
|
||||||
|
|
||||||
|
// Check for timer updates (fast polling during countdowns)
|
||||||
|
check_and_update_timers();
|
||||||
} else {
|
} else {
|
||||||
consecutive_failures_++;
|
consecutive_failures_++;
|
||||||
ESP_LOGW(TAG, log_messages::READ_FAILED, consecutive_failures_);
|
ESP_LOGW(TAG, log_messages::READ_FAILED, consecutive_failures_);
|
||||||
@@ -361,6 +365,16 @@ void UpsHidComponent::update_sensors() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update delay number components
|
||||||
|
for (auto* delay_number : delay_numbers_) {
|
||||||
|
if (delay_number != nullptr) {
|
||||||
|
// Determine which delay value to use based on the delay type
|
||||||
|
// This will be handled by the number component itself
|
||||||
|
// We just need to trigger an update with the current config values
|
||||||
|
// The number component will query the appropriate value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ESP_LOGV(TAG, "Updated %zu sensors, %zu binary sensors, %zu text sensors",
|
ESP_LOGV(TAG, "Updated %zu sensors, %zu binary sensors, %zu text sensors",
|
||||||
sensors_.size(), binary_sensors_.size(), text_sensors_.size());
|
sensors_.size(), binary_sensors_.size(), text_sensors_.size());
|
||||||
}
|
}
|
||||||
@@ -381,6 +395,11 @@ void UpsHidComponent::register_text_sensor(text_sensor::TextSensor *sens, const
|
|||||||
ESP_LOGD(TAG, "Registered text sensor: %s", type.c_str());
|
ESP_LOGD(TAG, "Registered text sensor: %s", type.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UpsHidComponent::register_delay_number(UpsDelayNumber *number) {
|
||||||
|
delay_numbers_.push_back(number);
|
||||||
|
ESP_LOGD(TAG, "Registered delay number component");
|
||||||
|
}
|
||||||
|
|
||||||
// Test control methods
|
// Test control methods
|
||||||
bool UpsHidComponent::start_battery_test_quick() {
|
bool UpsHidComponent::start_battery_test_quick() {
|
||||||
if (!active_protocol_) {
|
if (!active_protocol_) {
|
||||||
@@ -455,6 +474,31 @@ bool UpsHidComponent::beeper_test() {
|
|||||||
return active_protocol_->beeper_test();
|
return active_protocol_->beeper_test();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool UpsHidComponent::set_shutdown_delay(int seconds) {
|
||||||
|
if (!active_protocol_) {
|
||||||
|
ESP_LOGW(TAG, "No active protocol for delay configuration");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return active_protocol_->set_shutdown_delay(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UpsHidComponent::set_start_delay(int seconds) {
|
||||||
|
if (!active_protocol_) {
|
||||||
|
ESP_LOGW(TAG, "No active protocol for delay configuration");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return active_protocol_->set_start_delay(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UpsHidComponent::set_reboot_delay(int seconds) {
|
||||||
|
if (!active_protocol_) {
|
||||||
|
ESP_LOGW(TAG, "No active protocol for delay configuration");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return active_protocol_->set_reboot_delay(seconds);
|
||||||
|
}
|
||||||
|
|
||||||
// Additional protocol access method
|
// Additional protocol access method
|
||||||
std::string UpsHidComponent::get_protocol_name() const {
|
std::string UpsHidComponent::get_protocol_name() const {
|
||||||
if (active_protocol_) {
|
if (active_protocol_) {
|
||||||
@@ -505,6 +549,71 @@ void UpsHidComponent::cleanup() {
|
|||||||
ESP_LOGD(TAG, "Component cleanup completed");
|
ESP_LOGD(TAG, "Component cleanup completed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Timer polling implementation
|
||||||
|
void UpsHidComponent::check_and_update_timers() {
|
||||||
|
if (!active_protocol_) return;
|
||||||
|
|
||||||
|
uint32_t now = millis();
|
||||||
|
|
||||||
|
// Check if we need to poll timers
|
||||||
|
bool should_poll_timers = false;
|
||||||
|
|
||||||
|
if (fast_polling_mode_) {
|
||||||
|
// In fast polling mode, check every 2 seconds
|
||||||
|
if (now - last_timer_poll_ >= FAST_POLL_INTERVAL_MS) {
|
||||||
|
should_poll_timers = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Check if any timers might be active (less frequent check)
|
||||||
|
if (now - last_timer_poll_ >= get_update_interval()) {
|
||||||
|
should_poll_timers = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (should_poll_timers) {
|
||||||
|
last_timer_poll_ = now;
|
||||||
|
|
||||||
|
// Try to read timer data
|
||||||
|
UpsData timer_data = ups_data_; // Copy current data
|
||||||
|
if (active_protocol_->read_timer_data(timer_data)) {
|
||||||
|
// Update only timer-related fields
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||||
|
ups_data_.test.timer_shutdown = timer_data.test.timer_shutdown;
|
||||||
|
ups_data_.test.timer_start = timer_data.test.timer_start;
|
||||||
|
ups_data_.test.timer_reboot = timer_data.test.timer_reboot;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update fast polling mode based on timer activity
|
||||||
|
bool timers_active = has_active_timers();
|
||||||
|
if (timers_active != fast_polling_mode_) {
|
||||||
|
set_fast_polling_mode(timers_active);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update timer sensors immediately when values change
|
||||||
|
update_sensors();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UpsHidComponent::has_active_timers() const {
|
||||||
|
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||||
|
return (ups_data_.test.timer_shutdown > 0 ||
|
||||||
|
ups_data_.test.timer_start > 0 ||
|
||||||
|
ups_data_.test.timer_reboot > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpsHidComponent::set_fast_polling_mode(bool enable) {
|
||||||
|
if (enable != fast_polling_mode_) {
|
||||||
|
fast_polling_mode_ = enable;
|
||||||
|
if (enable) {
|
||||||
|
ESP_LOGI(TAG, "Enabled fast polling for timer countdown");
|
||||||
|
} else {
|
||||||
|
ESP_LOGI(TAG, "Disabled fast polling, returning to normal interval");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace ups_hid
|
} // namespace ups_hid
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
@@ -96,11 +96,18 @@ namespace esphome
|
|||||||
bool beeper_disable();
|
bool beeper_disable();
|
||||||
bool beeper_mute();
|
bool beeper_mute();
|
||||||
bool beeper_test();
|
bool beeper_test();
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
bool set_shutdown_delay(int seconds);
|
||||||
|
bool set_start_delay(int seconds);
|
||||||
|
bool set_reboot_delay(int seconds);
|
||||||
|
void request_delay_refresh() { /* No-op for now - could trigger update if needed */ }
|
||||||
|
|
||||||
// Sensor registration methods
|
// Sensor registration methods
|
||||||
void register_sensor(sensor::Sensor *sens, const std::string &type);
|
void register_sensor(sensor::Sensor *sens, const std::string &type);
|
||||||
void register_binary_sensor(binary_sensor::BinarySensor *sens, const std::string &type);
|
void register_binary_sensor(binary_sensor::BinarySensor *sens, const std::string &type);
|
||||||
void register_text_sensor(text_sensor::TextSensor *sens, const std::string &type);
|
void register_text_sensor(text_sensor::TextSensor *sens, const std::string &type);
|
||||||
|
void register_delay_number(class UpsDelayNumber *number);
|
||||||
|
|
||||||
// Protocol access for button components
|
// Protocol access for button components
|
||||||
UpsProtocolBase* get_active_protocol() const { return active_protocol_.get(); }
|
UpsProtocolBase* get_active_protocol() const { return active_protocol_.get(); }
|
||||||
@@ -120,6 +127,11 @@ namespace esphome
|
|||||||
UpsData ups_data_;
|
UpsData ups_data_;
|
||||||
mutable std::mutex data_mutex_; // Protect ups_data_ access
|
mutable std::mutex data_mutex_; // Protect ups_data_ access
|
||||||
|
|
||||||
|
// Fast polling for timer countdown
|
||||||
|
bool fast_polling_mode_{false};
|
||||||
|
uint32_t last_timer_poll_{0};
|
||||||
|
static constexpr uint32_t FAST_POLL_INTERVAL_MS = 2000; // 2 seconds during countdown
|
||||||
|
|
||||||
// Error rate limiting to prevent log spam
|
// Error rate limiting to prevent log spam
|
||||||
struct ErrorRateLimit {
|
struct ErrorRateLimit {
|
||||||
uint32_t last_error_time{0};
|
uint32_t last_error_time{0};
|
||||||
@@ -137,6 +149,7 @@ namespace esphome
|
|||||||
std::unordered_map<std::string, sensor::Sensor *> sensors_;
|
std::unordered_map<std::string, sensor::Sensor *> sensors_;
|
||||||
std::unordered_map<std::string, binary_sensor::BinarySensor *> binary_sensors_;
|
std::unordered_map<std::string, binary_sensor::BinarySensor *> binary_sensors_;
|
||||||
std::unordered_map<std::string, text_sensor::TextSensor *> text_sensors_;
|
std::unordered_map<std::string, text_sensor::TextSensor *> text_sensors_;
|
||||||
|
std::vector<class UpsDelayNumber *> delay_numbers_;
|
||||||
|
|
||||||
// Core methods
|
// Core methods
|
||||||
bool initialize_transport();
|
bool initialize_transport();
|
||||||
@@ -144,6 +157,11 @@ namespace esphome
|
|||||||
bool read_ups_data();
|
bool read_ups_data();
|
||||||
void update_sensors();
|
void update_sensors();
|
||||||
|
|
||||||
|
// Timer polling methods
|
||||||
|
void check_and_update_timers();
|
||||||
|
bool has_active_timers() const;
|
||||||
|
void set_fast_polling_mode(bool enable);
|
||||||
|
|
||||||
// Error rate limiting helpers
|
// Error rate limiting helpers
|
||||||
bool should_log_error(ErrorRateLimit& limiter);
|
bool should_log_error(ErrorRateLimit& limiter);
|
||||||
void log_suppressed_errors(ErrorRateLimit& limiter);
|
void log_suppressed_errors(ErrorRateLimit& limiter);
|
||||||
@@ -200,6 +218,14 @@ namespace esphome
|
|||||||
virtual bool stop_battery_test() { return false; }
|
virtual bool stop_battery_test() { return false; }
|
||||||
virtual bool start_ups_test() { return false; }
|
virtual bool start_ups_test() { return false; }
|
||||||
virtual bool stop_ups_test() { return false; }
|
virtual bool stop_ups_test() { return false; }
|
||||||
|
|
||||||
|
// Timer polling method for real-time countdown
|
||||||
|
virtual bool read_timer_data(UpsData &data) { return false; }
|
||||||
|
|
||||||
|
// Delay configuration methods
|
||||||
|
virtual bool set_shutdown_delay(int seconds) { return false; }
|
||||||
|
virtual bool set_start_delay(int seconds) { return false; }
|
||||||
|
virtual bool set_reboot_delay(int seconds) { return false; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
UpsHidComponent *parent_;
|
UpsHidComponent *parent_;
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
# UPS Delay Configuration Package
|
||||||
|
# Provides configurable delay settings for shutdown, start, and reboot operations
|
||||||
|
# Use with: packages: delay_config: !include configs/delay_config.yaml
|
||||||
|
|
||||||
|
number:
|
||||||
|
# Shutdown delay configuration (0-600 seconds)
|
||||||
|
- platform: ups_hid
|
||||||
|
ups_hid_id: ups_monitor
|
||||||
|
name: "UPS Shutdown Delay"
|
||||||
|
id: ups_shutdown_delay_config
|
||||||
|
delay_type: shutdown
|
||||||
|
min_value: 0
|
||||||
|
max_value: 600
|
||||||
|
step: 10
|
||||||
|
unit_of_measurement: "s"
|
||||||
|
device_class: duration
|
||||||
|
entity_category: config
|
||||||
|
icon: "mdi:timer-settings"
|
||||||
|
mode: box
|
||||||
|
|
||||||
|
# Start/power-on delay configuration (0-600 seconds)
|
||||||
|
- platform: ups_hid
|
||||||
|
ups_hid_id: ups_monitor
|
||||||
|
name: "UPS Start Delay"
|
||||||
|
id: ups_start_delay_config
|
||||||
|
delay_type: start
|
||||||
|
min_value: 0
|
||||||
|
max_value: 600
|
||||||
|
step: 10
|
||||||
|
unit_of_measurement: "s"
|
||||||
|
device_class: duration
|
||||||
|
entity_category: config
|
||||||
|
icon: "mdi:timer-settings"
|
||||||
|
mode: box
|
||||||
|
|
||||||
|
# Reboot delay configuration (0-600 seconds)
|
||||||
|
- platform: ups_hid
|
||||||
|
ups_hid_id: ups_monitor
|
||||||
|
name: "UPS Reboot Delay"
|
||||||
|
id: ups_reboot_delay_config
|
||||||
|
delay_type: reboot
|
||||||
|
min_value: 0
|
||||||
|
max_value: 600
|
||||||
|
step: 10
|
||||||
|
unit_of_measurement: "s"
|
||||||
|
device_class: duration
|
||||||
|
entity_category: config
|
||||||
|
icon: "mdi:timer-settings"
|
||||||
|
mode: box
|
||||||
|
|
||||||
|
# Script to read current delay values from UPS
|
||||||
|
script:
|
||||||
|
- id: read_delay_configuration
|
||||||
|
then:
|
||||||
|
- logger.log:
|
||||||
|
format: "Current delay configuration - Shutdown: %.0fs, Start: %.0fs, Reboot: %.0fs"
|
||||||
|
args:
|
||||||
|
- "id(ups_shutdown_delay_config).state"
|
||||||
|
- "id(ups_start_delay_config).state"
|
||||||
|
- "id(ups_reboot_delay_config).state"
|
||||||
|
level: INFO
|
||||||
|
# Note: These show the configuration values, not the actual UPS timer values
|
||||||
|
|
||||||
|
- id: apply_default_delays
|
||||||
|
then:
|
||||||
|
# Apply common delay settings for your environment
|
||||||
|
- number.set:
|
||||||
|
id: ups_shutdown_delay_config
|
||||||
|
value: 60.0
|
||||||
|
- number.set:
|
||||||
|
id: ups_start_delay_config
|
||||||
|
value: 120.0
|
||||||
|
- number.set:
|
||||||
|
id: ups_reboot_delay_config
|
||||||
|
value: 60.0
|
||||||
|
- logger.log: "Applied default delay configuration"
|
||||||
|
|
||||||
|
# Expected Behavior:
|
||||||
|
# CyberPower: Full bidirectional support - read and write delays
|
||||||
|
# Generic HID: Attempts common report IDs - may work with many devices
|
||||||
|
# APC: Limited support - INPUT-ONLY devices cannot write delays
|
||||||
|
#
|
||||||
|
# Configuration changes are sent immediately to the UPS via USB HID SET_REPORT
|
||||||
|
# The UPS will store these values in non-volatile memory
|
||||||
@@ -24,9 +24,15 @@ packages:
|
|||||||
# Extended monitoring (optional - comment out if not needed)
|
# Extended monitoring (optional - comment out if not needed)
|
||||||
extended: !include ../extended_sensors.yaml
|
extended: !include ../extended_sensors.yaml
|
||||||
|
|
||||||
|
# Timer countdown sensors (Real-time shutdown/startup monitoring)
|
||||||
|
timers: !include ../timer_sensors.yaml
|
||||||
|
|
||||||
# Control buttons (optional - comment out if not needed)
|
# Control buttons (optional - comment out if not needed)
|
||||||
controls: !include ../ups_controls.yaml
|
controls: !include ../ups_controls.yaml
|
||||||
|
|
||||||
|
# Delay configuration (Shutdown/Start/Reboot timers)
|
||||||
|
delay_config: !include ../delay_config.yaml
|
||||||
|
|
||||||
# APC-specific optimizations
|
# APC-specific optimizations
|
||||||
apc_device: !include ../device_types/apc_backups_es.yaml
|
apc_device: !include ../device_types/apc_backups_es.yaml
|
||||||
|
|
||||||
@@ -54,5 +60,63 @@ script:
|
|||||||
level: INFO
|
level: INFO
|
||||||
# Add your notification logic here
|
# Add your notification logic here
|
||||||
|
|
||||||
|
- id: timer_countdown_alert
|
||||||
|
then:
|
||||||
|
- logger.log:
|
||||||
|
format: "TIMER COUNTDOWN - Shutdown: %.0fs, Start: %.0fs, Reboot: %.0fs (Active: %.0f, Fast Poll: %.0f)"
|
||||||
|
args:
|
||||||
|
- "id(ups_timer_shutdown).state"
|
||||||
|
- "id(ups_timer_start).state"
|
||||||
|
- "id(ups_timer_reboot).state"
|
||||||
|
- "id(active_timer_count).state"
|
||||||
|
- "id(fast_polling_status).state"
|
||||||
|
level: INFO
|
||||||
|
# Add critical countdown automation here (e.g., server shutdown at 30s)
|
||||||
|
|
||||||
|
- id: shutdown_warning_alert
|
||||||
|
then:
|
||||||
|
- if:
|
||||||
|
condition:
|
||||||
|
# Timer is actively counting down and less than 60 seconds
|
||||||
|
lambda: |-
|
||||||
|
float timer = id(ups_timer_shutdown).state;
|
||||||
|
return (!std::isnan(timer) && timer > 0 && timer < 60);
|
||||||
|
then:
|
||||||
|
- logger.log:
|
||||||
|
format: "CRITICAL: APC UPS SHUTTING DOWN IN %.0f SECONDS!"
|
||||||
|
args:
|
||||||
|
- "id(ups_timer_shutdown).state"
|
||||||
|
level: ERROR
|
||||||
|
# Add emergency server shutdown logic here
|
||||||
|
|
||||||
|
# Enhanced monitoring for APC timer functionality
|
||||||
|
interval:
|
||||||
|
# Monitor timer status every 15 seconds (component handles fast polling automatically)
|
||||||
|
- interval: 15s
|
||||||
|
then:
|
||||||
|
- script.execute: timer_countdown_alert
|
||||||
|
- script.execute: shutdown_warning_alert
|
||||||
|
|
||||||
|
# Expected Timer Behavior:
|
||||||
|
# Normal Operation:
|
||||||
|
# - ups_timer_shutdown: -1 (APC default inactive state)
|
||||||
|
# - ups_timer_start: -1 (APC reboot timer inactive)
|
||||||
|
# - ups_timer_reboot: -1 (same as shutdown)
|
||||||
|
# - active_timer_count: 0
|
||||||
|
# - fast_polling_status: 0
|
||||||
|
# - Component polls every 15 seconds (from device config)
|
||||||
|
|
||||||
|
# During UPS Shutdown:
|
||||||
|
# - ups_timer_shutdown: 20→19→18→17... (LIVE COUNTDOWN!)
|
||||||
|
# - Component automatically switches to 2-second polling
|
||||||
|
# - active_timer_count: 1
|
||||||
|
# - fast_polling_status: 1
|
||||||
|
# - Critical alerts trigger for emergency server shutdown
|
||||||
|
|
||||||
|
# During UPS Startup:
|
||||||
|
# - ups_timer_start: similar countdown behavior
|
||||||
|
# - Component uses 2-second fast polling
|
||||||
|
# - Coordination for sequential server startup
|
||||||
|
|
||||||
# Custom automations would be handled in Home Assistant
|
# Custom automations would be handled in Home Assistant
|
||||||
# The base sensors from base_ups.yaml and essential_sensors.yaml provide all the data needed
|
# The timer sensors enable sophisticated countdown-based automation
|
||||||
@@ -27,9 +27,15 @@ packages:
|
|||||||
# Extended monitoring (recommended for CyberPower - rich sensor data)
|
# Extended monitoring (recommended for CyberPower - rich sensor data)
|
||||||
extended: !include ../extended_sensors.yaml
|
extended: !include ../extended_sensors.yaml
|
||||||
|
|
||||||
|
# Timer countdown sensors (Real-time shutdown/startup monitoring)
|
||||||
|
timers: !include ../timer_sensors.yaml
|
||||||
|
|
||||||
# Control buttons (recommended - CyberPower has excellent beeper support)
|
# Control buttons (recommended - CyberPower has excellent beeper support)
|
||||||
controls: !include ../ups_controls.yaml
|
controls: !include ../ups_controls.yaml
|
||||||
|
|
||||||
|
# Delay configuration (Shutdown/Start/Reboot timers)
|
||||||
|
delay_config: !include ../delay_config.yaml
|
||||||
|
|
||||||
# CyberPower-specific optimizations
|
# CyberPower-specific optimizations
|
||||||
cyberpower_device: !include ../device_types/cyberpower_cp1500.yaml
|
cyberpower_device: !include ../device_types/cyberpower_cp1500.yaml
|
||||||
|
|
||||||
|
|||||||
@@ -105,40 +105,6 @@ sensor:
|
|||||||
state_class: measurement
|
state_class: measurement
|
||||||
accuracy_decimals: 0
|
accuracy_decimals: 0
|
||||||
|
|
||||||
# Enhanced dynamic timer sensors (countdown when active)
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "UPS Timer Shutdown"
|
|
||||||
id: ups_timer_shutdown
|
|
||||||
type: ups_timer_shutdown
|
|
||||||
unit_of_measurement: "s"
|
|
||||||
device_class: duration
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
# Negative values indicate no active countdown
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "UPS Timer Start"
|
|
||||||
id: ups_timer_start
|
|
||||||
type: ups_timer_start
|
|
||||||
unit_of_measurement: "s"
|
|
||||||
device_class: duration
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
# Negative values indicate no active countdown
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "UPS Timer Reboot"
|
|
||||||
id: ups_timer_reboot
|
|
||||||
type: ups_timer_reboot
|
|
||||||
unit_of_measurement: "s"
|
|
||||||
device_class: duration
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
# Negative values indicate no active countdown
|
|
||||||
|
|
||||||
# Enhanced battery threshold monitoring
|
# Enhanced battery threshold monitoring
|
||||||
- platform: ups_hid
|
- platform: ups_hid
|
||||||
ups_hid_id: ups_monitor
|
ups_hid_id: ups_monitor
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ external_components:
|
|||||||
type: local
|
type: local
|
||||||
path: ../../components
|
path: ../../components
|
||||||
|
|
||||||
|
# Timer functionality package
|
||||||
|
packages:
|
||||||
|
timers: !include ../timer_sensors.yaml
|
||||||
|
|
||||||
# UPS component only
|
# UPS component only
|
||||||
ups_hid:
|
ups_hid:
|
||||||
id: ups_monitor
|
id: ups_monitor
|
||||||
@@ -45,7 +49,7 @@ ups_hid:
|
|||||||
button:
|
button:
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
|
|
||||||
# Single sensor test
|
# Additional sensors for testing
|
||||||
sensor:
|
sensor:
|
||||||
- platform: ups_hid
|
- platform: ups_hid
|
||||||
ups_hid_id: ups_monitor
|
ups_hid_id: ups_monitor
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ packages:
|
|||||||
base_ups: !include ../base_ups.yaml
|
base_ups: !include ../base_ups.yaml
|
||||||
essential: !include ../essential_sensors.yaml
|
essential: !include ../essential_sensors.yaml
|
||||||
extended: !include ../extended_sensors.yaml
|
extended: !include ../extended_sensors.yaml
|
||||||
|
timers: !include ../timer_sensors.yaml
|
||||||
controls: !include ../ups_controls.yaml
|
controls: !include ../ups_controls.yaml
|
||||||
|
|
||||||
# Override for simulation testing
|
# Override for simulation testing
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
# Timer Sensors Package
|
||||||
|
# Real-time countdown timer sensors for UPS shutdown/startup sequences
|
||||||
|
# Use with: packages: timer_sensors: !include configs/timer_sensors.yaml
|
||||||
|
|
||||||
|
sensor:
|
||||||
|
# ==================== TIMER COUNTDOWN SENSORS ====================
|
||||||
|
# These sensors show REAL-TIME countdowns during UPS operations
|
||||||
|
# Normal operation: Negative values (e.g., -60, -120)
|
||||||
|
# Active countdown: Positive values (e.g., 60, 59, 58, 57...)
|
||||||
|
# Fast polling: Automatically switches to 2-second intervals during countdown
|
||||||
|
|
||||||
|
- platform: ups_hid
|
||||||
|
ups_hid_id: ups_monitor
|
||||||
|
name: "UPS Timer Shutdown"
|
||||||
|
id: ups_timer_shutdown
|
||||||
|
type: ups_timer_shutdown
|
||||||
|
unit_of_measurement: "s"
|
||||||
|
device_class: duration
|
||||||
|
state_class: measurement
|
||||||
|
accuracy_decimals: 0
|
||||||
|
icon: "mdi:timer-outline"
|
||||||
|
# Shows countdown during UPS shutdown sequence (e.g., 60→59→58...)
|
||||||
|
# Negative during normal operation (e.g., -60)
|
||||||
|
|
||||||
|
- platform: ups_hid
|
||||||
|
ups_hid_id: ups_monitor
|
||||||
|
name: "UPS Timer Start"
|
||||||
|
id: ups_timer_start
|
||||||
|
type: ups_timer_start
|
||||||
|
unit_of_measurement: "s"
|
||||||
|
device_class: duration
|
||||||
|
state_class: measurement
|
||||||
|
accuracy_decimals: 0
|
||||||
|
icon: "mdi:timer-outline"
|
||||||
|
# Shows countdown during UPS startup sequence (e.g., 120→119→118...)
|
||||||
|
# Negative during normal operation (e.g., -120)
|
||||||
|
|
||||||
|
- platform: ups_hid
|
||||||
|
ups_hid_id: ups_monitor
|
||||||
|
name: "UPS Timer Reboot"
|
||||||
|
id: ups_timer_reboot
|
||||||
|
type: ups_timer_reboot
|
||||||
|
unit_of_measurement: "s"
|
||||||
|
device_class: duration
|
||||||
|
state_class: measurement
|
||||||
|
accuracy_decimals: 0
|
||||||
|
icon: "mdi:timer-outline"
|
||||||
|
# Shows countdown during UPS reboot sequence
|
||||||
|
# Negative during normal operation
|
||||||
|
|
||||||
|
# ==================== TIMER ANALYSIS SENSORS ====================
|
||||||
|
# Template sensors to analyze timer behavior and fast polling state
|
||||||
|
|
||||||
|
- platform: template
|
||||||
|
name: "Active Timer Count"
|
||||||
|
id: active_timer_count
|
||||||
|
icon: "mdi:timer-alert-outline"
|
||||||
|
lambda: |-
|
||||||
|
// Count how many timers are actively counting down (positive values)
|
||||||
|
int active_count = 0;
|
||||||
|
float shutdown_timer = id(ups_timer_shutdown).state;
|
||||||
|
float start_timer = id(ups_timer_start).state;
|
||||||
|
float reboot_timer = id(ups_timer_reboot).state;
|
||||||
|
|
||||||
|
if (!std::isnan(shutdown_timer) && shutdown_timer > 0) active_count++;
|
||||||
|
if (!std::isnan(start_timer) && start_timer > 0) active_count++;
|
||||||
|
if (!std::isnan(reboot_timer) && reboot_timer > 0) active_count++;
|
||||||
|
|
||||||
|
return (float)active_count;
|
||||||
|
update_interval: 2s
|
||||||
|
# 0 = No active countdowns, >0 = Timers are counting down
|
||||||
|
|
||||||
|
- platform: template
|
||||||
|
name: "Fast Polling Status"
|
||||||
|
id: fast_polling_status
|
||||||
|
icon: "mdi:speedometer"
|
||||||
|
lambda: |-
|
||||||
|
// Indicates when fast polling should be active
|
||||||
|
float active_timers = id(active_timer_count).state;
|
||||||
|
return (!std::isnan(active_timers) && active_timers > 0) ? 1.0 : 0.0;
|
||||||
|
update_interval: 2s
|
||||||
|
# 1 = Fast polling active (2s intervals), 0 = Normal polling (30s intervals)
|
||||||
|
|
||||||
|
# ==================== EXPECTED BEHAVIOR ====================
|
||||||
|
# Normal Operation (Simulation/No UPS Activity):
|
||||||
|
# - ups_timer_shutdown: -60 (APC: -20, CyberPower: -60)
|
||||||
|
# - ups_timer_start: -120 (CyberPower), -60 (APC reboot timer)
|
||||||
|
# - ups_timer_reboot: -60 (varies by protocol)
|
||||||
|
# - ups_delay_shutdown: 60 (static config)
|
||||||
|
# - ups_delay_start: 120 (static config)
|
||||||
|
# - active_timer_count: 0
|
||||||
|
# - fast_polling_status: 0
|
||||||
|
|
||||||
|
# During UPS Shutdown Sequence (Real Hardware):
|
||||||
|
# - ups_timer_shutdown: 60→59→58→57... (live countdown!)
|
||||||
|
# - Component switches to 2-second polling automatically
|
||||||
|
# - active_timer_count: 1
|
||||||
|
# - fast_polling_status: 1
|
||||||
|
# - Home Assistant sees real-time countdown updates
|
||||||
|
|
||||||
|
# During UPS Startup Sequence (Real Hardware):
|
||||||
|
# - ups_timer_start: 120→119→118→117... (live countdown!)
|
||||||
|
# - Component uses fast polling (2-second intervals)
|
||||||
|
# - active_timer_count: 1
|
||||||
|
# - fast_polling_status: 1
|
||||||
Reference in new issue
Block a user