// A lock whose bolt takes longer than the 7 seconds Alex2MQTT waits for an answer: Alexa.LockController. The // handler sends a DeferredResponse at once and the Response when the bolt has moved. Alex2MQTT waits 10 seconds for // the Response after a DeferredResponse. // // ALEX2MQTT_USERNAME=... ALEX2MQTT_PASSWORD=... ALEX2MQTT_ROOT_TOPIC=... node examples/lock.js // // "alex2node" is the installed package; inside this checkout it resolves to the built dist/. const { Alex2MQTT, LockController } = require("alex2node"); const missing = ["ALEX2MQTT_USERNAME", "ALEX2MQTT_PASSWORD", "ALEX2MQTT_ROOT_TOPIC"].filter((name) => !process.env[name]); if (missing.length > 0) { console.error(`${missing.join(", ")} not set. Set the MQTT user name, the password and the root topic of your Alex2MQTT account.`); process.exit(1); } const { ALEX2MQTT_USERNAME, ALEX2MQTT_PASSWORD, ALEX2MQTT_ROOT_TOPIC } = process.env; const bridge = new Alex2MQTT(ALEX2MQTT_USERNAME, ALEX2MQTT_PASSWORD, ALEX2MQTT_ROOT_TOPIC); bridge.on("connect", () => console.log("connected, discover the devices in the Alexa app")); bridge.on("error", (err) => console.error("bridge:", err.message)); // How long the bolt takes, in seconds const BOLT_SECONDS = Number(process.env.BOLT_SECONDS ?? 8); const lock = { state: "LOCKED" }; const moveBolt = (state) => new Promise((resolve) => { setTimeout(() => { lock.state = state; resolve(); }, BOLT_SECONDS * 1000); }); const device = bridge.addDevice({ endpointId: "front-door-lock", name: "Front Door", categories: ["SMARTLOCK"] }); const bolt = device.add(LockController); device.state((s) => s.set(bolt, "lockState", lock.state).health("OK")); function move(state) { return async (ctx) => { // The argument is optional: how many seconds the answer will take await ctx.defer(Math.ceil(BOLT_SECONDS)); await moveBolt(state); // After defer() the answer goes to //deferredResponse return ctx.respond(); }; } bolt.on("Lock", move("LOCKED")); bolt.on("Unlock", move("UNLOCKED")); bridge.connect();