Public Access
feat(ups-hid): UPS and Battery test initial implementation
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
13 files changed
+790
-43
No files matched your search
@@ -5,11 +5,13 @@ A production-ready ESPHome component for monitoring UPS devices via USB connecti
|
||||
## Features
|
||||
|
||||
- 🔋 **Real-time UPS monitoring**: Battery level, voltages, load, runtime, status
|
||||
- 🧪 **UPS self-test control**: Battery tests (quick/deep), panel tests, with real-time result monitoring
|
||||
- 🔊 **Beeper control**: Enable/disable/mute/test UPS audible alarms via HID write operations
|
||||
- 🌈 **Visual status indicator**: RGB LED with customizable status colors
|
||||
- 🏠 **Home Assistant integration**: Automatic entity discovery via ESPHome API
|
||||
- 🔌 **Multi-protocol support**: APC HID, CyberPower HID, Generic HID
|
||||
- 🎯 **Auto-detection**: Intelligent protocol detection based on USB vendor IDs
|
||||
- 🔧 **USB recovery**: Enhanced ESP-IDF v5.3 connection handling with automatic recovery
|
||||
- 🛡️ **Production-ready**: Thread-safe, error rate limiting, comprehensive logging
|
||||
- 🧪 **Simulation mode**: Test integration without physical UPS device
|
||||
|
||||
@@ -682,6 +684,123 @@ text_sensor:
|
||||
icon: "mdi:volume-high"
|
||||
```
|
||||
|
||||
### UPS Test Control (Battery & Panel Tests)
|
||||
|
||||
The component supports UPS self-test operations compatible with Network UPS Tools (NUT) standard:
|
||||
|
||||
```yaml
|
||||
button:
|
||||
# Battery Tests
|
||||
- platform: ups_hid
|
||||
ups_hid_id: ups_monitor
|
||||
test_action: battery_quick # Quick battery test (10-15 seconds)
|
||||
name: "UPS Quick Battery Test"
|
||||
icon: "mdi:battery-plus"
|
||||
|
||||
- platform: ups_hid
|
||||
ups_hid_id: ups_monitor
|
||||
test_action: battery_deep # Deep battery test (2-5 minutes)
|
||||
name: "UPS Deep Battery Test"
|
||||
icon: "mdi:battery-charging"
|
||||
|
||||
- platform: ups_hid
|
||||
ups_hid_id: ups_monitor
|
||||
test_action: battery_stop # Stop any running battery test
|
||||
name: "UPS Stop Battery Test"
|
||||
icon: "mdi:battery-remove"
|
||||
|
||||
# Panel Tests
|
||||
- platform: ups_hid
|
||||
ups_hid_id: ups_monitor
|
||||
test_action: ups_test # Panel/system test (LEDs, buzzer)
|
||||
name: "UPS Panel Test"
|
||||
icon: "mdi:test-tube"
|
||||
|
||||
- platform: ups_hid
|
||||
ups_hid_id: ups_monitor
|
||||
test_action: ups_stop # Stop panel test
|
||||
name: "UPS Stop Panel Test"
|
||||
icon: "mdi:stop"
|
||||
```
|
||||
|
||||
**Available test actions:**
|
||||
- `battery_quick`: Quick battery capacity test (10-15 seconds)
|
||||
- `battery_deep`: Comprehensive battery test (2-5 minutes)
|
||||
- `battery_stop`: Abort any running battery test
|
||||
- `ups_test`: Test panel indicators and systems
|
||||
- `ups_stop`: Stop panel/system test
|
||||
|
||||
**Test Result Monitoring:**
|
||||
```yaml
|
||||
text_sensor:
|
||||
- platform: ups_hid
|
||||
ups_hid_id: ups_monitor
|
||||
type: ups_test_result # Current test status
|
||||
name: "UPS Test Result"
|
||||
icon: "mdi:test-tube"
|
||||
```
|
||||
|
||||
**Test Result Values (NUT Compatible):**
|
||||
- `"No test initiated"` - Default state
|
||||
- `"In progress"` - Test currently running
|
||||
- `"Done and passed"` - ✅ Test completed successfully
|
||||
- `"Done and warning"` - ⚠️ Test completed with warnings
|
||||
- `"Done and error"` - ❌ Test failed
|
||||
- `"Aborted"` - 🛑 Test was stopped/aborted
|
||||
- `"Test scheduled"` - 📅 Test scheduled but not started
|
||||
|
||||
**Protocol Support:**
|
||||
- ✅ **APC Devices**: Battery test (0x52), Panel test (0x79) - Full NUT compatibility
|
||||
- ✅ **CyberPower Devices**: Battery test (0x14), Generic test support
|
||||
- ⚠️ **Generic HID**: Multi-report fallback (device-dependent)
|
||||
|
||||
**Important Test Behavior:**
|
||||
- **Battery Tests**: UPS may switch to battery power briefly during testing
|
||||
- **Deep Tests**: Can take several minutes - don't interrupt
|
||||
- **Panel Tests**: Test LED indicators and audible alarms
|
||||
- **Automatic Results**: Test results update automatically in real-time
|
||||
- **State Persistence**: Test results persist until next test is initiated
|
||||
|
||||
**Testing Best Practices:**
|
||||
```yaml
|
||||
# Monitor test progress with automation
|
||||
automation:
|
||||
- alias: "UPS Test Completed"
|
||||
trigger:
|
||||
platform: state
|
||||
entity_id: text_sensor.ups_test_result
|
||||
from: "In progress"
|
||||
condition:
|
||||
condition: not
|
||||
conditions:
|
||||
- condition: state
|
||||
entity_id: text_sensor.ups_test_result
|
||||
state: "In progress"
|
||||
action:
|
||||
service: notify.mobile_app
|
||||
data:
|
||||
message: "UPS test completed: {{ states('text_sensor.ups_test_result') }}"
|
||||
```
|
||||
|
||||
**USB Connection Recovery:**
|
||||
|
||||
Enhanced connection handling for ESP-IDF v5.3 issues:
|
||||
|
||||
- **Automatic Recovery**: Handles `ESP_ERR_INVALID_STATE` USB library errors
|
||||
- **Connection Reuse**: Maintains existing connections when possible
|
||||
- **State Trust**: Trusts previous device configuration during ESP-IDF bugs
|
||||
- **Multi-Strategy**: Dual recovery approach for maximum reliability
|
||||
|
||||
```yaml
|
||||
# Enable detailed USB debugging if needed
|
||||
logger:
|
||||
level: DEBUG
|
||||
logs:
|
||||
ups_hid: DEBUG # Component debugging
|
||||
ups_hid.apc: DEBUG # APC protocol debugging
|
||||
ups_hid.cyberpower: DEBUG # CyberPower protocol debugging
|
||||
```
|
||||
|
||||
## Advanced Configuration
|
||||
|
||||
### Custom Protocol Priority
|
||||
|
||||
@@ -228,6 +228,11 @@ bool ApcHidProtocol::read_data(UpsData &data) {
|
||||
data.model.clear();
|
||||
}
|
||||
|
||||
// Set default test result
|
||||
if (data.ups_test_result.empty()) {
|
||||
data.ups_test_result = "No test initiated";
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -1102,6 +1107,13 @@ void ApcHidProtocol::read_missing_dynamic_values(UpsData &data) {
|
||||
ESP_LOGI(APC_HID_TAG, "APC Battery status: %s (%.0f%%)", data.battery_status.c_str(), data.battery_level);
|
||||
}
|
||||
|
||||
// 8. Test result reading (Report 0x52 - same as test command)
|
||||
// Based on NUT: "UPS.Battery.Test" maps to test result
|
||||
HidReport test_result_report;
|
||||
if (read_hid_report(0x52, test_result_report)) {
|
||||
parse_test_result_report(test_result_report, data);
|
||||
}
|
||||
|
||||
ESP_LOGD(APC_HID_TAG, "Completed reading APC missing dynamic values");
|
||||
}
|
||||
|
||||
@@ -1488,5 +1500,136 @@ bool ApcHidProtocol::beeper_test() {
|
||||
}
|
||||
}
|
||||
|
||||
// UPS and Battery Test implementations based on NUT analysis
|
||||
bool ApcHidProtocol::start_battery_test_quick() {
|
||||
ESP_LOGI(APC_HID_TAG, "Starting APC quick battery test");
|
||||
|
||||
// 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};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x52, test_data, sizeof(test_data));
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(APC_HID_TAG, "APC quick battery test command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(APC_HID_TAG, "Failed to send APC quick battery test: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ApcHidProtocol::start_battery_test_deep() {
|
||||
ESP_LOGI(APC_HID_TAG, "Starting APC deep battery test");
|
||||
|
||||
// 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};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x52, test_data, sizeof(test_data));
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(APC_HID_TAG, "APC deep battery test command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(APC_HID_TAG, "Failed to send APC deep battery test: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ApcHidProtocol::stop_battery_test() {
|
||||
ESP_LOGI(APC_HID_TAG, "Stopping APC battery test");
|
||||
|
||||
// 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};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x52, test_data, sizeof(test_data));
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(APC_HID_TAG, "APC battery test stop command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(APC_HID_TAG, "Failed to send APC battery test stop: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ApcHidProtocol::start_ups_test() {
|
||||
ESP_LOGI(APC_HID_TAG, "Starting APC UPS panel test");
|
||||
|
||||
// Based on NUT debug logs, APC uses report ID 0x79 for panel test
|
||||
// Command value 1 = Start panel test (based on NUT analysis)
|
||||
uint8_t test_data[2] = {0x79, 1};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x79, test_data, sizeof(test_data));
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(APC_HID_TAG, "APC UPS panel test command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(APC_HID_TAG, "Failed to send APC UPS panel test: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ApcHidProtocol::stop_ups_test() {
|
||||
ESP_LOGI(APC_HID_TAG, "Stopping APC UPS panel test");
|
||||
|
||||
// Based on NUT debug logs, APC uses report ID 0x79 for panel test
|
||||
// Command value 0 = Stop/abort panel test (based on NUT analysis)
|
||||
uint8_t test_data[2] = {0x79, 0};
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x79, test_data, sizeof(test_data));
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(APC_HID_TAG, "APC UPS panel test stop command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(APC_HID_TAG, "Failed to send APC UPS panel test stop: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void ApcHidProtocol::parse_test_result_report(const HidReport &report, UpsData &data) {
|
||||
if (report.data.size() < 2) {
|
||||
ESP_LOGW(APC_HID_TAG, "Test result report too short: %zu bytes", report.data.size());
|
||||
data.ups_test_result = "Error reading test result";
|
||||
return;
|
||||
}
|
||||
|
||||
// Based on NUT test_read_info lookup table:
|
||||
// 1 = "Done and passed", 2 = "Done and warning", 3 = "Done and error",
|
||||
// 4 = "Aborted", 5 = "In progress", 6 = "No test initiated", 7 = "Test scheduled"
|
||||
uint8_t test_result_value = report.data[1];
|
||||
|
||||
ESP_LOGD(APC_HID_TAG, "Raw test result from report 0x52: 0x%02X (%d)", test_result_value, test_result_value);
|
||||
|
||||
switch (test_result_value) {
|
||||
case 1:
|
||||
data.ups_test_result = "Done and passed";
|
||||
break;
|
||||
case 2:
|
||||
data.ups_test_result = "Done and warning";
|
||||
break;
|
||||
case 3:
|
||||
data.ups_test_result = "Done and error";
|
||||
break;
|
||||
case 4:
|
||||
data.ups_test_result = "Aborted";
|
||||
break;
|
||||
case 5:
|
||||
data.ups_test_result = "In progress";
|
||||
break;
|
||||
case 6:
|
||||
data.ups_test_result = "No test initiated";
|
||||
break;
|
||||
case 7:
|
||||
data.ups_test_result = "Test scheduled";
|
||||
break;
|
||||
default:
|
||||
data.ups_test_result = "Unknown test result (" + std::to_string(test_result_value) + ")";
|
||||
ESP_LOGW(APC_HID_TAG, "Unknown APC test result value: %d", test_result_value);
|
||||
break;
|
||||
}
|
||||
|
||||
ESP_LOGI(APC_HID_TAG, "APC Test result: %s (raw: %d)", data.ups_test_result.c_str(), test_result_value);
|
||||
}
|
||||
|
||||
} // namespace ups_hid
|
||||
} // namespace esphome
|
||||
@@ -21,6 +21,13 @@ public:
|
||||
bool beeper_disable() override;
|
||||
bool beeper_mute() override;
|
||||
bool beeper_test() override;
|
||||
|
||||
// UPS and battery test methods
|
||||
bool start_battery_test_quick() override;
|
||||
bool start_battery_test_deep() override;
|
||||
bool stop_battery_test() override;
|
||||
bool start_ups_test() override;
|
||||
bool stop_ups_test() override;
|
||||
|
||||
private:
|
||||
struct HidReport {
|
||||
@@ -67,6 +74,7 @@ private:
|
||||
void parse_ups_delay_shutdown_report(const HidReport &report, UpsData &data);
|
||||
void parse_battery_charge_threshold_report(const HidReport &report, UpsData &data, bool is_low_threshold);
|
||||
void parse_battery_chemistry_report(const HidReport &report, UpsData &data);
|
||||
void parse_test_result_report(const HidReport &report, UpsData &data);
|
||||
std::string convert_apc_date(uint16_t date_value);
|
||||
};
|
||||
|
||||
|
||||
@@ -8,7 +8,11 @@ static const char *const BUTTON_TAG = "ups_hid.button";
|
||||
|
||||
void UpsHidButton::dump_config() {
|
||||
ESP_LOGCONFIG(BUTTON_TAG, "UPS HID Button:");
|
||||
ESP_LOGCONFIG(BUTTON_TAG, " Beeper action: %s", beeper_action_.c_str());
|
||||
if (button_type_ == BUTTON_TYPE_BEEPER) {
|
||||
ESP_LOGCONFIG(BUTTON_TAG, " Beeper action: %s", beeper_action_.c_str());
|
||||
} else if (button_type_ == BUTTON_TYPE_TEST) {
|
||||
ESP_LOGCONFIG(BUTTON_TAG, " Test action: %s", test_action_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void UpsHidButton::press_action() {
|
||||
@@ -18,47 +22,75 @@ void UpsHidButton::press_action() {
|
||||
}
|
||||
|
||||
if (!parent_->is_connected()) {
|
||||
ESP_LOGW(BUTTON_TAG, "UPS not connected, cannot execute beeper action: %s", beeper_action_.c_str());
|
||||
if (button_type_ == BUTTON_TYPE_BEEPER) {
|
||||
ESP_LOGW(BUTTON_TAG, "UPS not connected, cannot execute beeper action: %s", beeper_action_.c_str());
|
||||
} else {
|
||||
ESP_LOGW(BUTTON_TAG, "UPS not connected, cannot execute test action: %s", test_action_.c_str());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(BUTTON_TAG, "Executing beeper action: %s", beeper_action_.c_str());
|
||||
|
||||
// Get the current protocol from the parent component
|
||||
UpsData ups_data = parent_->get_ups_data();
|
||||
if (ups_data.detected_protocol == PROTOCOL_UNKNOWN) {
|
||||
ESP_LOGW(BUTTON_TAG, "UPS protocol not detected, cannot execute beeper action");
|
||||
ESP_LOGW(BUTTON_TAG, "UPS protocol not detected, cannot execute button action");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the active protocol and call the appropriate beeper method
|
||||
bool success = false;
|
||||
|
||||
// Access the active protocol through the parent's internal methods
|
||||
// Note: This requires adding a public method to get the active protocol
|
||||
// Get the active protocol
|
||||
auto active_protocol = parent_->get_active_protocol();
|
||||
if (!active_protocol) {
|
||||
ESP_LOGE(BUTTON_TAG, "No active protocol available");
|
||||
return;
|
||||
}
|
||||
|
||||
if (beeper_action_ == "enable") {
|
||||
success = active_protocol->beeper_enable();
|
||||
} else if (beeper_action_ == "disable") {
|
||||
success = active_protocol->beeper_disable();
|
||||
} else if (beeper_action_ == "mute") {
|
||||
success = active_protocol->beeper_mute();
|
||||
} else if (beeper_action_ == "test") {
|
||||
success = active_protocol->beeper_test();
|
||||
} else {
|
||||
ESP_LOGE(BUTTON_TAG, "Unknown beeper action: %s", beeper_action_.c_str());
|
||||
return;
|
||||
}
|
||||
bool success = false;
|
||||
|
||||
if (button_type_ == BUTTON_TYPE_BEEPER) {
|
||||
ESP_LOGI(BUTTON_TAG, "Executing beeper action: %s", beeper_action_.c_str());
|
||||
|
||||
if (beeper_action_ == "enable") {
|
||||
success = active_protocol->beeper_enable();
|
||||
} else if (beeper_action_ == "disable") {
|
||||
success = active_protocol->beeper_disable();
|
||||
} else if (beeper_action_ == "mute") {
|
||||
success = active_protocol->beeper_mute();
|
||||
} else if (beeper_action_ == "test") {
|
||||
success = active_protocol->beeper_test();
|
||||
} else {
|
||||
ESP_LOGE(BUTTON_TAG, "Unknown beeper action: %s", beeper_action_.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
ESP_LOGI(BUTTON_TAG, "Beeper action '%s' executed successfully", beeper_action_.c_str());
|
||||
} else {
|
||||
ESP_LOGW(BUTTON_TAG, "Failed to execute beeper action: %s", beeper_action_.c_str());
|
||||
if (success) {
|
||||
ESP_LOGI(BUTTON_TAG, "Beeper action '%s' executed successfully", beeper_action_.c_str());
|
||||
} else {
|
||||
ESP_LOGW(BUTTON_TAG, "Failed to execute beeper action: %s", beeper_action_.c_str());
|
||||
}
|
||||
}
|
||||
else if (button_type_ == BUTTON_TYPE_TEST) {
|
||||
ESP_LOGI(BUTTON_TAG, "Executing test action: %s", test_action_.c_str());
|
||||
|
||||
if (test_action_ == "battery_quick") {
|
||||
success = active_protocol->start_battery_test_quick();
|
||||
} else if (test_action_ == "battery_deep") {
|
||||
success = active_protocol->start_battery_test_deep();
|
||||
} else if (test_action_ == "battery_stop") {
|
||||
success = active_protocol->stop_battery_test();
|
||||
} else if (test_action_ == "ups_test") {
|
||||
success = active_protocol->start_ups_test();
|
||||
} else if (test_action_ == "ups_stop") {
|
||||
success = active_protocol->stop_ups_test();
|
||||
} else {
|
||||
ESP_LOGE(BUTTON_TAG, "Unknown test action: %s", test_action_.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
ESP_LOGI(BUTTON_TAG, "Test action '%s' executed successfully", test_action_.c_str());
|
||||
} else {
|
||||
ESP_LOGW(BUTTON_TAG, "Failed to execute test action: %s", test_action_.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,18 +7,41 @@
|
||||
namespace esphome {
|
||||
namespace ups_hid {
|
||||
|
||||
enum UpsHidBeeperAction {
|
||||
UPS_HID_BEEPER_ENABLE,
|
||||
UPS_HID_BEEPER_DISABLE,
|
||||
UPS_HID_BEEPER_MUTE,
|
||||
UPS_HID_BEEPER_TEST
|
||||
};
|
||||
|
||||
enum UpsHidTestAction {
|
||||
UPS_HID_TEST_BATTERY_QUICK,
|
||||
UPS_HID_TEST_BATTERY_DEEP,
|
||||
UPS_HID_TEST_BATTERY_STOP,
|
||||
UPS_HID_TEST_UPS_TEST,
|
||||
UPS_HID_TEST_UPS_STOP
|
||||
};
|
||||
|
||||
class UpsHidButton : public button::Button, public Component {
|
||||
public:
|
||||
void set_ups_hid_parent(UpsHidComponent *parent) { parent_ = parent; }
|
||||
void set_beeper_action(const std::string &action) { beeper_action_ = action; }
|
||||
void set_beeper_action(const std::string &action) { beeper_action_ = action; button_type_ = BUTTON_TYPE_BEEPER; }
|
||||
void set_test_action(const std::string &action) { test_action_ = action; button_type_ = BUTTON_TYPE_TEST; }
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
void press_action() override;
|
||||
|
||||
enum ButtonType {
|
||||
BUTTON_TYPE_BEEPER,
|
||||
BUTTON_TYPE_TEST
|
||||
};
|
||||
|
||||
UpsHidComponent *parent_{nullptr};
|
||||
std::string beeper_action_{};
|
||||
std::string test_action_{};
|
||||
ButtonType button_type_{BUTTON_TYPE_BEEPER};
|
||||
};
|
||||
|
||||
} // namespace ups_hid
|
||||
|
||||
@@ -9,6 +9,7 @@ DEPENDENCIES = ["ups_hid"]
|
||||
UpsHidButton = ups_hid_ns.class_("UpsHidButton", button.Button, cg.Component)
|
||||
|
||||
CONF_BEEPER_ACTION = "beeper_action"
|
||||
CONF_TEST_ACTION = "test_action"
|
||||
|
||||
BEEPER_ACTIONS = {
|
||||
"enable": "enable",
|
||||
@@ -17,10 +18,35 @@ BEEPER_ACTIONS = {
|
||||
"test": "test"
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = button.button_schema(UpsHidButton).extend({
|
||||
cv.GenerateID(CONF_UPS_HID_ID): cv.use_id(UpsHidComponent),
|
||||
cv.Required(CONF_BEEPER_ACTION): cv.enum(BEEPER_ACTIONS, lower=True),
|
||||
}).extend(cv.COMPONENT_SCHEMA)
|
||||
TEST_ACTIONS = {
|
||||
"battery_quick": "battery_quick",
|
||||
"battery_deep": "battery_deep",
|
||||
"battery_stop": "battery_stop",
|
||||
"ups_test": "ups_test",
|
||||
"ups_stop": "ups_stop"
|
||||
}
|
||||
|
||||
# Support either beeper_action or test_action, but not both
|
||||
def validate_button_config(config):
|
||||
beeper_action = config.get(CONF_BEEPER_ACTION)
|
||||
test_action = config.get(CONF_TEST_ACTION)
|
||||
|
||||
if beeper_action is not None and test_action is not None:
|
||||
raise cv.Invalid("Cannot specify both 'beeper_action' and 'test_action' on the same button")
|
||||
|
||||
if beeper_action is None and test_action is None:
|
||||
raise cv.Invalid("Must specify either 'beeper_action' or 'test_action'")
|
||||
|
||||
return config
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
button.button_schema(UpsHidButton).extend({
|
||||
cv.GenerateID(CONF_UPS_HID_ID): cv.use_id(UpsHidComponent),
|
||||
cv.Optional(CONF_BEEPER_ACTION): cv.enum(BEEPER_ACTIONS, lower=True),
|
||||
cv.Optional(CONF_TEST_ACTION): cv.enum(TEST_ACTIONS, lower=True),
|
||||
}).extend(cv.COMPONENT_SCHEMA),
|
||||
validate_button_config,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
@@ -29,4 +55,9 @@ async def to_code(config):
|
||||
|
||||
parent = await cg.get_variable(config[CONF_UPS_HID_ID])
|
||||
cg.add(var.set_ups_hid_parent(parent))
|
||||
cg.add(var.set_beeper_action(config[CONF_BEEPER_ACTION]))
|
||||
|
||||
# Set action based on which type was configured
|
||||
if CONF_BEEPER_ACTION in config:
|
||||
cg.add(var.set_beeper_action(config[CONF_BEEPER_ACTION]))
|
||||
elif CONF_TEST_ACTION in config:
|
||||
cg.add(var.set_test_action(config[CONF_TEST_ACTION]))
|
||||
@@ -1055,5 +1055,68 @@ bool CyberPowerProtocol::beeper_test() {
|
||||
}
|
||||
}
|
||||
|
||||
// Test control methods implementation (based on NUT CPS-HID driver analysis)
|
||||
bool CyberPowerProtocol::start_battery_test_quick() {
|
||||
ESP_LOGI(CP_TAG, "Initiating quick battery test");
|
||||
|
||||
// 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
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x14, test_data, 2);
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "Quick battery test command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to send quick battery test command: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x14, test_data, 2);
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "Deep battery test command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to send deep battery test command: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, 0x14, test_data, 2);
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(CP_TAG, "Battery test stop command sent successfully");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to send battery test stop command: %s", esp_err_to_name(ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::start_ups_test() {
|
||||
// CyberPower doesn't seem to have separate UPS test from battery test
|
||||
// Redirect to battery test quick as the closest equivalent
|
||||
ESP_LOGI(CP_TAG, "CyberPower UPS test redirected to quick battery test");
|
||||
return start_battery_test_quick();
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::stop_ups_test() {
|
||||
// Redirect to battery test stop
|
||||
ESP_LOGI(CP_TAG, "CyberPower UPS test stop redirected to battery test stop");
|
||||
return stop_battery_test();
|
||||
}
|
||||
|
||||
} // namespace ups_hid
|
||||
} // namespace esphome
|
||||
@@ -32,6 +32,13 @@ class CyberPowerProtocol : public UpsProtocolBase {
|
||||
bool beeper_disable() override;
|
||||
bool beeper_mute() override;
|
||||
bool beeper_test() override;
|
||||
|
||||
// UPS and battery test methods
|
||||
bool start_battery_test_quick() override;
|
||||
bool start_battery_test_deep() override;
|
||||
bool stop_battery_test() override;
|
||||
bool start_ups_test() override;
|
||||
bool stop_ups_test() override;
|
||||
|
||||
private:
|
||||
// Report ID constants (based on NUT debug logs)
|
||||
|
||||
@@ -221,6 +221,9 @@ bool GenericHidProtocol::read_data(UpsData &data) {
|
||||
data.status_flags = UPS_STATUS_ONLINE;
|
||||
}
|
||||
|
||||
// Set default test result
|
||||
data.ups_test_result = "No test initiated";
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -575,5 +578,132 @@ void GenericHidProtocol::parse_input_sensitivity(uint8_t* data, size_t len, UpsD
|
||||
}
|
||||
}
|
||||
|
||||
// Generic HID test implementations (basic functionality for unknown devices)
|
||||
bool GenericHidProtocol::start_battery_test_quick() {
|
||||
ESP_LOGI(GEN_TAG, "Starting Generic HID quick battery test");
|
||||
|
||||
// Try common test report IDs used by various UPS vendors
|
||||
// Based on NUT analysis: report IDs 0x14 (CyberPower), 0x52 (APC), and common alternatives
|
||||
uint8_t test_report_ids[] = {0x14, 0x52, 0x0f, 0x1a};
|
||||
|
||||
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
|
||||
|
||||
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));
|
||||
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(GEN_TAG, "Generic quick battery test command sent with report ID 0x%02X", report_id);
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGD(GEN_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGW(GEN_TAG, "Failed to send generic quick battery test with all tried report IDs");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GenericHidProtocol::start_battery_test_deep() {
|
||||
ESP_LOGI(GEN_TAG, "Starting Generic HID deep battery test");
|
||||
|
||||
// Try common test report IDs used by various UPS vendors
|
||||
uint8_t test_report_ids[] = {0x14, 0x52, 0x0f, 0x1a};
|
||||
|
||||
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
|
||||
|
||||
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));
|
||||
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(GEN_TAG, "Generic deep battery test command sent with report ID 0x%02X", report_id);
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGD(GEN_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGW(GEN_TAG, "Failed to send generic deep battery test with all tried report IDs");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GenericHidProtocol::stop_battery_test() {
|
||||
ESP_LOGI(GEN_TAG, "Stopping Generic HID battery test");
|
||||
|
||||
// Try common test report IDs used by various UPS vendors
|
||||
uint8_t test_report_ids[] = {0x14, 0x52, 0x0f, 0x1a};
|
||||
|
||||
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
|
||||
|
||||
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));
|
||||
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(GEN_TAG, "Generic battery test stop command sent with report ID 0x%02X", report_id);
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGD(GEN_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGW(GEN_TAG, "Failed to send generic battery test stop with all tried report IDs");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GenericHidProtocol::start_ups_test() {
|
||||
ESP_LOGI(GEN_TAG, "Starting Generic HID UPS test");
|
||||
|
||||
// Try common panel test report IDs (less standardized than battery test)
|
||||
uint8_t test_report_ids[] = {0x79, 0x0c, 0x1f, 0x15};
|
||||
|
||||
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 = Start test
|
||||
|
||||
ESP_LOGD(GEN_TAG, "Trying UPS test with report ID 0x%02X", report_id);
|
||||
esp_err_t ret = parent_->hid_set_report(0x03, report_id, test_data, sizeof(test_data));
|
||||
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(GEN_TAG, "Generic UPS test command sent with report ID 0x%02X", report_id);
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGD(GEN_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGW(GEN_TAG, "Failed to send generic UPS test with all tried report IDs");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GenericHidProtocol::stop_ups_test() {
|
||||
ESP_LOGI(GEN_TAG, "Stopping Generic HID UPS test");
|
||||
|
||||
// Try common panel test report IDs
|
||||
uint8_t test_report_ids[] = {0x79, 0x0c, 0x1f, 0x15};
|
||||
|
||||
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, 0}; // Command value 0 = Stop test
|
||||
|
||||
ESP_LOGD(GEN_TAG, "Trying UPS 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));
|
||||
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(GEN_TAG, "Generic UPS test stop command sent with report ID 0x%02X", report_id);
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGD(GEN_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret));
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGW(GEN_TAG, "Failed to send generic UPS test stop with all tried report IDs");
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace ups_hid
|
||||
} // namespace esphome
|
||||
@@ -1111,12 +1111,32 @@ namespace esphome
|
||||
{
|
||||
if (ret == ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGW(TAG, "USB Host Library in invalid state for device %d", dev_addr_list[i]);
|
||||
ESP_LOGI(TAG, "This is a known ESP-IDF v5.3 issue");
|
||||
ESP_LOGI(TAG, "This is a known ESP-IDF v5.3 issue - attempting recovery");
|
||||
|
||||
// Simple handling: just skip the device and continue
|
||||
// The ESP-IDF v5.3 USB Host Library has fundamental bugs with device state
|
||||
// management that cannot be reliably worked around. The best approach is
|
||||
// to log the issue and continue operation.
|
||||
// RECOVERY STRATEGY 1: Try to reuse existing connection if available
|
||||
if (usb_device_.dev_hdl != nullptr && usb_device_.dev_addr == dev_addr_list[i]) {
|
||||
ESP_LOGI(TAG, "Reusing existing connection for device address %d", dev_addr_list[i]);
|
||||
// Check if we can still communicate with the device
|
||||
if (test_device_communication()) {
|
||||
device_connected_ = true;
|
||||
ESP_LOGI(TAG, "Successfully recovered USB connection");
|
||||
return ESP_OK;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Existing connection no longer functional");
|
||||
}
|
||||
}
|
||||
|
||||
// RECOVERY STRATEGY 2: If we had a working UPS before and there's only one device,
|
||||
// assume it's the same UPS and trust the previous configuration
|
||||
if (num_dev == 1 && usb_device_.vid != 0 && usb_device_.pid != 0) {
|
||||
ESP_LOGI(TAG, "Single device with ESP_ERR_INVALID_STATE - trusting previous UPS config");
|
||||
ESP_LOGI(TAG, "Previous UPS was: VID=0x%04X, PID=0x%04X", usb_device_.vid, usb_device_.pid);
|
||||
// Mark as connected but don't update device handle (keep existing one)
|
||||
device_connected_ = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// If both recovery strategies failed, continue to try next device
|
||||
continue;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to open USB device %d: %s", dev_addr_list[i], esp_err_to_name(ret));
|
||||
@@ -1951,6 +1971,92 @@ namespace esphome
|
||||
}
|
||||
}
|
||||
|
||||
// Test control method implementations
|
||||
bool UpsHidComponent::start_battery_test_quick()
|
||||
{
|
||||
if (!connected_ || !active_protocol_) {
|
||||
ESP_LOGW(TAG, "Cannot start battery test: UPS not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Starting quick battery test");
|
||||
bool result = active_protocol_->start_battery_test_quick();
|
||||
if (result) {
|
||||
ESP_LOGI(TAG, "Quick battery test initiated successfully");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to start quick battery test");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool UpsHidComponent::start_battery_test_deep()
|
||||
{
|
||||
if (!connected_ || !active_protocol_) {
|
||||
ESP_LOGW(TAG, "Cannot start battery test: UPS not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Starting deep battery test");
|
||||
bool result = active_protocol_->start_battery_test_deep();
|
||||
if (result) {
|
||||
ESP_LOGI(TAG, "Deep battery test initiated successfully");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to start deep battery test");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool UpsHidComponent::stop_battery_test()
|
||||
{
|
||||
if (!connected_ || !active_protocol_) {
|
||||
ESP_LOGW(TAG, "Cannot stop battery test: UPS not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Stopping battery test");
|
||||
bool result = active_protocol_->stop_battery_test();
|
||||
if (result) {
|
||||
ESP_LOGI(TAG, "Battery test stopped successfully");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to stop battery test");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool UpsHidComponent::start_ups_test()
|
||||
{
|
||||
if (!connected_ || !active_protocol_) {
|
||||
ESP_LOGW(TAG, "Cannot start UPS test: UPS not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Starting UPS test");
|
||||
bool result = active_protocol_->start_ups_test();
|
||||
if (result) {
|
||||
ESP_LOGI(TAG, "UPS test initiated successfully");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to start UPS test");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool UpsHidComponent::stop_ups_test()
|
||||
{
|
||||
if (!connected_ || !active_protocol_) {
|
||||
ESP_LOGW(TAG, "Cannot stop UPS test: UPS not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Stopping UPS test");
|
||||
bool result = active_protocol_->stop_ups_test();
|
||||
if (result) {
|
||||
ESP_LOGI(TAG, "UPS test stopped successfully");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to stop UPS test");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Asynchronous HID GET_REPORT operation
|
||||
esp_err_t UpsHidComponent::hid_get_report_async(uint8_t report_type, uint8_t report_id)
|
||||
{
|
||||
@@ -2015,6 +2121,30 @@ namespace esphome
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool UpsHidComponent::test_device_communication() {
|
||||
if (!usb_device_.dev_hdl || !active_protocol_) {
|
||||
ESP_LOGD(TAG, "Cannot test communication: no device handle or protocol");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Testing device communication...");
|
||||
|
||||
// Try a simple HID communication test using the active protocol
|
||||
UpsData test_data;
|
||||
test_data.reset(); // Clear all data
|
||||
|
||||
// Attempt to read some data from the device
|
||||
bool success = active_protocol_->read_data(test_data);
|
||||
|
||||
if (success) {
|
||||
ESP_LOGD(TAG, "Device communication test successful");
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Device communication test failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace ups_hid
|
||||
|
||||
@@ -234,6 +234,13 @@ namespace esphome
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Test control methods
|
||||
bool start_battery_test_quick();
|
||||
bool start_battery_test_deep();
|
||||
bool stop_battery_test();
|
||||
bool start_ups_test();
|
||||
bool stop_ups_test();
|
||||
|
||||
// Sensor registration methods
|
||||
void register_sensor(sensor::Sensor *sens, const std::string &type);
|
||||
@@ -279,6 +286,7 @@ namespace esphome
|
||||
bool read_ups_data();
|
||||
void update_sensors();
|
||||
void simulate_ups_data();
|
||||
bool test_device_communication();
|
||||
|
||||
// Error rate limiting helpers
|
||||
bool should_log_error(ErrorRateLimit& limiter);
|
||||
@@ -385,6 +393,13 @@ namespace esphome
|
||||
virtual bool beeper_disable() { return false; }
|
||||
virtual bool beeper_mute() { return false; }
|
||||
virtual bool beeper_test() { return false; }
|
||||
|
||||
// UPS and battery test methods
|
||||
virtual bool start_battery_test_quick() { return false; }
|
||||
virtual bool start_battery_test_deep() { return false; }
|
||||
virtual bool stop_battery_test() { return false; }
|
||||
virtual bool start_ups_test() { return false; }
|
||||
virtual bool stop_ups_test() { return false; }
|
||||
|
||||
protected:
|
||||
UpsHidComponent *parent_;
|
||||
@@ -406,6 +421,13 @@ namespace esphome
|
||||
UpsProtocol get_protocol_type() const override { return PROTOCOL_GENERIC_HID; }
|
||||
std::string get_protocol_name() const override { return "Generic HID"; }
|
||||
|
||||
// Test method overrides
|
||||
bool start_battery_test_quick() override;
|
||||
bool start_battery_test_deep() override;
|
||||
bool stop_battery_test() override;
|
||||
bool start_ups_test() override;
|
||||
bool stop_ups_test() override;
|
||||
|
||||
private:
|
||||
// Report discovery and caching
|
||||
std::set<uint8_t> available_input_reports_;
|
||||
|
||||
Reference in new issue
Block a user