diff --git a/esphome/components/usb_device/__init__.py b/esphome/components/usb_device/__init__.py index f0150d6941ed..a04553b1b805 100644 --- a/esphome/components/usb_device/__init__.py +++ b/esphome/components/usb_device/__init__.py @@ -15,6 +15,10 @@ CODEOWNERS = ["@tomaszduda23"] CONF_USB_DEVICE_ID = "usb_device_id" +CONF_VENDOR_ID = "vendor_id" +CONF_PRODUCT_ID = "product_id" +CONF_MANUFACTURER_NAME = "manufacturer_name" +CONF_PRODUCT_NAME = "product_name" def _validate_variant(value): @@ -33,6 +37,10 @@ def _validate_variant(value): cv.Schema( { cv.GenerateID(): cv.declare_id(UsbDevice), + cv.Optional(CONF_VENDOR_ID): cv.hex_uint16_t, + cv.Optional(CONF_PRODUCT_ID): cv.hex_uint16_t, + cv.Optional(CONF_MANUFACTURER_NAME): cv.string, + cv.Optional(CONF_PRODUCT_NAME): cv.string, } ).extend(cv.polling_component_schema("10s")), cv.only_with_arduino, @@ -64,6 +72,17 @@ def final_validate_number_of_device(config): async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) + if CONF_VENDOR_ID in config: + cg.add_define("USE_VENDOR_ID") + cg.add(var.set_vendor_id(config[CONF_VENDOR_ID])) + if CONF_PRODUCT_ID in config: + cg.add_define("USE_PRODUCT_ID") + cg.add(var.set_product_id(config[CONF_PRODUCT_ID])) + if CONF_MANUFACTURER_NAME in config: + cg.add(var.set_manufacturer_name(config[CONF_MANUFACTURER_NAME])) + if CONF_PRODUCT_NAME in config: + cg.add(var.set_product_name(config[CONF_PRODUCT_NAME])) + await cg.register_component(var, config) cg.add_library("adafruit/Adafruit TinyUSB Library", "2.2.4", None) cg.add_build_flag("-DCFG_TUSB_MCU=OPT_MCU_ESP32S2") diff --git a/esphome/components/usb_device/usb_device.cpp b/esphome/components/usb_device/usb_device.cpp index cd5eaf629e47..ab5400e5fdcc 100644 --- a/esphome/components/usb_device/usb_device.cpp +++ b/esphome/components/usb_device/usb_device.cpp @@ -9,7 +9,21 @@ namespace usb_device { static const char *const TAG = "usb_device"; -void UsbDevice::setup() { USB.begin(); } +void UsbDevice::setup() { +#ifdef USE_VENDOR_ID + USB.VID(this->vendor_id_); +#endif +#ifdef USE_PRODUCT_ID + USB.PID(this->product_id_); +#endif + if (!this->manufacturer_name_.empty()) { + USB.manufacturerName(this->manufacturer_name_.c_str()); + } + if (!this->product_name_.empty()) { + USB.productName(this->product_name_.c_str()); + } + USB.begin(); +} void UsbDevice::update() { #ifdef USE_BINARY_SENSOR @@ -35,6 +49,11 @@ void UsbDevice::dump_config() { YESNO(TinyUSBDevice.suspended()), YESNO(TinyUSBDevice.ready())); } +void UsbDevice::set_vendor_id(const uint16_t vid) { this->vendor_id_ = vid; } +void UsbDevice::set_product_id(const uint16_t pid) { this->product_id_ = pid; } +void UsbDevice::set_manufacturer_name(const std::string &manufacturer_name) { this->manufacturer_name_ = manufacturer_name; } +void UsbDevice::set_product_name(const std::string &product_name) { this->product_name_ = product_name; } + #ifdef USE_BINARY_SENSOR void UsbDevice::set_mounted_binary_sensor(binary_sensor::BinarySensor *sensor) { mounted_ = sensor; }; void UsbDevice::set_ready_binary_sensor(binary_sensor::BinarySensor *sensor) { ready_ = sensor; }; diff --git a/esphome/components/usb_device/usb_device.h b/esphome/components/usb_device/usb_device.h index 25caf8adb128..b2922e28a5b0 100644 --- a/esphome/components/usb_device/usb_device.h +++ b/esphome/components/usb_device/usb_device.h @@ -14,12 +14,20 @@ class UsbDevice : public PollingComponent { void update() override; float get_setup_priority() const override; void dump_config() override; + void set_vendor_id(uint16_t vendor_id); + void set_product_id(uint16_t product_id); + void set_manufacturer_name(const std::string &manufacturer_name); + void set_product_name(const std::string &product_name); #ifdef USE_BINARY_SENSOR void set_mounted_binary_sensor(binary_sensor::BinarySensor *sensor); void set_ready_binary_sensor(binary_sensor::BinarySensor *sensor); void set_suspended_binary_sensor(binary_sensor::BinarySensor *sensor); #endif protected: + uint16_t vendor_id_; + uint16_t product_id_; + std::string manufacturer_name_; + std::string product_name_; #ifdef USE_BINARY_SENSOR binary_sensor::BinarySensor *mounted_; binary_sensor::BinarySensor *ready_;