From 87979326bcbcc88bfeb296868559959a5bc833b0 Mon Sep 17 00:00:00 2001 From: sudhaus <33701944+sudhaus@users.noreply.github.com> Date: Fri, 6 Dec 2024 23:50:31 +0100 Subject: [PATCH] Allow overwriting of hostname from config (#170) --- src/config.py.example | 4 ++++ src/rpi-cpu2mqtt.py | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/config.py.example b/src/config.py.example index 99266bd..ab74267 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -35,6 +35,10 @@ display_control = False # user for which display_control is enabled os_user = 'os_user_to_be_replaced' +# By default, the 'hostname' will be used as device name in home assistant. +# To set a custom device name, uncomment the following line and set the custom name. +# ha_device_name = 'device_name_to_use' + # Binary sensor that displays when there are updates git_update = True diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index d32a2e7..baf6af3 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -982,7 +982,10 @@ stop_event = threading.Event() script_dir = os.path.dirname(os.path.realpath(__file__)) # get device host name - used in mqtt topic # and adhere to the allowed character set -hostname = re.sub(r'[^a-zA-Z0-9_-]', '_', socket.gethostname()) +if hasattr(config, 'ha_device_name') and config.ha_device_name: + hostname = re.sub(r'[^a-zA-Z0-9_-]', '_', config.ha_device_name) +else: + hostname = re.sub(r'[^a-zA-Z0-9_-]', '_', socket.gethostname()) if __name__ == '__main__': args = parse_arguments();