feat(usp-hid): data provider pattern

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-08-31 15:03:28 +02:00
1 parent 1df84b0473
commit 49e24d3170
5 files changed
+308 -44

No files matched your search

+47 -7
View File
@@ -1,6 +1,6 @@
# UPS HID Configuration Management
This directory provides a modular, maintainable approach to UPS HID device configuration using ESPHome packages with the new Protocol Factory system. Instead of maintaining large, complex configuration files, you can compose configurations from reusable components.
This directory provides a modular, maintainable approach to UPS HID device configuration using ESPHome packages with the a Protocol Factory system. Instead of maintaining large, complex configuration files, you can compose configurations from reusable components.
## 📦 Modular Configuration Packages
@@ -11,9 +11,9 @@ Essential foundation for all UPS devices:
- ESP32-S3 hardware configuration
- Network setup (WiFi, OTA, Web server)
- UPS HID component initialization with Protocol Factory
- Status LED with smart patterns
- Essential binary sensors: `online`, `on_battery`, `low_battery`, `fault`
- Essential text sensors: `status` (as "UPS Status"), `protocol` (as "Detected Protocol")
- **Data Provider Pattern**: LED automation accesses UPS data directly (no sensor entities required!)
- Status LED with smart patterns using `id(ups_monitor).is_online()`, etc.
- **Optional sensors**: Only `status` and `protocol` text sensors (for Home Assistant integration)
#### `essential_sensors.yaml` / `essential_sensors_grouped.yaml`
Core monitoring sensors (5 sensors) available on all protocols:
@@ -171,6 +171,21 @@ packages:
```
**Use for**: Development, CI/CD testing, feature demonstration
### `testing/minimal_data_provider.yaml`
Data provider pattern testing with zero sensor entities:
```yaml
substitutions:
simulation_mode: "true" # Enable simulation for testing
packages:
base_ups: !include base_ups.yaml
# ZERO sensor entities defined - tests pure data provider mode!
```
**Features**:
- Tests all direct data access methods: `id(ups_monitor).is_online()`, etc.
- LED automation works without any sensor entities
- Comprehensive validation of data provider pattern
- **Use for**: Testing component as pure data provider, custom automation development
## Production Examples
### `examples/apc-ups-monitor.yaml`
@@ -314,7 +329,32 @@ substitutions:
```
### 6. **Testing Strategy**
- **Protocol Testing**: Use `basic_test.yaml`
- **Feature Testing**: Use specific test configurations
- **Integration Testing**: Use `simulation_test.yaml`
- **Protocol Testing**: Use `testing/basic_test.yaml`
- **Feature Testing**: Use specific test configurations
- **Integration Testing**: Use `testing/simulation_test.yaml`
- **Data Provider Testing**: Use `testing/minimal_data_provider.yaml`
- **Production Validation**: Use device-specific examples
### 7. **Data Provider Pattern Usage**
The UPS HID component now functions as a **data provider**, allowing direct access to UPS data without requiring sensor entities:
#### **Direct Data Access Methods**:
```yaml
interval:
- interval: 10s
then:
- lambda: |-
// Boolean state methods (no sensor entities needed!)
if (id(ups_monitor).is_online()) {
ESP_LOGI("ups", "UPS is online");
}
if (id(ups_monitor).is_on_battery()) {
ESP_LOGI("ups", "Running on battery: %.1f%% remaining",
id(ups_monitor).get_battery_level());
}
```
#### **Available Direct Access Methods**:
**Boolean States**: `is_online()`, `is_on_battery()`, `is_low_battery()`, `is_charging()`, `has_fault()`, `is_overloaded()`
**Value Getters**: `get_battery_level()`, `get_input_voltage()`, `get_output_voltage()`, `get_load_percent()`, `get_runtime_minutes()`
+8 -33
View File
@@ -104,22 +104,23 @@ light:
min_brightness: 20%
max_brightness: 60%
# LED control automation
# LED control automation (no sensor entities required!)
interval:
- interval: 2s
then:
- lambda: |-
if (id(low_battery).state || id(ups_fault).state) {
// Direct access to UPS data - no sensor entities needed
if (id(ups_monitor).is_low_battery() || id(ups_monitor).has_fault()) {
auto call = id(ups_status_led).turn_on();
call.set_rgb(1.0, 0.0, 0.0);
call.set_effect("Critical Alert");
call.perform();
} else if (id(on_battery).state) {
} else if (id(ups_monitor).is_on_battery()) {
auto call = id(ups_status_led).turn_on();
call.set_rgb(1.0, 0.5, 0.0);
call.set_effect("Battery Warning");
call.perform();
} else if (id(ups_online).state) {
} else if (id(ups_monitor).is_online()) {
auto call = id(ups_status_led).turn_on();
call.set_rgb(0.0, 1.0, 0.0);
call.set_effect("Normal Status");
@@ -128,35 +129,9 @@ interval:
id(ups_status_led).turn_off().perform();
}
# Essential binary sensors (required for LED automation)
binary_sensor:
- platform: ups_hid
ups_hid_id: ups_monitor
name: "UPS Online"
id: ups_online
type: online
device_class: connectivity
- platform: ups_hid
ups_hid_id: ups_monitor
name: "On Battery Power"
id: on_battery
type: on_battery
device_class: battery
- platform: ups_hid
ups_hid_id: ups_monitor
name: "Low Battery Warning"
id: low_battery
type: low_battery
device_class: battery
- platform: ups_hid
ups_hid_id: ups_monitor
name: "UPS Fault"
id: ups_fault
type: fault
device_class: problem
# Note: Binary sensors are now completely OPTIONAL!
# Only define them if you need Home Assistant entities
# The LED automation above works without any sensor entities
# Essential text sensors
text_sensor:
@@ -0,0 +1,88 @@
# Data Provider Testing Configuration
# Tests UPS HID component as pure data provider with comprehensive validation
# Validates all direct access methods work without any sensor entities
substitutions:
name: "data-provider-test"
friendly_name: "Data Provider Test"
simulation_mode: "true" # Enable simulation for testing
# Core ESPHome configuration
esphome:
name: ${name}
friendly_name: ${friendly_name}
# Use base UPS configuration (now has optional sensors)
packages:
base_ups: !include base_ups.yaml
# ZERO sensor entities defined - testing pure data provider mode!
# Override base_ups interval with comprehensive testing
interval:
- interval: 30s
then:
- lambda: |-
// Test all boolean state methods
ESP_LOGI("test", "=== UPS Data Provider Test ===");
ESP_LOGI("test", "Online: %s", id(ups_monitor).is_online() ? "YES" : "NO");
ESP_LOGI("test", "On Battery: %s", id(ups_monitor).is_on_battery() ? "YES" : "NO");
ESP_LOGI("test", "Low Battery: %s", id(ups_monitor).is_low_battery() ? "YES" : "NO");
ESP_LOGI("test", "Charging: %s", id(ups_monitor).is_charging() ? "YES" : "NO");
ESP_LOGI("test", "Fault: %s", id(ups_monitor).has_fault() ? "YES" : "NO");
ESP_LOGI("test", "Overload: %s", id(ups_monitor).is_overloaded() ? "YES" : "NO");
// Test all value getter methods
float battery = id(ups_monitor).get_battery_level();
float input_v = id(ups_monitor).get_input_voltage();
float output_v = id(ups_monitor).get_output_voltage();
float load = id(ups_monitor).get_load_percent();
float runtime = id(ups_monitor).get_runtime_minutes();
ESP_LOGI("test", "Battery: %.1f%%, Input: %.1fV, Output: %.1fV",
isnan(battery) ? -1.0f : battery,
isnan(input_v) ? -1.0f : input_v,
isnan(output_v) ? -1.0f : output_v);
ESP_LOGI("test", "Load: %.1f%%, Runtime: %.1f min",
isnan(load) ? -1.0f : load,
isnan(runtime) ? -1.0f : runtime);
- interval: 60s
then:
- lambda: |-
// Advanced LED state testing - cycles through different conditions
static int test_phase = 0;
test_phase = (test_phase + 1) % 6;
ESP_LOGI("led_test", "LED Test Phase: %d", test_phase);
switch(test_phase) {
case 0: // Test critical state display
if (id(ups_monitor).is_low_battery() || id(ups_monitor).has_fault()) {
ESP_LOGI("led_test", "Critical state detected - RED strobe should be active");
}
break;
case 1: // Test battery operation
if (id(ups_monitor).is_on_battery()) {
ESP_LOGI("led_test", "Battery operation - ORANGE fade should be active");
}
break;
case 2: // Test charging state
if (id(ups_monitor).is_charging()) {
ESP_LOGI("led_test", "Charging state - YELLOW pattern should be active");
}
break;
case 3: // Test normal operation
if (id(ups_monitor).is_online()) {
ESP_LOGI("led_test", "Normal operation - GREEN breathing should be active");
}
break;
case 4: // Test offline state
if (!id(ups_monitor).is_online()) {
ESP_LOGI("led_test", "Offline state - BLUE fade should be active");
}
break;
case 5: // Data provider mode validation
ESP_LOGI("led_test", "Data provider mode: Component should report zero sensors registered");
break;
}