Plugins¶
Python API reference for the tolomeo.plugins package.
A plugin exposes metrics and commands to its service. Derive from PluginBase for a custom plugin, or from SensorPlugin
for one that reads a sensor. For the lifecycle and the Meta class pattern, see
Plugin System.
PluginBase¶
tolomeo.plugins.PluginBase
¶
Bases: PluginABC
Base class supplying every plugin behaviour except hardware access.
Abstract: connect and disconnect are left to the concrete plugin, so
instantiating PluginBase itself raises TypeError.
Provides command registration, metric registration (input and output),
and task management. The setup method automatically registers all
commands and metrics declared in Meta at attach time.
PluginBase pre-registers the commands from
tolomeo.commands.plugin.BASE_COMMANDS.
Subclasses must implement connect and disconnect. Use
after_setup to start background tasks via task_manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger | None
|
Optional logger. Defaults to the module logger. |
None
|
Source code in tolomeo/plugins/base.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
cmd_registry
property
¶
Mapping of command name → command class for this plugin.
id
property
writable
¶
Unique identifier for this plugin instance.
Set by the subclass inside connect. Raises ValueError if
accessed before being set.
in_params
property
¶
SenML parameter dicts for all registered input metrics.
on_data_available
property
writable
¶
Callback invoked when new datapoints are ready to be published.
on_info_available
property
writable
¶
Callback invoked when new device info is ready to be published.
options
property
¶
This plugin's validated configuration, per Meta.config_model.
The raw [plugins.<id>] table is validated against the declared model
on first access and reused until the plugin id changes. setup touches
it, so a bad table stops the plugin at attach time rather than surfacing
later, in the middle of whatever the option was needed for.
Typed Any so a plugin can narrow the return type to its own model
without fighting the base signature.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the plugin declares no |
ValueError
|
If accessed before |
ConfigError
|
If the table does not satisfy the model. |
out_params
property
¶
SenML parameter dicts for all registered output metrics.
plugin_config
property
¶
Deployment configuration for this plugin, from [plugins.<id>].
Read-only, and resolved on every access rather than at construction,
because a plugin's id is assigned during connect. The result is a
copy, so a plugin that mutates what it reads cannot reconfigure later
readers.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
The plugin's configuration table. An empty mapping when no table is |
Dict[str, Any]
|
declared for its id, or when its id has not been assigned yet. |
task_manager
property
¶
The TaskManager for this plugin's background tasks.
command_handler(cmd_name, payload)
async
¶
Execute one of this plugin's registered commands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_name
|
str
|
Command class name. |
required |
payload
|
Dict
|
The decoded message body. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The command result. |
Raises:
| Type | Description |
|---|---|
UnknownCommandError
|
If no command is registered under |
CommandError
|
Any expected client error, such as an
|
Exception
|
Whatever the command raised, after logging it. |
Source code in tolomeo/plugins/base.py
register_command(cmd_class)
¶
Add a command to the registry under its class name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd_class
|
Type[PluginCmd]
|
Command class whose |
required |
Source code in tolomeo/plugins/base.py
register_input_metrics()
¶
Register the declared input metrics, un-namespaced.
register_metrics(metrics, group, *, namespace=False)
¶
Add metrics to a group, optionally prefixed with the plugin id.
The declared metrics are copied rather than added directly, because several plugin instances can share one declaration and namespacing would otherwise rename the metric for all of them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
List[Metric]
|
The metrics declared on |
required |
group
|
MetricGroup
|
The group to add them to. |
required |
namespace
|
bool
|
Whether to prefix each name with |
False
|
Returns:
| Type | Description |
|---|---|
List[Metric]
|
The registered copies, in declaration order. |
Source code in tolomeo/plugins/base.py
register_output_metrics()
¶
Register the declared output metrics, namespaced by plugin id.
setup()
async
¶
Validate the plugin's options, then register commands and metrics.
Options are validated here rather than lazily, so a bad configuration table fails the attach instead of surfacing at an arbitrary later read.
Returns:
| Type | Description |
|---|---|
bool
|
Always True; failures surface as exceptions. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If the plugin's configuration table does not satisfy
|
Source code in tolomeo/plugins/base.py
SensorPlugin¶
tolomeo.plugins.SensorPlugin
¶
Bases: PluginBase, SensorMixin
Plugin base class for sensor devices.
Combines PluginBase (command registry, metric registration, task
management) with SensorMixin (connection state, push-based data
delivery). Subclass this to implement a concrete sensor plugin.
Subclasses must implement connect and disconnect. Declare metrics
in Meta.out_metrics and commands in Meta.commands. Call
await self.push_reading(raw) to publish sensor readings from a
background task.
Example
A concrete plugin declares its metrics and implements the two hardware hooks::
class TemperaturePlugin(SensorPlugin):
class Meta:
out_metrics = [Metric("temperature", "°C")]
async def connect(self) -> bool:
self.id = "temperature"
return True
async def disconnect(self) -> bool:
return True
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger | None
|
Optional logger. Defaults to the module logger. |
None
|
Source code in tolomeo/plugins/sensor.py
__init__(logger=None)
¶
Initialise both PluginBase and SensorMixin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger | None
|
Optional logger. Defaults to the module logger. |
None
|
Source code in tolomeo/plugins/sensor.py
SensorSpec¶
tolomeo.plugins.SensorSpec
¶
Describes the set of metrics exposed by a sensor.
Wraps an OutputMetricGroup and exposes convenience accessors for
metric names, IDs, and SenML parameter structures used when
registering metrics with a service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
List[Metric]
|
List of |
required |
Source code in tolomeo/plugins/sensor.py
group
property
¶
The underlying output metric group.
metric_ids
property
¶
Unique IDs of all metrics in this spec.
metric_names
property
¶
Short names of all metrics in this spec.
metrics
property
¶
All Metric instances in this spec.
params
property
¶
SenML parameter dicts for all metrics, ready for publishing.
get_metric_by_id(id)
¶
SensorState¶
tolomeo.plugins.sensor.SensorState
¶
Bases: Enum
Connection and data state for a sensor device.
Attributes:
| Name | Type | Description |
|---|---|---|
DISCONNECTED |
Sensor is not connected. |
|
CONNECTED |
Sensor is connected but not yet notifying or streaming. |
|
NOTIFYING |
Sensor is connected and sending characteristic notifications. |
|
STREAMING |
Sensor is actively streaming data continuously. |
Source code in tolomeo/plugins/sensor.py
BLESensorPlugin¶
tolomeo.plugins.ble.BLESensorPlugin
¶
Bases: PluginBase, BLESensorMixin
BLE sensor plugin combining PluginBase with BLESensorMixin.
Abstract: parse_data is left to the concrete plugin, so instantiating
BLESensorPlugin raises TypeError. Subclass it and implement
parse_data to decode the device's own notification format.
Source code in tolomeo/plugins/ble/plugin.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
Meta
¶
connect()
async
¶
Connect to the discovered device, introspect it, and monitor it.
Requires discover to have run. On success the plugin id becomes the
device address and a connection_monitor task is started.
Returns:
| Type | Description |
|---|---|
bool
|
True when the connection is up; False when it failed. |
Source code in tolomeo/plugins/ble/plugin.py
disconnect()
async
¶
Stop notifications and disconnect, without triggering a reconnect.
Marks the disconnect as user-initiated so connection_monitor leaves
it alone.
Returns:
| Type | Description |
|---|---|
bool
|
True when the device is disconnected. |
Source code in tolomeo/plugins/ble/plugin.py
setup()
async
¶
Register commands and metrics, then start notifications.
Returns:
| Type | Description |
|---|---|
bool
|
True when setup completed; False when it failed, which is logged |
bool
|
rather than raised so the caller can decide whether to give up. |