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

Fix IPAddress/Gateway show up for wired devices #298

Merged
merged 1 commit into from
Oct 6, 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
Binary file added assets/ethernet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons.qrc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions tdmgr/GUI/delegates/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def paint(self, p: QPainter, option: QStyleOptionViewItem, index):
col_name = self.get_column_name(index)
if col_name == "Device":
# draw signal strength icon
self.draw_rssi_pixmap(index, option, p)
self.draw_network_pixmap(index, option, p)

device_rect = option.rect.adjusted(2 * GAP + ICON_SIZE.width(), 3, 0, 0)
p.save()
Expand Down Expand Up @@ -392,12 +392,15 @@ def paint(self, p: QPainter, option: QStyleOptionViewItem, index):
else:
QStyledItemDelegate.paint(self, p, option, index)

def draw_rssi_pixmap(self, index, option, p):
def draw_network_pixmap(self, index, option, p):
p.save()
px = self.rssi_offline
if index.data(DeviceRoles.LWTRole):
rssi = index.data(DeviceRoles.RSSIRole)
px = get_pixmap_for_rssi(rssi)
if index.data(DeviceRoles.IsEthernetRole):
px = QPixmap(":/ethernet.png")
else:
rssi = index.data(DeviceRoles.RSSIRole)
px = get_pixmap_for_rssi(rssi)

px_y = option.rect.y() + (option.rect.height() - ICON_SIZE.height()) // 2
px_rect = QRect(QPoint(option.rect.x() + GAP, px_y), ICON_SIZE)
Expand Down
168 changes: 103 additions & 65 deletions tdmgr/GUI/icons.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions tdmgr/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def notify_change(self, d, key):
[
key.startswith("POWER"),
key.startswith("FriendlyName"),
key in ("RSSI", "LWT", "Color", "HSBColor"),
key in ("RSSI", "LWT", "Color", "HSBColor", "IPAddress", "Gateway", "Ethernet"),
key.startswith("Channel"),
key.startswith("Dimmer"),
key.startswith("ShutterRelay"),
Expand Down Expand Up @@ -128,9 +128,6 @@ def data(self, idx, role=Qt.DisplayRole):
if col_name == "RSSI":
return int(d.p.get("RSSI", 0))

if col_name == "IPAddress":
return d.ip_address

return val

if role == DeviceRoles.LWTRole:
Expand Down Expand Up @@ -160,6 +157,18 @@ def data(self, idx, role=Qt.DisplayRole):
if role == DeviceRoles.HardwareRole:
return getattr(d.p, "Hardware", "ESP8266")

if role == DeviceRoles.IsEthernetRole:
return (
d.p.get("IPAddress") == "0.0.0.0"
and d.p.get("Ethernet", {}).get("IPAddress") != "0.0.0.0"
)

if role == DeviceRoles.IPAddressRole:
return d.ip_address

if role == DeviceRoles.GatewayRole:
return d.gateway

if role == Qt.TextAlignmentRole:
# Left-aligned columns
if col_name in (
Expand Down
4 changes: 4 additions & 0 deletions tdmgr/models/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ class DeviceRoles(int, Enum):
ColorRole = auto()
ModuleRole = auto()
HardwareRole = auto()

IPAddressRole = auto()
GatewayRole = auto()
IsEthernetRole = auto()
21 changes: 14 additions & 7 deletions tdmgr/tasmota/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,23 @@ def color(self):
)
return None

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

@property
def ip_address(self) -> str:
return self._get_addr_from_wifi_or_ethernet("IPAddress")

@property
def gateway(self) -> str:
return self._get_addr_from_wifi_or_ethernet("Gateway")

def setoption(self, o):
if 0 <= o < 32:
reg = 0
Expand Down
2 changes: 1 addition & 1 deletion tests/status_parsing/jsonfiles/14.2.0.4/STATUS5.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"Mac": "34:98:7A:68:FF:97",
"Subnetmask": "255.255.255.0"
},
"Gateway": "192.168.7.1",
"Gateway": "0.0.0.0",
"HTTP_API": 1,
"Hostname": "tasmota-68FF94-8084",
"IP6Global": "",
Expand Down
16 changes: 16 additions & 0 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ def test_ip_address(device, version, filename, expected):
assert device.ip_address == expected


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

assert device.gateway == expected


@pytest.mark.parametrize(
"fname, expected",
[
Expand Down
Loading