Skip to content

Commit f64ee10

Browse files
committed
show ethernet address when wifi is disabled (fixes #266) (#273)
1 parent 31fe439 commit f64ee10

File tree

8 files changed

+65
-21
lines changed

8 files changed

+65
-21
lines changed

tdmgr/GUI/console.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919

2020
from tdmgr.GUI.widgets import GroupBoxV, HLayout, VLayout, console_font
21-
from tdmgr.util import Message
21+
from tdmgr.util import Message, TasmotaDevice
2222
from tdmgr.util.commands import commands
2323

2424

@@ -29,7 +29,7 @@ def __init__(self, settings, device, *args, **kwargs):
2929
super().__init__()
3030
self.setAllowedAreas(Qt.BottomDockWidgetArea)
3131
self.setWindowTitle(f"Console [{device.name}]")
32-
self.device = device
32+
self.device: TasmotaDevice = device
3333

3434
self.settings = settings
3535

@@ -113,7 +113,7 @@ def command_changed(self, text):
113113

114114
@pyqtSlot(Message)
115115
def consoleAppend(self, msg: Message):
116-
if self.device.matches(msg.topic):
116+
if self.device.message_topic_matches_fulltopic(msg):
117117
self.console.appendPlainText(
118118
f"[{msg.timestamp.strftime('%X')}] {msg.topic} {msg.payload}"
119119
)

tdmgr/GUI/dialogs/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def mqtt_message(self, msg: Message):
363363
# lwt = self.env.lwts.pop(f"{obj.ft}/LWT", obj.ofln)
364364
# device.update_property("LWT", lwt)
365365

366-
if device := self.env.find_device(msg.topic):
366+
if device := self.env.find_device(msg):
367367
if msg.is_lwt:
368368
log.debug("MQTT: LWT message for %s: %s", device.p["Topic"], msg.payload)
369369
device.update_property("LWT", msg.payload)
@@ -373,9 +373,6 @@ def mqtt_message(self, msg: Message):
373373
self.initial_query(device, True)
374374

375375
else:
376-
_prefix = re.match(device.fulltopic_regex, msg.topic)
377-
if _prefix:
378-
msg.prefix = _prefix.groupdict()["prefix"]
379376
# forward the message for processing
380377
device.update_property("LWT", device.p["Online"])
381378
device.process_message(msg)

tdmgr/GUI/dialogs/timers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
)
1515

1616
from tdmgr.GUI.widgets import GroupBoxH, GroupBoxV, HLayout, VLayout
17-
from tdmgr.util import Message
17+
from tdmgr.util import Message, TasmotaDevice
1818

1919

2020
class TimersDialog(QDialog):
2121
sendCommand = pyqtSignal(str, str)
2222

2323
def __init__(self, device, *args, **kwargs):
2424
super(TimersDialog, self).__init__(*args, **kwargs)
25-
self.device = device
25+
self.device: TasmotaDevice = device
2626
self.timers = {}
2727
self.armKey = "Arm"
2828
self.setWindowTitle(f"Timers [{self.device.name}]")
@@ -257,7 +257,7 @@ def parseMessage(self, msg: Message):
257257
"Days":"0000000","Repeat":0,
258258
"Action":0}, ....
259259
"""
260-
if self.device.matches(msg.topic):
260+
if self.device.message_topic_matches_fulltopic(msg):
261261
if msg.is_result or msg.endpoint == "TIMERS":
262262
payload = msg.dict()
263263
all = list(payload)

tdmgr/GUI/rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def unfold_rule(self, rules: str):
270270

271271
@pyqtSlot(Message)
272272
def parseMessage(self, msg: Message):
273-
if self.device.matches(msg.topic):
273+
if self.device.message_topic_matches_fulltopic(msg):
274274
payload = msg.dict()
275275
if payload:
276276
if msg.is_result and msg.first_key == "T1" or msg.endpoint == "RULETIMER":

tdmgr/util/__init__.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ class DiscoveryMode(int, Enum):
8484

8585
class TasmotaEnvironment:
8686
def __init__(self):
87-
self.devices = []
87+
self.devices: list[TasmotaDevice] = []
8888
self.lwts = dict()
8989
self.retained = set()
9090

91-
def find_device(self, topic) -> 'TasmotaDevice':
91+
def find_device(self, msg: Message) -> 'TasmotaDevice':
9292
for d in self.devices:
93-
if d.matches(topic):
93+
if d.message_topic_matches_fulltopic(msg):
9494
return d
9595

9696

@@ -142,6 +142,14 @@ def __init__(self, topic: str, fulltopic: str = "%prefix%/%topic%/", devicename:
142142
r"GPIOs\d?": self._process_gpios,
143143
}
144144

145+
@property
146+
def topic(self) -> str:
147+
return self.p["Topic"]
148+
149+
@property
150+
def fulltopic(self) -> str:
151+
return self.p["FullTopic"]
152+
145153
@classmethod
146154
def from_discovery(cls, obj: DiscoverySchema):
147155
_ft = obj.ft.replace(obj.t, "%topic%").replace(obj.tp.tele, "%prefix%")
@@ -184,6 +192,13 @@ def tele_topic(self, endpoint=""):
184192
def is_default(self):
185193
return self.p["FullTopic"] in DEFAULT_PATTERNS
186194

195+
def message_topic_matches_fulltopic(self, msg: Message) -> bool:
196+
if _prefix := re.match(self.fulltopic_regex, msg.topic):
197+
if not msg.prefix:
198+
msg.prefix = _prefix.groupdict()["prefix"]
199+
return True
200+
return False
201+
187202
@property
188203
def subscribable_topics(self):
189204
topics = []
@@ -218,9 +233,6 @@ def fulltopic_regex(self) -> str:
218233
)
219234
return f'^{_ft_pattern}'
220235

221-
def matches(self, topic: str) -> bool:
222-
return re.match(self.fulltopic_regex, topic) is not None
223-
224236
def _process_module(self, msg: Message):
225237
print(msg.payload)
226238

@@ -352,6 +364,13 @@ def color(self):
352364
color.update({15: self.setoption(15), 17: self.setoption(17), 68: self.setoption(68)})
353365
return color
354366

367+
@property
368+
def ip_address(self) -> str:
369+
for ip in [self.p.get("IPAddress"), self.p.get("Ethernet", {}).get("IPAddress")]:
370+
if ip != "0.0.0.0":
371+
return ip
372+
return "0.0.0.0"
373+
355374
def setoption(self, o):
356375
if 0 <= o < 32:
357376
reg = 0

tests/conftest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import os
2+
13
import pytest
24

35
from tdmgr.util import Message, TasmotaDevice
46

57

8+
def get_payload(version: str, filename: str) -> bytes:
9+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'status_parsing', 'jsonfiles')
10+
fname = os.path.join(path, version, filename)
11+
with open(fname, 'rb') as f:
12+
return f.read()
13+
14+
615
@pytest.fixture
716
def device(request):
817
topic = "device"
9-
if request.param:
18+
if hasattr(request, "param"):
1019
topic = request.param
1120
yield TasmotaDevice(topic)
1221

tests/test_device.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from tdmgr.util import Message, TasmotaDevice
4+
from tests.conftest import get_payload
45

56

67
@pytest.mark.parametrize(
@@ -14,7 +15,8 @@
1415
)
1516
def test_matches(fulltopic, topic, expected):
1617
device = TasmotaDevice("some_topic", fulltopic)
17-
assert device.matches(topic) is expected
18+
msg = Message(topic)
19+
assert device.message_topic_matches_fulltopic(msg) is expected
1820

1921

2022
@pytest.mark.parametrize("topic", ["stat/device/RESULT", "stat/device/TEMPLATE"])
@@ -27,3 +29,19 @@ def test_process_template(topic):
2729

2830
assert message.first_key == "NAME"
2931
assert device.p.get("Template") == "NodeMCU"
32+
33+
34+
@pytest.mark.parametrize("version", ("14.2.0.4",))
35+
@pytest.mark.parametrize(
36+
"filename, expected",
37+
[
38+
("STATUS5.json", "192.168.0.1"),
39+
("STATUS5.1.json", "192.168.7.187"),
40+
],
41+
)
42+
def test_ip_address(device, version, filename, expected):
43+
payload = get_payload(version, filename)
44+
msg = Message("stat/topic/STATUS5", payload, prefix="stat")
45+
device.process_message(msg)
46+
47+
assert device.ip_address == expected

tests/test_environment.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from tdmgr.util import TasmotaDevice, TasmotaEnvironment
3+
from tdmgr.util import Message, TasmotaDevice, TasmotaEnvironment
44

55

66
@pytest.mark.parametrize(
@@ -19,6 +19,7 @@ def test_find_device(full_topic, test_topic, expected):
1919
dev = TasmotaDevice(topic='device', fulltopic=full_topic)
2020
env.devices.append(dev)
2121

22-
device = env.find_device(test_topic)
22+
msg = Message(test_topic)
23+
device = env.find_device(msg)
2324

2425
assert (device == dev) is expected

0 commit comments

Comments
 (0)