diff --git a/src/meshtastic2hass/config.toml b/src/meshtastic2hass/config.toml index c6da9e7..a412926 100644 --- a/src/meshtastic2hass/config.toml +++ b/src/meshtastic2hass/config.toml @@ -16,4 +16,11 @@ user = "" password = "" # MQTT topic prefix -topic_prefix = "msh/2/json" \ No newline at end of file +topic_prefix = "msh/2/json" + +[meshtastic] +# Set of Meshtastic nodes short names to be includes in filter. +# Only these nodes will be forwarded to home assistant via MQTT topic, hence creating entities. +# Keep empty to forward all nodes. +# Receiving channels text from nodes is not filtered at all. +filter_nodes = [] \ No newline at end of file diff --git a/src/meshtastic2hass/globals.py b/src/meshtastic2hass/globals.py index 5157e44..b0d8520 100644 --- a/src/meshtastic2hass/globals.py +++ b/src/meshtastic2hass/globals.py @@ -196,6 +196,7 @@ def __init__(self): ] self.mqttTopicPrefix = "msh/2/json" self.channelList = [] + self.filterNodes = [] def reset(self): """Reset all of our globals. If you add a member, add it to this method, too.""" @@ -226,6 +227,10 @@ def setTopicPrefix(self, prefix): """Set the MQTT topic prefix""" self.mqttTopicPrefix = prefix + def setFilterNodes(self, filterNodes): + """Set node short names to be included in filter""" + self.filterNodes = filterNodes + # getters def getArgs(self): """Get args""" @@ -257,4 +262,8 @@ def getChannelList(self): def getSpecialChars(self): """Get a regex pattern of special characters to be removed from strings""" - return self.specialChars \ No newline at end of file + return self.specialChars + + def getFilterNodes(self): + """Get a set of node short names to be included in filter""" + return self.filterNodes diff --git a/src/meshtastic2hass/meshtastic2hass.py b/src/meshtastic2hass/meshtastic2hass.py index 08150d7..b673b45 100644 --- a/src/meshtastic2hass/meshtastic2hass.py +++ b/src/meshtastic2hass/meshtastic2hass.py @@ -36,7 +36,7 @@ __author__ = "Michael Wolf aka Mictronics" __copyright__ = "2024, (C) Michael Wolf" __license__ = "GPL v3+" -__version__ = "1.0.9" +__version__ = "1.0.10" def onReceiveTelemetry(packet, interface, topic=pub.AUTO_TOPIC): @@ -49,6 +49,13 @@ def onReceiveTelemetry(packet, interface, topic=pub.AUTO_TOPIC): jsonObj = {} fromId = packet.get("fromId") shortName = interface.nodes.get(fromId).get("user").get("shortName") + # Filter nodes + filterNodes = _globals.getFilterNodes() + if len(filterNodes) > 0: + try: + filterNodes.index(shortName) + except ValueError: + return # No special characters allowed in Hass config topic pattern = _globals.getSpecialChars() fromId = re.sub(pattern, '', fromId) @@ -118,6 +125,13 @@ def onReceivePosition(packet, interface, topic=pub.AUTO_TOPIC): jsonObj = {} fromId = packet.get("fromId") shortName = interface.nodes.get(fromId).get("user").get("shortName") + # Filter nodes + filterNodes = _globals.getFilterNodes() + if len(filterNodes) > 0: + try: + filterNodes.index(shortName) + except ValueError: + return # No special characters allowed in config topic pattern = _globals.getSpecialChars() fromId = re.sub(pattern, '', fromId) @@ -208,6 +222,9 @@ async def publishChannelConfig(): jsonObj = {} for channelName in channelList: + # No special characters allowed in config topic + pattern = _globals.getSpecialChars() + channelName = re.sub(pattern, '', channelName) # Publish auto discovery configuration for MQTT text entity per channel mqttTopic = f"homeassistant/text/{channelName}/config" jsonObj["name"] = f"{channelName}" @@ -440,6 +457,7 @@ def signal_handler(signal, frame): args.mqtt_password = cfg.get("mqtt").get("password") args.mqtt_host = cfg.get("mqtt").get("host") args.mqtt_port = cfg.get("mqtt").get("port") + _globals.setFilterNodes(cfg.get("meshtastic").get("filter_nodes")) else: print(f"Error: configuration file {args.config} not found!") sys.exit(1)