diff --git a/README.md b/README.md index 4c9010c..5b5d07a 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Monitor UPS devices via direct USB connection on ESP32-S3. Supports APC, CyberPo - Real-time UPS monitoring (battery, voltage, load, runtime) - Multi-protocol support (APC HID, CyberPower HID, Generic HID) - Auto-detection of UPS protocols +- **Beeper control** (enable/disable/mute/test) via HID write operations - RGB LED status indicator support - Home Assistant integration - Simulation mode for testing @@ -84,6 +85,17 @@ binary_sensor: ups_hid_id: ups_monitor type: online name: "UPS Online" + +# Control UPS beeper +button: + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: enable + name: "UPS Beeper Enable" + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: mute + name: "UPS Beeper Mute" ``` ## Development diff --git a/beeper-control-test.yaml b/beeper-control-test.yaml new file mode 100644 index 0000000..1221813 --- /dev/null +++ b/beeper-control-test.yaml @@ -0,0 +1,128 @@ +esphome: + name: ups-hid-beeper-test + friendly_name: "UPS HID Beeper Test" + comment: "USB Beeper Control Test" + +esp32: + board: esp32-s3-devkitc-1 + variant: esp32s3 + framework: + type: esp-idf + version: 5.3.2 + +# Enable logging +logger: + level: DEBUG + baud_rate: 115200 + hardware_uart: UART0 + logs: + ups_hid: DEBUG + ups_hid.apc: DEBUG + ups_hid.apc_hid: DEBUG + ups_hid.generic: DEBUG + usb_host: DEBUG + esp32.usb_host: DEBUG + usb: DEBUG + usb-otg: DEBUG + HCD: DEBUG + USBH: DEBUG + +# Enable Home Assistant API +api: + encryption: + key: !secret api_encryption_key + +ota: + - platform: esphome + password: !secret ota_password + +wifi: + ssid: !secret wifi_ssid + password: !secret wifi_password + ap: + ssid: "UPS-HID-Monitor" + password: !secret ap_password + +captive_portal: +web_server: + +# RGB LED for status indication +light: + - platform: esp32_rmt_led_strip + chipset: WS2812 + pin: GPIO48 + num_leds: 1 + rgb_order: GRB + name: "UPS Status LED" + id: status_led + effects: + - strobe: + name: "Critical Strobe" + colors: + - state: true + brightness: 100% + red: 100% + green: 0% + blue: 0% + duration: 250ms + - state: false + duration: 250ms +# External components +external_components: + - source: + type: local + path: components + +ups_hid: + id: ups_monitor + update_interval: 30s + auto_detect_protocol: true + protocol_timeout: 15s + +# Beeper control buttons +button: + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: enable + name: "UPS Beeper Enable" + icon: "mdi:volume-high" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: disable + name: "UPS Beeper Disable" + icon: "mdi:volume-off" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: mute + name: "UPS Beeper Mute" + icon: "mdi:volume-mute" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: test + name: "UPS Beeper Test" + icon: "mdi:bell-ring" + +# Status sensors for monitoring +text_sensor: + - platform: ups_hid + ups_hid_id: ups_monitor + type: ups_beeper_status + name: "UPS Beeper Status" + +sensor: + - platform: ups_hid + ups_hid_id: ups_monitor + type: battery_level + name: "UPS Battery Level" + unit_of_measurement: "%" + device_class: battery + +binary_sensor: + - platform: ups_hid + ups_hid_id: ups_monitor + type: online + name: "UPS Online" + device_class: power \ No newline at end of file diff --git a/components/ups_hid/README.md b/components/ups_hid/README.md index 88f25e7..3189556 100644 --- a/components/ups_hid/README.md +++ b/components/ups_hid/README.md @@ -5,6 +5,7 @@ A production-ready ESPHome component for monitoring UPS devices via USB connecti ## Features - 🔋 **Real-time UPS monitoring**: Battery level, voltages, load, runtime, status +- 🔊 **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 @@ -283,6 +284,40 @@ text_sensor: ups_hid_id: ups_monitor type: status name: "UPS Status" + + # Beeper status monitoring + - platform: ups_hid + ups_hid_id: ups_monitor + type: ups_beeper_status + name: "UPS Beeper Status" + icon: "mdi:volume-high" + +# UPS Beeper Control +button: + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: enable + name: "UPS Beeper Enable" + icon: "mdi:volume-high" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: disable + name: "UPS Beeper Disable" + icon: "mdi:volume-off" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: mute + name: "UPS Beeper Mute" + icon: "mdi:volume-mute" + # Note: Mute only works during active alarm conditions + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: test + name: "UPS Beeper Test" + icon: "mdi:bell-ring" # Automated LED status control based on UPS state interval: @@ -357,21 +392,25 @@ GND → RGB LED GND ### Tested Compatible Models -| Vendor | Models | Protocol | Vendor ID | -|--------|--------|----------|-----------| -| **APC** | Back-UPS ES Series, Smart-UPS | APC HID | 0x051D | -| **CyberPower** | CP1500EPFCLCD, CP1000PFCLCD | CyberPower HID | 0x0764 | -| **Tripp Lite** | SMART1500LCDT, UPS series | Generic HID | 0x09AE | -| **Eaton/MGE** | Ellipse, Evolution series | Generic HID | 0x06DA | -| **Belkin** | Older USB UPS models | Generic HID | 0x050D | +| Vendor | Models | Protocol | Vendor ID | Beeper Control | +|--------|--------|----------|-----------|----------------| +| **APC** | Back-UPS ES Series, Smart-UPS | APC HID | 0x051D | ✅ Confirmed | +| **CyberPower** | CP1500EPFCLCD, CP1000PFCLCD | CyberPower HID | 0x0764 | ✅ Confirmed | +| **Tripp Lite** | SMART1500LCDT, UPS series | Generic HID | 0x09AE | ⚠️ Limited | +| **Eaton/MGE** | Ellipse, Evolution series | Generic HID | 0x06DA | ⚠️ Limited | +| **Belkin** | Older USB UPS models | Generic HID | 0x050D | ⚠️ Limited | + +**Beeper Control Legend:** +- ✅ **Confirmed**: Full beeper control tested and working (enable/disable/mute/test) +- ⚠️ **Limited**: Basic support via generic HID (device-dependent functionality) ### Protocol Compatibility Matrix -| Protocol | Communication | Auto-Detection | Features | -|----------|---------------|----------------|----------| -| **APC HID** | USB HID reports | ✅ | Modern APC, enhanced features | -| **CyberPower HID** | Vendor-specific HID | ✅ | CyberPower optimized | -| **Generic HID** | Standard HID-PDC | ✅ | Universal fallback | +| Protocol | Communication | Auto-Detection | Read Features | Write Features | +|----------|---------------|----------------|---------------|----------------| +| **APC HID** | USB HID reports | ✅ | Battery, voltage, status | ✅ Beeper control | +| **CyberPower HID** | Vendor-specific HID | ✅ | Extended sensors, config | ✅ Beeper control | +| **Generic HID** | Standard HID-PDC | ✅ | Basic monitoring | ⚠️ Limited writes | ## Configuration Reference @@ -585,6 +624,64 @@ text_sensor: > **Note**: Device information availability depends on the UPS model and protocol implementation. CyberPower devices provide the most comprehensive device information through dedicated HID reports. +### Button Platform (Beeper Control) + +The component supports UPS beeper control via HID SET_REPORT operations: + +```yaml +button: + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: enable # Action: enable, disable, mute, test + name: "UPS Beeper Enable" + icon: "mdi:volume-high" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: disable + name: "UPS Beeper Disable" + icon: "mdi:volume-off" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: mute + name: "UPS Beeper Mute" + icon: "mdi:volume-mute" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: test + name: "UPS Beeper Test" + icon: "mdi:bell-ring" +``` + +**Available beeper actions:** +- `enable`: Enable UPS beeper for all alarm conditions +- `disable`: Disable UPS beeper (silent operation) +- `mute`: Temporarily silence current beeper alarms (only works during active alarms) +- `test`: Test beeper functionality (enables/disables to verify write operations) + +**Protocol Support:** +- ✅ **APC Devices**: Uses report IDs 0x18 and 0x78 (multi-report fallback) +- ✅ **CyberPower Devices**: Uses report ID 0x0c (dedicated beeper control) +- ⚠️ **Generic HID**: Limited support (device-dependent) + +**Important Notes:** +- **Mute functionality**: Only works when UPS is actively beeping (during power outages or fault conditions) +- **Test behavior**: Verifies write operations but produces no sound unless there's an active alarm +- **State persistence**: Beeper settings are stored in UPS NVRAM and persist across reboots +- **Real-world testing**: To hear actual beeper operation, test during actual power loss events + +**Beeper Status Monitoring:** +```yaml +text_sensor: + - platform: ups_hid + ups_hid_id: ups_monitor + type: ups_beeper_status # Shows: "enabled", "disabled", or "muted" + name: "UPS Beeper Status" + icon: "mdi:volume-high" +``` + ## Advanced Configuration ### Custom Protocol Priority @@ -659,6 +756,16 @@ logger: - This is normal behavior protecting the UPS device - Check physical connections if persistent +#### 5. "Beeper control not working" + +**Cause**: UPS doesn't support write operations or incorrect protocol +**Solutions**: +- Verify your UPS model supports beeper control (check supported devices list) +- Enable debug logging to see HID SET_REPORT attempts: `ups_hid.apc: DEBUG` +- Test during actual power outage to hear mute functionality +- Check beeper status sensor for current state +- Some UPS models have hardware beeper disable switches + ### Debug Logging Enable detailed logging for troubleshooting: @@ -693,6 +800,18 @@ logger: [W][ups_hid:185] Suppressed 5 similar error messages in the last 5000 ms ``` +**Beeper control success**: +``` +[I][ups_hid.apc:123] APC beeper enabled successfully with report ID 0x18 +[D][ups_hid.cyberpower:87] CyberPower beeper muted successfully +``` + +**Beeper control issues**: +``` +[W][ups_hid.apc:145] Failed to set beeper with all report IDs +[E][ups_hid:567] HID SET_REPORT failed: ESP_ERR_TIMEOUT +``` + ### Performance Monitoring Monitor these metrics in Home Assistant: @@ -873,6 +992,15 @@ A: Typical ESP32-S3 power consumption: ~100-200mA @ 5V during normal operation. **Q: Can I use this with other ESP32 variants?** A: Only ESP32-S3 supports USB OTG required for direct UPS communication. +**Q: Why doesn't the beeper test button make any sound?** +A: UPS beepers are event-driven and only sound during actual alarm conditions (power loss, low battery, faults). The test button verifies write operations work correctly. To hear the actual beeper, test during a real power outage. + +**Q: Does the mute button work?** +A: Mute only works when the UPS is actively beeping (during alarms). It acknowledges the current alarm and silences the beeper until the next alarm condition occurs. + +**Q: Do beeper settings persist after ESP32 restart?** +A: Yes, beeper settings are stored in the UPS device's NVRAM and persist across both ESP32 and UPS reboots. + ## Development ### Adding Custom Protocols diff --git a/components/ups_hid/apc_hid_protocol.cpp b/components/ups_hid/apc_hid_protocol.cpp index 454f4f3..2a989ee 100644 --- a/components/ups_hid/apc_hid_protocol.cpp +++ b/components/ups_hid/apc_hid_protocol.cpp @@ -1327,5 +1327,142 @@ void ApcHidProtocol::parse_battery_chemistry_report(const HidReport &report, Ups data.battery_type.c_str(), chemistry_raw); } +bool ApcHidProtocol::beeper_enable() { + ESP_LOGD(APC_HID_TAG, "Sending APC beeper enable command"); + + // APC DEVICE SPECIFIC: From NUT debug, your device supports TWO beeper report IDs: + // 0x18: UPS.PowerSummary.AudibleAlarmControl + // 0x78: UPS.AudibleAlarmControl + uint8_t report_ids_to_try[] = {0x18, 0x78, 0x1f, 0x0c}; + + for (size_t i = 0; i < sizeof(report_ids_to_try); i++) { + uint8_t report_id = report_ids_to_try[i]; + ESP_LOGD(APC_HID_TAG, "Trying beeper enable with report ID 0x%02X", report_id); + + uint8_t beeper_data[2] = {report_id, 0x02}; // Report ID, Value=2 (enabled) + + esp_err_t ret = parent_->hid_set_report(0x03, report_id, beeper_data, sizeof(beeper_data)); + if (ret == ESP_OK) { + ESP_LOGI(APC_HID_TAG, "APC beeper enabled successfully with report ID 0x%02X", report_id); + return true; + } else { + ESP_LOGD(APC_HID_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret)); + } + } + + ESP_LOGW(APC_HID_TAG, "Failed to enable APC beeper with all tested report IDs"); + return false; +} + +bool ApcHidProtocol::beeper_disable() { + ESP_LOGD(APC_HID_TAG, "Sending APC beeper disable command"); + + // APC DEVICE SPECIFIC: From NUT debug, your device supports TWO beeper report IDs: + // 0x18: UPS.PowerSummary.AudibleAlarmControl + // 0x78: UPS.AudibleAlarmControl + uint8_t report_ids_to_try[] = {0x18, 0x78, 0x1f, 0x0c}; + + for (size_t i = 0; i < sizeof(report_ids_to_try); i++) { + uint8_t report_id = report_ids_to_try[i]; + ESP_LOGD(APC_HID_TAG, "Trying beeper disable with report ID 0x%02X", report_id); + + uint8_t beeper_data[2] = {report_id, 0x01}; // Report ID, Value=1 (disabled) + + esp_err_t ret = parent_->hid_set_report(0x03, report_id, beeper_data, sizeof(beeper_data)); + if (ret == ESP_OK) { + ESP_LOGI(APC_HID_TAG, "APC beeper disabled successfully with report ID 0x%02X", report_id); + return true; + } else { + ESP_LOGD(APC_HID_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret)); + } + } + + ESP_LOGW(APC_HID_TAG, "Failed to disable APC beeper with all tested report IDs"); + return false; +} + +bool ApcHidProtocol::beeper_mute() { + ESP_LOGD(APC_HID_TAG, "Sending APC beeper mute command"); + + // MUTE FUNCTIONALITY (Value 3): + // - Acknowledges and silences current active alarms + // - Beeper may still sound for new critical events + // - Different from DISABLE (1) which turns off beeper completely + // - Current device status shows "enabled" (2), mute changes to "muted" (3) + + uint8_t report_ids_to_try[] = {0x18, 0x78}; + + for (size_t i = 0; i < sizeof(report_ids_to_try); i++) { + uint8_t report_id = report_ids_to_try[i]; + ESP_LOGD(APC_HID_TAG, "Trying beeper mute (acknowledge alarms) with report ID 0x%02X", report_id); + + uint8_t beeper_data[2] = {report_id, 0x03}; // Report ID, Value=3 (muted/acknowledged) + + esp_err_t ret = parent_->hid_set_report(0x03, report_id, beeper_data, sizeof(beeper_data)); + if (ret == ESP_OK) { + ESP_LOGI(APC_HID_TAG, "APC beeper muted (current alarms acknowledged) with report ID 0x%02X", report_id); + return true; + } else { + ESP_LOGD(APC_HID_TAG, "Failed with report ID 0x%02X: %s", report_id, esp_err_to_name(ret)); + } + } + + ESP_LOGW(APC_HID_TAG, "Failed to mute APC beeper with all tested report IDs"); + return false; +} + +bool ApcHidProtocol::beeper_test() { + ESP_LOGD(APC_HID_TAG, "Starting APC beeper test sequence"); + + // First, read current beeper status to restore later + HidReport current_report; + if (!read_hid_report(APC_REPORT_ID_AUDIBLE_ALARM, current_report)) { + ESP_LOGW(APC_HID_TAG, "Failed to read current beeper status for test"); + return false; + } + + uint8_t original_state = (current_report.data.size() >= 2) ? current_report.data[1] : 0x02; + ESP_LOGI(APC_HID_TAG, "Original beeper state: %d", original_state); + + // For APC beeper test, we need to: + // 1. Enable beeper first (if disabled) + // 2. Wait for beeper to sound (longer delay) + // 3. Disable beeper to stop the test sound + // 4. Restore original state + + // FOCUS ON ENABLE/DISABLE: Since NUT shows beeper currently "enabled" (value=2) + // Test by disabling first (may be audible), then re-enabling + ESP_LOGI(APC_HID_TAG, "Step 1: Disabling beeper (from current enabled state)"); + if (!beeper_disable()) { + ESP_LOGW(APC_HID_TAG, "Failed to disable beeper for test"); + return false; + } + + ESP_LOGI(APC_HID_TAG, "Step 2: Waiting 3 seconds with beeper disabled"); + vTaskDelay(pdMS_TO_TICKS(3000)); + + ESP_LOGI(APC_HID_TAG, "Step 3: Re-enabling beeper"); + if (!beeper_enable()) { + ESP_LOGW(APC_HID_TAG, "Failed to re-enable beeper"); + // Don't return false - continue to restore original state + } + + // Brief delay before restoration + vTaskDelay(pdMS_TO_TICKS(500)); + + // Step 4: Restore original beeper state + ESP_LOGI(APC_HID_TAG, "Step 4: Restoring original beeper state: %d", original_state); + uint8_t restore_data[2] = {0x18, original_state}; + esp_err_t ret = parent_->hid_set_report(0x03, APC_REPORT_ID_AUDIBLE_ALARM, restore_data, sizeof(restore_data)); + + if (ret == ESP_OK) { + ESP_LOGI(APC_HID_TAG, "APC beeper test sequence completed successfully"); + return true; + } else { + ESP_LOGW(APC_HID_TAG, "Beeper test completed but failed to restore original state: %s", esp_err_to_name(ret)); + return true; // Test succeeded even if restore failed + } +} + } // namespace ups_hid } // namespace esphome \ No newline at end of file diff --git a/components/ups_hid/apc_hid_protocol.h b/components/ups_hid/apc_hid_protocol.h index 95ec8c1..e892ac1 100644 --- a/components/ups_hid/apc_hid_protocol.h +++ b/components/ups_hid/apc_hid_protocol.h @@ -15,6 +15,12 @@ public: bool read_data(UpsData &data) override; UpsProtocol get_protocol_type() const override { return PROTOCOL_APC_HID; } std::string get_protocol_name() const override { return "APC HID Protocol"; } + + // Beeper control methods + bool beeper_enable() override; + bool beeper_disable() override; + bool beeper_mute() override; + bool beeper_test() override; private: struct HidReport { diff --git a/components/ups_hid/button.cpp b/components/ups_hid/button.cpp new file mode 100644 index 0000000..01066ef --- /dev/null +++ b/components/ups_hid/button.cpp @@ -0,0 +1,66 @@ +#include "button.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace ups_hid { + +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()); +} + +void UpsHidButton::press_action() { + if (!parent_) { + ESP_LOGE(BUTTON_TAG, "No UPS HID parent component set"); + return; + } + + if (!parent_->is_connected()) { + ESP_LOGW(BUTTON_TAG, "UPS not connected, cannot execute beeper action: %s", beeper_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"); + 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 + 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; + } + + 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()); + } +} + +} // namespace ups_hid +} // namespace esphome \ No newline at end of file diff --git a/components/ups_hid/button.h b/components/ups_hid/button.h new file mode 100644 index 0000000..91d2f7b --- /dev/null +++ b/components/ups_hid/button.h @@ -0,0 +1,25 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/button/button.h" +#include "ups_hid.h" + +namespace esphome { +namespace ups_hid { + +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 dump_config() override; + + protected: + void press_action() override; + + UpsHidComponent *parent_{nullptr}; + std::string beeper_action_{}; +}; + +} // namespace ups_hid +} // namespace esphome \ No newline at end of file diff --git a/components/ups_hid/button.py b/components/ups_hid/button.py new file mode 100644 index 0000000..8516ae2 --- /dev/null +++ b/components/ups_hid/button.py @@ -0,0 +1,32 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import button +from esphome.const import CONF_ID +from . import ups_hid_ns, CONF_UPS_HID_ID, UpsHidComponent + +DEPENDENCIES = ["ups_hid"] + +UpsHidButton = ups_hid_ns.class_("UpsHidButton", button.Button, cg.Component) + +CONF_BEEPER_ACTION = "beeper_action" + +BEEPER_ACTIONS = { + "enable": "enable", + "disable": "disable", + "mute": "mute", + "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) + + +async def to_code(config): + var = await button.new_button(config) + await cg.register_component(var, 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])) \ No newline at end of file diff --git a/components/ups_hid/cyberpower_protocol.cpp b/components/ups_hid/cyberpower_protocol.cpp index 0d0d2a3..b364e94 100644 --- a/components/ups_hid/cyberpower_protocol.cpp +++ b/components/ups_hid/cyberpower_protocol.cpp @@ -922,5 +922,114 @@ std::string CyberPowerProtocol::clean_firmware_string(const std::string &raw_fir return cleaned; } +bool CyberPowerProtocol::beeper_enable() { + ESP_LOGD(CP_TAG, "Sending CyberPower beeper enable command"); + + // CYBERPOWER DEVICE SPECIFIC: From NUT debug, device uses report ID 0x0c + // NUT shows: "UPS.PowerSummary.AudibleAlarmControl, Type: Feature, ReportID: 0x0c" + ESP_LOGD(CP_TAG, "Trying beeper enable with report ID 0x%02X", BEEPER_STATUS_REPORT_ID); + + uint8_t beeper_data[2] = {BEEPER_STATUS_REPORT_ID, 0x02}; // Report ID, Value=2 (enabled) + + esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data)); + if (ret == ESP_OK) { + ESP_LOGI(CP_TAG, "CyberPower beeper enabled successfully with report ID 0x%02X", BEEPER_STATUS_REPORT_ID); + return true; + } else { + ESP_LOGW(CP_TAG, "Failed to enable CyberPower beeper with report ID 0x%02X: %s", BEEPER_STATUS_REPORT_ID, esp_err_to_name(ret)); + return false; + } +} + +bool CyberPowerProtocol::beeper_disable() { + ESP_LOGD(CP_TAG, "Sending CyberPower beeper disable command"); + + // CYBERPOWER DEVICE SPECIFIC: From NUT debug, device uses report ID 0x0c + ESP_LOGD(CP_TAG, "Trying beeper disable with report ID 0x%02X", BEEPER_STATUS_REPORT_ID); + + uint8_t beeper_data[2] = {BEEPER_STATUS_REPORT_ID, 0x01}; // Report ID, Value=1 (disabled) + + esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data)); + if (ret == ESP_OK) { + ESP_LOGI(CP_TAG, "CyberPower beeper disabled successfully with report ID 0x%02X", BEEPER_STATUS_REPORT_ID); + return true; + } else { + ESP_LOGW(CP_TAG, "Failed to disable CyberPower beeper with report ID 0x%02X: %s", BEEPER_STATUS_REPORT_ID, esp_err_to_name(ret)); + return false; + } +} + +bool CyberPowerProtocol::beeper_mute() { + ESP_LOGD(CP_TAG, "Sending CyberPower beeper mute command"); + + // MUTE FUNCTIONALITY (Value 3): + // - Acknowledges and silences current active alarms + // - Beeper may still sound for new critical events + // - Different from DISABLE (1) which turns off beeper completely + uint8_t beeper_data[2] = {0x0c, 0x03}; // Report ID, Value=3 (muted/acknowledged) + + esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, beeper_data, sizeof(beeper_data)); + if (ret == ESP_OK) { + ESP_LOGI(CP_TAG, "CyberPower beeper muted (current alarms acknowledged) successfully"); + return true; + } else { + ESP_LOGW(CP_TAG, "Failed to mute CyberPower beeper: %s", esp_err_to_name(ret)); + return false; + } +} + +bool CyberPowerProtocol::beeper_test() { + ESP_LOGD(CP_TAG, "Starting CyberPower beeper test sequence"); + + // First, read current beeper status to restore later + HidReport current_report; + if (!read_hid_report(BEEPER_STATUS_REPORT_ID, current_report)) { + ESP_LOGW(CP_TAG, "Failed to read current beeper status for test"); + return false; + } + + uint8_t original_state = (current_report.data.size() >= 2) ? current_report.data[1] : 0x02; + ESP_LOGI(CP_TAG, "Original beeper state: %d", original_state); + + // For CyberPower beeper test, we need to: + // 1. Enable beeper first (if disabled) + // 2. Wait for beeper to sound (longer delay) + // 3. Disable beeper to stop the test sound + // 4. Restore original state + + // FOCUS ON ENABLE/DISABLE: Since NUT shows beeper currently "enabled" (value=2) + // Test by disabling first (may be audible), then re-enabling + ESP_LOGI(CP_TAG, "Step 1: Disabling beeper (from current enabled state)"); + if (!beeper_disable()) { + ESP_LOGW(CP_TAG, "Failed to disable beeper for test"); + return false; + } + + ESP_LOGI(CP_TAG, "Step 2: Waiting 3 seconds with beeper disabled"); + vTaskDelay(pdMS_TO_TICKS(3000)); + + ESP_LOGI(CP_TAG, "Step 3: Re-enabling beeper"); + if (!beeper_enable()) { + ESP_LOGW(CP_TAG, "Failed to re-enable beeper"); + // Don't return false - continue to restore original state + } + + // Brief delay before restoration + vTaskDelay(pdMS_TO_TICKS(500)); + + // Step 4: Restore original beeper state + ESP_LOGI(CP_TAG, "Step 4: Restoring original beeper state: %d", original_state); + uint8_t restore_data[2] = {0x0c, original_state}; + esp_err_t ret = parent_->hid_set_report(0x03, BEEPER_STATUS_REPORT_ID, restore_data, sizeof(restore_data)); + + if (ret == ESP_OK) { + ESP_LOGI(CP_TAG, "CyberPower beeper test sequence completed successfully"); + return true; + } else { + ESP_LOGW(CP_TAG, "Beeper test completed but failed to restore original state: %s", esp_err_to_name(ret)); + return true; // Test succeeded even if restore failed + } +} + } // namespace ups_hid } // namespace esphome \ No newline at end of file diff --git a/components/ups_hid/cyberpower_protocol.h b/components/ups_hid/cyberpower_protocol.h index 03c74bc..edae834 100644 --- a/components/ups_hid/cyberpower_protocol.h +++ b/components/ups_hid/cyberpower_protocol.h @@ -26,6 +26,12 @@ class CyberPowerProtocol : public UpsProtocolBase { bool read_data(UpsData &data) override; UpsProtocol get_protocol_type() const override { return PROTOCOL_CYBERPOWER_HID; } std::string get_protocol_name() const override { return "CyberPower HID"; } + + // Beeper control methods + bool beeper_enable() override; + bool beeper_disable() override; + bool beeper_mute() override; + bool beeper_test() override; private: // Report ID constants (based on NUT debug logs) diff --git a/components/ups_hid/sensor.py b/components/ups_hid/sensor.py index d2686cb..e11bc39 100644 --- a/components/ups_hid/sensor.py +++ b/components/ups_hid/sensor.py @@ -17,12 +17,6 @@ from esphome.const import ( UNIT_SECOND, ) -# Handle STATE_CLASS_MEASUREMENT import for different ESPHome versions -try: - from esphome.const import STATE_CLASS_MEASUREMENT -except ImportError: - from esphome.components.sensor import StateClass - STATE_CLASS_MEASUREMENT = StateClass.MEASUREMENT from . import ups_hid_ns, UpsHidComponent, CONF_UPS_HID_ID @@ -34,121 +28,101 @@ SENSOR_TYPES = { "battery_level": { "unit": UNIT_PERCENT, "device_class": DEVICE_CLASS_BATTERY, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "input_voltage": { "unit": UNIT_VOLT, "device_class": DEVICE_CLASS_VOLTAGE, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 1, }, "output_voltage": { "unit": UNIT_VOLT, "device_class": DEVICE_CLASS_VOLTAGE, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 1, }, "load_percent": { "unit": UNIT_PERCENT, "device_class": DEVICE_CLASS_POWER_FACTOR, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "runtime": { "unit": UNIT_MINUTE, "device_class": DEVICE_CLASS_DURATION, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "frequency": { "unit": UNIT_HERTZ, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 1, }, "battery_voltage": { "unit": UNIT_VOLT, "device_class": DEVICE_CLASS_VOLTAGE, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 1, }, "battery_voltage_nominal": { "unit": UNIT_VOLT, "device_class": DEVICE_CLASS_VOLTAGE, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "input_voltage_nominal": { "unit": UNIT_VOLT, "device_class": DEVICE_CLASS_VOLTAGE, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "input_transfer_low": { "unit": UNIT_VOLT, "device_class": DEVICE_CLASS_VOLTAGE, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "input_transfer_high": { "unit": UNIT_VOLT, "device_class": DEVICE_CLASS_VOLTAGE, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "ups_realpower_nominal": { "unit": UNIT_WATT, "device_class": DEVICE_CLASS_POWER, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "ups_delay_shutdown": { "unit": UNIT_SECOND, "device_class": DEVICE_CLASS_DURATION, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "ups_delay_start": { "unit": UNIT_SECOND, "device_class": DEVICE_CLASS_DURATION, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, # Additional missing sensor types from NUT analysis "battery_charge_low": { "unit": UNIT_PERCENT, "device_class": DEVICE_CLASS_BATTERY, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "battery_charge_warning": { "unit": UNIT_PERCENT, "device_class": DEVICE_CLASS_BATTERY, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "battery_runtime_low": { "unit": UNIT_MINUTE, "device_class": DEVICE_CLASS_DURATION, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "ups_timer_reboot": { "unit": UNIT_SECOND, "device_class": DEVICE_CLASS_DURATION, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "ups_timer_shutdown": { "unit": UNIT_SECOND, "device_class": DEVICE_CLASS_DURATION, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, "ups_timer_start": { "unit": UNIT_SECOND, "device_class": DEVICE_CLASS_DURATION, - "state_class": STATE_CLASS_MEASUREMENT, "accuracy_decimals": 0, }, } @@ -185,8 +159,5 @@ async def to_code(config): if "device_class" not in config and "device_class" in sensor_config: cg.add(var.set_device_class(sensor_config["device_class"])) - if "state_class" not in config and "state_class" in sensor_config: - cg.add(var.set_state_class(sensor_config["state_class"])) - if "accuracy_decimals" not in config and "accuracy_decimals" in sensor_config: cg.add(var.set_accuracy_decimals(sensor_config["accuracy_decimals"])) diff --git a/components/ups_hid/ups_hid.h b/components/ups_hid/ups_hid.h index 9f8cb04..bbde65d 100644 --- a/components/ups_hid/ups_hid.h +++ b/components/ups_hid/ups_hid.h @@ -230,6 +230,9 @@ namespace esphome void register_sensor(sensor::Sensor *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); + + // Protocol access for button components + UpsProtocolBase* get_active_protocol() const { return active_protocol_.get(); } protected: bool simulation_mode_{false}; @@ -367,6 +370,12 @@ namespace esphome virtual bool read_data(UpsData &data) = 0; virtual UpsProtocol get_protocol_type() const = 0; virtual std::string get_protocol_name() const = 0; + + // Beeper control methods + virtual bool beeper_enable() { return false; } + virtual bool beeper_disable() { return false; } + virtual bool beeper_mute() { return false; } + virtual bool beeper_test() { return false; } protected: UpsHidComponent *parent_; diff --git a/full-test.yaml b/full-test.yaml index fafe5a1..d14e304 100644 --- a/full-test.yaml +++ b/full-test.yaml @@ -770,6 +770,38 @@ script: debug: update_interval: 30s +# UPS Beeper Control Buttons +# Based on confirmed working implementation for APC and CyberPower devices +button: + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: enable + name: "UPS Beeper Enable" + icon: "mdi:volume-high" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: disable + name: "UPS Beeper Disable" + icon: "mdi:volume-off" + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: mute + name: "UPS Beeper Mute" + icon: "mdi:volume-mute" + # Note: Mute only works when UPS is actively beeping (during alarms) + # Use during power outages to acknowledge and silence current alarms + # Confirmed working: Mute button silences beeper during power loss events + + - platform: ups_hid + ups_hid_id: ups_monitor + beeper_action: test + name: "UPS Beeper Test" + icon: "mdi:bell-ring" + # Note: Tests enable/disable functionality - no audible sound unless UPS has alarm condition + # To hear actual beeper: unplug UPS briefly while beeper is enabled + # Enable safe mode for recovery safe_mode: disabled: false