Force protocol registration at factory initialization to work with ESPHome 2026.7.0

The self-registration mechanism using static initializers can fail with certain compiler/linker settings. Move built-in protocol registration into ensure_initialized() to guarantee all protocols (APC, CyberPower, GoldenMate, Generic) are registered before any protocol lookup occurs.

This fixes the issue where Goldenmate detection fails and there are no fallback protocols registered, resulting in "No suitable protocol found for vendor 0x075D".
This commit is contained in:
rwalker777 committed 2026-07-17 13:18:12 -05:00
1 parent 2283a38ff7
commit 6d84a83236
1 file changed
+65 -15
+65 -15
View File
@@ -9,6 +9,13 @@ 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() {
@@ -23,18 +30,69 @@ ProtocolFactory::get_fallback_registry() {
} }
void ProtocolFactory::ensure_initialized() { void ProtocolFactory::ensure_initialized() {
// Registries are initialized on first access due to static storage // Initialize built-in protocols on first access
// This function exists for explicit initialization if needed // This ensures all protocols are registered regardless of static initializer order
static bool initialized = false; static bool initialized = false;
if (!initialized) { if (initialized) return;
// if (esphome::logger::global_logger != nullptr) initialized = true;
// ESP_LOGD(FACTORY_TAG, "Protocol factory registries initialized");
initialized = true; ESP_LOGI(FACTORY_TAG, "Initializing built-in UPS protocol support");
// 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,
const ProtocolInfo& info) { const ProtocolInfo& info) {
ensure_initialized(); ensure_initialized();
auto& registry = get_vendor_registry(); auto& registry = get_vendor_registry();
@@ -45,10 +103,6 @@ void ProtocolFactory::register_protocol_for_vendor(uint16_t vendor_id,
[](const ProtocolInfo& a, const ProtocolInfo& b) { [](const ProtocolInfo& a, const ProtocolInfo& b) {
return a.priority > b.priority; return a.priority > b.priority;
}); });
// if (esphome::logger::global_logger != nullptr)
// ESP_LOGI(FACTORY_TAG, "Registered protocol '%s' for vendor 0x%04X (priority %d)",
// info.name.c_str(), vendor_id, info.priority);
} }
void ProtocolFactory::register_fallback_protocol(const ProtocolInfo& info) { void ProtocolFactory::register_fallback_protocol(const ProtocolInfo& info) {
@@ -62,10 +116,6 @@ void ProtocolFactory::register_fallback_protocol(const ProtocolInfo& info) {
[](const ProtocolInfo& a, const ProtocolInfo& b) { [](const ProtocolInfo& a, const ProtocolInfo& b) {
return a.priority > b.priority; return a.priority > b.priority;
}); });
// if (esphome::logger::global_logger != nullptr)
// ESP_LOGI(FACTORY_TAG, "Registered fallback protocol '%s' (priority %d)",
// info.name.c_str(), info.priority);
} }
std::unique_ptr<UpsProtocolBase> std::unique_ptr<UpsProtocolBase>