1.5.2: send() never rejects (status, error, scene, change report: resolves the topic or "" and reports the failure through the bridge's error event when listened to - 1.5.1 rejected, and an un-caught .send() killed the host on any broker hiccup); types ship (declaration: true, dist/*.d.ts tracked; addSupportedModes takes {value, modeResources}, ActionMapping payload optional, addHealthProp accepts EndpointHealth or the string); disconnect()/connect() re-binds devices to the new client; registerDevice returns the existing device on a duplicate endpointId (warning via the log hook, console.warn without one); ThermostatController discovery lists targetSetpoint and drops adaptiveRecoveryStatus; the UNSUPORTED INTERFACE TYPE stderr spam goes through the log hook. Examples: require("alex2node"), an error listener in each, EndpointHealth.OK, neutral endpoint ids, BlindControl reads correlationToken from the header, the thermostat reports Fahrenheit as Fahrenheit, ExamplePowerController is power-only again + new ExamplePowerControllerWithBrightness. readme (install from Forgejo, 1.5.2 changelog, table syntax), LICENSE (MIT); tests for the non-rejecting send, reconnect, duplicate, thermostat discovery and the shipped declaration signatures (8/8 on an in-process broker).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
717af632de
commit
275f00f8a7
35 changed files with 1011 additions and 176 deletions
|
|
@ -1,11 +1,12 @@
|
|||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||
const {
|
||||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
ActionMapping,
|
||||
AlexaActions,
|
||||
PowerController
|
||||
} = require("../dist/index.js");
|
||||
PowerController,
|
||||
EndpointHealth
|
||||
} = require("alex2node");
|
||||
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
|
@ -21,12 +22,13 @@ const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
|||
// Create and initialize a new Alexa-to-MQTT client
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
// Define the device name
|
||||
const deviceName = "Bedroom Blinds";
|
||||
|
||||
// Register the device with a unique endpoint ID
|
||||
const blinds = alex2NodeClient.registerDevice(deviceName, "endpoint1");
|
||||
const blinds = alex2NodeClient.registerDevice(deviceName, "bedroom-blinds-1");
|
||||
|
||||
// Add basic Alexa capabilities to the device
|
||||
blinds.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||
|
|
@ -63,12 +65,12 @@ console.log(blinds.getName());
|
|||
blinds.on("ReportState", (payload) => {
|
||||
console.log("ReportState received!", payload);
|
||||
|
||||
const { correlationToken } = payload;
|
||||
const { correlationToken } = payload.header;
|
||||
|
||||
const status = blinds.getStatusMessage(correlationToken);
|
||||
|
||||
status
|
||||
.addHealthProp("OK") // Device is healthy
|
||||
.addHealthProp(EndpointHealth.OK) // Device is healthy
|
||||
.addPowerControllerProp(outputState) // Report power state
|
||||
.addToggleControllerProp(outputState, "NodeJS.Toggle"); // Report toggle controller state
|
||||
|
||||
|
|
@ -101,7 +103,7 @@ blinds.on("Event", (directive, interfaceType) => {
|
|||
// Respond to the directive with the updated device status
|
||||
const status = blinds.getStatusMessage(token, true);
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addToggleControllerProp(outputState, "NodeJS.Toggle")
|
||||
.addPowerControllerProp(outputState)
|
||||
.send();
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController,
|
||||
DisplayCategory,
|
||||
} = require("../dist/index.js");
|
||||
EndpointHealth,
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
||||
|
|
@ -19,14 +20,15 @@ const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
|||
// Create and initialize a new Alexa-to-MQTT client
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
// Define the device name (Test mode controller)
|
||||
const deviceName = "Test mode controller";
|
||||
|
||||
// Register the device with a unique endpoint ID (you can use something like "endpoint1")
|
||||
// Register the device with a unique endpoint ID (unique per root topic, e.g. "fan-1")
|
||||
const modeControllerDevice = alex2NodeClient.registerDevice(
|
||||
deviceName,
|
||||
"endpoint6",
|
||||
"fan-1",
|
||||
[DisplayCategory.OTHER]
|
||||
);
|
||||
|
||||
|
|
@ -83,7 +85,7 @@ modeControllerDevice.on("ReportState", (payload) => {
|
|||
const status = modeControllerDevice.getStatusMessage(correlationToken);
|
||||
|
||||
status
|
||||
.addHealthProp("OK") // Device is healthy
|
||||
.addHealthProp(EndpointHealth.OK) // Device is healthy
|
||||
.addModeControllerProp("mode.fanmode", currentMode);
|
||||
|
||||
status.send(); // Send the state report back to Alexa
|
||||
|
|
@ -110,7 +112,7 @@ modeControllerDevice.on("Event", (directive, interfaceType) => {
|
|||
const status = modeControllerDevice.getStatusMessage(token, true);
|
||||
|
||||
status
|
||||
.addHealthProp("OK") // Device is healthy
|
||||
.addHealthProp(EndpointHealth.OK) // Device is healthy
|
||||
.addModeControllerProp("mode.fanmode", currentMode);
|
||||
|
||||
status.send(); // Send the state report back to Alexa
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||
const {
|
||||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController
|
||||
} = require("../dist/index.js");
|
||||
PowerController,
|
||||
EndpointHealth
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
||||
|
|
@ -18,12 +19,13 @@ const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
|||
// Create and initialize a new Alexa-to-MQTT client
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
// Define the device name (bedroom light)
|
||||
const deviceName = "Bedroom Light";
|
||||
|
||||
// Register the device with a unique endpoint ID (you can use something like "endpoint1")
|
||||
const bedroomLight = alex2NodeClient.registerDevice(deviceName, "2E3AE9");
|
||||
// Register the device with a unique endpoint ID (unique per root topic, e.g. "bedroom-light-1")
|
||||
const bedroomLight = alex2NodeClient.registerDevice(deviceName, "bedroom-light-1");
|
||||
|
||||
// Add the PowerController capability (for turning on/off the device)
|
||||
bedroomLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||
|
|
@ -31,9 +33,6 @@ bedroomLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
|||
// Log the device name to verify registration
|
||||
console.log(bedroomLight.getName());
|
||||
|
||||
const momentaryModeInstance = "mode.signal.beep";
|
||||
const defaultMomentaryMode = "mode.signal.beep.Normal";
|
||||
|
||||
/**
|
||||
* Handle Alexa's ReportState directive.
|
||||
* This occurs when Alexa queries the current state of the device (e.g., during routines or device status checks).
|
||||
|
|
@ -46,10 +45,8 @@ bedroomLight.on("ReportState", (payload) => {
|
|||
const status = bedroomLight.getStatusMessage(correlationToken);
|
||||
|
||||
status
|
||||
.addHealthProp("OK") // Device is healthy
|
||||
.addPowerControllerProp(outputState) // Report the current power state (ON/OFF)
|
||||
.addBrightnessControllerProp(100)
|
||||
.addModeControllerProp(momentaryModeInstance, defaultMomentaryMode);
|
||||
.addHealthProp(EndpointHealth.OK) // Device is healthy
|
||||
.addPowerControllerProp(outputState); // Report the current power state (ON/OFF)
|
||||
|
||||
status.send(); // Send the state report back to Alexa
|
||||
});
|
||||
|
|
@ -78,9 +75,8 @@ bedroomLight.on("Event", (directive, interfaceType) => {
|
|||
// Respond to the directive with the updated device status
|
||||
const status = bedroomLight.getStatusMessage(token, true);
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addPowerControllerProp(outputState)
|
||||
.addBrightnessControllerProp(100)
|
||||
.send();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||
const {
|
||||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController
|
||||
} = require("../dist/index.js");
|
||||
PowerController,
|
||||
EndpointHealth
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
||||
|
|
@ -18,12 +19,13 @@ const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
|||
// Create and initialize a new Alexa-to-MQTT client
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
// Define the device name (deferred light)
|
||||
const deviceName = "Deferred Light";
|
||||
|
||||
// Register the device with a unique endpoint ID (you can use something like "endpoint1")
|
||||
const deferredLight = alex2NodeClient.registerDevice(deviceName, "endpoint3");
|
||||
// Register the device with a unique endpoint ID (unique per root topic, e.g. "deferred-light-1")
|
||||
const deferredLight = alex2NodeClient.registerDevice(deviceName, "deferred-light-1");
|
||||
|
||||
// Add the PowerController capability (for turning on/off the device)
|
||||
deferredLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||
|
|
@ -40,7 +42,7 @@ deferredLight.on("ReportState", (payload) => {
|
|||
|
||||
const { correlationToken } = payload.header;
|
||||
|
||||
// Step 1: Send a DeferredResponse after ~1 second
|
||||
// Step 1: Send a DeferredResponse after ~2 seconds
|
||||
setTimeout(() => {
|
||||
const deferred = deferredLight.getStatusMessage(correlationToken, false, true); // isDeferred = true
|
||||
deferred.addEstimatedDeferralTime(20).send();
|
||||
|
|
@ -51,7 +53,7 @@ deferredLight.on("ReportState", (payload) => {
|
|||
setTimeout(() => {
|
||||
const status = deferredLight.getStatusMessage(correlationToken,false,false); // normal StateReport
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addPowerControllerProp(outputState)
|
||||
.send(true);
|
||||
console.log("Sent actual StateReport with device status");
|
||||
|
|
@ -83,7 +85,7 @@ deferredLight.on("Event", (directive, interfaceType) => {
|
|||
// Respond to the directive with the updated device status
|
||||
const status = deferredLight.getStatusMessage(token, true);
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addPowerControllerProp(outputState)
|
||||
.send();
|
||||
}
|
||||
|
|
|
|||
99
examples/ExamplePowerControllerWithBrightness.js
Normal file
99
examples/ExamplePowerControllerWithBrightness.js
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController,
|
||||
EndpointHealth
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
||||
// Simulated in-memory device state (used for example/demo purposes)
|
||||
let outputState = PowerController.OFF; // Initialize the power state to OFF
|
||||
let brightness = 100; // 0-100, what Alexa.BrightnessController reports
|
||||
|
||||
// Load MQTT connection credentials and root topic from environment variables
|
||||
const username = process.env.MQTT_USERNAME;
|
||||
const password = process.env.MQTT_PASSWORD;
|
||||
const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
||||
|
||||
// Create and initialize a new Alexa-to-MQTT client
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
// Define the device name (dimmable living room light)
|
||||
const deviceName = "Living Room Light";
|
||||
|
||||
// Register the device with a unique endpoint ID (unique per root topic, e.g. "living-room-light-1")
|
||||
const livingRoomLight = alex2NodeClient.registerDevice(deviceName, "living-room-light-1");
|
||||
|
||||
// Declare every capability the device reports: on/off AND brightness (a property Alexa has not been told about is ignored)
|
||||
livingRoomLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||
livingRoomLight.addCapability(AlexaInterfaceType.BRIGHTNESS_CONTROLLER);
|
||||
|
||||
// Log the device name to verify registration
|
||||
console.log(livingRoomLight.getName());
|
||||
|
||||
// Clamp a brightness to Alexa's 0-100 range
|
||||
const clamp = (value) => Math.max(0, Math.min(100, Math.round(value)));
|
||||
|
||||
/**
|
||||
* Handle Alexa's ReportState directive.
|
||||
* This occurs when Alexa queries the current state of the device (e.g., during routines or device status checks).
|
||||
*/
|
||||
livingRoomLight.on("ReportState", (payload) => {
|
||||
console.log("ReportState received!", payload);
|
||||
|
||||
const { correlationToken } = payload.header;
|
||||
|
||||
const status = livingRoomLight.getStatusMessage(correlationToken);
|
||||
|
||||
status
|
||||
.addHealthProp(EndpointHealth.OK) // Device is healthy
|
||||
.addPowerControllerProp(outputState) // Report the current power state (ON/OFF)
|
||||
.addBrightnessControllerProp(brightness); // Report the current brightness (0-100)
|
||||
|
||||
status.send(); // Send the state report back to Alexa
|
||||
});
|
||||
|
||||
/**
|
||||
* Handle incoming control directives (TurnOn / TurnOff, SetBrightness / AdjustBrightness).
|
||||
* These directives come from Alexa when a user issues a command.
|
||||
*/
|
||||
livingRoomLight.on("Event", (directive, interfaceType) => {
|
||||
console.log("Event received", { directive, interfaceType });
|
||||
|
||||
const name = directive.header.name;
|
||||
const token = directive.header.correlationToken;
|
||||
|
||||
if (interfaceType === AlexaInterfaceType.POWER_CONTROLLER) {
|
||||
// Update the internal state based on the command (TurnOn / TurnOff)
|
||||
if (name === "TurnOn") {
|
||||
outputState = PowerController.ON;
|
||||
console.log("Turning ON the Living Room Light");
|
||||
} else if (name === "TurnOff") {
|
||||
outputState = PowerController.OFF;
|
||||
console.log("Turning OFF the Living Room Light");
|
||||
}
|
||||
} else if (interfaceType === AlexaInterfaceType.BRIGHTNESS_CONTROLLER) {
|
||||
// "Set the light to 40 percent" -> SetBrightness { brightness }; "dim the light" -> AdjustBrightness { brightnessDelta }
|
||||
if (name === "SetBrightness") {
|
||||
brightness = clamp(directive.payload.brightness);
|
||||
} else if (name === "AdjustBrightness") {
|
||||
brightness = clamp(brightness + directive.payload.brightnessDelta);
|
||||
}
|
||||
outputState = brightness > 0 ? PowerController.ON : PowerController.OFF; // a dimmer at 0 is off
|
||||
console.log(`Living Room Light brightness is now ${brightness}%`);
|
||||
} else {
|
||||
return; // not one of ours
|
||||
}
|
||||
|
||||
// Respond to the directive with the full device status (both properties, whichever one changed)
|
||||
const status = livingRoomLight.getStatusMessage(token, true);
|
||||
status
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addPowerControllerProp(outputState)
|
||||
.addBrightnessControllerProp(brightness)
|
||||
.send();
|
||||
});
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController,
|
||||
AlexaErrorType,
|
||||
} = require("../dist/index.js");
|
||||
EndpointHealth,
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config();
|
||||
|
||||
|
|
@ -15,8 +17,9 @@ const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
|||
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect();
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
const errorLight = alex2NodeClient.registerDevice("bad light", "endpoint4");
|
||||
const errorLight = alex2NodeClient.registerDevice("bad light", "bad-light-1");
|
||||
errorLight.addCapability(AlexaInterfaceType.POWER_CONTROLLER);
|
||||
|
||||
console.log(errorLight.getName());
|
||||
|
|
@ -85,7 +88,7 @@ errorLight.on("Event", (directive, interfaceType) => {
|
|||
|
||||
const status = errorLight.getStatusMessage(token, true);
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addPowerControllerProp(outputState)
|
||||
.send();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController,
|
||||
DisplayCategory
|
||||
} = require("../dist/index.js");
|
||||
DisplayCategory,
|
||||
EndpointHealth
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
||||
|
|
@ -21,12 +22,13 @@ const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
|||
// Create and initialize a new Alexa-to-MQTT client
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
// Define the device name and register it
|
||||
const deviceName = "Kitchen Light";
|
||||
const kitchenLight = alex2NodeClient.registerDevice(
|
||||
deviceName,
|
||||
"endpoint7",
|
||||
"kitchen-light-1",
|
||||
[DisplayCategory.LIGHT]
|
||||
);
|
||||
|
||||
|
|
@ -69,7 +71,7 @@ kitchenLight.on("ReportState", (payload) => {
|
|||
const { correlationToken } = payload.header;
|
||||
const status = kitchenLight.getStatusMessage(correlationToken);
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addPowerControllerProp(outputState)
|
||||
.addModeControllerProp(momentaryModeInstance, defaultMomentaryMode);
|
||||
status.send();
|
||||
|
|
@ -82,7 +84,7 @@ kitchenLight.on("Event", (directive, interfaceType) => {
|
|||
console.log("Event received", { directive, interfaceType });
|
||||
const { name, correlationToken, instance } = directive.header;
|
||||
|
||||
const status = kitchenLight.getStatusMessage(correlationToken, true).addHealthProp("OK");
|
||||
const status = kitchenLight.getStatusMessage(correlationToken, true).addHealthProp(EndpointHealth.OK);
|
||||
|
||||
// PowerController logic
|
||||
if (interfaceType === AlexaInterfaceType.POWER_CONTROLLER) {
|
||||
|
|
|
|||
|
|
@ -1,18 +1,15 @@
|
|||
// Import required modules and types from the compiled TypeScript distribution (via dist/index.js)
|
||||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController,
|
||||
TemperatureSensorScale,
|
||||
DisplayCategory,
|
||||
ThermostatMode,
|
||||
} = require("../dist/index.js");
|
||||
EndpointHealth,
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config(); // Load environment variables from .env file
|
||||
|
||||
// Simulated in-memory device state (used for example/demo purposes)
|
||||
let outputState = PowerController.OFF; // Initialize the power state to OFF (if thermostat supports ON/OFF modes)
|
||||
|
||||
// Load MQTT connection credentials and root topic from environment variables
|
||||
const username = process.env.MQTT_USERNAME;
|
||||
const password = process.env.MQTT_PASSWORD;
|
||||
|
|
@ -21,6 +18,7 @@ const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
|||
// Create and initialize a new Alexa-to-MQTT client
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false);
|
||||
alex2NodeClient.connect(); // Connect to the MQTT broker
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
// Define the device name (Thermostat Test)
|
||||
const deviceName = "Thermostat Test";
|
||||
|
|
@ -28,7 +26,7 @@ const deviceName = "Thermostat Test";
|
|||
// Register the thermostat device with a unique endpoint ID
|
||||
const thermostat = alex2NodeClient.registerDevice(
|
||||
deviceName,
|
||||
"thermostatEndpoint1",
|
||||
"thermostat-1",
|
||||
[DisplayCategory.TEMPERATURE_SENSOR, DisplayCategory.THERMOSTAT,DisplayCategory.OTHER]
|
||||
);
|
||||
|
||||
|
|
@ -49,7 +47,7 @@ modeController.addSupportedModes([
|
|||
{
|
||||
"@type": "text",
|
||||
value: {
|
||||
text: "Bannana",
|
||||
text: "Auto",
|
||||
locale: "en-US",
|
||||
},
|
||||
},
|
||||
|
|
@ -63,7 +61,7 @@ modeController.addSupportedModes([
|
|||
{
|
||||
"@type": "text",
|
||||
value: {
|
||||
text: "Apple",
|
||||
text: "On",
|
||||
locale: "en-US",
|
||||
},
|
||||
},
|
||||
|
|
@ -80,7 +78,7 @@ console.log(thermostat.getName());
|
|||
* This occurs when Alexa queries the current state of the thermostat (e.g., during routines or device status checks).
|
||||
*/
|
||||
|
||||
const deviceData = {
|
||||
const deviceData = { // all temperatures in Fahrenheit; the library converts to Celsius for Alexa
|
||||
lowerSetpoint: 50,
|
||||
upperSetpoint: 70,
|
||||
targetSetpoint: 60,
|
||||
|
|
@ -96,10 +94,10 @@ thermostat.on("ReportState", (payload) => {
|
|||
let status = thermostat.getStatusMessage(correlationToken);
|
||||
|
||||
status
|
||||
.addHealthProp("OK") // Device is healthy and reachable
|
||||
.addHealthProp(EndpointHealth.OK) // Device is healthy and reachable
|
||||
.addModeControllerProp("mode.fanmode", deviceData.fanMode)
|
||||
.addTemperatureSensorProp(
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.current
|
||||
) // Report current temperature
|
||||
.addThermostatModeProp(deviceData.thermostatMode); // Report current thermostat mode
|
||||
|
|
@ -108,20 +106,20 @@ thermostat.on("ReportState", (payload) => {
|
|||
status = status
|
||||
.addThermostatControllerProp(
|
||||
"lowerSetpoint",
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.lowerSetpoint
|
||||
) // Lower bound of temperature range
|
||||
.addThermostatControllerProp(
|
||||
"upperSetpoint",
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.upperSetpoint
|
||||
); // Upper bound of temperature range
|
||||
} else {
|
||||
status = status.addThermostatControllerProp(
|
||||
"targetSetpoint",
|
||||
TemperatureSensorScale.CELSIUS,
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.targetSetpoint
|
||||
); // Lower bound of temperature range
|
||||
); // Single setpoint (HEAT / COOL)
|
||||
}
|
||||
status.send(); // Send the full state report back to Alexa
|
||||
});
|
||||
|
|
@ -164,7 +162,7 @@ thermostat.on("Event", (directive, interfaceType) => {
|
|||
let status = thermostat.getStatusMessage(token, true);
|
||||
|
||||
status
|
||||
.addHealthProp("OK") // Device is healthy and reachable
|
||||
.addHealthProp(EndpointHealth.OK) // Device is healthy and reachable
|
||||
.addModeControllerProp("mode.fanmode", deviceData.fanMode)
|
||||
.addTemperatureSensorProp(
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
|
|
@ -188,7 +186,7 @@ thermostat.on("Event", (directive, interfaceType) => {
|
|||
"targetSetpoint",
|
||||
TemperatureSensorScale.FAHRENHEIT,
|
||||
deviceData.targetSetpoint
|
||||
); // Lower bound of temperature range
|
||||
); // Single setpoint (HEAT / COOL)
|
||||
}
|
||||
status.send(); // Send the full state report back to Alexa
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,23 @@
|
|||
// Import the library ("alex2node" is the installed package; inside this checkout it resolves to the built dist/ via package.json "exports")
|
||||
const {
|
||||
Alex2MQTT,
|
||||
AlexaInterfaceType,
|
||||
PowerController,
|
||||
} = require("../dist/index.js");
|
||||
|
||||
EndpointHealth,
|
||||
} = require("alex2node");
|
||||
|
||||
require("dotenv").config();
|
||||
|
||||
|
||||
const username = process.env.MQTT_USERNAME;
|
||||
const password = process.env.MQTT_PASSWORD;
|
||||
const rootTopic = process.env.MQTT_ROOT_TOPIC;
|
||||
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, true);
|
||||
|
||||
const alex2NodeClient = new Alex2MQTT(username, password, rootTopic, false); // set the 4th argument to true to print every MQTT payload
|
||||
alex2NodeClient.connect();
|
||||
|
||||
alex2NodeClient.on("error", (e) => console.error("broker:", e.message)); // Log broker errors (on 1.5.0 this listener is also what keeps an outage from killing the process)
|
||||
|
||||
const deviceName = "Custom Device";
|
||||
const customToggle = alex2NodeClient.registerDevice(deviceName, "endpoint8");
|
||||
const customToggle = alex2NodeClient.registerDevice(deviceName, "custom-device-1");
|
||||
|
||||
// Define 8 toggle instances and friendly names
|
||||
const switchCount = 8;
|
||||
|
|
@ -40,7 +43,7 @@ const {
|
|||
const { correlationToken } = payload.header;
|
||||
const status = customToggle.getStatusMessage(correlationToken);
|
||||
|
||||
status.addHealthProp("OK");
|
||||
status.addHealthProp(EndpointHealth.OK);
|
||||
|
||||
// Report all toggle states
|
||||
for (let i = 0; i < switchCount; i++) {
|
||||
|
|
@ -68,7 +71,7 @@ const {
|
|||
|
||||
const status = customToggle.getStatusMessage(correlationToken, true);
|
||||
status
|
||||
.addHealthProp("OK")
|
||||
.addHealthProp(EndpointHealth.OK)
|
||||
.addToggleControllerProp(toggleStates[index], instance)
|
||||
.send();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue