feat(nut-server): nut v2.8 compatibile implementation

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-09-01 16:38:16 +02:00
1 parent 5ac47b9462
commit efba7a84be
14 files changed
+2307 -11

No files matched your search

+35
View File
@@ -108,6 +108,40 @@ UPS delay configuration package (3 number entities + automation scripts):
**Scripts**: Default delay application and configuration reading
**Home Assistant**: Number entities with proper device classes and validation
#### `nut_server.yaml` / `nut_server_grouped.yaml`
Network UPS Tools (NUT) TCP server for standard protocol integration:
**Server Features**:
- **Standard NUT Protocol**: v1.3 compliant TCP server on port 3493
- **Authentication**: Optional username/password protection
- **Multi-client Support**: Up to 4 simultaneous connections
- **Dynamic UPS Discovery**: Automatically exposes connected UPS data
**Protocol Support**:
- `LIST UPS` - Available UPS devices with dynamic description
- `LIST VAR` - All UPS variables with real-time data
- `LIST CMD` - Available instant commands
- `LIST CLIENT` - Connected monitoring clients
- `INSTCMD` - Execute UPS commands (beeper, tests)
- `NETVER` - Protocol version negotiation
**Data Integration**:
- Uses Data Provider Pattern from ups_hid component
- Maps UPS data to standard NUT variables (battery.charge, input.voltage, etc.)
- Dynamic manufacturer/model detection from connected device
**Client Compatibility**:
- Standard NUT tools (`upsc`, `upscmd`)
- Home Assistant NUT integration
- Any NUT-compatible monitoring software
- TCP plain-text protocol (no SSL/TLS required)
**Grouped Version Features**:
- **NUT Server Group**: All NUT-related entities in dedicated "🌐 NUT Server" section
- **Enhanced Sensors**: Connection string helper, client counter placeholder
- **Entity Categories**: Marked as diagnostic for proper Home Assistant organization
- **Web Interface**: Clean separation from UPS monitoring sensors
#### `entity_groups.yaml`
ESPHome Web Server v3 entity organization (10 logical groups):
@@ -144,6 +178,7 @@ CyberPower CP1500 series optimizations:
| **Minimal** | `base_ups` + `essential_sensors` | **9 sensors** | Basic monitoring |
| **Complete** | + `extended_sensors` + `ups_controls` + `timer_sensors` | **32 sensors + 10 controls** | Full featured |
| **Complete + Config** | + `delay_config` | **32 sensors + 10 controls + 3 number entities** | With UPS configuration |
| **Complete + NUT Server** | + `nut_server` | **32 sensors + 10 controls + TCP server** | Network monitoring integration |
| **Device-Optimized** | + device-specific package | **33+ sensors + 13+ controls** | Production ready |
| **Organized Interface** | Use `*_grouped.yaml` + `entity_groups.yaml` | **Same sensors, better layout** | Enhanced web interface |
+3 -1
View File
@@ -31,7 +31,9 @@ logger:
baud_rate: 115200
hardware_uart: UART0
logs:
ups_hid: DEBUG
ups_hid: INFO
nut_server: DEBUG
# Network configuration
wifi:
+7
View File
@@ -42,6 +42,13 @@ packages:
# APC-specific optimizations
apc_device: !include ../device_types/apc_backups_es.yaml
# NUT Server (Network UPS Tools) - uncomment to enable TCP server
# Exposes UPS data via standard NUT protocol for monitoring tools
# Standard package:
nut_server: !include ../nut_server.yaml
# OR grouped package (with NUT Server group in web interface):
# nut_server_grouped: !include ../nut_server_grouped.yaml
# Optional: Home Assistant API (uncomment to enable)
# api:
+22 -10
View File
@@ -17,26 +17,38 @@ packages:
# Regional defaults (North American voltage/frequency standards)
regional_defaults: !include ../regional/north_american_base.yaml
# OPTIONAL: Entity grouping (improves web interface organization)
# Uncomment to enable organized entity groups in ESPHome web interface
# entity_groups: !include ../entity_groups.yaml
# Core functionality
base_ups: !include ../base_ups.yaml
# Choose EITHER grouped OR standard sensor packages (not both):
# Standard packages (current default):
essential: !include ../essential_sensors.yaml
extended: !include ../extended_sensors.yaml # Recommended for CyberPower - rich sensor support
timers: !include ../timer_sensors.yaml # Real-time shutdown/startup monitoring
controls: !include ../ups_controls.yaml # Recommended - full beeper/test control
# Extended monitoring (recommended for CyberPower - rich sensor support)
extended: !include ../extended_sensors.yaml
# Timer countdown sensors (Real-time shutdown/startup monitoring)
timers: !include ../timer_sensors.yaml
# Control buttons (recommended - full beeper/test control)
controls: !include ../ups_controls.yaml
# OR grouped packages (better organization):
# essential_grouped: !include ../essential_sensors_grouped.yaml
# extended_grouped: !include ../extended_sensors_grouped.yaml
# timers_grouped: !include ../timer_sensors_grouped.yaml
# controls_grouped: !include ../ups_controls_grouped.yaml
# Delay configuration (Shutdown/Start/Reboot timers)
delay_config: !include ../delay_config.yaml
# CyberPower-specific optimizations
cyberpower_device: !include ../device_types/cyberpower_cp1500.yaml
controls_grouped: !include ../ups_controls_grouped.yaml
# NUT Server (Network UPS Tools) - uncomment to enable TCP server
# Exposes UPS data via standard NUT protocol for monitoring tools
# Standard package:
# nut_server: !include ../nut_server.yaml
# OR grouped package (with NUT Server group in web interface):
# nut_server_grouped: !include ../nut_server_grouped.yaml
# Optional: Home Assistant API (uncomment to enable)
# api:
+7
View File
@@ -45,6 +45,13 @@ packages:
# CyberPower-specific optimizations
cyberpower_device: !include ../device_types/cyberpower_cp1500.yaml
# NUT Server (Network UPS Tools) - uncomment to enable TCP server
# Exposes UPS data via standard NUT protocol for monitoring tools
# Standard package:
nut_server: !include ../nut_server.yaml
# OR grouped package (with NUT Server group in web interface):
# nut_server_grouped: !include ../nut_server_grouped.yaml
# Optional: Home Assistant API (uncomment to enable)
# api:
+147
View File
@@ -0,0 +1,147 @@
# NUT Server Configuration Package
# Network UPS Tools (NUT) TCP server for exposing UPS data over standard NUT protocol
# Use with: packages: nut_server: !include configs/nut_server.yaml
# Substitutions with sensible defaults (can be overridden in parent config)
substitutions:
# NUT Server Configuration
nut_port: "3493" # Standard NUT port
nut_username: "nutuser" # Username for authentication
nut_password: "nutpass" # Password (leave empty to disable auth)
nut_max_clients: "4" # Maximum simultaneous clients (1-10)
# UPS name is now automatically derived from the ups_hid component ID
# Network Configuration (static IP recommended for servers)
use_static_ip: "false" # Set to "true" to enable static IP
static_ip: "192.168.1.100" # Static IP address
static_gateway: "192.168.1.1" # Gateway address
static_subnet: "255.255.255.0" # Subnet mask
static_dns1: "192.168.1.1" # Primary DNS server
# NUT Server Component
nut_server:
ups_hid_id: ups_monitor
port: ${nut_port}
username: "${nut_username}"
password: "${nut_password}"
max_clients: ${nut_max_clients}
# UPS name is automatically derived from ups_hid component ID (ups_monitor)
# Static IP configuration (uncomment to enable)
# Recommended for NUT server to ensure consistent network access
# Uncomment the following wifi section and customize the IP addresses:
#
# wifi:
# manual_ip:
# static_ip: ${static_ip}
# gateway: ${static_gateway}
# subnet: ${static_subnet}
# dns1: ${static_dns1}
# Enhanced logging for NUT server
logger:
logs:
nut_server: DEBUG # Set to DEBUG for troubleshooting
# Status indicators for NUT server
binary_sensor:
# Template sensors to indicate NUT server status
- platform: template
name: "NUT Server Active"
id: nut_server_status
icon: "mdi:network-outline"
lambda: |-
// NUT server is active if UPS is connected and has valid data
return id(ups_monitor).is_connected();
on_state:
then:
- if:
condition:
binary_sensor.is_on: nut_server_status
then:
- logger.log:
format: "NUT Server: Online - Serving UPS on port ${nut_port}"
level: INFO
else:
- logger.log: "NUT Server: Offline - UPS not connected or no valid data"
text_sensor:
# NUT server connection info
- platform: template
name: "NUT Server Info"
id: nut_server_info
icon: "mdi:information-outline"
lambda: |-
std::string info = "UPS: ups_monitor, Port: ${nut_port}";
if (std::string("${nut_password}").empty()) {
info += ", Auth: Disabled";
} else {
info += ", Auth: Enabled (${nut_username})";
}
info += ", Max Clients: ${nut_max_clients}";
return info;
# Automation for NUT server status monitoring
script:
- id: nut_server_status_check
then:
- lambda: |-
// Log current NUT server status and UPS data availability using data provider pattern
if (id(ups_monitor).is_connected()) {
ESP_LOGI("nut_server", "Status Check: UPS Connected=YES, Data Available=YES");
ESP_LOGI("nut_server", "Available Data: Battery=%.1f%%, Input=%.1fV, Load=%.1f%%, Status=%s",
id(ups_monitor).get_battery_level(),
id(ups_monitor).get_input_voltage(),
id(ups_monitor).get_load_percent(),
id(ups_monitor).is_online() ? "Online" : "On Battery");
} else {
ESP_LOGW("nut_server", "Status Check: UPS not connected - NUT server inactive");
}
# Status monitoring interval
interval:
- interval: 60s
then:
- script.execute: nut_server_status_check
# Client connection examples and documentation (commented)
#
# Connect with NUT client tools:
#
# 1. Using upsc command:
# upsc ups_monitor@<esp32_ip>:${nut_port}
# upsc -u ${nut_username} -p ${nut_password} ups_monitor@<esp32_ip>:${nut_port}
#
# 2. Using telnet for testing:
# telnet <esp32_ip> ${nut_port}
# Commands: HELP, VERSION, LOGIN ${nut_username} ${nut_password}, LIST UPS, LIST VAR ups_monitor
#
# 3. Home Assistant NUT integration:
# sensor:
# - platform: nut
# host: <esp32_ip>
# port: ${nut_port}
# username: ${nut_username}
# password: ${nut_password}
# resources:
# - battery.charge
# - battery.runtime
# - input.voltage
# - ups.load
# - ups.status
#
# 4. Python client example:
# import socket
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.connect(('<esp32_ip>', ${nut_port}))
# sock.send(b"LOGIN ${nut_username} ${nut_password}\n")
# sock.send(b"LIST VAR ups_monitor\n")
# response = sock.recv(4096).decode()
# print(response)
# sock.close()
#
# Security Notes:
# - Use strong passwords in production
# - Consider firewall rules to limit access
# - Static IP recommended for consistent server access
# - Monitor logs for authentication failures
+195
View File
@@ -0,0 +1,195 @@
# NUT Server Configuration Package (Grouped Version)
# Network UPS Tools (NUT) TCP server with organized entity grouping
# Use with: packages: nut_server_grouped: !include configs/nut_server_grouped.yaml
# Substitutions with sensible defaults (can be overridden in parent config)
substitutions:
# NUT Server Configuration
nut_port: "3493" # Standard NUT port
nut_username: "nutuser" # Username for authentication
nut_password: "nutpass" # Password (leave empty to disable auth)
nut_max_clients: "4" # Maximum simultaneous clients (1-10)
# UPS name is now automatically derived from the ups_hid component ID
# Network Configuration (static IP recommended for servers)
use_static_ip: "false" # Set to "true" to enable static IP
static_ip: "192.168.1.100" # Static IP address
static_gateway: "192.168.1.1" # Gateway address
static_subnet: "255.255.255.0" # Subnet mask
static_dns1: "192.168.1.1" # Primary DNS server
# NUT Server Component
nut_server:
ups_hid_id: ups_monitor
port: ${nut_port}
username: "${nut_username}"
password: "${nut_password}"
max_clients: ${nut_max_clients}
# UPS name is automatically derived from ups_hid component ID (ups_monitor)
# Static IP configuration (uncomment to enable)
# Recommended for NUT server to ensure consistent network access
# Uncomment the following wifi section and customize the IP addresses:
#
# wifi:
# manual_ip:
# static_ip: ${static_ip}
# gateway: ${static_gateway}
# subnet: ${static_subnet}
# dns1: ${static_dns1}
# Enhanced logging for NUT server
logger:
logs:
nut_server: DEBUG # Set to DEBUG for troubleshooting
# Status indicators for NUT server - GROUPED VERSION
binary_sensor:
# Template sensors to indicate NUT server status
- platform: template
name: "NUT Server Active"
id: nut_server_status
icon: "mdi:network-outline"
entity_category: diagnostic
web_server:
sorting_group_id: nut_server_group
sorting_weight: 1
lambda: |-
// NUT server is active if UPS is connected and has valid data
return id(ups_monitor).is_connected();
on_state:
then:
- if:
condition:
binary_sensor.is_on: nut_server_status
then:
- logger.log:
format: "NUT Server: Online - Serving UPS on port ${nut_port}"
level: INFO
else:
- logger.log: "NUT Server: Offline - UPS not connected or no valid data"
text_sensor:
# NUT server connection info
- platform: template
name: "NUT Server Info"
id: nut_server_info
icon: "mdi:information-outline"
entity_category: diagnostic
web_server:
sorting_group_id: nut_server_group
sorting_weight: 2
lambda: |-
std::string info = "UPS: ups_monitor, Port: ${nut_port}";
if (std::string("${nut_password}").empty()) {
info += ", Auth: Disabled";
} else {
info += ", Auth: Enabled (${nut_username})";
}
info += ", Max Clients: ${nut_max_clients}";
return info;
# NUT server connection string for easy client setup
- platform: template
name: "NUT Connection String"
id: nut_connection_string
icon: "mdi:connection"
entity_category: diagnostic
web_server:
sorting_group_id: nut_server_group
sorting_weight: 3
lambda: |-
// Provide ready-to-use connection string for clients
return "ups_monitor@" + WiFi.localIP().toString().c_str() + ":${nut_port}";
sensor:
# NUT server connected clients counter (future feature placeholder)
- platform: template
name: "NUT Connected Clients"
id: nut_connected_clients
icon: "mdi:account-multiple"
unit_of_measurement: "clients"
accuracy_decimals: 0
entity_category: diagnostic
web_server:
sorting_group_id: nut_server_group
sorting_weight: 4
lambda: |-
// TODO: Implement client counter in nut_server component
// For now, return 0 if server inactive, -1 if active but no counter
if (!id(ups_monitor).is_connected()) {
return 0.0f;
}
return NAN; // Not yet implemented
# Define the NUT Server group for ESPHome Web Server v3
web_server:
sorting_groups:
- id: nut_server_group
name: "🌐 NUT Server"
sorting_weight: 85 # Place after Device Info (80) but before System (90)
# Automation for NUT server status monitoring
script:
- id: nut_server_status_check
then:
- lambda: |-
// Log current NUT server status and UPS data availability using data provider pattern
if (id(ups_monitor).is_connected()) {
ESP_LOGI("nut_server", "Status Check: UPS Connected=YES, Data Available=YES");
ESP_LOGI("nut_server", "Available Data: Battery=%.1f%%, Input=%.1fV, Load=%.1f%%, Status=%s",
id(ups_monitor).get_battery_level(),
id(ups_monitor).get_input_voltage(),
id(ups_monitor).get_load_percent(),
id(ups_monitor).is_online() ? "Online" : "On Battery");
} else {
ESP_LOGW("nut_server", "Status Check: UPS not connected - NUT server inactive");
}
# Status monitoring interval
interval:
- interval: 60s
then:
- script.execute: nut_server_status_check
# Client connection examples and documentation (commented)
#
# Connect with NUT client tools:
#
# 1. Using upsc command:
# upsc ups_monitor@<esp32_ip>:${nut_port}
# upsc -u ${nut_username} -p ${nut_password} ups_monitor@<esp32_ip>:${nut_port}
#
# 2. Using telnet for testing:
# telnet <esp32_ip> ${nut_port}
# Commands: HELP, VERSION, LOGIN ${nut_username} ${nut_password}, LIST UPS, LIST VAR ups_monitor
#
# 3. Home Assistant NUT integration:
# sensor:
# - platform: nut
# host: <esp32_ip>
# port: ${nut_port}
# username: ${nut_username}
# password: ${nut_password}
# resources:
# - battery.charge
# - battery.runtime
# - input.voltage
# - ups.load
# - ups.status
#
# 4. Python client example:
# import socket
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.connect(('<esp32_ip>', ${nut_port}))
# sock.send(b"LOGIN ${nut_username} ${nut_password}\n")
# sock.send(b"LIST VAR ups_monitor\n")
# response = sock.recv(4096).decode()
# print(response)
# sock.close()
#
# Security Notes:
# - Use strong passwords in production
# - Consider firewall rules to limit access
# - Static IP recommended for consistent server access
# - Monitor logs for authentication failures
+66
View File
@@ -0,0 +1,66 @@
# NUT Server Test Configuration
# Tests the NUT server component with UPS HID integration
#
# This configuration exposes UPS data via NUT protocol on port 3493
# Connect with: upsc test_ups@<esp32_ip>
# Or telnet: telnet <esp32_ip> 3493
substitutions:
name: "ups-nut-server"
friendly_name: "UPS NUT Server"
simulation_mode: true
esphome:
name: "${name}"
friendly_name: "${friendly_name}"
# External components
external_components:
- source:
type: local
path: ../../components
# Logging
logger:
level: DEBUG
logs:
nut_server: DEBUG
ups_hid: INFO
# Use base UPS configuration (now has optional sensors)
packages:
base_ups: !include ../base_ups.yaml
# NUT Server Component Configuration
nut_server:
ups_hid_id: ups_monitor
port: 3493
username: "nutmon"
password: "nutsecret"
max_clients: 4
# Add basic sensors to ensure sensor platform is loaded
sensor:
- platform: ups_hid
ups_hid_id: ups_monitor
name: "Test Battery Level"
type: battery_level
binary_sensor:
- platform: ups_hid
ups_hid_id: ups_monitor
name: "Test UPS Online"
type: online
text_sensor:
- platform: ups_hid
ups_hid_id: ups_monitor
name: "Test UPS Status"
type: status
button:
- platform: ups_hid
ups_hid_id: ups_monitor
name: "Test Beeper Mute"
beeper_action: mute