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

Default port.name to "Port N" #722

Merged
merged 2 commits into from
Oct 23, 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
7 changes: 6 additions & 1 deletion aiounifi/models/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def media(self) -> str | None:
@property
def name(self) -> str:
"""Port name."""
return self.raw["name"]
if (name := self.raw["name"]) == "":
# Unifi controller allows to set an empty port name, but it
# shows up as "Port N" consistently across UI. We mirror the
# behavior, as empty name is rarely visually helpful.
return f"Port {self.port_idx}"
return name

@property
def port_idx(self) -> int | None:
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@
"lldp_table": [],
"masked": False,
"media": "GE",
"name": "Port 2",
"name": "",
"op_mode": "switch",
"poe_caps": 7,
"poe_class": "Unknown",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,7 @@ async def test_port(unifi_controller):
assert port.tx_bytes_r == 330

assert repr(port) == "<Port 1: Poe False>"

unnamed_port = unifi_controller.ports["fc:ec:da:11:22:33_2"]
# Port name is unset, but we verify that it's correctly substituted.
assert unnamed_port.name == "Port 2"