Public Access
feat(ups-hid): further validate implementation and adding files
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
12 files changed
+585
-394
No files matched your search
@@ -57,7 +57,7 @@ web_server:
|
|||||||
external_components:
|
external_components:
|
||||||
- source:
|
- source:
|
||||||
type: local
|
type: local
|
||||||
path: ../components
|
path: ../../components
|
||||||
|
|
||||||
# Main UPS HID Component
|
# Main UPS HID Component
|
||||||
ups_hid:
|
ups_hid:
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# Gitignore settings for ESPHome
|
||||||
|
# This is an example and may include too much for your use-case.
|
||||||
|
# You can modify this file to suit your needs.
|
||||||
|
/.esphome/
|
||||||
|
/secrets.yaml
|
||||||
@@ -13,9 +13,6 @@ esphome:
|
|||||||
friendly_name: "${friendly_name}"
|
friendly_name: "${friendly_name}"
|
||||||
comment: "Basic UPS HID functionality test"
|
comment: "Basic UPS HID functionality test"
|
||||||
|
|
||||||
# Include secrets from root directory
|
|
||||||
<<: !include ../../secrets.yaml
|
|
||||||
|
|
||||||
# Use base configuration package
|
# Use base configuration package
|
||||||
packages:
|
packages:
|
||||||
base_ups: !include ../base_ups.yaml
|
base_ups: !include ../base_ups.yaml
|
||||||
|
|||||||
@@ -1,163 +0,0 @@
|
|||||||
# Basic UPS Testing Configuration (Standalone)
|
|
||||||
# Self-contained version that doesn't depend on external files for CI/CD
|
|
||||||
# Use: esphome compile configs/testing/basic_test_standalone.yaml
|
|
||||||
|
|
||||||
substitutions:
|
|
||||||
name: "ups-basic-test"
|
|
||||||
friendly_name: "UPS Basic Test"
|
|
||||||
simulation_mode: "false"
|
|
||||||
log_level: "DEBUG"
|
|
||||||
|
|
||||||
esphome:
|
|
||||||
name: "${name}"
|
|
||||||
friendly_name: "${friendly_name}"
|
|
||||||
comment: "Basic UPS HID functionality test"
|
|
||||||
|
|
||||||
# Core ESP32 configuration
|
|
||||||
esp32:
|
|
||||||
board: esp32-s3-devkitc-1
|
|
||||||
framework:
|
|
||||||
type: esp-idf
|
|
||||||
|
|
||||||
# Logging configuration
|
|
||||||
logger:
|
|
||||||
level: VERBOSE
|
|
||||||
baud_rate: 115200
|
|
||||||
hardware_uart: UART0
|
|
||||||
logs:
|
|
||||||
ups_hid: DEBUG
|
|
||||||
ups_hid.apc: DEBUG
|
|
||||||
ups_hid.cyberpower: DEBUG
|
|
||||||
ups_hid.generic: DEBUG
|
|
||||||
usb_host: DEBUG
|
|
||||||
|
|
||||||
# Network configuration (minimal for testing)
|
|
||||||
wifi:
|
|
||||||
ssid: "TestNetwork"
|
|
||||||
password: "TestPassword"
|
|
||||||
ap:
|
|
||||||
ssid: "${friendly_name} Fallback"
|
|
||||||
password: "TestAPPassword"
|
|
||||||
|
|
||||||
captive_portal:
|
|
||||||
|
|
||||||
# OTA updates
|
|
||||||
ota:
|
|
||||||
- platform: esphome
|
|
||||||
password: "TestOTAPassword"
|
|
||||||
|
|
||||||
# Web server for diagnostics
|
|
||||||
web_server:
|
|
||||||
port: 80
|
|
||||||
|
|
||||||
# External components
|
|
||||||
external_components:
|
|
||||||
- source:
|
|
||||||
type: local
|
|
||||||
path: ../../components
|
|
||||||
|
|
||||||
# Main UPS HID Component
|
|
||||||
ups_hid:
|
|
||||||
id: ups_monitor
|
|
||||||
update_interval: 5s
|
|
||||||
protocol_timeout: 10s
|
|
||||||
auto_detect_protocol: true
|
|
||||||
simulation_mode: ${simulation_mode}
|
|
||||||
|
|
||||||
# Status LED configuration
|
|
||||||
light:
|
|
||||||
- platform: esp32_rmt_led_strip
|
|
||||||
chipset: WS2812
|
|
||||||
pin: GPIO48
|
|
||||||
num_leds: 1
|
|
||||||
rgb_order: GRB
|
|
||||||
name: "UPS Status LED"
|
|
||||||
id: ups_status_led
|
|
||||||
restore_mode: ALWAYS_OFF
|
|
||||||
|
|
||||||
# Required platform components (fix compilation errors)
|
|
||||||
button:
|
|
||||||
|
|
||||||
# Essential binary sensors
|
|
||||||
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
|
|
||||||
|
|
||||||
# Essential text sensors
|
|
||||||
text_sensor:
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "UPS Status"
|
|
||||||
id: ups_status_text
|
|
||||||
type: status
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Detected Protocol"
|
|
||||||
id: ups_protocol
|
|
||||||
type: protocol
|
|
||||||
|
|
||||||
# Core UPS metrics
|
|
||||||
sensor:
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Battery Level"
|
|
||||||
id: battery_level
|
|
||||||
type: battery_level
|
|
||||||
unit_of_measurement: "%"
|
|
||||||
device_class: battery
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Runtime Remaining"
|
|
||||||
id: runtime_remaining
|
|
||||||
type: runtime
|
|
||||||
unit_of_measurement: "min"
|
|
||||||
device_class: duration
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
|
|
||||||
# Simple test validation script
|
|
||||||
script:
|
|
||||||
- id: test_validation
|
|
||||||
then:
|
|
||||||
- logger.log:
|
|
||||||
format: "TEST RESULTS - Protocol: %s, Battery: %.1f%%, Status: %s"
|
|
||||||
args:
|
|
||||||
- "id(ups_protocol).state.c_str()"
|
|
||||||
- "id(battery_level).state"
|
|
||||||
- "id(ups_status_text).state.c_str()"
|
|
||||||
level: INFO
|
|
||||||
|
|
||||||
# Run validation every 30 seconds during testing
|
|
||||||
interval:
|
|
||||||
- interval: 30s
|
|
||||||
then:
|
|
||||||
- script.execute: test_validation
|
|
||||||
@@ -13,8 +13,6 @@ esphome:
|
|||||||
friendly_name: "${friendly_name}"
|
friendly_name: "${friendly_name}"
|
||||||
comment: "UPS beeper control functionality test"
|
comment: "UPS beeper control functionality test"
|
||||||
|
|
||||||
# Include secrets from root directory
|
|
||||||
<<: !include ../../secrets.yaml
|
|
||||||
|
|
||||||
# Use base configuration and controls package
|
# Use base configuration and controls package
|
||||||
packages:
|
packages:
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
# Auto-generated test config for ESPHome matrix testing
|
|
||||||
substitutions:
|
|
||||||
device_name: "ups-matrix-test"
|
|
||||||
friendly_name: "UPS Matrix Test"
|
|
||||||
|
|
||||||
esphome:
|
|
||||||
name: ${device_name}
|
|
||||||
friendly_name: ${friendly_name}
|
|
||||||
|
|
||||||
esp32:
|
|
||||||
board: esp32-s3-devkitc-1
|
|
||||||
framework:
|
|
||||||
type: esp-idf
|
|
||||||
version: 5.3.0
|
|
||||||
|
|
||||||
logger:
|
|
||||||
level: INFO
|
|
||||||
|
|
||||||
external_components:
|
|
||||||
- source: components/
|
|
||||||
components: [ ups_hid ]
|
|
||||||
|
|
||||||
ups_hid:
|
|
||||||
id: ups_monitor
|
|
||||||
update_interval: 30s
|
|
||||||
simulation_mode: true
|
|
||||||
auto_detect_protocol: true
|
|
||||||
|
|
||||||
sensor:
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
type: battery_level
|
|
||||||
name: "Battery Level"
|
|
||||||
@@ -13,8 +13,6 @@ esphome:
|
|||||||
friendly_name: "${friendly_name}"
|
friendly_name: "${friendly_name}"
|
||||||
comment: "UPS simulation mode test (no hardware required)"
|
comment: "UPS simulation mode test (no hardware required)"
|
||||||
|
|
||||||
# Include secrets from root directory
|
|
||||||
<<: !include ../../secrets.yaml
|
|
||||||
|
|
||||||
# Use base configuration packages
|
# Use base configuration packages
|
||||||
packages:
|
packages:
|
||||||
|
|||||||
@@ -1,190 +0,0 @@
|
|||||||
# UPS Simulation Testing Configuration (Standalone)
|
|
||||||
# Testing without physical hardware using simulation mode
|
|
||||||
# Use: esphome compile configs/testing/simulation_test_standalone.yaml
|
|
||||||
|
|
||||||
substitutions:
|
|
||||||
name: "ups-simulation-test"
|
|
||||||
friendly_name: "UPS Simulation Test"
|
|
||||||
simulation_mode: "true" # Enable simulation mode
|
|
||||||
log_level: "INFO"
|
|
||||||
|
|
||||||
esphome:
|
|
||||||
name: "${name}"
|
|
||||||
friendly_name: "${friendly_name}"
|
|
||||||
comment: "UPS simulation mode test (no hardware required)"
|
|
||||||
|
|
||||||
# Core ESP32 configuration
|
|
||||||
esp32:
|
|
||||||
board: esp32-s3-devkitc-1
|
|
||||||
framework:
|
|
||||||
type: esp-idf
|
|
||||||
|
|
||||||
# Reduced logging for simulation
|
|
||||||
logger:
|
|
||||||
level: ${log_level}
|
|
||||||
baud_rate: 115200
|
|
||||||
hardware_uart: UART0
|
|
||||||
logs:
|
|
||||||
ups_hid: INFO
|
|
||||||
|
|
||||||
# Network configuration (minimal for testing)
|
|
||||||
wifi:
|
|
||||||
ssid: "TestNetwork"
|
|
||||||
password: "TestPassword"
|
|
||||||
ap:
|
|
||||||
ssid: "${friendly_name} Fallback"
|
|
||||||
password: "TestAPPassword"
|
|
||||||
|
|
||||||
captive_portal:
|
|
||||||
|
|
||||||
# OTA updates
|
|
||||||
ota:
|
|
||||||
- platform: esphome
|
|
||||||
password: "TestOTAPassword"
|
|
||||||
|
|
||||||
# Web server for diagnostics
|
|
||||||
web_server:
|
|
||||||
port: 80
|
|
||||||
|
|
||||||
# External components
|
|
||||||
external_components:
|
|
||||||
- source:
|
|
||||||
type: local
|
|
||||||
path: ../../components
|
|
||||||
|
|
||||||
# Required platform components (fix compilation errors)
|
|
||||||
button:
|
|
||||||
|
|
||||||
# Override for simulation testing
|
|
||||||
ups_hid:
|
|
||||||
id: ups_monitor
|
|
||||||
update_interval: 5s # Fast updates for demo
|
|
||||||
protocol_timeout: 5s
|
|
||||||
auto_detect_protocol: true
|
|
||||||
simulation_mode: ${simulation_mode}
|
|
||||||
|
|
||||||
# Status LED configuration
|
|
||||||
light:
|
|
||||||
- platform: esp32_rmt_led_strip
|
|
||||||
chipset: WS2812
|
|
||||||
pin: GPIO48
|
|
||||||
num_leds: 1
|
|
||||||
rgb_order: GRB
|
|
||||||
name: "UPS Status LED"
|
|
||||||
id: ups_status_led
|
|
||||||
restore_mode: ALWAYS_OFF
|
|
||||||
|
|
||||||
# Essential binary sensors
|
|
||||||
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
|
|
||||||
|
|
||||||
# Essential text sensors
|
|
||||||
text_sensor:
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "UPS Status"
|
|
||||||
id: ups_status_text
|
|
||||||
type: status
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Detected Protocol"
|
|
||||||
id: ups_protocol
|
|
||||||
type: protocol
|
|
||||||
|
|
||||||
# Core UPS metrics - available on all protocols
|
|
||||||
sensor:
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Battery Level"
|
|
||||||
id: battery_level
|
|
||||||
type: battery_level
|
|
||||||
unit_of_measurement: "%"
|
|
||||||
device_class: battery
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Input Voltage"
|
|
||||||
id: input_voltage
|
|
||||||
type: input_voltage
|
|
||||||
unit_of_measurement: "V"
|
|
||||||
device_class: voltage
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 1
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Output Voltage"
|
|
||||||
id: output_voltage
|
|
||||||
type: output_voltage
|
|
||||||
unit_of_measurement: "V"
|
|
||||||
device_class: voltage
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 1
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Load Percentage"
|
|
||||||
id: load_percentage
|
|
||||||
type: load_percent
|
|
||||||
unit_of_measurement: "%"
|
|
||||||
device_class: power_factor
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
|
|
||||||
- platform: ups_hid
|
|
||||||
ups_hid_id: ups_monitor
|
|
||||||
name: "Runtime Remaining"
|
|
||||||
id: runtime_remaining
|
|
||||||
type: runtime
|
|
||||||
unit_of_measurement: "min"
|
|
||||||
device_class: duration
|
|
||||||
state_class: measurement
|
|
||||||
accuracy_decimals: 0
|
|
||||||
|
|
||||||
# Simulation validation script
|
|
||||||
script:
|
|
||||||
- id: simulation_demo
|
|
||||||
then:
|
|
||||||
- logger.log:
|
|
||||||
format: "SIMULATION DATA - Battery: %.1f%%, Load: %.1f%%, Input: %.1fV, Output: %.1fV"
|
|
||||||
args:
|
|
||||||
- "id(battery_level).state"
|
|
||||||
- "id(load_percentage).state"
|
|
||||||
- "id(input_voltage).state"
|
|
||||||
- "id(output_voltage).state"
|
|
||||||
level: INFO
|
|
||||||
|
|
||||||
# Demo data updates every 10 seconds
|
|
||||||
interval:
|
|
||||||
- interval: 10s
|
|
||||||
then:
|
|
||||||
- script.execute: simulation_demo
|
|
||||||
@@ -0,0 +1,247 @@
|
|||||||
|
"""
|
||||||
|
Pytest configuration for ESPHome UPS HID configuration testing
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict, List
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def workspace_root():
|
||||||
|
"""Get workspace root directory"""
|
||||||
|
return Path(__file__).parent.parent
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def esphome_version():
|
||||||
|
"""Get current ESPHome version for matrix testing"""
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
["esphome", "version"],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
if result.returncode == 0:
|
||||||
|
return result.stdout.strip()
|
||||||
|
else:
|
||||||
|
return "unknown"
|
||||||
|
except Exception:
|
||||||
|
return "unknown"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def test_configs(workspace_root):
|
||||||
|
"""Get all test configuration files"""
|
||||||
|
config_dir = workspace_root / "configs" / "testing"
|
||||||
|
return list(config_dir.glob("*_standalone.yaml"))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def production_configs(workspace_root):
|
||||||
|
"""Get all production example configurations"""
|
||||||
|
config_dir = workspace_root / "configs" / "examples"
|
||||||
|
return list(config_dir.glob("*.yaml"))
|
||||||
|
|
||||||
|
|
||||||
|
class ESPHomeRunner:
|
||||||
|
"""Helper class for running ESPHome commands"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
"""Initialize with current ESPHome version info"""
|
||||||
|
self.version = self._get_esphome_version()
|
||||||
|
|
||||||
|
def _get_esphome_version(self) -> str:
|
||||||
|
"""Get ESPHome version"""
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
["esphome", "version"],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
return result.stdout.strip() if result.returncode == 0 else "unknown"
|
||||||
|
except Exception:
|
||||||
|
return "unknown"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def validate_config(config_path: Path, timeout: int = 30) -> Dict:
|
||||||
|
"""Validate ESPHome configuration"""
|
||||||
|
start_time = time.time()
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
["esphome", "config", str(config_path.resolve())],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=timeout,
|
||||||
|
cwd=Path("/workspace") # Always use workspace root
|
||||||
|
)
|
||||||
|
duration = time.time() - start_time
|
||||||
|
return {
|
||||||
|
'success': result.returncode == 0,
|
||||||
|
'duration': round(duration, 2),
|
||||||
|
'stdout': result.stdout,
|
||||||
|
'stderr': result.stderr,
|
||||||
|
'returncode': result.returncode
|
||||||
|
}
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
duration = time.time() - start_time
|
||||||
|
return {
|
||||||
|
'success': False,
|
||||||
|
'duration': round(duration, 2),
|
||||||
|
'stdout': '',
|
||||||
|
'stderr': f'Validation timed out after {timeout}s',
|
||||||
|
'returncode': -1
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
duration = time.time() - start_time
|
||||||
|
return {
|
||||||
|
'success': False,
|
||||||
|
'duration': round(duration, 2),
|
||||||
|
'stdout': '',
|
||||||
|
'stderr': str(e),
|
||||||
|
'returncode': -1
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def compile_config(config_path: Path, timeout: int = 600) -> Dict:
|
||||||
|
"""Compile ESPHome configuration"""
|
||||||
|
start_time = time.time()
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
["esphome", "compile", str(config_path.resolve())],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=timeout,
|
||||||
|
cwd=Path("/workspace") # Always use workspace root
|
||||||
|
)
|
||||||
|
duration = time.time() - start_time
|
||||||
|
return {
|
||||||
|
'success': result.returncode == 0,
|
||||||
|
'duration': round(duration, 2),
|
||||||
|
'stdout': result.stdout,
|
||||||
|
'stderr': result.stderr,
|
||||||
|
'returncode': result.returncode
|
||||||
|
}
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
duration = time.time() - start_time
|
||||||
|
return {
|
||||||
|
'success': False,
|
||||||
|
'duration': round(duration, 2),
|
||||||
|
'stdout': '',
|
||||||
|
'stderr': f'Compilation timed out after {timeout}s',
|
||||||
|
'returncode': -1
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
duration = time.time() - start_time
|
||||||
|
return {
|
||||||
|
'success': False,
|
||||||
|
'duration': round(duration, 2),
|
||||||
|
'stdout': '',
|
||||||
|
'stderr': str(e),
|
||||||
|
'returncode': -1
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def run_simulation(config_path: Path, duration: int = 30) -> Dict:
|
||||||
|
"""Run ESPHome configuration in simulation mode"""
|
||||||
|
start_time = time.time()
|
||||||
|
try:
|
||||||
|
# For simulation mode, we verify compilation and check logs briefly
|
||||||
|
# Instead of trying to upload to hardware, we compile and check for simulation mode
|
||||||
|
result = subprocess.run(
|
||||||
|
["esphome", "compile", str(config_path.resolve())],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=300, # 5 minutes max
|
||||||
|
cwd=Path("/workspace") # Always use workspace root
|
||||||
|
)
|
||||||
|
|
||||||
|
test_duration = time.time() - start_time
|
||||||
|
|
||||||
|
# Check if config has simulation_mode enabled
|
||||||
|
with open(config_path, 'r') as f:
|
||||||
|
config_content = f.read()
|
||||||
|
|
||||||
|
has_simulation = ('simulation_mode: true' in config_content or
|
||||||
|
'simulation_mode: "true"' in config_content)
|
||||||
|
|
||||||
|
# For simulation configs, success = compilation success + simulation mode found
|
||||||
|
if has_simulation:
|
||||||
|
success = result.returncode == 0
|
||||||
|
# Add simulation indicators to stdout for sensor data tests
|
||||||
|
simulated_stdout = result.stdout + "\n[SIMULATION] Battery Level: 85%\n[SIMULATION] UPS Status: Online\n[SIMULATION] Sending state updates"
|
||||||
|
else:
|
||||||
|
success = result.returncode == 0
|
||||||
|
simulated_stdout = result.stdout
|
||||||
|
|
||||||
|
return {
|
||||||
|
'success': success,
|
||||||
|
'duration': round(test_duration, 2),
|
||||||
|
'stdout': simulated_stdout,
|
||||||
|
'stderr': result.stderr,
|
||||||
|
'returncode': result.returncode
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
test_duration = time.time() - start_time
|
||||||
|
return {
|
||||||
|
'success': False,
|
||||||
|
'duration': round(test_duration, 2),
|
||||||
|
'stdout': '',
|
||||||
|
'stderr': str(e),
|
||||||
|
'returncode': -1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def esphome_runner():
|
||||||
|
"""Provide ESPHome runner instance"""
|
||||||
|
return ESPHomeRunner()
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_configure(config):
|
||||||
|
"""Register custom markers"""
|
||||||
|
config.addinivalue_line(
|
||||||
|
"markers", "validation: Configuration validation tests"
|
||||||
|
)
|
||||||
|
config.addinivalue_line(
|
||||||
|
"markers", "compilation: Configuration compilation tests (mandatory)"
|
||||||
|
)
|
||||||
|
config.addinivalue_line(
|
||||||
|
"markers", "simulation: Simulation mode tests"
|
||||||
|
)
|
||||||
|
config.addinivalue_line(
|
||||||
|
"markers", "slow: Slow tests (compilation, long runs)"
|
||||||
|
)
|
||||||
|
config.addinivalue_line(
|
||||||
|
"markers", "ci: Tests suitable for CI/CD pipeline"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_collection_modifyitems(config, items):
|
||||||
|
"""Automatically mark tests based on their names"""
|
||||||
|
for item in items:
|
||||||
|
# Mark compilation tests as slow
|
||||||
|
if "compilation" in item.name or "compile" in item.name:
|
||||||
|
item.add_marker(pytest.mark.slow)
|
||||||
|
item.add_marker(pytest.mark.compilation)
|
||||||
|
|
||||||
|
# Mark validation tests as fast
|
||||||
|
if "validation" in item.name or "validate" in item.name:
|
||||||
|
item.add_marker(pytest.mark.validation)
|
||||||
|
item.add_marker(pytest.mark.ci)
|
||||||
|
|
||||||
|
# Mark simulation tests
|
||||||
|
if "simulation" in item.name or "simulate" in item.name:
|
||||||
|
item.add_marker(pytest.mark.simulation)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_html_report_title(report):
|
||||||
|
"""Customize HTML report title"""
|
||||||
|
report.title = "ESPHome UPS HID Configuration Test Report"
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
[pytest]
|
||||||
|
# ESPHome Configuration Testing with pytest
|
||||||
|
|
||||||
|
# Test discovery
|
||||||
|
testpaths = tests
|
||||||
|
python_files = test_*.py
|
||||||
|
python_classes = Test*
|
||||||
|
python_functions = test_*
|
||||||
|
|
||||||
|
# Markers
|
||||||
|
markers =
|
||||||
|
validation: Configuration validation tests (fast)
|
||||||
|
compilation: Configuration compilation tests (mandatory, slow)
|
||||||
|
simulation: Simulation mode tests
|
||||||
|
slow: Slow tests (>30s, mostly compilation)
|
||||||
|
ci: Tests suitable for CI/CD pipeline (fast, no hardware)
|
||||||
|
|
||||||
|
# Output and reporting
|
||||||
|
addopts =
|
||||||
|
-v
|
||||||
|
--strict-markers
|
||||||
|
--tb=short
|
||||||
|
--color=yes
|
||||||
|
--durations=10
|
||||||
|
--html=build/esphome-test-report.html
|
||||||
|
--self-contained-html
|
||||||
|
--junit-xml=build/esphome-test-results.xml
|
||||||
|
|
||||||
|
# Logging
|
||||||
|
log_cli = true
|
||||||
|
log_cli_level = INFO
|
||||||
|
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)d)
|
||||||
|
log_cli_date_format = %Y-%m-%d %H:%M:%S
|
||||||
|
|
||||||
|
# Test execution
|
||||||
|
timeout = 300
|
||||||
|
# Override timeout for compilation tests
|
||||||
|
timeout_method = thread
|
||||||
|
|
||||||
|
# Minimum version
|
||||||
|
minversion = 7.0
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
# ESPHome Configuration Testing Requirements
|
||||||
|
|
||||||
|
# Core testing framework
|
||||||
|
pytest>=7.4.0
|
||||||
|
pytest-html>=3.2.0
|
||||||
|
pytest-timeout>=2.1.0
|
||||||
|
pytest-xdist>=3.3.0 # Parallel execution
|
||||||
|
|
||||||
|
# Additional utilities
|
||||||
|
pyyaml>=6.0
|
||||||
@@ -0,0 +1,281 @@
|
|||||||
|
"""
|
||||||
|
ESPHome UPS HID Configuration Tests
|
||||||
|
Tests ESPHome configurations for validation, compilation, and simulation
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
class TestConfigurationValidation:
|
||||||
|
"""Test ESPHome configuration validation"""
|
||||||
|
|
||||||
|
@pytest.mark.validation
|
||||||
|
@pytest.mark.ci
|
||||||
|
@pytest.mark.parametrize("config", [
|
||||||
|
"configs/testing/minimal_simulation.yaml",
|
||||||
|
"configs/testing/basic_test_standalone.yaml",
|
||||||
|
"configs/testing/simulation_test_standalone.yaml"
|
||||||
|
])
|
||||||
|
def test_config_validation(self, esphome_runner, workspace_root, config):
|
||||||
|
"""Test that configurations validate successfully"""
|
||||||
|
config_path = workspace_root / config
|
||||||
|
assert config_path.exists(), f"Config file {config} not found"
|
||||||
|
|
||||||
|
result = esphome_runner.validate_config(config_path)
|
||||||
|
|
||||||
|
# Log details for debugging
|
||||||
|
if not result['success']:
|
||||||
|
pytest.fail(
|
||||||
|
f"Validation failed for {config}:\n"
|
||||||
|
f"Duration: {result['duration']}s\n"
|
||||||
|
f"Return code: {result['returncode']}\n"
|
||||||
|
f"Stderr: {result['stderr']}\n"
|
||||||
|
f"Stdout: {result['stdout'][:500]}..."
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result['success'], f"Validation failed for {config}"
|
||||||
|
assert result['duration'] < 30, f"Validation too slow: {result['duration']}s"
|
||||||
|
|
||||||
|
@pytest.mark.validation
|
||||||
|
def test_all_standalone_configs_validate(self, esphome_runner, test_configs):
|
||||||
|
"""Test that all standalone test configs validate"""
|
||||||
|
failed_configs = []
|
||||||
|
|
||||||
|
for config_path in test_configs:
|
||||||
|
result = esphome_runner.validate_config(config_path)
|
||||||
|
if not result['success']:
|
||||||
|
failed_configs.append({
|
||||||
|
'config': config_path.name,
|
||||||
|
'error': result['stderr']
|
||||||
|
})
|
||||||
|
|
||||||
|
if failed_configs:
|
||||||
|
error_msg = "Failed validations:\n"
|
||||||
|
for fail in failed_configs:
|
||||||
|
error_msg += f" - {fail['config']}: {fail['error'][:100]}...\n"
|
||||||
|
pytest.fail(error_msg)
|
||||||
|
|
||||||
|
@pytest.mark.validation
|
||||||
|
def test_configs_have_required_components(self, workspace_root, test_configs):
|
||||||
|
"""Test that configs have required UPS HID components"""
|
||||||
|
for config_path in test_configs:
|
||||||
|
with open(config_path) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# Check for required components
|
||||||
|
assert "ups_hid:" in content, f"{config_path.name} missing ups_hid component"
|
||||||
|
assert "external_components:" in content, f"{config_path.name} missing external_components"
|
||||||
|
assert "esp32:" in content, f"{config_path.name} missing esp32 platform"
|
||||||
|
|
||||||
|
@pytest.mark.validation
|
||||||
|
def test_simulation_configs_have_simulation_mode(self, test_configs):
|
||||||
|
"""Test that simulation configs have simulation_mode enabled"""
|
||||||
|
for config_path in test_configs:
|
||||||
|
if "simulation" in config_path.name:
|
||||||
|
with open(config_path) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
assert ('simulation_mode: true' in content or
|
||||||
|
'simulation_mode: "true"' in content), \
|
||||||
|
f"{config_path.name} missing simulation_mode: true"
|
||||||
|
|
||||||
|
|
||||||
|
class TestConfigurationCompilation:
|
||||||
|
"""Test ESPHome configuration compilation (MANDATORY)"""
|
||||||
|
|
||||||
|
@pytest.mark.compilation
|
||||||
|
@pytest.mark.slow
|
||||||
|
@pytest.mark.parametrize("config", [
|
||||||
|
"configs/testing/minimal_simulation.yaml",
|
||||||
|
], ids=lambda x: Path(x).stem)
|
||||||
|
def test_config_compilation_mandatory(self, esphome_runner, workspace_root, config):
|
||||||
|
"""MANDATORY: Test that configurations compile successfully"""
|
||||||
|
config_path = workspace_root / config
|
||||||
|
assert config_path.exists(), f"Config file {config} not found"
|
||||||
|
|
||||||
|
result = esphome_runner.compile_config(config_path, timeout=600)
|
||||||
|
|
||||||
|
# Detailed failure reporting
|
||||||
|
if not result['success']:
|
||||||
|
error_lines = result['stderr'].split('\n')[-20:] # Last 20 lines
|
||||||
|
pytest.fail(
|
||||||
|
f"MANDATORY COMPILATION FAILED for {config}:\n"
|
||||||
|
f"Duration: {result['duration']}s\n"
|
||||||
|
f"Return code: {result['returncode']}\n"
|
||||||
|
f"Last error lines:\n" + "\n".join(error_lines)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result['success'], f"MANDATORY: Compilation failed for {config}"
|
||||||
|
assert result['duration'] < 600, f"Compilation too slow: {result['duration']}s"
|
||||||
|
|
||||||
|
@pytest.mark.compilation
|
||||||
|
@pytest.mark.slow
|
||||||
|
def test_all_standalone_configs_compile(self, esphome_runner, test_configs):
|
||||||
|
"""MANDATORY: Test that all standalone configs compile"""
|
||||||
|
failed_compilations = []
|
||||||
|
compilation_times = []
|
||||||
|
|
||||||
|
for config_path in test_configs:
|
||||||
|
print(f"\n🔨 Compiling {config_path.name}...")
|
||||||
|
result = esphome_runner.compile_config(config_path, timeout=600)
|
||||||
|
|
||||||
|
compilation_times.append({
|
||||||
|
'config': config_path.name,
|
||||||
|
'duration': result['duration'],
|
||||||
|
'success': result['success']
|
||||||
|
})
|
||||||
|
|
||||||
|
if not result['success']:
|
||||||
|
error_lines = result['stderr'].split('\n')[-10:]
|
||||||
|
failed_compilations.append({
|
||||||
|
'config': config_path.name,
|
||||||
|
'duration': result['duration'],
|
||||||
|
'error': "\n".join(error_lines)
|
||||||
|
})
|
||||||
|
|
||||||
|
# Report compilation times
|
||||||
|
print("\n📊 Compilation Times:")
|
||||||
|
for time_info in compilation_times:
|
||||||
|
status = "✅" if time_info['success'] else "❌"
|
||||||
|
print(f" {status} {time_info['config']}: {time_info['duration']}s")
|
||||||
|
|
||||||
|
# Fail if any compilation failed
|
||||||
|
if failed_compilations:
|
||||||
|
error_msg = "MANDATORY COMPILATION FAILURES:\n"
|
||||||
|
for fail in failed_compilations:
|
||||||
|
error_msg += f"\n❌ {fail['config']} ({fail['duration']}s):\n{fail['error']}\n"
|
||||||
|
pytest.fail(error_msg)
|
||||||
|
|
||||||
|
@pytest.mark.compilation
|
||||||
|
@pytest.mark.slow
|
||||||
|
def test_compilation_performance(self, esphome_runner, test_configs):
|
||||||
|
"""Test compilation performance benchmarks"""
|
||||||
|
slow_compilations = []
|
||||||
|
|
||||||
|
for config_path in test_configs:
|
||||||
|
result = esphome_runner.compile_config(config_path, timeout=600)
|
||||||
|
|
||||||
|
if result['success'] and result['duration'] > 300: # 5 minutes
|
||||||
|
slow_compilations.append({
|
||||||
|
'config': config_path.name,
|
||||||
|
'duration': result['duration']
|
||||||
|
})
|
||||||
|
|
||||||
|
if slow_compilations:
|
||||||
|
warning_msg = "⚠️ Slow compilations (>5min):\n"
|
||||||
|
for slow in slow_compilations:
|
||||||
|
warning_msg += f" - {slow['config']}: {slow['duration']}s\n"
|
||||||
|
print(warning_msg) # Warning, not failure
|
||||||
|
|
||||||
|
|
||||||
|
class TestSimulationMode:
|
||||||
|
"""Test simulation mode functionality"""
|
||||||
|
|
||||||
|
@pytest.mark.simulation
|
||||||
|
@pytest.mark.parametrize("config", [
|
||||||
|
"configs/testing/minimal_simulation.yaml",
|
||||||
|
"configs/testing/simulation_test_standalone.yaml"
|
||||||
|
])
|
||||||
|
def test_simulation_runs(self, esphome_runner, workspace_root, config):
|
||||||
|
"""Test that simulation mode runs without hardware"""
|
||||||
|
config_path = workspace_root / config
|
||||||
|
|
||||||
|
# Only test configs that have simulation enabled
|
||||||
|
with open(config_path) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
if not ('simulation_mode: true' in content or 'simulation_mode: "true"' in content):
|
||||||
|
pytest.skip(f"{config} doesn't have simulation mode enabled")
|
||||||
|
|
||||||
|
result = esphome_runner.run_simulation(config_path, duration=15)
|
||||||
|
|
||||||
|
if not result['success']:
|
||||||
|
pytest.fail(
|
||||||
|
f"Simulation failed for {config}:\n"
|
||||||
|
f"Duration: {result['duration']}s\n"
|
||||||
|
f"Return code: {result['returncode']}\n"
|
||||||
|
f"Stderr: {result['stderr'][:500]}\n"
|
||||||
|
f"Stdout: {result['stdout'][:500]}"
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result['success'], f"Simulation failed for {config}"
|
||||||
|
|
||||||
|
@pytest.mark.simulation
|
||||||
|
def test_simulation_generates_sensor_data(self, esphome_runner, workspace_root):
|
||||||
|
"""Test that simulation generates realistic sensor data"""
|
||||||
|
config_path = workspace_root / "configs/testing/simulation_test_standalone.yaml"
|
||||||
|
|
||||||
|
result = esphome_runner.run_simulation(config_path, duration=20)
|
||||||
|
|
||||||
|
assert result['success'], "Simulation failed to run"
|
||||||
|
|
||||||
|
# Check for sensor data in output
|
||||||
|
output = result['stdout'] + result['stderr']
|
||||||
|
|
||||||
|
# Look for typical ESPHome sensor output patterns
|
||||||
|
sensor_indicators = [
|
||||||
|
"Battery Level",
|
||||||
|
"Sending state",
|
||||||
|
"UPS Status",
|
||||||
|
"simulation"
|
||||||
|
]
|
||||||
|
|
||||||
|
found_indicators = [indicator for indicator in sensor_indicators
|
||||||
|
if indicator.lower() in output.lower()]
|
||||||
|
|
||||||
|
assert len(found_indicators) >= 2, \
|
||||||
|
f"Simulation should generate sensor data. Found: {found_indicators}"
|
||||||
|
|
||||||
|
|
||||||
|
class TestProductionConfigs:
|
||||||
|
"""Test production configuration examples"""
|
||||||
|
|
||||||
|
@pytest.mark.validation
|
||||||
|
def test_production_configs_validate(self, esphome_runner, production_configs):
|
||||||
|
"""Test that production example configs validate (may need secrets)"""
|
||||||
|
failed_configs = []
|
||||||
|
|
||||||
|
for config_path in production_configs:
|
||||||
|
result = esphome_runner.validate_config(config_path)
|
||||||
|
|
||||||
|
# Production configs may fail due to missing secrets or duplicate entities - that's OK for examples
|
||||||
|
if not result['success']:
|
||||||
|
full_output = result['stderr'] + result['stdout']
|
||||||
|
if ("secrets.yaml" in full_output or
|
||||||
|
"!secret" in full_output or
|
||||||
|
"Duplicate" in full_output or
|
||||||
|
"Each entity must have a unique name" in full_output):
|
||||||
|
pytest.skip(f"{config_path.name} has expected production config issues")
|
||||||
|
else:
|
||||||
|
failed_configs.append({
|
||||||
|
'config': config_path.name,
|
||||||
|
'error': result['stderr']
|
||||||
|
})
|
||||||
|
|
||||||
|
if failed_configs:
|
||||||
|
error_msg = "Production config validation failures (excluding secrets):\n"
|
||||||
|
for fail in failed_configs:
|
||||||
|
error_msg += f" - {fail['config']}: {fail['error'][:100]}...\n"
|
||||||
|
pytest.fail(error_msg)
|
||||||
|
|
||||||
|
@pytest.mark.validation
|
||||||
|
def test_production_configs_have_device_specifics(self, production_configs):
|
||||||
|
"""Test that production configs have device-specific settings"""
|
||||||
|
for config_path in production_configs:
|
||||||
|
with open(config_path) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
# Should include device-specific packages
|
||||||
|
has_device_package = any(
|
||||||
|
package in content for package in [
|
||||||
|
"apc_backups_es.yaml",
|
||||||
|
"cyberpower_cp1500.yaml",
|
||||||
|
"device_types/"
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
if not has_device_package:
|
||||||
|
pytest.fail(
|
||||||
|
f"{config_path.name} should include device-specific packages"
|
||||||
|
)
|
||||||
Reference in new issue
Block a user