Build an integration
Device integrations, service connections, and exporters — each one a typed instance record, with secrets, test, and a migration when the stored shape changes.
Every integration is a typed record of the addon's own kind, served by
integration-instances. The helper is RecordIntegrationInstances: you pass
the record kind and the hooks formSchema(id), initialBody, configured,
and, when they apply, mintId, onChanged, and test.
describe() states role (devices, services, or both), instanceMode
(single or multiple), and an optional discovery. A string field marked
.meta({ secret: true }) is a keychain secret bound to that row. A map of
secret strings is one keychain value per entry. onChanged(id, change) runs
in the process that serves the instance after create, update, enable, and
delete. Read the record and apply it. An edit takes effect on the next use.
formSchema(null) is the create form. configured is your rule for "this
instance can work". test probes the remote with the values it is given,
including values that are not saved yet. Set canTest only when that probe
is real.
Worked copies are in addon-examples.
Device integration
role: 'devices'. The instance is one account, hub, or box that brings
devices. It is listed on the Integrations page. The integrations registry
owns the on/off switch, so the record body does not declare enabled.
The record holds the connection. Secrets stay on the schema, not in an
environment variable. test is the Test connection action.
discovery.mode is how devices are found:
| Mode | Meaning |
|---|---|
none | The operator adds devices by hand. |
lan | The addon scans the network. The wizard offers the candidates. |
account | Devices appear after the instance signs in. |
scopedToInstance: true means a discovery run belongs to one instance, and
that instance claims the candidates. Adoption stamps integrationId on every
device the instance creates. The same id is the record id.
Declare what each device is (MAC, serial, vendor id) with
declareIdentifiers, so the same object seen by two integrations can be
recognised. See Device identity, which also
says when a machine is a Host and not a Gateway.
A physical bridge box (a radio or serial protocol) is DeviceType.Gateway. Its products are sibling
devices of the same integration. Do not parent them under the gateway, and do
not model the box as a Hub or a Container. A physical hub (an NVR, a
console) is its own integration instance, named after the hub, not a Hub
device with children.
Per-device configuration goes through ctx.settings.readDeviceStore and
ctx.settings.writeDeviceStore. The framework clears those rows when the
device is deleted. Do not keep a private table of per-device settings.
Per-device data that is not settings (footage, events, history, your own
rows) is removed by device-removal-hook. Implement onDeviceRemoved (idempotent;
answer one count per store you own, for example { counts: { clips: 12 } }) and
listDeviceData (which devices you hold data for, per store; it never deletes). Device manager writes a tombstone first, then calls the
hook. Catch up from deviceManager.listDeletedSince. A device that is
missing from a list, offline, or whose provider is restarting is not a
delete and has no tombstone.
A machine the integration talks to (a server, a console) is not a Gateway:
it is one Workload device with role host. A LAN scan proposes a gateway,
hub or controller as a candidate with setupIntegration: true: the wizard then
sets the integration up instead of adopting a device.
Copy: packages/addon-provider-enpal (adoptGateway) and
packages/addon-provider-vesync in the Dominevo server repository.
Worked example:
packages/example-device-integration.
Service / connection integration
role: 'services'. The instance is a typed connection: a log destination, a
mail relay, a broker. It has a test and the same secret rules. It is never
listed on the Integrations page. The operator edits it in that service's own
list.
enabled lives on the record body. Absent means on. setInstanceEnabled
is the switch. It is not a field of the form patch.
instanceMode: 'single' allows one record, named after the addon.
instanceMode: 'multiple' allows many. Provide mintId when
describe().mintsId is true. The helper does not invent an id you did not
choose.
Copy: packages/system/src/builtins/loki-logging,
packages/addon-smtp-nodemailer, and packages/addon-mqtt-broker.
Worked example:
packages/example-service-integration.
Exporter
An exporter provides device-export and integration-instances together.
describe().role is 'services' (the target is not on the Integrations
page) and instanceMode is 'multiple'. A device-export provider with no
instances is a bug.
device-export keeps status and per-device exposure, keyed by the same
instance id. A write names instanceId. A read may omit it to mean the
whole addon.
State that is not configuration (a signing key, the exported-endpoint signatures) lives in a per-instance state record. It does not belong in the form.
Actions live in the instance form as addon-action-button fields. Pass the
instance id on the action. Do not add a separate settings page for them.
Copy: packages/addon-provider-homeassistant/src/ha-export/target-instances.ts,
packages/addon-export-hap, packages/addon-export-alexa, and
packages/addon-export-google.
Worked example:
packages/example-exporter.
Both
Use role: 'both' when one addon adopts devices and also holds a service
connection. The Integrations page lists it because it brings devices. The
connection's on/off switch stays on the instance body.
Changing stored data later
When the stored shape changes, ship a migration in the addon's catalogue
(src/migrations/index.ts). Bind it with defineAddonMigrations and run it
with runMigrations during the addon's own boot, under that addon.
The step reads what it wrote, including secrets, and treats a mismatch as failure. A second run is a no-op: an existing row is left as it is, and the ledger records that the step is already done. After the migration, the addon reads only the new shape. Do not keep a second read path for the old one.
A procedure must be safe to run again. A crash between the write and the ledger row reruns it.