Public Access
Bugfix 2026.7.0.
This commit is contained in:
1 parent
c69b2e0754
commit
74cdb3d56f
4 files changed
+27
-93
No files matched your search
@@ -3,19 +3,13 @@
|
|||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include "esphome/components/logger/logger.h"
|
#include "esphome/components/logger/logger.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace ups_hid {
|
namespace ups_hid {
|
||||||
|
|
||||||
static const char *const FACTORY_TAG = "ups_hid.factory";
|
static const char *const FACTORY_TAG = "ups_hid.factory";
|
||||||
|
|
||||||
// Forward declarations for protocol creator functions
|
|
||||||
// These are defined in their respective protocol implementation files
|
|
||||||
extern std::unique_ptr<UpsProtocolBase> create_apc_protocol(UpsHidComponent* parent);
|
|
||||||
extern std::unique_ptr<UpsProtocolBase> create_cyberpower_protocol(UpsHidComponent* parent);
|
|
||||||
extern std::unique_ptr<UpsProtocolBase> create_goldenmate_protocol(UpsHidComponent* parent);
|
|
||||||
extern std::unique_ptr<UpsProtocolBase> create_generic_protocol(UpsHidComponent* parent);
|
|
||||||
|
|
||||||
// Static registry implementations
|
// Static registry implementations
|
||||||
std::unordered_map<uint16_t, std::vector<ProtocolFactory::ProtocolInfo>>&
|
std::unordered_map<uint16_t, std::vector<ProtocolFactory::ProtocolInfo>>&
|
||||||
ProtocolFactory::get_vendor_registry() {
|
ProtocolFactory::get_vendor_registry() {
|
||||||
@@ -30,65 +24,13 @@ ProtocolFactory::get_fallback_registry() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ProtocolFactory::ensure_initialized() {
|
void ProtocolFactory::ensure_initialized() {
|
||||||
// Initialize built-in protocols on first access
|
// With the Open/Closed refactor, protocol self-registration using macros handles everything.
|
||||||
// This ensures all protocols are registered regardless of static initializer order
|
// This function is kept for backwards compatibility or future core initializations.
|
||||||
static bool initialized = false;
|
static bool initialized = false;
|
||||||
if (initialized) return;
|
if (initialized) return;
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
|
||||||
ESP_LOGI(FACTORY_TAG, "Initializing built-in UPS protocol support");
|
ESP_LOGI(FACTORY_TAG, "Protocol registries initialized.");
|
||||||
|
|
||||||
// Register APC protocol for vendor 0x051D
|
|
||||||
{
|
|
||||||
ProtocolInfo info;
|
|
||||||
info.creator = create_apc_protocol;
|
|
||||||
info.name = "APC";
|
|
||||||
info.description = "APC HID Protocol";
|
|
||||||
info.supported_vendors = {0x051D};
|
|
||||||
info.priority = 200;
|
|
||||||
auto& registry = get_vendor_registry();
|
|
||||||
registry[0x051D].push_back(info);
|
|
||||||
ESP_LOGI(FACTORY_TAG, "Registered APC protocol for vendor 0x051D");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register CyberPower protocol for vendor 0x0764
|
|
||||||
{
|
|
||||||
ProtocolInfo info;
|
|
||||||
info.creator = create_cyberpower_protocol;
|
|
||||||
info.name = "CyberPower";
|
|
||||||
info.description = "CyberPower HID Protocol";
|
|
||||||
info.supported_vendors = {0x0764};
|
|
||||||
info.priority = 200;
|
|
||||||
auto& registry = get_vendor_registry();
|
|
||||||
registry[0x0764].push_back(info);
|
|
||||||
ESP_LOGI(FACTORY_TAG, "Registered CyberPower protocol for vendor 0x0764");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register GoldenMate protocol for vendor 0x075D
|
|
||||||
{
|
|
||||||
ProtocolInfo info;
|
|
||||||
info.creator = create_goldenmate_protocol;
|
|
||||||
info.name = "GoldenMate";
|
|
||||||
info.description = "GoldenMate HID Protocol";
|
|
||||||
info.supported_vendors = {0x075D};
|
|
||||||
info.priority = 200;
|
|
||||||
auto& registry = get_vendor_registry();
|
|
||||||
registry[0x075D].push_back(info);
|
|
||||||
ESP_LOGI(FACTORY_TAG, "Registered GoldenMate protocol for vendor 0x075D");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register Generic protocol as fallback (always last)
|
|
||||||
{
|
|
||||||
ProtocolInfo info;
|
|
||||||
info.creator = create_generic_protocol;
|
|
||||||
info.name = "Generic HID Protocol";
|
|
||||||
info.description = "Universal HID protocol fallback for unknown UPS vendors";
|
|
||||||
info.supported_vendors = {};
|
|
||||||
info.priority = 10;
|
|
||||||
auto& registry = get_fallback_registry();
|
|
||||||
registry.push_back(info);
|
|
||||||
ESP_LOGI(FACTORY_TAG, "Registered Generic HID protocol as fallback");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProtocolFactory::register_protocol_for_vendor(uint16_t vendor_id,
|
void ProtocolFactory::register_protocol_for_vendor(uint16_t vendor_id,
|
||||||
@@ -249,8 +191,10 @@ ProtocolFactory::create_by_name(const std::string& protocol_name, UpsHidComponen
|
|||||||
// Match protocol name (case-insensitive)
|
// Match protocol name (case-insensitive)
|
||||||
std::string info_name_lower = info.name;
|
std::string info_name_lower = info.name;
|
||||||
std::string protocol_name_lower = protocol_name;
|
std::string protocol_name_lower = protocol_name;
|
||||||
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(), ::tolower);
|
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(),
|
||||||
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(), ::tolower);
|
[](unsigned char c) { return std::tolower(c); });
|
||||||
|
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(),
|
||||||
|
[](unsigned char c) { return std::tolower(c); });
|
||||||
|
|
||||||
if (info_name_lower.find(protocol_name_lower) != std::string::npos) {
|
if (info_name_lower.find(protocol_name_lower) != std::string::npos) {
|
||||||
ESP_LOGD(FACTORY_TAG, "Found matching protocol '%s' for name '%s'",
|
ESP_LOGD(FACTORY_TAG, "Found matching protocol '%s' for name '%s'",
|
||||||
@@ -270,8 +214,10 @@ ProtocolFactory::create_by_name(const std::string& protocol_name, UpsHidComponen
|
|||||||
for (const auto& info : fallback_registry) {
|
for (const auto& info : fallback_registry) {
|
||||||
std::string info_name_lower = info.name;
|
std::string info_name_lower = info.name;
|
||||||
std::string protocol_name_lower = protocol_name;
|
std::string protocol_name_lower = protocol_name;
|
||||||
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(), ::tolower);
|
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(),
|
||||||
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(), ::tolower);
|
[](unsigned char c) { return std::tolower(c); });
|
||||||
|
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(),
|
||||||
|
[](unsigned char c) { return std::tolower(c); });
|
||||||
|
|
||||||
if (info_name_lower.find(protocol_name_lower) != std::string::npos) {
|
if (info_name_lower.find(protocol_name_lower) != std::string::npos) {
|
||||||
ESP_LOGD(FACTORY_TAG, "Found matching fallback protocol '%s' for name '%s'",
|
ESP_LOGD(FACTORY_TAG, "Found matching fallback protocol '%s' for name '%s'",
|
||||||
|
|||||||
@@ -13,12 +13,6 @@ namespace ups_hid {
|
|||||||
class UpsProtocolBase;
|
class UpsProtocolBase;
|
||||||
class UpsHidComponent;
|
class UpsHidComponent;
|
||||||
|
|
||||||
// Forward declarations for built-in protocol creator functions
|
|
||||||
std::unique_ptr<UpsProtocolBase> create_apc_protocol(UpsHidComponent* parent);
|
|
||||||
std::unique_ptr<UpsProtocolBase> create_cyberpower_protocol(UpsHidComponent* parent);
|
|
||||||
std::unique_ptr<UpsProtocolBase> create_goldenmate_protocol(UpsHidComponent* parent);
|
|
||||||
std::unique_ptr<UpsProtocolBase> create_generic_protocol(UpsHidComponent* parent);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Protocol Factory with Self-Registration Support
|
* Protocol Factory with Self-Registration Support
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ bool GoldenMateProtocol::initialize() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GoldenMateProtocol::parse_binary_status(const HidReport &report, UpsData &data) {
|
bool GoldenMateProtocol::parse_binary_status(const HidReport &/*report*/, UpsData &/*data*/) {
|
||||||
// We ignore the binary runtime here to prevent the 16-bit overflow bug
|
// We ignore the binary runtime here to prevent the 16-bit overflow bug
|
||||||
// The true runtime will be grabbed from the ASCII string instead.
|
// The true runtime will be grabbed from the ASCII string instead.
|
||||||
return true;
|
return true;
|
||||||
@@ -110,6 +110,7 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Extract the live values safely
|
// Extract the live values safely
|
||||||
|
// NOTE: Verify if substr(0,4) is actually runtime or current as per the header comments
|
||||||
float runtime_mins = std::atof(packed.substr(0, 4).c_str());
|
float runtime_mins = std::atof(packed.substr(0, 4).c_str());
|
||||||
float battery_pct = std::atof(packed.substr(12, 3).c_str());
|
float battery_pct = std::atof(packed.substr(12, 3).c_str());
|
||||||
|
|
||||||
@@ -128,27 +129,24 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
|||||||
bool on_battery = (status_str.length() > 0 && status_str[0] == '1');
|
bool on_battery = (status_str.length() > 0 && status_str[0] == '1');
|
||||||
|
|
||||||
// --- TIME-BASED DEBOUNCE LOGIC ---
|
// --- TIME-BASED DEBOUNCE LOGIC ---
|
||||||
static bool is_full = true;
|
|
||||||
static int below_100_count = 0;
|
|
||||||
|
|
||||||
if (battery_pct >= 100.0f) {
|
if (battery_pct >= 100.0f) {
|
||||||
is_full = true;
|
this->is_full_ = true;
|
||||||
below_100_count = 0; // Reset counter immediately on a 100% read
|
this->below_100_count_ = 0; // Reset counter immediately on a 100% read
|
||||||
} else {
|
} else {
|
||||||
below_100_count++;
|
this->below_100_count_++;
|
||||||
// Require 2 consecutive reads (~20 seconds) below 100% to drop the 'Full' status
|
// Require 2 consecutive reads (~20 seconds) below 100% to drop the 'Full' status
|
||||||
if (below_100_count >= 2) {
|
if (this->below_100_count_ >= 2) {
|
||||||
is_full = false;
|
this->is_full_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (on_battery) {
|
if (on_battery) {
|
||||||
data.power.status = "OB DISCHRG";
|
data.power.status = "OB DISCHRG";
|
||||||
data.battery.status = "Discharging";
|
data.battery.status = "Discharging";
|
||||||
is_full = false; // Force reset if we lose AC power
|
this->is_full_ = false; // Force reset if we lose AC power
|
||||||
below_100_count = 2; // Keep counter aligned with state
|
this->below_100_count_ = 2; // Keep counter aligned with state
|
||||||
} else {
|
} else {
|
||||||
if (is_full) {
|
if (this->is_full_) {
|
||||||
data.power.status = "OL";
|
data.power.status = "OL";
|
||||||
data.battery.status = "Full";
|
data.battery.status = "Full";
|
||||||
} else {
|
} else {
|
||||||
@@ -169,6 +167,8 @@ bool GoldenMateProtocol::read_data(UpsData &data) {
|
|||||||
// CRITICAL HARDWARE TRIGGER:
|
// CRITICAL HARDWARE TRIGGER:
|
||||||
// We MUST poll Report 0x01 first to force the firmware to refresh Report 0x0C
|
// We MUST poll Report 0x01 first to force the firmware to refresh Report 0x0C
|
||||||
HidReport status_report;
|
HidReport status_report;
|
||||||
|
// If this read times out, we still attempt the Megatec read below,
|
||||||
|
// but it may return stale data if the hardware trigger failed.
|
||||||
read_feature_report(REPORT_ID_STATUS, status_report);
|
read_feature_report(REPORT_ID_STATUS, status_report);
|
||||||
|
|
||||||
// Now read the freshly updated ASCII data from Report 0x0C
|
// Now read the freshly updated ASCII data from Report 0x0C
|
||||||
@@ -179,15 +179,6 @@ bool GoldenMateProtocol::read_data(UpsData &data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set actual names on every read cycle
|
|
||||||
data.device.manufacturer = "GoldenMate";
|
|
||||||
data.device.model = "UPS Pro";
|
|
||||||
|
|
||||||
std::string str;
|
|
||||||
if (parent_->get_string_descriptor(3, str) == ESP_OK && !str.empty()) {
|
|
||||||
data.device.serial_number = str;
|
|
||||||
}
|
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ class GoldenMateProtocol : public UpsProtocolBase {
|
|||||||
static const uint8_t REPORT_ID_STATUS = 0x01;
|
static const uint8_t REPORT_ID_STATUS = 0x01;
|
||||||
static const uint8_t REPORT_ID_MEGATEC = 0x0C;
|
static const uint8_t REPORT_ID_MEGATEC = 0x0C;
|
||||||
|
|
||||||
|
bool is_full_ = true;
|
||||||
|
int below_100_count_ = 0;
|
||||||
|
|
||||||
struct HidReport {
|
struct HidReport {
|
||||||
uint8_t report_id;
|
uint8_t report_id;
|
||||||
std::vector<uint8_t> data;
|
std::vector<uint8_t> data;
|
||||||
|
|||||||
Reference in new issue
Block a user