Plugin System¶
A plugin holds the domain logic of a service, declares what it offers in a Meta class, and goes through a fixed
attach and detach lifecycle.
Plugins Are Separate from Services¶
Services own messaging and routing. Plugins own domain logic and hardware access. As a result:
- A plugin can be tested without a NATS connection.
- One service hosts many instances of one plugin class, each with its own id.
- The same plugin class works under different services.
Meta Declares What a Plugin Offers¶
Plugins declare their commands and metrics in an inner Meta class. PluginMetaclass collects them when the class
is defined, across the whole class hierarchy, so a subclass inherits its bases' commands:
class MyPlugin(SensorPlugin):
class Meta:
commands = [MyCommand] # added to base SensorPlugin commands
out_metrics = [Metric("v", "V")] # added to any inherited metrics
For commands, in_metrics and out_metrics, the collectors in tolomeo/collectors.py build the final list in
this order:
- Each ancestor's already-computed
_commands/_in_metrics/_out_metrics(walked over the MRO) - Entries from the current class's own
Metaclass only - Entries declared directly on the class body (outside
Meta)
Duplicates are dropped, keeping the first. A Meta that is not a class raises TypeError when the class is defined.
Options Extend, They Do Not Accumulate¶
A plugin's configuration options are declared in Meta too, as a Pydantic model:
A model is a single value, not a list. A class either inherits its ancestor's model or declares one that extends
it. An unrelated model raises TypeError when the class is defined, because the ancestor's methods read the fields
the ancestor declared.
PluginBase.options validates the plugin's [plugins.<id>] table against the model and caches the result. See
Configuration File.
Abstract Methods Are Enforced¶
PluginMetaclass derives from abc.ABCMeta, so a plugin that does not implement connect and disconnect cannot be
created:
TypeError: Can't instantiate abstract class MyPlugin without an
implementation for abstract method 'disconnect'
PluginBase, SensorPlugin, OTAPlugin and BLESensorPlugin are therefore abstract. They supply everything except
what only the concrete plugin knows: how to reach its hardware, how to install an update for OTA (install_update,
monitor_update), and how to decode its data for BLE (parse_data).
Plugins Attach and Detach in Fixed Phases¶
Every plugin goes through a two-phase lifecycle:
attach() -> bool
└─ before_connect() → connect() → after_connect()
└─ before_setup() → setup() → after_setup()
detach()
└─ before_disconnect() → disconnect() → after_disconnect()
└─ task_manager.stop_all() (only when disconnect succeeded)
PluginBase implements setup(): it validates the plugin's options and registers its commands and metrics. Start
background tasks in after_setup, when everything is registered.
A failed attach() leaves no usable plugin
attach() returns False when connect fails, and then skips setup. When setup fails, attach()
disconnects and stops the plugin's tasks; it returns False if setup returned False, and re-raises if setup
raised, for example a ConfigError from a bad options table. ServiceBase.attach_plugin registers a plugin only
when attach() returned True.
Hooks Extend the Lifecycle Without Replacing It¶
The six hooks (before_connect, after_connect, before_setup, after_setup, before_disconnect,
after_disconnect) do nothing by default. Override them to add logic without replacing the lifecycle.
From after_connect on, both the plugin id and its options are known. Setup work that needs both belongs in
before_setup. OTAPlugin uses it to wire its task callbacks and set its checkpoint path, then bootstraps its state
machine in after_setup; see OTA Mechanism.
Mixins Carry the Reusable Behavior¶
A plugin family has two parts: a mixin holding the reusable logic, and a thin class combining it with PluginBase.
The mixins only add methods: they override nothing on PluginBase, so listing the mixin last is safe. They rely on
PluginBase for the output metric group and the push callbacks, so they are tested through a plugin, not bare.
The sensor family splits along what its members reuse:
| Mixin | Provides | Used by |
|---|---|---|
SensorDataMixin |
push_reading, push_info |
SensorMixin, BLESensorMixin |
SensorConnectionMixin |
conn_context, on_disconnect, on_reconnect |
SensorMixin, BLESensorMixin |
SensorMixin |
the two above, plus SensorState tracking |
SensorPlugin |
BLESensorMixin |
the two above, plus Bleak client and FSM state | BLESensorPlugin |
See also
- Implement a Sensor Plugin — write a plugin that streams sensor data
- Implement a Custom Plugin — write a plugin that answers commands
- Configuration File — the
[plugins.<plugin_id>]tables and their keys - Architecture — how plugins fit with services, messaging and tasks