mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Sort usb ports in Z-Wave flow so unknown devices are last (#145211)
* Sort usb ports in Z-Wave flow so unknown devices are last * tweak * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
f4b0baecd3
commit
b84e93f462
@ -149,7 +149,14 @@ def get_usb_ports() -> dict[str, str]:
|
|||||||
pid,
|
pid,
|
||||||
)
|
)
|
||||||
port_descriptions[dev_path] = human_name
|
port_descriptions[dev_path] = human_name
|
||||||
return port_descriptions
|
|
||||||
|
# Sort the dictionary by description, putting "n/a" last
|
||||||
|
return dict(
|
||||||
|
sorted(
|
||||||
|
port_descriptions.items(),
|
||||||
|
key=lambda x: x[1].lower().startswith("n/a"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_get_usb_ports(hass: HomeAssistant) -> dict[str, str]:
|
async def async_get_usb_ports(hass: HomeAssistant) -> dict[str, str]:
|
||||||
|
@ -17,7 +17,7 @@ from zwave_js_server.exceptions import FailedCommand
|
|||||||
from zwave_js_server.version import VersionInfo
|
from zwave_js_server.version import VersionInfo
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow
|
from homeassistant import config_entries, data_entry_flow
|
||||||
from homeassistant.components.zwave_js.config_flow import TITLE
|
from homeassistant.components.zwave_js.config_flow import TITLE, get_usb_ports
|
||||||
from homeassistant.components.zwave_js.const import ADDON_SLUG, CONF_USB_PATH, DOMAIN
|
from homeassistant.components.zwave_js.const import ADDON_SLUG, CONF_USB_PATH, DOMAIN
|
||||||
from homeassistant.components.zwave_js.helpers import SERVER_VERSION_TIMEOUT
|
from homeassistant.components.zwave_js.helpers import SERVER_VERSION_TIMEOUT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -4661,3 +4661,31 @@ async def test_configure_addon_usb_ports_failure(
|
|||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "usb_ports_failed"
|
assert result["reason"] == "usb_ports_failed"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_usb_ports_sorting(hass: HomeAssistant) -> None:
|
||||||
|
"""Test that get_usb_ports sorts ports with 'n/a' descriptions last."""
|
||||||
|
mock_ports = [
|
||||||
|
ListPortInfo("/dev/ttyUSB0"),
|
||||||
|
ListPortInfo("/dev/ttyUSB1"),
|
||||||
|
ListPortInfo("/dev/ttyUSB2"),
|
||||||
|
ListPortInfo("/dev/ttyUSB3"),
|
||||||
|
]
|
||||||
|
mock_ports[0].description = "n/a"
|
||||||
|
mock_ports[1].description = "Device A"
|
||||||
|
mock_ports[2].description = "N/A"
|
||||||
|
mock_ports[3].description = "Device B"
|
||||||
|
|
||||||
|
with patch("serial.tools.list_ports.comports", return_value=mock_ports):
|
||||||
|
result = get_usb_ports()
|
||||||
|
|
||||||
|
descriptions = list(result.values())
|
||||||
|
|
||||||
|
# Verify that descriptions containing "n/a" are at the end
|
||||||
|
|
||||||
|
assert descriptions == [
|
||||||
|
"Device A - /dev/ttyUSB1, s/n: n/a",
|
||||||
|
"Device B - /dev/ttyUSB3, s/n: n/a",
|
||||||
|
"n/a - /dev/ttyUSB0, s/n: n/a",
|
||||||
|
"N/A - /dev/ttyUSB2, s/n: n/a",
|
||||||
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user