Devices¶
Device templates and registry for common components.
devices ¶
Device templates and registry for pinviz.
All devices are now loaded from JSON configurations in device_configs/. Use the registry to create device instances:
from pinviz.devices import get_registry
registry = get_registry()
sensor = registry.create('bh1750')
led = registry.create('led', color_name='Blue')
display = registry.create('ssd1306')
For testing or isolated contexts, create independent registries:
from pinviz.devices import create_registry
test_registry = create_registry()
# Use test_registry independently without affecting global state
Available devices are automatically discovered from JSON configs.
DeviceRegistry ¶
Central registry for device templates loaded from JSON configurations.
Source code in src/pinviz/devices/registry.py
create ¶
Create a device instance from JSON configuration.
| PARAMETER | DESCRIPTION |
|---|---|
type_id
|
Device type identifier (matches JSON config filename)
TYPE:
|
**kwargs
|
Parameters to pass to the config loader (e.g., color_name, num_leds)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Device
|
Device instance with metadata |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If device config file not found |
Source code in src/pinviz/devices/registry.py
get ¶
Get device template metadata by type ID.
| PARAMETER | DESCRIPTION |
|---|---|
type_id
|
Device type identifier (case-insensitive)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DeviceTemplate | None
|
DeviceTemplate with metadata, or None if not found |
Example
registry = get_registry() template = registry.get('bh1750') print(template.name) BH1750 Light Sensor
Source code in src/pinviz/devices/registry.py
get_categories ¶
Get all unique device categories.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
Sorted list of category names |
Example
registry = get_registry() categories = registry.get_categories() print(categories) ['displays', 'generic', 'io', 'leds', 'sensors']
Source code in src/pinviz/devices/registry.py
get_failed_configs ¶
Get list of config files that failed to load.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of filenames that failed during registry initialization |
Example
registry = get_registry() failed = registry.get_failed_configs() if failed: ... print(f"Warning: {len(failed)} configs failed to load")
Source code in src/pinviz/devices/registry.py
get_health_status ¶
Get registry health status.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, int]
|
Dictionary with 'loaded', 'failed', and 'total' counts |
Example
registry = get_registry() status = registry.get_health_status() print(f"Loaded: {status['loaded']}/{status['total']}")
Source code in src/pinviz/devices/registry.py
list_all ¶
Get all registered device templates.
| RETURNS | DESCRIPTION |
|---|---|
list[DeviceTemplate]
|
List of all DeviceTemplate objects in the registry |
Example
registry = get_registry() all_devices = registry.list_all() print(f"Found {len(all_devices)} devices") Found 8 devices
Source code in src/pinviz/devices/registry.py
list_by_category ¶
Get all device templates in a specific category.
| PARAMETER | DESCRIPTION |
|---|---|
category
|
Device category (e.g., 'sensors', 'leds', 'displays')
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[DeviceTemplate]
|
List of DeviceTemplate objects in the specified category |
Example
registry = get_registry() sensors = registry.list_by_category('sensors') for sensor in sensors: ... print(f"{sensor.name}: {sensor.description}")
Source code in src/pinviz/devices/registry.py
DeviceTemplate
dataclass
¶
DeviceTemplate(
type_id: str,
name: str,
description: str,
category: str,
url: str | None = None,
i2c_address: int | None = None,
)
Metadata for a device template.
create_registry ¶
Create a new independent device registry.
Useful for: - Unit testing with isolated state - Multiple concurrent contexts - Custom device configurations
Each call creates a fresh registry that scans the device_configs/ directory independently. Changes to this registry do not affect the default registry returned by get_registry().
| RETURNS | DESCRIPTION |
|---|---|
DeviceRegistry
|
A new DeviceRegistry instance |
Example
from pinviz.devices import create_registry test_registry = create_registry()
Use test_registry independently¶
No pollution of global state¶
Source code in src/pinviz/devices/registry.py
get_registry ¶
Get the default device registry instance.
This is a convenience function for common use cases. The registry is lazily initialized on first access and cached for subsequent calls. For testing or isolated contexts, use create_registry() instead.
| RETURNS | DESCRIPTION |
|---|---|
DeviceRegistry
|
The default DeviceRegistry instance |
Example
from pinviz.devices import get_registry registry = get_registry() device = registry.create('bh1750') print(device.name) BH1750 Light Sensor
Source code in src/pinviz/devices/registry.py
reset_default_registry ¶
Reset the default registry (mainly for testing).
This clears the cached default registry, causing get_registry() to create a fresh instance on the next call. This is useful for resetting state between test runs.
Example
from pinviz.devices import reset_default_registry reset_default_registry()
Next call to get_registry() will create a new instance¶