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:
David 2026-09-28 04:58:39 +00:00
parent 717af632de
commit 275f00f8a7
35 changed files with 1011 additions and 176 deletions

View file

@ -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
}