22import logging
33import re
44
5+ from paho .mqtt import MQTTException
56from PyQt5 .QtCore import QDir , QFileInfo , QSettings , QSize , Qt , QTimer , QUrl , pyqtSlot
67from PyQt5 .QtGui import QDesktopServices , QFont , QIcon
78from PyQt5 .QtWidgets import (
1516 QPushButton ,
1617 QStatusBar ,
1718)
18- from paho .mqtt import MQTTException
1919
2020from tdmgr .GUI .console import ConsoleWidget
2121from tdmgr .GUI .devices import DevicesListWidget
@@ -63,9 +63,13 @@ def __init__(
6363
6464 self .menuBar ().setNativeMenuBar (False )
6565
66+ self .mqtt = None
67+ self .setup_mqtt ()
68+
6669 self .unknown = []
6770 self .custom_patterns = []
6871 self .env = TasmotaEnvironment ()
72+ self .env .mqtt = self .mqtt
6973 self .device = None
7074
7175 self .topics = []
@@ -88,8 +92,7 @@ def __init__(
8892 )
8993 device .debug = self .devices .value ("debug" , False , bool )
9094 device .p ["Mac" ] = mac .replace ("-" , ":" )
91- device .env = self .env
92- self .env .devices .append (device )
95+ self .env .add_device (device )
9396
9497 # load device command history
9598 self .devices .beginGroup ("history" )
@@ -101,7 +104,6 @@ def __init__(
101104
102105 self .device_model = TasmotaDevicesModel (self .settings , self .devices , self .env )
103106
104- self .setup_mqtt ()
105107 self .setup_main_layout ()
106108 self .add_devices_tab ()
107109 self .build_mainmenu ()
@@ -186,7 +188,9 @@ def build_mainmenu(self):
186188
187189 def build_toolbars (self ):
188190 main_toolbar = Toolbar (
189- orientation = Qt .Horizontal , iconsize = 24 , label_position = Qt .ToolButtonTextBesideIcon
191+ orientation = Qt .Horizontal ,
192+ iconsize = 24 ,
193+ label_position = Qt .ToolButtonTextBesideIcon ,
190194 )
191195 main_toolbar .setObjectName ("main_toolbar" )
192196
@@ -304,7 +308,8 @@ def mqtt_subscribe(self):
304308 # the custom patterns
305309 for custom_pattern in self .custom_patterns :
306310 custom_pattern_match = re .match (
307- custom_pattern .replace ("+" , f"({ MQTT_PATH_REGEX } )" ), d .p ["FullTopic" ]
311+ custom_pattern .replace ("+" , f"({ MQTT_PATH_REGEX } )" ),
312+ d .p ["FullTopic" ],
308313 )
309314 if not d .is_default () and not custom_pattern_match :
310315 # if pattern is not found then add the device topics to subscription list.
@@ -428,7 +433,7 @@ def mqtt_message(self, msg: Message):
428433 elif msg .endpoint in ("RESULT" , "FULLTOPIC" ):
429434 # reply from an unknown device
430435 if d := lwt_discovery_stage2 (self .env , msg ):
431- self .env .devices . append (d )
436+ self .env .add_device (d )
432437 self .device_model .addDevice (d )
433438 log .debug ("DISCOVERY: Sending initial query to topic %s" , d .p ["Topic" ])
434439 self .initial_query (d , True )
@@ -438,7 +443,10 @@ def mqtt_message(self, msg: Message):
438443
439444 def export (self ):
440445 fname , _ = QFileDialog .getSaveFileName (
441- self , "Export device list as..." , directory = QDir .homePath (), filter = "CSV files (*.csv)"
446+ self ,
447+ "Export device list as..." ,
448+ directory = QDir .homePath (),
449+ filter = "CSV files (*.csv)" ,
442450 )
443451 if fname :
444452 if not fname .endswith (".csv" ):
@@ -564,7 +572,9 @@ def openTelemetry(self):
564572 self .mqtt_publish (self .device .cmnd_topic ("STATUS" ), "8" )
565573 self .tele_docks .append (tele_widget )
566574 self .resizeDocks (
567- self .tele_docks , [100 // len (self .tele_docks ) for _ in self .tele_docks ], Qt .Vertical
575+ self .tele_docks ,
576+ [100 // len (self .tele_docks ) for _ in self .tele_docks ],
577+ Qt .Vertical ,
568578 )
569579
570580 @pyqtSlot ()
@@ -577,7 +587,9 @@ def openConsole(self):
577587 console_widget .command .setFocus ()
578588 self .consoles .append (console_widget )
579589 self .resizeDocks (
580- self .consoles , [100 // len (self .consoles ) for _ in self .consoles ], Qt .Horizontal
590+ self .consoles ,
591+ [100 // len (self .consoles ) for _ in self .consoles ],
592+ Qt .Horizontal ,
581593 )
582594
583595 @pyqtSlot ()
0 commit comments