[usb_host] Allow 0x0000 VID/PID wildcards to apply to just one field

[usb_host] Fix VID/PID wildcards
This commit is contained in:
puddly
2026-09-10 18:48:05 +00:00
parent ffb4ca997e
commit 703ffbd22b
3 changed files with 67 additions and 13 deletions
+5 -7
View File
@@ -68,13 +68,11 @@ def validate_usb_clients(configs: list[ConfigType]) -> list[ConfigType]:
for index, first in enumerate(configs):
# Ensure matching logic does not overlap between entries
for second in configs[index + 1 :]:
if (
not (first[CONF_VID] == 0 and first[CONF_PID] == 0)
and not (second[CONF_VID] == 0 and second[CONF_PID] == 0)
and (
first[CONF_VID] != second[CONF_VID]
or first[CONF_PID] != second[CONF_PID]
)
# A zero VID or PID is a wildcard for that field, so only a differing non-zero
# value separates two entries
if not all(
first[key] == 0 or second[key] == 0 or first[key] == second[key]
for key in (CONF_VID, CONF_PID)
):
continue
@@ -355,12 +355,10 @@ void USBClient::handle_open_state_() {
return;
}
ESP_LOGD(TAG, "Device descriptor: vid %X pid %X", desc->idVendor, desc->idProduct);
if (desc->idVendor != this->vid_ || desc->idProduct != this->pid_) {
if (this->vid_ != 0 || this->pid_ != 0) {
ESP_LOGD(TAG, "Not our device, closing");
this->disconnect();
return;
}
if ((this->vid_ != 0 && desc->idVendor != this->vid_) || (this->pid_ != 0 && desc->idProduct != this->pid_)) {
ESP_LOGD(TAG, "Not our device, closing");
this->disconnect();
return;
}
usb_device_info_t dev_info;
err = usb_host_device_info(this->device_handle_, &dev_info);
@@ -70,6 +70,36 @@ from esphome.types import ConfigType
},
id="different_manufacturer",
),
pytest.param(
{
"id": "a",
"vid": 0x303A,
"pid": 0,
"manufacturer": "Nabu Casa",
"product": "ZBT-2",
},
{
"id": "b",
"vid": 0x303A,
"pid": 0x4001,
"manufacturer": "Nabu Casa",
"product": "ZWA-2",
},
id="wildcard_pid_separated_by_product",
),
pytest.param(
{
"id": "a",
"vid": 0,
"pid": 0x4001,
},
{
"id": "b",
"vid": 0x303A,
"pid": 0x4002,
},
id="wildcard_vid_different_pid",
),
],
)
def test_disjoint_clients_are_accepted(first: ConfigType, second: ConfigType) -> None:
@@ -140,6 +170,34 @@ def test_disjoint_clients_are_accepted(first: ConfigType, second: ConfigType) ->
},
id="zero_ids_match_every_device",
),
pytest.param(
{
"id": "a",
"vid": 0x303A,
"pid": 0,
},
{
"id": "b",
"vid": 0x303A,
"pid": 0x4001,
"manufacturer": "Nabu Casa",
"product": "ZBT-2",
},
id="wildcard_pid_shadows_filtered",
),
pytest.param(
{
"id": "a",
"vid": 0,
"pid": 0x4001,
},
{
"id": "b",
"vid": 0x303A,
"pid": 0,
},
id="wildcards_on_different_fields",
),
],
)
def test_overlapping_clients_are_rejected(