feat(example): Ability to output raw data from the thermostats (#36)

This commit is contained in:
Pascal Bourque
2025-06-21 10:33:08 -04:00
committed by GitHub
parent 51b8f64dab
commit 0cf7a1756c
3 changed files with 40 additions and 27 deletions

View File

@@ -51,6 +51,12 @@ Then, run the example:
npm run example npm run example
``` ```
If you prefer to see the raw data published by your Mysa smart thermostats, run:
```bash
npm run example:raw
```
## Using ## Using
The Mysa SDK provides a simple interface to interact with Mysa smart thermostats. The Mysa SDK provides a simple interface to interact with Mysa smart thermostats.

View File

@@ -57,35 +57,41 @@ async function main() {
const devices = await client.getDevices(); const devices = await client.getDevices();
client.emitter.on('statusChanged', (status) => { if (process.env.MYSA_OUTPUT_RAW_DATA === 'true') {
try { client.emitter.on('rawRealtimeMessageReceived', (data) => {
const device = devices.DevicesObj[status.deviceId]; rootLogger.info(data, 'Raw message received');
const watts = status.current !== undefined ? status.current * device.Voltage : undefined; });
rootLogger.info( } else {
`'${device.Name}' status changed: ${status.temperature}°C, ${status.humidity}%, ${watts ?? 'na'}W` client.emitter.on('statusChanged', (status) => {
); try {
} catch (error) { const device = devices.DevicesObj[status.deviceId];
rootLogger.error(`Error processing status update for device '${status.deviceId}':`, error); const watts = status.current !== undefined ? status.current * device.Voltage : undefined;
} rootLogger.info(
}); `'${device.Name}' status changed: ${status.temperature}°C, ${status.humidity}%, ${watts ?? 'na'}W`
);
} catch (error) {
rootLogger.error(`Error processing status update for device '${status.deviceId}':`, error);
}
});
client.emitter.on('setPointChanged', (change) => { client.emitter.on('setPointChanged', (change) => {
try { try {
const device = devices.DevicesObj[change.deviceId]; const device = devices.DevicesObj[change.deviceId];
rootLogger.info(`'${device.Name}' setpoint changed from ${change.previousSetPoint} to ${change.newSetPoint}`); rootLogger.info(`'${device.Name}' setpoint changed from ${change.previousSetPoint} to ${change.newSetPoint}`);
} catch (error) { } catch (error) {
rootLogger.error(`Error processing setpoint update for device '${change.deviceId}':`, error); rootLogger.error(`Error processing setpoint update for device '${change.deviceId}':`, error);
} }
}); });
client.emitter.on('stateChanged', (change) => { client.emitter.on('stateChanged', (change) => {
try { try {
const device = devices.DevicesObj[change.deviceId]; const device = devices.DevicesObj[change.deviceId];
rootLogger.info(change, `'${device.Name}' state changed.`); rootLogger.info(change, `'${device.Name}' state changed.`);
} catch (error) { } catch (error) {
rootLogger.error(`Error processing setpoint update for device '${change.deviceId}':`, error); rootLogger.error(`Error processing setpoint update for device '${change.deviceId}':`, error);
} }
}); });
}
for (const device of Object.entries(devices.DevicesObj)) { for (const device of Object.entries(devices.DevicesObj)) {
const serial = await client.getDeviceSerialNumber(device[0]); const serial = await client.getDeviceSerialNumber(device[0]);

View File

@@ -40,6 +40,7 @@
"browser": false, "browser": false,
"scripts": { "scripts": {
"example": "tsx --watch ./example/main.ts", "example": "tsx --watch ./example/main.ts",
"example:raw": "MYSA_OUTPUT_RAW_DATA=true tsx --watch ./example/main.ts",
"lint": "eslint --max-warnings 0 src/**/*.ts", "lint": "eslint --max-warnings 0 src/**/*.ts",
"style-lint": "prettier -c .", "style-lint": "prettier -c .",
"build": "tsup", "build": "tsup",