MQTT, DHCPLSS work🔌

This commit is contained in:
Jokob-sk
2023-10-15 22:39:21 +11:00
parent a2f3666134
commit 2a4ac2f2be
6 changed files with 56 additions and 43 deletions

View File

@@ -67,7 +67,7 @@
"column": "Object_PrimaryID",
"css_classes": "col-sm-2",
"show": true,
"type": "url",
"type": "label",
"default_value":"",
"options": [],
"localized": ["name"],
@@ -161,8 +161,8 @@
{
"column": "Watched_Value4",
"css_classes": "col-sm-2",
"show": false,
"type": "label",
"show": true,
"type": "device_mac",
"default_value":"",
"options": [],
"localized": ["name"],

View File

@@ -78,7 +78,7 @@ def check_config():
#-------------------------------------------------------------------------------
class sensor_config:
def __init__(self, deviceId, deviceName, sensorType, sensorName, icon):
def __init__(self, deviceId, deviceName, sensorType, sensorName, icon, mac):
self.deviceId = deviceId
self.deviceName = deviceName
self.sensorType = sensorType
@@ -102,13 +102,16 @@ class sensor_config:
# Log sensor
global plugin_objects
if mac == '':
mac = "N/A"
plugin_objects.add_object(
primaryId = deviceId,
secondaryId = sensorName,
watched1 = deviceName,
watched2 = sensorType,
watched3 = hash,
watched4 = 'null',
watched3 = hash_value,
watched4 = mac,
extra = 'null',
foreignKey = deviceId
)
@@ -147,11 +150,11 @@ def create_generic_device(client):
#-------------------------------------------------------------------------------
def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon):
def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon, mac=""):
global mqtt_sensors
new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon)
new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon, mac)
# save if new
if new_sensor_config.isNew:
@@ -271,11 +274,11 @@ def mqtt_start(db):
deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower()
deviceNameDisplay = re.sub('[^a-zA-Z0-9-_\s]', '', device["dev_Name"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'last_ip', 'ip-network')
create_sensor(client, deviceId, deviceNameDisplay, 'binary_sensor', 'is_present', 'wifi')
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network')
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline')
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog')
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'last_ip', 'ip-network', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'binary_sensor', 'is_present', 'wifi', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog', device["dev_MAC"])
# update device sensors in home assistant

View File

@@ -44,25 +44,31 @@ def main():
def get_entries(path, plugin_objects):
# Check if the path exists
if os.path.exists(path) == False:
mylog('none', [f'[{pluginName}] Error: "{path}" does not exist.'])
if not os.path.exists(path):
mylog('none', [f'[{pluginName}] Error: "{path}" does not exist.'])
else:
# Handle pihole specific dhcp.leases files
if 'pihole' in path:
with open(path, 'r') as f:
for line in f:
# Detect file encoding
with open(path, 'rb') as f:
result = chardet.detect(f.read())
# Use the detected encoding
encoding = result['encoding']
# Handle pihole-specific dhcp.leases files
if 'pihole' in path:
with open(path, 'r', encoding=encoding, errors='replace') as f:
for line in f:
row = line.rstrip().split()
if len(row) == 5:
plugin_objects.add_object(
primaryId = handleEmpty(row[1]),
secondaryId = handleEmpty(row[2]),
watched1 = handleEmpty('True'),
watched2 = handleEmpty(row[3]),
watched3 = handleEmpty(row[4]),
watched4 = handleEmpty('True'),
extra = handleEmpty(path),
foreignKey = handleEmpty(row[1])
primaryId=handleEmpty(row[1]),
secondaryId=handleEmpty(row[2]),
watched1=handleEmpty('True'),
watched2=handleEmpty(row[3]),
watched3=handleEmpty(row[4]),
watched4=handleEmpty('True'),
extra=handleEmpty(path),
foreignKey=handleEmpty(row[1])
)
else:
# Handle generic dhcp.leases files