feat(nut-server): overwrite UPS name

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-09-01 22:12:44 +02:00
1 parent 7ee6783fd7
commit 564bcc0f86
4 files changed
+27 -18

No files matched your search

+7 -2
View File
@@ -15,6 +15,7 @@ MULTI_CONF = False
CONF_UPS_HID_ID = "ups_hid_id" CONF_UPS_HID_ID = "ups_hid_id"
CONF_MAX_CLIENTS = "max_clients" CONF_MAX_CLIENTS = "max_clients"
CONF_UPS_NAME = "ups_name"
nut_server_ns = cg.esphome_ns.namespace("nut_server") nut_server_ns = cg.esphome_ns.namespace("nut_server")
NutServerComponent = nut_server_ns.class_("NutServerComponent", cg.Component) NutServerComponent = nut_server_ns.class_("NutServerComponent", cg.Component)
@@ -31,6 +32,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional(CONF_USERNAME, default="nutuser"): cv.string, cv.Optional(CONF_USERNAME, default="nutuser"): cv.string,
cv.Optional(CONF_PASSWORD, default=""): cv.string, cv.Optional(CONF_PASSWORD, default=""): cv.string,
cv.Optional(CONF_MAX_CLIENTS, default=4): cv.int_range(min=1, max=10), cv.Optional(CONF_MAX_CLIENTS, default=4): cv.int_range(min=1, max=10),
cv.Optional(CONF_UPS_NAME): cv.string,
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@@ -67,6 +69,9 @@ async def to_code(config):
# Set max clients # Set max clients
cg.add(var.set_max_clients(config[CONF_MAX_CLIENTS])) cg.add(var.set_max_clients(config[CONF_MAX_CLIENTS]))
# Set UPS name automatically from the UPS HID component ID # Set UPS name - use custom name if provided, otherwise use component ID
ups_name = str(config[CONF_UPS_HID_ID]) if CONF_UPS_NAME in config:
ups_name = config[CONF_UPS_NAME]
else:
ups_name = str(config[CONF_UPS_HID_ID])
cg.add(var.set_ups_name(ups_name)) cg.add(var.set_ups_name(ups_name))
+4
View File
@@ -10,6 +10,10 @@ substitutions:
# Override timezone if needed (defaults to Europe/Warsaw from regional template) # Override timezone if needed (defaults to Europe/Warsaw from regional template)
timezone: "Europe/Vienna" # Austrian timezone timezone: "Europe/Vienna" # Austrian timezone
# My QNAP NAS acts as a slave and forces these settings
nut_password: "" # disable nutpassword
nut_ups_name: "qnapups" # Custom name for NUT protocol (different from component ID)
esphome: esphome:
name: "${name}" name: "${name}"
friendly_name: "${friendly_name}" friendly_name: "${friendly_name}"
+8 -8
View File
@@ -9,7 +9,7 @@ substitutions:
nut_username: "nutuser" # Username for authentication nut_username: "nutuser" # Username for authentication
nut_password: "nutpass" # Password (leave empty to disable auth) nut_password: "nutpass" # Password (leave empty to disable auth)
nut_max_clients: "4" # Maximum simultaneous clients (1-10) nut_max_clients: "4" # Maximum simultaneous clients (1-10)
# UPS name is now automatically derived from the ups_hid component ID nut_ups_name: "ups_monitor" # NUT protocol UPS name (optional, defaults to component ID)
# Network Configuration (static IP recommended for servers) # Network Configuration (static IP recommended for servers)
use_static_ip: "false" # Set to "true" to enable static IP use_static_ip: "false" # Set to "true" to enable static IP
@@ -20,12 +20,12 @@ substitutions:
# NUT Server Component # NUT Server Component
nut_server: nut_server:
ups_hid_id: ups_monitor ups_hid_id: ups_monitor # UPS HID component ID (fixed)
port: ${nut_port} port: ${nut_port}
username: "${nut_username}" username: "${nut_username}"
password: "${nut_password}" password: "${nut_password}"
max_clients: ${nut_max_clients} max_clients: ${nut_max_clients}
# UPS name is automatically derived from ups_hid component ID (ups_monitor) ups_name: "${nut_ups_name}" # Custom NUT protocol name (optional)
# Static IP configuration (uncomment to enable) # Static IP configuration (uncomment to enable)
# Recommended for NUT server to ensure consistent network access # Recommended for NUT server to ensure consistent network access
@@ -72,7 +72,7 @@ text_sensor:
id: nut_server_info id: nut_server_info
icon: "mdi:information-outline" icon: "mdi:information-outline"
lambda: |- lambda: |-
std::string info = "UPS: ups_monitor, Port: ${nut_port}"; std::string info = "UPS: ${nut_ups_name}, Port: ${nut_port}";
if (std::string("${nut_password}").empty()) { if (std::string("${nut_password}").empty()) {
info += ", Auth: Disabled"; info += ", Auth: Disabled";
} else { } else {
@@ -109,12 +109,12 @@ interval:
# Connect with NUT client tools: # Connect with NUT client tools:
# #
# 1. Using upsc command: # 1. Using upsc command:
# upsc ups_monitor@<esp32_ip>:${nut_port} # upsc ${nut_ups_name}@<esp32_ip>:${nut_port}
# upsc -u ${nut_username} -p ${nut_password} ups_monitor@<esp32_ip>:${nut_port} # upsc -u ${nut_username} -p ${nut_password} ${nut_ups_name}@<esp32_ip>:${nut_port}
# #
# 2. Using telnet for testing: # 2. Using telnet for testing:
# telnet <esp32_ip> ${nut_port} # telnet <esp32_ip> ${nut_port}
# Commands: HELP, VERSION, LOGIN ${nut_username} ${nut_password}, LIST UPS, LIST VAR ups_monitor # Commands: HELP, VERSION, LOGIN ${nut_username} ${nut_password}, LIST UPS, LIST VAR ${nut_ups_name}
# #
# 3. Home Assistant NUT integration: # 3. Home Assistant NUT integration:
# sensor: # sensor:
@@ -135,7 +135,7 @@ interval:
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.connect(('<esp32_ip>', ${nut_port})) # sock.connect(('<esp32_ip>', ${nut_port}))
# sock.send(b"LOGIN ${nut_username} ${nut_password}\n") # sock.send(b"LOGIN ${nut_username} ${nut_password}\n")
# sock.send(b"LIST VAR ups_monitor\n") # sock.send(b"LIST VAR ${nut_ups_name}\n")
# response = sock.recv(4096).decode() # response = sock.recv(4096).decode()
# print(response) # print(response)
# sock.close() # sock.close()
+8 -8
View File
@@ -9,7 +9,7 @@ substitutions:
nut_username: "nutuser" # Username for authentication nut_username: "nutuser" # Username for authentication
nut_password: "nutpass" # Password (leave empty to disable auth) nut_password: "nutpass" # Password (leave empty to disable auth)
nut_max_clients: "4" # Maximum simultaneous clients (1-10) nut_max_clients: "4" # Maximum simultaneous clients (1-10)
# UPS name is now automatically derived from the ups_hid component ID nut_ups_name: "ups_monitor" # NUT protocol UPS name (optional, defaults to component ID)
# Network Configuration (static IP recommended for servers) # Network Configuration (static IP recommended for servers)
use_static_ip: "false" # Set to "true" to enable static IP use_static_ip: "false" # Set to "true" to enable static IP
@@ -25,7 +25,7 @@ nut_server:
username: "${nut_username}" username: "${nut_username}"
password: "${nut_password}" password: "${nut_password}"
max_clients: ${nut_max_clients} max_clients: ${nut_max_clients}
# UPS name is automatically derived from ups_hid component ID (ups_monitor) ups_name: "${nut_ups_name}" # Custom NUT protocol name (optional)
# Static IP configuration (uncomment to enable) # Static IP configuration (uncomment to enable)
# Recommended for NUT server to ensure consistent network access # Recommended for NUT server to ensure consistent network access
@@ -80,7 +80,7 @@ text_sensor:
sorting_group_id: nut_server_group sorting_group_id: nut_server_group
sorting_weight: 2 sorting_weight: 2
lambda: |- lambda: |-
std::string info = "UPS: ups_monitor, Port: ${nut_port}"; std::string info = "UPS: ${nut_ups_name}, Port: ${nut_port}";
if (std::string("${nut_password}").empty()) { if (std::string("${nut_password}").empty()) {
info += ", Auth: Disabled"; info += ", Auth: Disabled";
} else { } else {
@@ -100,7 +100,7 @@ text_sensor:
sorting_weight: 3 sorting_weight: 3
lambda: |- lambda: |-
// Provide ready-to-use connection string for clients // Provide ready-to-use connection string for clients
return "ups_monitor@" + WiFi.localIP().toString().c_str() + ":${nut_port}"; return "${nut_ups_name}@" + WiFi.localIP().toString().c_str() + ":${nut_port}";
sensor: sensor:
# NUT server connected clients counter (future feature placeholder) # NUT server connected clients counter (future feature placeholder)
@@ -157,12 +157,12 @@ interval:
# Connect with NUT client tools: # Connect with NUT client tools:
# #
# 1. Using upsc command: # 1. Using upsc command:
# upsc ups_monitor@<esp32_ip>:${nut_port} # upsc ${nut_ups_name}@<esp32_ip>:${nut_port}
# upsc -u ${nut_username} -p ${nut_password} ups_monitor@<esp32_ip>:${nut_port} # upsc -u ${nut_username} -p ${nut_password} ${nut_ups_name}@<esp32_ip>:${nut_port}
# #
# 2. Using telnet for testing: # 2. Using telnet for testing:
# telnet <esp32_ip> ${nut_port} # telnet <esp32_ip> ${nut_port}
# Commands: HELP, VERSION, LOGIN ${nut_username} ${nut_password}, LIST UPS, LIST VAR ups_monitor # Commands: HELP, VERSION, LOGIN ${nut_username} ${nut_password}, LIST UPS, LIST VAR ${nut_ups_name}
# #
# 3. Home Assistant NUT integration: # 3. Home Assistant NUT integration:
# sensor: # sensor:
@@ -183,7 +183,7 @@ interval:
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.connect(('<esp32_ip>', ${nut_port})) # sock.connect(('<esp32_ip>', ${nut_port}))
# sock.send(b"LOGIN ${nut_username} ${nut_password}\n") # sock.send(b"LOGIN ${nut_username} ${nut_password}\n")
# sock.send(b"LIST VAR ups_monitor\n") # sock.send(b"LIST VAR ${nut_ups_name}\n")
# response = sock.recv(4096).decode() # response = sock.recv(4096).decode()
# print(response) # print(response)
# sock.close() # sock.close()