From 8bb93282017dd04fee6145fa6dfd6877ef23ee01 Mon Sep 17 00:00:00 2001 From: Ulrich Wisser Date: Wed, 24 Apr 2024 18:02:33 +0200 Subject: [PATCH] make new cleanDeviceName configurable, fix debug logging --- front/plugins/newdev_template/config.json | 24 ++++++++++++++++--- server/helper.py | 29 +++++++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/front/plugins/newdev_template/config.json b/front/plugins/newdev_template/config.json index 6ac9b477..b59caa0c 100755 --- a/front/plugins/newdev_template/config.json +++ b/front/plugins/newdev_template/config.json @@ -605,6 +605,25 @@ "string": "The icon associated with the device. Check the documentation on icons for more details." } ] + }, + { + "function": "dev_CleanDeviceName", + "type": "integer.checkbox", + "default_value": 0, + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Use new algorithm to cleanup device names" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Check to start using the new code for cleaning device names. Removes all labels starting with underscore and removes network domain and search list." + } + ] } ], "required": [ @@ -627,10 +646,9 @@ "dev_Archived", "dev_Network_Node_MAC_ADDR", "dev_Network_Node_port", - "dev_Icon" + "dev_Icon", + "dev_CleanDeviceName" ], "additionalProperties": false } - - \ No newline at end of file diff --git a/server/helper.py b/server/helper.py index b28ef79b..2ed5e9bc 100755 --- a/server/helper.py +++ b/server/helper.py @@ -573,8 +573,33 @@ def resolve_device_name_pholus (pMAC, pIP, allRes, nameNotFound, match_IP = Fals import dns.resolver def cleanDeviceName(str, match_IP): + if get_setting_value('NEWDEV_dev_CleanDeviceName'): + return NEW_cleanDeviceName(str, match_IP) - print("START cleanDeviceName(" + str + ")") + # alternative str.split('.')[0] + str = str.replace("._airplay", "") + str = str.replace("._tcp", "") + str = str.replace(".localdomain", "") + str = str.replace(".local", "") + str = str.replace("._esphomelib", "") + str = str.replace("._googlecast", "") + str = str.replace(".lan", "") + str = str.replace(".home", "") + str = re.sub(r'-[a-fA-F0-9]{32}', '', str) # removing last part of e.g. Nest-Audio-ff77ff77ff77ff77ff77ff77ff77ff77 + str = re.sub(r'#.*', '', str) # Remove everything after '#' including the '#' + # remove trailing dots + if str.endswith('.'): + str = str[:-1] + + + if match_IP: + str = str + " (IP match)" + + return str + +def NEW_cleanDeviceName(str, match_IP): + + mylog('debug', ["START cleanDeviceName(" + str + ")"]) # replace all labels starting with underscore str = re.sub(r'^_[^\.]*\.', '', str) # leading label @@ -604,7 +629,7 @@ def cleanDeviceName(str, match_IP): if match_IP: str = str + " (IP match)" - print("END cleanDeviceName = " + str) + mylog('debug', ["END cleanDeviceName = " + str]) # done return str