Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show ethernet address when wifi is disabled (fixes #266) #273

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tdmgr/GUI/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

from tdmgr.GUI.widgets import GroupBoxV, HLayout, VLayout, console_font
from tdmgr.util import Message
from tdmgr.util import Message, TasmotaDevice
from tdmgr.util.commands import commands


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

self.settings = settings

Expand Down Expand Up @@ -113,7 +113,7 @@ def command_changed(self, text):

@pyqtSlot(Message)
def consoleAppend(self, msg: Message):
if self.device.matches(msg.topic):
if self.device.message_topic_matches_fulltopic(msg):
self.console.appendPlainText(
f"[{msg.timestamp.strftime('%X')}] {msg.topic} {msg.payload}"
)
Expand Down
5 changes: 1 addition & 4 deletions tdmgr/GUI/dialogs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def mqtt_message(self, msg: Message):
# lwt = self.env.lwts.pop(f"{obj.ft}/LWT", obj.ofln)
# device.update_property("LWT", lwt)

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

else:
_prefix = re.match(device.fulltopic_regex, msg.topic)
if _prefix:
msg.prefix = _prefix.groupdict()["prefix"]
# forward the message for processing
device.update_property("LWT", device.p["Online"])
device.process_message(msg)
Expand Down
6 changes: 3 additions & 3 deletions tdmgr/GUI/dialogs/timers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
)

from tdmgr.GUI.widgets import GroupBoxH, GroupBoxV, HLayout, VLayout
from tdmgr.util import Message
from tdmgr.util import Message, TasmotaDevice


class TimersDialog(QDialog):
sendCommand = pyqtSignal(str, str)

def __init__(self, device, *args, **kwargs):
super(TimersDialog, self).__init__(*args, **kwargs)
self.device = device
self.device: TasmotaDevice = device
self.timers = {}
self.armKey = "Arm"
self.setWindowTitle(f"Timers [{self.device.name}]")
Expand Down Expand Up @@ -257,7 +257,7 @@ def parseMessage(self, msg: Message):
"Days":"0000000","Repeat":0,
"Action":0}, ....
"""
if self.device.matches(msg.topic):
if self.device.message_topic_matches_fulltopic(msg):
if msg.is_result or msg.endpoint == "TIMERS":
payload = msg.dict()
all = list(payload)
Expand Down
2 changes: 1 addition & 1 deletion tdmgr/GUI/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def unfold_rule(self, rules: str):

@pyqtSlot(Message)
def parseMessage(self, msg: Message):
if self.device.matches(msg.topic):
if self.device.message_topic_matches_fulltopic(msg):
payload = msg.dict()
if payload:
if msg.is_result and msg.first_key == "T1" or msg.endpoint == "RULETIMER":
Expand Down
31 changes: 25 additions & 6 deletions tdmgr/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ class DiscoveryMode(int, Enum):

class TasmotaEnvironment:
def __init__(self):
self.devices = []
self.devices: list[TasmotaDevice] = []
self.lwts = dict()
self.retained = set()

def find_device(self, topic) -> 'TasmotaDevice':
def find_device(self, msg: Message) -> 'TasmotaDevice':
for d in self.devices:
if d.matches(topic):
if d.message_topic_matches_fulltopic(msg):
return d


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

@property
def topic(self) -> str:
return self.p["Topic"]

@property
def fulltopic(self) -> str:
return self.p["FullTopic"]

@classmethod
def from_discovery(cls, obj: DiscoverySchema):
_ft = obj.ft.replace(obj.t, "%topic%").replace(obj.tp.tele, "%prefix%")
Expand Down Expand Up @@ -184,6 +192,13 @@ def tele_topic(self, endpoint=""):
def is_default(self):
return self.p["FullTopic"] in DEFAULT_PATTERNS

def message_topic_matches_fulltopic(self, msg: Message) -> bool:
if _prefix := re.match(self.fulltopic_regex, msg.topic):
if not msg.prefix:
msg.prefix = _prefix.groupdict()["prefix"]
return True
return False

@property
def subscribable_topics(self):
topics = []
Expand Down Expand Up @@ -218,9 +233,6 @@ def fulltopic_regex(self) -> str:
)
return f'^{_ft_pattern}'

def matches(self, topic: str) -> bool:
return re.match(self.fulltopic_regex, topic) is not None

def _process_module(self, msg: Message):
print(msg.payload)

Expand Down Expand Up @@ -352,6 +364,13 @@ def color(self):
color.update({15: self.setoption(15), 17: self.setoption(17), 68: self.setoption(68)})
return color

@property
def ip_address(self) -> str:
for ip in [self.p.get("IPAddress"), self.p.get("Ethernet", {}).get("IPAddress")]:
if ip != "0.0.0.0":
return ip
return "0.0.0.0"

def setoption(self, o):
if 0 <= o < 32:
reg = 0
Expand Down
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import os

import pytest

from tdmgr.util import Message, TasmotaDevice


def get_payload(version: str, filename: str) -> bytes:
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'status_parsing', 'jsonfiles')
fname = os.path.join(path, version, filename)
with open(fname, 'rb') as f:
return f.read()


@pytest.fixture
def device(request):
topic = "device"
if request.param:
if hasattr(request, "param"):
topic = request.param
yield TasmotaDevice(topic)

Expand Down
20 changes: 19 additions & 1 deletion tests/test_device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from tdmgr.util import Message, TasmotaDevice
from tests.conftest import get_payload


@pytest.mark.parametrize(
Expand All @@ -14,7 +15,8 @@
)
def test_matches(fulltopic, topic, expected):
device = TasmotaDevice("some_topic", fulltopic)
assert device.matches(topic) is expected
msg = Message(topic)
assert device.message_topic_matches_fulltopic(msg) is expected


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

assert message.first_key == "NAME"
assert device.p.get("Template") == "NodeMCU"


@pytest.mark.parametrize("version", ("14.2.0.4",))
@pytest.mark.parametrize(
"filename, expected",
[
("STATUS5.json", "192.168.0.1"),
("STATUS5.1.json", "192.168.7.187"),
],
)
def test_ip_address(device, version, filename, expected):
payload = get_payload(version, filename)
msg = Message("stat/topic/STATUS5", payload, prefix="stat")
device.process_message(msg)

assert device.ip_address == expected
5 changes: 3 additions & 2 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from tdmgr.util import TasmotaDevice, TasmotaEnvironment
from tdmgr.util import Message, TasmotaDevice, TasmotaEnvironment


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

device = env.find_device(test_topic)
msg = Message(test_topic)
device = env.find_device(msg)

assert (device == dev) is expected
Loading