Skip to content

Commit

Permalink
Added forwarding node filter to avoid entity creating for any node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mictronics committed Jun 28, 2024
1 parent e5dc782 commit ce32059
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/meshtastic2hass/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ user = ""
password = ""

# MQTT topic prefix
topic_prefix = "msh/2/json"
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 = []
11 changes: 10 additions & 1 deletion src/meshtastic2hass/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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
return self.specialChars

def getFilterNodes(self):
"""Get a set of node short names to be included in filter"""
return self.filterNodes
20 changes: 19 additions & 1 deletion src/meshtastic2hass/meshtastic2hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ce32059

Please sign in to comment.