mirror of
https://github.com/esphome/esphome.git
synced 2024-11-27 17:27:59 +01:00
add usb_device
This commit is contained in:
parent
68928aee7c
commit
2b6ddd9d04
6 changed files with 141 additions and 0 deletions
43
esphome/components/usb_device/__init__.py
Normal file
43
esphome/components/usb_device/__init__.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.core import CORE
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
)
|
||||
from esphome.components.esp32.const import (
|
||||
KEY_ESP32,
|
||||
KEY_VARIANT,
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
)
|
||||
import esphome.final_validate as fv
|
||||
|
||||
CODEOWNERS = ["@tomaszduda23"]
|
||||
CONF_USB_DEVICE_ID = "usb_device_id"
|
||||
|
||||
|
||||
def _validate_variant(value):
|
||||
variant = CORE.data[KEY_ESP32][KEY_VARIANT]
|
||||
if variant not in [VARIANT_ESP32S2, VARIANT_ESP32S3]:
|
||||
raise cv.Invalid(f"USB device is unsupported by ESP32 variant {variant}")
|
||||
return value
|
||||
|
||||
CONF_USB_DEVICE = "usb_device"
|
||||
|
||||
usb_device_ns = cg.esphome_ns.namespace(CONF_USB_DEVICE)
|
||||
UsbDevice = usb_device_ns.class_("UsbDevice", cg.PollingComponent)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(UsbDevice),
|
||||
}
|
||||
).extend(cv.polling_component_schema("10s")),
|
||||
cv.only_with_arduino,
|
||||
cv.only_on_esp32,
|
||||
_validate_variant,
|
||||
)
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
32
esphome/components/usb_device/binary_sensor.py
Normal file
32
esphome/components/usb_device/binary_sensor.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
from . import CONF_USB_DEVICE_ID, UsbDevice, CONF_USB_DEVICE
|
||||
|
||||
AUTO_LOAD = [CONF_USB_DEVICE]
|
||||
|
||||
CONF_CONFIGURED = "configured"
|
||||
|
||||
TYPES = [
|
||||
CONF_CONFIGURED,
|
||||
]
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_USB_DEVICE_ID): cv.use_id(UsbDevice),
|
||||
cv.Optional(CONF_CONFIGURED): binary_sensor.binary_sensor_schema(),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def setup_conf(config, key, hub):
|
||||
if key in config:
|
||||
conf = config[key]
|
||||
var = await binary_sensor.new_binary_sensor(conf)
|
||||
cg.add(getattr(hub, f"set_{key}_binary_sensor")(var))
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
hub = await cg.get_variable(config[CONF_USB_DEVICE_ID])
|
||||
for key in TYPES:
|
||||
await setup_conf(config, key, hub)
|
32
esphome/components/usb_device/usb_device.cpp
Normal file
32
esphome/components/usb_device/usb_device.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
|
||||
#include "usb_device.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "USB.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace usb_device {
|
||||
|
||||
static const char *const TAG = "usb_device";
|
||||
|
||||
void UsbDevice::update() {
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (configured_ != nullptr) {
|
||||
bool configured = USB;
|
||||
configured_->publish_state(configured);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void UsbDevice::dump_config() {
|
||||
bool configured = USB;
|
||||
ESP_LOGCONFIG(TAG, "USB device - configured: %s", YESNO(configured));
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void UsbDevice::set_configured_binary_sensor(binary_sensor::BinarySensor *sensor) { configured_ = sensor; };
|
||||
#endif
|
||||
|
||||
} // namespace usb_device
|
||||
} // namespace esphome
|
||||
#endif
|
26
esphome/components/usb_device/usb_device.h
Normal file
26
esphome/components/usb_device/usb_device.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#endif
|
||||
namespace esphome {
|
||||
namespace usb_device {
|
||||
|
||||
class UsbDevice : public PollingComponent {
|
||||
public:
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void set_configured_binary_sensor(binary_sensor::BinarySensor *sensor);
|
||||
#endif
|
||||
protected:
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
binary_sensor::BinarySensor *configured_;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace usb_device
|
||||
} // namespace esphome
|
||||
#endif
|
4
tests/components/usb_device/test.esp32-s2-ard.yaml
Normal file
4
tests/components/usb_device/test.esp32-s2-ard.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
binary_sensor:
|
||||
- platform: usb_device
|
||||
configured:
|
||||
name: USB is configured
|
4
tests/components/usb_device/test.esp32-s3-ard.yaml
Normal file
4
tests/components/usb_device/test.esp32-s3-ard.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
binary_sensor:
|
||||
- platform: usb_device
|
||||
configured:
|
||||
name: USB is configured
|
Loading…
Reference in a new issue