discovery: generate capability JSON from the registry
A capability is a descriptor plus what the endpoint declares (device/Capability.ts), and its discovery object is
generated from the two. The new API is bridge.addDevice({ endpointId, name, categories, ... }) and
device.add(PowerController, options): both throw a DeclarationError that names the endpoint, the interface and
the instance (device/validate.ts), and leave the bridge and the device as they were. AlexaInterface is the same
Capability with the 1.x methods on it; it, ActionMapping and the enums moved to src/compat/, Device to src/device/.
What a 1.x caller can observe:
- every endpoint ends with { type: "AlexaInterface", interface: "Alexa", version: "3" } (alexa-interface.html);
new Alex2MQTT(..., { alexaInterface: false }) leaves it out
- the fields of a capability object come in the order of Amazon's examples; their content is unchanged
- addCapability() with a name that is not an interface throws (1.5.2 announced it with the version "UNKNOWN")
- ActionMapping takes the payload as an object; a JSON string is parsed (1.5.2 sent the string), any other throws
- what Alexa would reject in a 1.x declaration is not refused: device.check() lists it and the bridge logs each
line once, as "warning: ..." through the log hook, when it answers a discovery
- a device whose JSON cannot be built is left out of the answer and reported as an error event
- PowerController and EndpointHealth are the descriptors and keep ON/OFF and OK/UNREACHABLE; PowerState is new
Tests: six zoo devices declared the 1.x way give the JSON that Alexa accepted from 1.5.2 on 2026-09-28, plus the
Alexa capability. npm test: 85 pass (was 57) in 10-12 s, also on Node 18.20.8 and 20.20.2.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
aa0ffd64ea
commit
c492ef74d1
90 changed files with 5539 additions and 1760 deletions
79
dist/esm/device/Capability.d.ts
vendored
Normal file
79
dist/esm/device/Capability.d.ts
vendored
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import type { Declared, Directives, InterfaceDescriptor, Label, Properties, Semantics } from "../registry/types.js";
|
||||
/** What a declaration can set for any interface. */
|
||||
export interface CommonOptions {
|
||||
/** The name of the instance of a generic controller: "Blind.Lift", "Fan.Speed". */
|
||||
instance?: string;
|
||||
/** What the user calls the instance; the first name is the one the Alexa app shows. */
|
||||
friendlyNames?: Label[];
|
||||
/** The properties are in the answer to ReportState. Default: true. */
|
||||
retrievable?: boolean;
|
||||
/** A change of the properties is sent to Alexa as a ChangeReport. Default: false. */
|
||||
proactivelyReported?: boolean;
|
||||
/** The user can ask for the properties and cannot set them. Left out of discovery when not given. */
|
||||
nonControllable?: boolean;
|
||||
}
|
||||
/** The same as a schema. The friendly names are checked with the instance, by the rules of the interface. */
|
||||
export declare const commonOptions: import("../registry/schema.js").Schema<import("../registry/schema.js").InferShape<{
|
||||
instance: import("../registry/schema.js").OptionalSchema<string>;
|
||||
friendlyNames: import("../registry/schema.js").OptionalSchema<unknown[]>;
|
||||
retrievable: import("../registry/schema.js").OptionalSchema<boolean>;
|
||||
proactivelyReported: import("../registry/schema.js").OptionalSchema<boolean>;
|
||||
nonControllable: import("../registry/schema.js").OptionalSchema<boolean>;
|
||||
}>>;
|
||||
type Naming<I extends boolean> = I extends true ? {
|
||||
instance: string;
|
||||
friendlyNames: Label[];
|
||||
} : {
|
||||
instance?: string;
|
||||
friendlyNames?: Label[];
|
||||
};
|
||||
/** The options of device.add() for one interface: its own, and the ones every interface has. */
|
||||
export type Declaration<O, I extends boolean> = O & Omit<CommonOptions, "instance" | "friendlyNames"> & Naming<I>;
|
||||
/** A capability object of a discovery answer (alexa-discovery-objects.html, "Capability object"). */
|
||||
export interface CapabilityJson {
|
||||
type: "AlexaInterface";
|
||||
interface: string;
|
||||
instance?: string;
|
||||
version: string;
|
||||
properties?: {
|
||||
supported: Array<{
|
||||
name: string;
|
||||
}>;
|
||||
proactivelyReported: boolean;
|
||||
retrievable: boolean;
|
||||
nonControllable?: boolean;
|
||||
};
|
||||
capabilityResources?: {
|
||||
friendlyNames: Label[];
|
||||
};
|
||||
configuration?: Record<string, unknown>;
|
||||
configurations?: Record<string, unknown>;
|
||||
semantics?: Semantics;
|
||||
[field: string]: unknown;
|
||||
}
|
||||
/** One interface as an endpoint declares it: the descriptor, and what the declaration says about this endpoint. */
|
||||
export declare class Capability<P extends Properties = Properties, D extends Directives = Directives, O = {}> implements Declared<O> {
|
||||
readonly descriptor: InterfaceDescriptor<P, D, O, boolean>;
|
||||
/** The endpoint the capability is declared on; the device sets it. */
|
||||
endpointId: string;
|
||||
/** "" for an interface without instances. */
|
||||
instance: string;
|
||||
friendlyNames: Label[];
|
||||
retrievable: boolean;
|
||||
proactivelyReported: boolean;
|
||||
nonControllable?: boolean;
|
||||
/** The options of the interface: a range, the supported modes, the semantics. */
|
||||
options: O;
|
||||
/** What the bridge logs about the declaration, once, when it answers a discovery. */
|
||||
readonly notes: string[];
|
||||
constructor(descriptor: InterfaceDescriptor<P, D, O, boolean>, declared: CommonOptions & {
|
||||
options: O;
|
||||
});
|
||||
get namespace(): string;
|
||||
/** Mappings of "open", "close", "raise", "lower" and of states, when the declaration has any. */
|
||||
get semantics(): Semantics | undefined;
|
||||
/** The capability object for discovery, its fields in the order of the example on alexa-discovery-objects.html. */
|
||||
toJSON(): CapabilityJson;
|
||||
}
|
||||
export type AnyCapability = Capability<any, any, any>;
|
||||
export {};
|
||||
Loading…
Add table
Add a link
Reference in a new issue