diff --git a/src/meshtastic2hass/globals.py b/src/meshtastic2hass/globals.py index 8643478..6b4ea61 100644 --- a/src/meshtastic2hass/globals.py +++ b/src/meshtastic2hass/globals.py @@ -37,6 +37,7 @@ def __init__(self): self.parser = None self.loop = None self.mqtt = None + self.special_chars = r'[!]' # Home Assistant sensor configuration send via MQTT. self.mqttSensors = [ dict( @@ -252,4 +253,8 @@ def getTopicPrefix(self): def getChannelList(self): """Get the local nodes channel list""" - return self.channelList \ No newline at end of file + return self.channelList + + def getSpecialChars(self): + """Get a regex pattern of special characters to be removed from strings""" + return self.special_chars \ No newline at end of file diff --git a/src/meshtastic2hass/meshtastic2hass.py b/src/meshtastic2hass/meshtastic2hass.py index c7b8e97..08150d7 100644 --- a/src/meshtastic2hass/meshtastic2hass.py +++ b/src/meshtastic2hass/meshtastic2hass.py @@ -23,6 +23,7 @@ import os import signal import sys +import re import meshtastic import meshtastic.serial_interface @@ -35,7 +36,7 @@ __author__ = "Michael Wolf aka Mictronics" __copyright__ = "2024, (C) Michael Wolf" __license__ = "GPL v3+" -__version__ = "1.0.8" +__version__ = "1.0.9" def onReceiveTelemetry(packet, interface, topic=pub.AUTO_TOPIC): @@ -49,7 +50,8 @@ def onReceiveTelemetry(packet, interface, topic=pub.AUTO_TOPIC): fromId = packet.get("fromId") shortName = interface.nodes.get(fromId).get("user").get("shortName") # No special characters allowed in Hass config topic - fromId = fromId.strip("!") + pattern = _globals.getSpecialChars() + fromId = re.sub(pattern, '', fromId) # Publish auto discovery configuration for sensors for sensor in sensors: jsonObj.clear() @@ -117,7 +119,8 @@ def onReceivePosition(packet, interface, topic=pub.AUTO_TOPIC): fromId = packet.get("fromId") shortName = interface.nodes.get(fromId).get("user").get("shortName") # No special characters allowed in config topic - fromId = fromId.strip("!") + pattern = _globals.getSpecialChars() + fromId = re.sub(pattern, '', fromId) # Publish auto discovery configuration for device tracker mqttTopic = f"homeassistant/device_tracker/{fromId}/config" jsonObj["name"] = f"{shortName} Position" @@ -156,10 +159,12 @@ def onReceiveText(packet, interface, topic=pub.AUTO_TOPIC): channelNumber = packet["channel"] else: channelNumber = 0 - channelName = channelList[channelNumber] + # No special characters allowed in config topic + pattern = _globals.getSpecialChars() + channelName = re.sub(pattern, '', channelList[channelNumber]) # Publish auto discovery configuration for MQTT text entity per channel mqttTopic = f"homeassistant/text/{channelName}/config" - jsonObj["name"] = f"{channelName}" + jsonObj["name"] = f"{channelList[channelNumber]}" jsonObj["unique_id"] = f"channel_{channelName.lower()}" jsonObj["command_topic"] = f"{topicPrefix}/{channelName.lower()}/command" jsonObj["state_topic"] = f"{topicPrefix}/{channelName.lower()}/state"