Skip to content

Hardware

busylight_core.hardware

USB Hardware Description

Attributes

busylight_core.hardware.HardwareHandle module-attribute

HardwareHandle = Device | Serial

Classes

busylight_core.hardware.ConnectionType

Bases: int, Enum

USB Harddware connection types.

Attributes
busylight_core.hardware.ConnectionType.ANY class-attribute instance-attribute
ANY = -1
busylight_core.hardware.ConnectionType.UNKNOWN class-attribute instance-attribute
UNKNOWN = 0
busylight_core.hardware.ConnectionType.HID class-attribute instance-attribute
HID = 1
busylight_core.hardware.ConnectionType.SERIAL class-attribute instance-attribute
SERIAL = 2
busylight_core.hardware.ConnectionType.BLUETOOTH class-attribute instance-attribute
BLUETOOTH = 3

busylight_core.hardware.Hardware dataclass

Hardware(
    device_type,
    path,
    vendor_id,
    product_id,
    serial_number,
    manufacturer_string,
    product_string=None,
    release_number=None,
    usage=None,
    usage_page=None,
    interface_number=None,
    bus_type=None,
    is_acquired=False,
)

USB Hardware description.

Attributes
busylight_core.hardware.Hardware.device_type instance-attribute
device_type
busylight_core.hardware.Hardware.path instance-attribute
path
busylight_core.hardware.Hardware.vendor_id instance-attribute
vendor_id
busylight_core.hardware.Hardware.product_id instance-attribute
product_id
busylight_core.hardware.Hardware.serial_number instance-attribute
serial_number
busylight_core.hardware.Hardware.manufacturer_string instance-attribute
manufacturer_string
busylight_core.hardware.Hardware.product_string class-attribute instance-attribute
product_string = None
busylight_core.hardware.Hardware.release_number class-attribute instance-attribute
release_number = None
busylight_core.hardware.Hardware.usage class-attribute instance-attribute
usage = None
busylight_core.hardware.Hardware.usage_page class-attribute instance-attribute
usage_page = None
busylight_core.hardware.Hardware.interface_number class-attribute instance-attribute
interface_number = None
busylight_core.hardware.Hardware.bus_type class-attribute instance-attribute
bus_type = None
busylight_core.hardware.Hardware.is_acquired class-attribute instance-attribute
is_acquired = False
busylight_core.hardware.Hardware.device_id cached property
device_id

A tuple of the vendor and product identifiers.

busylight_core.hardware.Hardware.handle cached property
handle

An I/O handle for this hardware device.

Functions
busylight_core.hardware.Hardware.enumerate classmethod
enumerate(by_type=ANY)

List of all connected hardware devices.

Source code in src/busylight_core/hardware.py
@classmethod
def enumerate(cls, by_type: ConnectionType = ConnectionType.ANY) -> list[Hardware]:
    """List of all connected hardware devices."""
    hardware_info = []

    match by_type:
        case ConnectionType.ANY:
            for connection_type in ConnectionType:
                if connection_type <= 0:
                    continue
                with contextlib.suppress(NotImplementedError):
                    hardware_info.extend(cls.enumerate(connection_type))
        case ConnectionType.HID:
            hardware_info.extend(
                cls.from_hid(device_dict) for device_dict in hid.enumerate()
            )
        case ConnectionType.SERIAL:
            hardware_info.extend(
                cls.from_portinfo(port_info) for port_info in list_ports.comports()
            )

        case _:
            msg = f"Device type {by_type} not implemented"
            raise NotImplementedError(msg)

    return hardware_info
busylight_core.hardware.Hardware.from_portinfo classmethod
from_portinfo(port_info)

Create a Hardware object from a serial port info object.

Source code in src/busylight_core/hardware.py
@classmethod
def from_portinfo(cls, port_info: ListPortInfo) -> Hardware:
    """Create a Hardware object from a serial port info object."""
    return cls(
        device_type=ConnectionType.SERIAL,
        vendor_id=port_info.vid,
        product_id=port_info.pid,
        path=port_info.device.encode("utf-8"),
        serial_number=port_info.serial_number,
        manufacturer_string=port_info.manufacturer,
        product_string=port_info.product,
        bus_type=1,
    )
busylight_core.hardware.Hardware.from_hid classmethod
from_hid(device)

Create a Hardware object from a HID dictionary.

Source code in src/busylight_core/hardware.py
@classmethod
def from_hid(cls, device: dict) -> Hardware:
    """Create a Hardware object from a HID dictionary."""
    return cls(device_type=ConnectionType.HID, **device)
busylight_core.hardware.Hardware.acquire
acquire()

Open the hardware device.

Source code in src/busylight_core/hardware.py
def acquire(self) -> None:
    """Open the hardware device."""
    if self.is_acquired:
        logger.debug(f"{self} already acquired")
        return

    match self.device_type:
        case ConnectionType.HID:
            self.handle.open_path(self.path)
            self.is_acquired = True
        case ConnectionType.SERIAL:
            self.handle.open()
            self.is_acquired = True
        case _:
            msg = f"{self.device_type.value.title()} hardware not implemented"
            raise NotImplementedError(msg)
busylight_core.hardware.Hardware.release
release()

Close the hardware device.

Source code in src/busylight_core/hardware.py
def release(self) -> None:
    """Close the hardware device."""
    if not self.is_acquired:
        logger.debug(f"{self} already released")
        return

    match self.device_type:
        case ConnectionType.HID | ConnectionType.SERIAL:
            self.handle.close()
            self.is_acquired = False
        case _:
            msg = f"{self.device_type.value.title()} hardware not implemented"
            raise NotImplementedError(msg)