Skip to content

Commit

Permalink
DeviceName support with fallback to FriendlyName
Browse files Browse the repository at this point in the history
  • Loading branch information
jziolkowski committed Dec 17, 2020
1 parent 59200c2 commit 061e0f8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
4 changes: 2 additions & 2 deletions GUI/Devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self, parent, *args, **kwargs):
self.device_list.setSortingEnabled(True)
self.device_list.setWordWrap(True)
self.device_list.setItemDelegate(DeviceDelegate())
self.device_list.sortByColumn(self.model.columnIndex("FriendlyName"), Qt.AscendingOrder)
self.device_list.sortByColumn(self.model.columnIndex("Device"), Qt.AscendingOrder)
self.device_list.setContextMenuPolicy(Qt.CustomContextMenu)
self.device_list.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
self.layout().addWidget(self.device_list)
Expand Down Expand Up @@ -282,7 +282,7 @@ def ctx_menu_refresh(self):
def ctx_menu_delete_device(self):
if self.device:
if QMessageBox.question(self, "Confirm", "Do you want to remove the following device?\n'{}' ({})"
.format(self.device.p['FriendlyName1'], self.device.p['Topic'])) == QMessageBox.Yes:
.format(self.device.p['DeviceName'], self.device.p['Topic'])) == QMessageBox.Yes:
self.model.deleteDevice(self.idx)

def ctx_menu_teleperiod(self):
Expand Down
6 changes: 3 additions & 3 deletions GUI/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QHBoxLayout, QGroupBox, QTableView, QSpinBox, QAction, QToolBar, \
QHeaderView, QComboBox, QDoubleSpinBox, QWidget, QSizePolicy, QSlider, QWidgetAction, QFrame, QLineEdit

base_view = ["FriendlyName"]
base_view = ["Device"]
default_views = {
"Home": base_view + ["Module", "Power", "Color", "LoadAvg", "LinkCount", "Uptime"],
"Health": base_view + ["Uptime", "BootCount", "RestartReason", "LoadAvg", "Sleep", "MqttCount", "LinkCount", "Downtime", "RSSI"],
Expand Down Expand Up @@ -149,7 +149,7 @@ def setupColumns(self, columns, hidden=None):

def setupView(self, view):
for i, c in enumerate(view):
if c in ("FriendlyName", "Module", "Topic", "FullTopic"):
if c in ("Device", "Module", "Topic", "FullTopic"):
self.horizontalHeader().setSectionResizeMode(i, QHeaderView.Stretch)
else:
self.horizontalHeader().setSectionResizeMode(i, QHeaderView.ResizeToContents)
Expand Down Expand Up @@ -429,4 +429,4 @@ def __init__(self, command, meta, value=None, *args, **kwargs):
line.setFrameStyle(QFrame.HLine)
line.setFrameShadow(QFrame.Sunken)
vl.addWidget(line)
self.setLayout(vl)
self.setLayout(vl)
13 changes: 7 additions & 6 deletions Util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ def find_device(self, topic):
class TasmotaDevice(QObject):
update_telemetry = pyqtSignal()

def __init__(self, topic, fulltopic, friendlyname=""):
def __init__(self, topic, fulltopic, devicename=""):
super(TasmotaDevice, self).__init__()
self.p = {
"LWT": "undefined",
"Topic": topic,
"FullTopic": fulltopic,
"FriendlyName1": friendlyname if friendlyname else topic,
"DeviceName": devicename,
"Template": {},
}

Expand Down Expand Up @@ -350,8 +350,9 @@ def setoption(self, o):
return state
return -1

def __repr__(self):
fname = self.p.get('FriendlyName1')
fname = fname if fname else self.p['Topic']
@property
def name(self):
return self.p.get('DeviceName') or self.p.get('FriendlyName1', self.p['Topic'])

return "<TasmotaDevice {}: {}>".format(fname, self.p['Topic'])
def __repr__(self):
return "<TasmotaDevice {}: {}>".format(self.name, self.p['Topic'])
20 changes: 10 additions & 10 deletions Util/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
RSSIRole = Qt.UserRole + 2
FirmwareRole = Qt.UserRole + 3


class TasmotaDevicesModel(QAbstractTableModel):
def __init__(self, tasmota_env):
super().__init__()
Expand Down Expand Up @@ -40,9 +41,8 @@ def notify_change(self, d, key):
idx = self.index(row, power_idx)
self.dataChanged.emit(idx, idx)

elif key in ("RSSI", "LWT"):
fname_idx = self.columns.index("FriendlyName")
idx = self.index(row, fname_idx)
elif key in ("RSSI", "LWT", "DeviceName", 'FriendlyName1'):
idx = self.index(row, self.columns.index("Device"))
self.dataChanged.emit(idx, idx)

elif key in self.columns:
Expand Down Expand Up @@ -76,8 +76,8 @@ def data(self, idx, role=Qt.DisplayRole):
if role in [Qt.DisplayRole, Qt.EditRole]:
val = d.p.get(col_name, "")

if col_name == "FriendlyName":
val = d.p.get("FriendlyName1", d.p['Topic'])
if col_name == "Device":
val = d.name

elif col_name == "Module":
if val == 0:
Expand Down Expand Up @@ -149,7 +149,7 @@ def data(self, idx, role=Qt.DisplayRole):

elif role == Qt.TextAlignmentRole:
# Left-aligned columns
if col_name in ("FriendlyName", "Module", "RestartReason", "OtaUrl", "Hostname") or col_name.endswith("Topic"):
if col_name in ("Device", "Module", "RestartReason", "OtaUrl", "Hostname") or col_name.endswith("Topic"):
return Qt.AlignLeft | Qt.AlignVCenter | Qt.TextWordWrap

# Right-aligned columns
Expand All @@ -159,7 +159,7 @@ def data(self, idx, role=Qt.DisplayRole):
else:
return Qt.AlignCenter

elif role == Qt.DecorationRole and col_name == "FriendlyName":
elif role == Qt.DecorationRole and col_name == "Device":
if d.p['LWT'] == "Online":
rssi = int(d.p.get("RSSI", 0))

Expand Down Expand Up @@ -194,8 +194,8 @@ def data(self, idx, role=Qt.DisplayRole):
elif col_name == "BSSId":
return d.p.get('BSSId')

elif col_name == "FriendlyName":
fns = [d.p['FriendlyName1']]
elif col_name == "Device":
fns = [d.name]

for i in range(2, 5):
fn = d.p.get("FriendlyName{}".format(i))
Expand Down Expand Up @@ -262,7 +262,7 @@ def paint(self, p, option, index):
col = index.column()
col_name = index.model().sourceModel().columns[col]

if col_name == "FriendlyName":
if col_name == "DeviceName":
if index.data():
px = QPixmap(":/status_offline.png")
if index.data(LWTRole) == "Online":
Expand Down
8 changes: 5 additions & 3 deletions tdmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
__version__ = "0.2.7"
__tasmota_minimum__ = "6.6.0.17"


class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -74,7 +75,7 @@ def __init__(self, *args, **kwargs):
# load devices from the devices file, create TasmotaDevices and add the to the envvironment
for mac in self.devices.childGroups():
self.devices.beginGroup(mac)
device = TasmotaDevice(self.devices.value("topic"), self.devices.value("full_topic"), self.devices.value("friendly_name"))
device = TasmotaDevice(self.devices.value("topic"), self.devices.value("full_topic"), self.devices.value("device_name"))
device.debug = self.devices.value("debug", False, bool)
device.p['Mac'] = mac.replace("-", ":")
device.env = self.env
Expand Down Expand Up @@ -546,13 +547,13 @@ def closeEvent(self, e):
mac = d.p.get('Mac')
topic = d.p['Topic']
full_topic = d.p['FullTopic']
friendly_name = d.p['FriendlyName1']
device_name = d.name

if mac:
self.devices.beginGroup(mac.replace(":", "-"))
self.devices.setValue("topic", topic)
self.devices.setValue("full_topic", full_topic)
self.devices.setValue("friendly_name", friendly_name)
self.devices.setValue("device_name", device_name)

for i, h in enumerate(d.history):
self.devices.setValue("history/{}".format(i), h)
Expand Down Expand Up @@ -580,3 +581,4 @@ def start():
start()
except Exception as e:
logging.exception("EXCEPTION: %s", e)
print("TDM has crashed. Sorry for that. Check tdm.log for more information.")

0 comments on commit 061e0f8

Please sign in to comment.