From 564bcc0f866126fb15a551479a820e5e47debba9 Mon Sep 17 00:00:00 2001 From: bullshit Date: Mon, 1 Sep 2025 22:12:44 +0200 Subject: [PATCH] feat(nut-server): overwrite UPS name Signed-off-by: bullshit --- components/nut_server/__init__.py | 9 +++++++-- configs/examples/rack-ups-monitor.yaml | 4 ++++ configs/nut_server.yaml | 16 ++++++++-------- configs/nut_server_grouped.yaml | 16 ++++++++-------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/components/nut_server/__init__.py b/components/nut_server/__init__.py index 3538547..301895d 100644 --- a/components/nut_server/__init__.py +++ b/components/nut_server/__init__.py @@ -15,6 +15,7 @@ MULTI_CONF = False CONF_UPS_HID_ID = "ups_hid_id" CONF_MAX_CLIENTS = "max_clients" +CONF_UPS_NAME = "ups_name" nut_server_ns = cg.esphome_ns.namespace("nut_server") 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_PASSWORD, default=""): cv.string, 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) @@ -67,6 +69,9 @@ async def to_code(config): # Set max clients cg.add(var.set_max_clients(config[CONF_MAX_CLIENTS])) - # Set UPS name automatically from the UPS HID component ID - ups_name = str(config[CONF_UPS_HID_ID]) + # Set UPS name - use custom name if provided, otherwise use component 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)) \ No newline at end of file diff --git a/configs/examples/rack-ups-monitor.yaml b/configs/examples/rack-ups-monitor.yaml index a8a5120..728aa6b 100644 --- a/configs/examples/rack-ups-monitor.yaml +++ b/configs/examples/rack-ups-monitor.yaml @@ -10,6 +10,10 @@ substitutions: # Override timezone if needed (defaults to Europe/Warsaw from regional template) 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: name: "${name}" friendly_name: "${friendly_name}" diff --git a/configs/nut_server.yaml b/configs/nut_server.yaml index 2c7a4a4..eca64f4 100644 --- a/configs/nut_server.yaml +++ b/configs/nut_server.yaml @@ -9,7 +9,7 @@ substitutions: 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 + nut_ups_name: "ups_monitor" # NUT protocol UPS name (optional, defaults to component ID) # Network Configuration (static IP recommended for servers) use_static_ip: "false" # Set to "true" to enable static IP @@ -20,12 +20,12 @@ substitutions: # NUT Server Component nut_server: - ups_hid_id: ups_monitor + ups_hid_id: ups_monitor # UPS HID component ID (fixed) 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) + ups_name: "${nut_ups_name}" # Custom NUT protocol name (optional) # Static IP configuration (uncomment to enable) # Recommended for NUT server to ensure consistent network access @@ -72,7 +72,7 @@ text_sensor: id: nut_server_info icon: "mdi:information-outline" 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()) { info += ", Auth: Disabled"; } else { @@ -109,12 +109,12 @@ interval: # Connect with NUT client tools: # # 1. Using upsc command: -# upsc ups_monitor@:${nut_port} -# upsc -u ${nut_username} -p ${nut_password} ups_monitor@:${nut_port} +# upsc ${nut_ups_name}@:${nut_port} +# upsc -u ${nut_username} -p ${nut_password} ${nut_ups_name}@:${nut_port} # # 2. Using telnet for testing: # telnet ${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: # sensor: @@ -135,7 +135,7 @@ interval: # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock.connect(('', ${nut_port})) # 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() # print(response) # sock.close() diff --git a/configs/nut_server_grouped.yaml b/configs/nut_server_grouped.yaml index 6c6f37a..6d2e081 100644 --- a/configs/nut_server_grouped.yaml +++ b/configs/nut_server_grouped.yaml @@ -9,7 +9,7 @@ substitutions: 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 + nut_ups_name: "ups_monitor" # NUT protocol UPS name (optional, defaults to component ID) # Network Configuration (static IP recommended for servers) use_static_ip: "false" # Set to "true" to enable static IP @@ -25,7 +25,7 @@ nut_server: username: "${nut_username}" password: "${nut_password}" 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) # Recommended for NUT server to ensure consistent network access @@ -80,7 +80,7 @@ text_sensor: sorting_group_id: nut_server_group sorting_weight: 2 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()) { info += ", Auth: Disabled"; } else { @@ -100,7 +100,7 @@ text_sensor: sorting_weight: 3 lambda: |- // 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: # NUT server connected clients counter (future feature placeholder) @@ -157,12 +157,12 @@ interval: # Connect with NUT client tools: # # 1. Using upsc command: -# upsc ups_monitor@:${nut_port} -# upsc -u ${nut_username} -p ${nut_password} ups_monitor@:${nut_port} +# upsc ${nut_ups_name}@:${nut_port} +# upsc -u ${nut_username} -p ${nut_password} ${nut_ups_name}@:${nut_port} # # 2. Using telnet for testing: # telnet ${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: # sensor: @@ -183,7 +183,7 @@ interval: # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # sock.connect(('', ${nut_port})) # 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() # print(response) # sock.close()