Skip to content

Commit

Permalink
Remove special characters from MQTT topic strings. Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Mictronics committed Jun 28, 2024
1 parent 03bdf37 commit d423d52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/meshtastic2hass/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -252,4 +253,8 @@ def getTopicPrefix(self):

def getChannelList(self):
"""Get the local nodes channel list"""
return self.channelList
return self.channelList

def getSpecialChars(self):
"""Get a regex pattern of special characters to be removed from strings"""
return self.special_chars
15 changes: 10 additions & 5 deletions src/meshtastic2hass/meshtastic2hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
import signal
import sys
import re

import meshtastic
import meshtastic.serial_interface
Expand All @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit d423d52

Please sign in to comment.