[SYS] Refactory HASS Discovery #2243
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This module contains the refactored Home Assistant Discovery system for OpenMQTTGateway, implementing modern C++17 architecture following SOLID principles.
Overview
The HMD (Home Assistant Discovery Manager) module provides a comprehensive system for automatic discovery and configuration of IoT devices in Home Assistant. It replaces the legacy discovery system with a modern, modular, and efficient architecture.
Activation
The HMD module uses conditional compilation to coexist with the legacy discovery system:
ZmqttDiscovery2in your build configurationZmqttDiscovery(default behavior)ZmqttDiscovery2andZmqttDiscoveryare defined, HMD takes priority and legacy code is disabledArchitecture Overview
The module is organized following Single Responsibility Principle with each class handling a specific aspect of the discovery system:
Technical Requirements
Compiler Requirements
std::string_view,std::optional)-DPIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONSfor ESP8266)Build Configuration
The global build configuration in
platformio.iniensures C++17 support:Key Design Principles
1. Single Responsibility Principle (SRP)
2. Open/Closed Principle (OCP)
3. Liskov Substitution Principle (LSP)
4. Interface Segregation Principle (ISP)
ISettingsProvider,IMqttPublisher)5. Dependency Inversion Principle (DIP)
Core Components
Interfaces
ISettingsProvider
Provides access to configuration settings:
IMqttPublisher
Handles MQTT publishing operations:
Core Classes
HassValidators
HassTopicBuilder
HassDevice
Entity System
HassEntity (Abstract Base Class)
HassSensor
HassSwitch
HassButton
Manager
HassDiscoveryManager
Home Assistant Constants
All Home Assistant specific constants are centralized in
core/HassConstants.h:Device Classes (42+ constants)
Measurement Units (35+ constants)
Component Types
JSON Value Templates (25+ templates)
Usage Examples
Basic Sensor Creation
Creating External Device
Legacy Array Support
Memory Efficiency
The new architecture provides significant memory improvements:
Error Handling
The system provides robust error handling:
Performance Metrics
Target improvements achieved:
Testing
🧪 Comprehensive Test Suite
Comprehensive unit tests are available in
/test/unit/test_hmd/:🚀 CI/CD Integration Status: ✅ FULLY OPERATIONAL
Workflow:
.github/workflows/run-tests.ymlAutomatic Testing On:
main,development,feature/*branchesmain,developmentQuick Test Execution
See: Testing Documentation for complete details and CI/CD Integration Report for GitHub Actions status.
Contributing
When adding new entity types:
HassEntitybase classaddSpecificFields()methodHassDiscoveryManagerMigration from Legacy System
Phase 1: Core Infrastructure ✅
Phase 2: System Entities ✅
Phase 3: Conditional Compilation ✅
ZmqttDiscovery2flag for HMD activationPhase 4: Sensor Modules (In Progress)
Phase 5: Gateway Modules (Planned)
Deployment Status
Current Implementation
ZmqttDiscovery2)ZmqttDiscovery) remains active unlessZmqttDiscovery2is explicitly definedPlatform Compatibility
Enabling HMD in Your Build
environments.ini):platformio.ini):Checklist: