Dominevo
Develop

Device capabilities for providers

The device-level capabilities a provider implements beyond the basics — switch options and power cycle, the timed siren, low-power sleep, re-sync, and reboot — with the rule each one enforces.

A device-native capability is registered from the device class with this.ctx.registerNativeCap(capability, provider). Type the provider with InferNativeProvider<typeof theCapability>; the compiler then lists the methods you owe. Every method receives deviceId: check it matches the device you serve. In the snippets below, this.client and this.readSirenOptions stand for your own driver code.

Switch: what it can do

A switch is a plain on/off control unless it says otherwise. Implement getOptions to declare two facts:

FieldDefaultMeaning
supportsSetStatetruefalse when the output reports state but cannot be set
supportsPowerCyclefalsetrue when the device can cut and restore its own output in one call
const provider: InferNativeProvider<typeof switchCapability> = {
  setState: async ({ deviceId, on }) => this.client.setPoe(deviceId, on),
  getOptions: async () => ({ supportsSetState: false, supportsPowerCycle: true }),
  powerCycle: async ({ deviceId }) => this.client.cyclePoe(deviceId),
}
this.ctx.registerNativeCap(switchCapability, provider)

powerCycle has no button and no automation action of its own: a forced restart of the device the switch powers uses it instead of off, wait, on. It has the same access tier as setState, because it actuates the same output.

The trap: when supportsSetState is false, your setState must refuse. The interface withdraws on/off only after the device answered false; a provider with no getOptions, or a read that failed, keeps on/off. Do not return false as a guess. A switch that can only power-cycle is still a switch, so keep it a switch rather than inventing a button capability: a power-source link compiles against switch.

Siren: timed, in seconds or plays

The plain on/off switch of a siren stays. The siren capability adds how long, and when it is expected to stop.

  • getOptions returns what the camera reports: a list or a { min, max, step } range of seconds, the default, whether the camera counts seconds or plays, the seconds-per-play factor, and source (camera, or fallback when it could not say). Never hardcode durations.
  • trigger takes durationSeconds or plays, never both, and answers the status at the moment of the trigger.
  • stop silences it.

The pure maths is shared; do not re-derive it. From @dominevo/types: planSirenTrigger(options, request) decides the plan (a count on a siren that counts seconds is refused), sirenTriggeredStatus(now, seconds, activation) builds the status with activeUntil, and sirenIdleStatus(previous) the idle one that keeps the last activation.

trigger: async ({ deviceId, durationSeconds, plays }) => {
  const options = await this.readSirenOptions()
  const plan = planSirenTrigger(options, { durationSeconds, plays })
  if (plan.asked === 'refused') throw new Error(plan.reason)
  await this.client.sound(plan)
  return plan.seconds === null
    ? { ...sirenIdleStatus(undefined), active: true, lastTriggeredAt: Date.now() } // end unknown: activeUntil stays null
    : sirenTriggeredStatus(Date.now(), plan.seconds, {
        plays: plan.asked === 'plays' ? plan.plays : null,
        estimated: plan.estimated,
      })
},

activeUntilEstimated is true when the end comes from a seconds-per-play factor the camera did not report: say so rather than presenting a guess as a fact.

Low-power sleep

low-power-sleep is for a device that sleeps and must be woken before it streams (a battery camera). A battery sensor is not one: it reports charge on battery and never sleeps. Binding the capability with wakeUp implemented is what marks the device as sleeping, so consumers know to wake it first.

The status carries sleeping and lastContactAt. lastContactAt is the last time the device proved it was reachable, from passive signals only: a completed round trip, an observed wake, a push. It is the one input that separates asleep from gone, so never set it from your own polling.

wakeUp({ deviceId, timeoutMs }) answers { awoke, durationMs }. Return awoke: false on a timeout; callers still try the stream.

The pitfall is polling: a read wakes the device and drains the battery. While sleeping is true, do not poll.

Re-sync

resync re-aligns a parent device (a container, a workload, a weather station an integration adopted) with its upstream source, optionally from zero (resetToSource purges the children first). The work belongs to the addon's device-adoption provider; the capability only forwards to it, so the generic Re-sync button works. Implement adoption and answer resync by asking for exactly that. Do not write a second re-derivation path.

Reboot

reboot is a soft firmware reboot for cameras, sensors, switches and gateways. Return only after the remote acknowledged, and do not block waiting for it to come back: the online indicator shows that. Let errors propagate verbatim so the interface shows them next to the button.

Then