diff --git a/esphome/components/usb_host/__init__.py b/esphome/components/usb_host/__init__.py index 5aa3763f03..213f16286d 100644 --- a/esphome/components/usb_host/__init__.py +++ b/esphome/components/usb_host/__init__.py @@ -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 diff --git a/esphome/components/usb_host/usb_host_client.cpp b/esphome/components/usb_host/usb_host_client.cpp index 76daf68b02..58914ced6e 100644 --- a/esphome/components/usb_host/usb_host_client.cpp +++ b/esphome/components/usb_host/usb_host_client.cpp @@ -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); diff --git a/tests/unit_tests/components/test_usb_host.py b/tests/unit_tests/components/test_usb_host.py index 3d6b8dc1c0..39deec1f51 100644 --- a/tests/unit_tests/components/test_usb_host.py +++ b/tests/unit_tests/components/test_usb_host.py @@ -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(