Skip to content

Commit

Permalink
MQTT handling diacritics #813
Browse files Browse the repository at this point in the history
  • Loading branch information
jokob-sk committed Sep 29, 2024
1 parent e648acd commit e2d84a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 9 additions & 0 deletions front/plugins/_publisher_mqtt/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
"enabled": true,
"data_source": "script",
"show_ui": true,
"data_filters": [
{
"compare_column": "Watched_Value4",
"compare_operator": "==",
"compare_field_id": "txtMacFilter",
"compare_js_template": "'{value}'.toString()",
"compare_use_quotes": true
}
],
"localized": ["display_name", "description", "icon"],
"display_name": [
{
Expand Down
6 changes: 4 additions & 2 deletions front/plugins/_publisher_mqtt/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from datetime import datetime
import time
import re
import unicodedata
import paho.mqtt.client as mqtt
# from paho.mqtt import client as mqtt_client
# from paho.mqtt import CallbackAPIVersion as mqtt_CallbackAPIVersion
Expand All @@ -26,7 +27,7 @@
from plugin_utils import getPluginObject
from plugin_helper import Plugin_Objects
from logger import mylog, append_line_to_file
from helper import timeNowTZ, get_setting_value, bytes_to_string, sanitize_string
from helper import timeNowTZ, get_setting_value, bytes_to_string, sanitize_string, normalize_string
from notification import Notification_obj
from database import DB, get_device_stats
from pytz import timezone
Expand Down Expand Up @@ -414,7 +415,8 @@ def mqtt_start(db):

# Create devices in Home Assistant - send config messages
deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower()
devDisplayName = re.sub('[^a-zA-Z0-9-_\\s]', '', device["dev_Name"])
# Normalize the string and remove unwanted characters
devDisplayName = re.sub('[^a-zA-Z0-9-_\\s]', '', normalize_string(device["dev_Name"]))

sensorConfig = create_sensor(mqtt_client, deviceId, devDisplayName, 'sensor', 'last_ip', 'ip-network', device["dev_MAC"])
sensorConfig = create_sensor(mqtt_client, deviceId, devDisplayName, 'sensor', 'mac_address', 'folder-key-network', device["dev_MAC"])
Expand Down
10 changes: 9 additions & 1 deletion server/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import io
import sys
import datetime
# from datetime import strptime
import os
import re
import unicodedata
import subprocess
import pytz
from pytz import timezone
Expand Down Expand Up @@ -812,6 +812,14 @@ def sanitize_SQL_input(val):
return ''
return val.replace("'", "_")

#-------------------------------------------------------------------------------
# Function to normalize the string and remove diacritics
def normalize_string(text):
# Normalize the text to 'NFD' to separate base characters and diacritics
normalized_text = unicodedata.normalize('NFD', text)
# Filter out diacritics and unwanted characters
return ''.join(c for c in normalized_text if unicodedata.category(c) != 'Mn')


#-------------------------------------------------------------------------------
def generate_mac_links (html, deviceUrl):
Expand Down

0 comments on commit e2d84a1

Please sign in to comment.