import { timestamp } from "../events.js"; import { labels, shownAs } from "../resources.js"; import { s } from "../schema.js"; import { DeclarationError, defineInterface } from "../types.js"; /** * A button of a remote, or anything else that routines start on. Each instance is one button with the events it * has, a single and a double push; device.raise(SimpleEventSource, "Event", { id }, { instance }) says that one * happened. Alexa wants the instance to be unique among all endpoints, "preferably a version 4 UUID". * * The page folds its discovery example away. The capability is announced with the fields of its table and without a * properties object, as the interface has no property. */ export const SimpleEventSource = defineInterface({ namespace: "Alexa.SimpleEventSource", version: "1.0", doc: "https://developer.amazon.com/docs/alexaplus/device-apis/alexa-simpleeventsource.html", kind: "eventSource", tier: 2, instanced: true, properties: {}, directives: {}, events: { Event: { name: "Event", /** id: the id of one of the supportedEvents of the instance. */ payload: s.object({ id: s.string({ min: 1 }), timestamp }), topic: "proactive", }, }, options: s.object({ /** For a REMOTE the names are assets of the catalog: Alexa.Button.SinglePush, Alexa.Gesture.Tap. */ supportedEvents: s.array(s.object({ id: s.string({ min: 1 }), friendlyNames: labels }, { unknownKeys: "reject" }), { min: 1 }), }, { unknownKeys: "reject" }), // A capability declared the 1.x way has no events; check() says so discovery: ({ options: { supportedEvents } }) => ({ properties: false, ...(supportedEvents && { configuration: { supportedEvents } }), }), validate(capability) { const { supportedEvents } = capability.options; const ids = supportedEvents.map(({ id }) => id); // "The first friendly name in the array must be unique per instance of the capability." const names = supportedEvents.map(({ friendlyNames: [first] }) => shownAs(first)); const twice = (list) => list.find((entry, i) => list.indexOf(entry) !== i); if (twice(ids)) throw new DeclarationError(capability, `the event ${twice(ids)} is listed twice`); if (twice(names)) throw new DeclarationError(capability, `two events have ${twice(names)} as their first friendly name, the Alexa app shows an event by it`); }, });