mirror of
https://github.com/bourquep/mysa2mqtt.git
synced 2025-10-22 23:48:07 +00:00
build: Build and publish a proper CLI tool with options, also packaged as a Docker image (#2)
* Renamed environment variables * Moved MqttSettings to main.tsx * Using Commander for CLI arguments * PinoLogger * Option for json log format * Updated mysa-js-sdk to latest version * Moved options to their own module * Extracted session file management to the session module * Added deviceId meta to thermostat instance logger * Display version from package.json; added copyright * Create README.md * Build with tsup * Update .gitignore * Remove prepublishOnly npm script * Distributed CLI executable is now working * Update README.md * Dockerfile * Minify the build output * Update README.md * Create initial Github workflow * Create release.config.mjs * Read package version at run-time, not build-time * Update README.md * Create CONTRIBUTING.md * WIP: docker CI job * Trying multiple tags * Enable docker build cache * Testing the docker build cache * Dockerfile: set npm version in final stage for better caching * Testing docker build cache * Moved VERSION arg to the final build stage * Finalized the `docker` build job * Added copyright header to all source files * Specify radix when parsing integer options
This commit is contained in:
27
src/commander.d.ts
vendored
Normal file
27
src/commander.d.ts
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
mysa2mqtt
|
||||
Copyright (C) 2025 Pascal Bourque
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
// commander.d.ts
|
||||
declare module 'commander' {
|
||||
export * from '@commander-js/extra-typings';
|
||||
}
|
65
src/logger.ts
Normal file
65
src/logger.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
mysa2mqtt
|
||||
Copyright (C) 2025 Pascal Bourque
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import { Logger } from 'mqtt2ha';
|
||||
import { pino } from 'pino';
|
||||
|
||||
export class PinoLogger implements Logger {
|
||||
constructor(private readonly logger: pino.Logger) {}
|
||||
|
||||
debug(message: string, ...meta: unknown[]): void {
|
||||
const obj = meta.at(0);
|
||||
if (obj) {
|
||||
this.logger.debug(obj, message, ...meta);
|
||||
} else {
|
||||
this.logger.debug(message, ...meta);
|
||||
}
|
||||
}
|
||||
|
||||
info(message: string, ...meta: unknown[]): void {
|
||||
const obj = meta.at(0);
|
||||
if (obj) {
|
||||
this.logger.info(obj, message, ...meta);
|
||||
} else {
|
||||
this.logger.info(message, ...meta);
|
||||
}
|
||||
}
|
||||
|
||||
warn(message: string, ...meta: unknown[]): void {
|
||||
const obj = meta.at(0);
|
||||
if (obj) {
|
||||
this.logger.warn(obj, message, ...meta);
|
||||
} else {
|
||||
this.logger.warn(message, ...meta);
|
||||
}
|
||||
}
|
||||
|
||||
error(message: string, ...meta: unknown[]): void {
|
||||
const obj = meta.at(0);
|
||||
if (obj) {
|
||||
this.logger.error(obj, message, ...meta);
|
||||
} else {
|
||||
this.logger.error(message, ...meta);
|
||||
}
|
||||
}
|
||||
}
|
114
src/main.ts
114
src/main.ts
@@ -1,73 +1,89 @@
|
||||
import { configDotenv } from 'dotenv';
|
||||
import { readFile, rm, writeFile } from 'fs/promises';
|
||||
import { MysaApiClient, MysaSession } from 'mysa-js-sdk';
|
||||
import { pino } from 'pino';
|
||||
import { Thermostat } from './thermostat';
|
||||
#!/usr/bin/env node
|
||||
|
||||
configDotenv({
|
||||
path: ['.env', '.env.local'],
|
||||
override: true
|
||||
});
|
||||
/*
|
||||
mysa2mqtt
|
||||
Copyright (C) 2025 Pascal Bourque
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import { MqttSettings } from 'mqtt2ha';
|
||||
import { MysaApiClient } from 'mysa-js-sdk';
|
||||
import { pino } from 'pino';
|
||||
import { PinoLogger } from './logger';
|
||||
import { options } from './options';
|
||||
import { loadSession, saveSession } from './session';
|
||||
import { Thermostat } from './thermostat';
|
||||
|
||||
const rootLogger = pino({
|
||||
name: 'mysa2mqtt',
|
||||
level: process.env.MYSA_2_MQTT_LOG_LEVEL,
|
||||
transport: {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
colorize: true,
|
||||
singleLine: true,
|
||||
ignore: 'hostname,module',
|
||||
messageFormat: '\x1b[33m[{module}]\x1b[39m {msg}'
|
||||
}
|
||||
}
|
||||
}).child({ module: 'mysa2mqtt' });
|
||||
level: options.logLevel,
|
||||
transport:
|
||||
options.logFormat === 'pretty'
|
||||
? {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
colorize: true,
|
||||
singleLine: true,
|
||||
ignore: 'hostname,module',
|
||||
messageFormat: '\x1b[33m[{module}]\x1b[39m {msg}'
|
||||
}
|
||||
}
|
||||
: undefined
|
||||
});
|
||||
|
||||
/** Mysa2mqtt entry-point. */
|
||||
async function main() {
|
||||
rootLogger.info('Starting mysa2mqtt...');
|
||||
|
||||
let session: MysaSession | undefined;
|
||||
try {
|
||||
rootLogger.debug('Loading Mysa session...');
|
||||
const sessionJson = await readFile('session.json', 'utf8');
|
||||
session = JSON.parse(sessionJson);
|
||||
} catch {
|
||||
rootLogger.debug('No valid Mysa session file found.');
|
||||
}
|
||||
const client = new MysaApiClient(session, { logger: rootLogger.child({ module: 'mysa-js-sdk' }) });
|
||||
const session = await loadSession(options.mysaSessionFile, rootLogger);
|
||||
const client = new MysaApiClient(session, { logger: new PinoLogger(rootLogger.child({ module: 'mysa-js-sdk' })) });
|
||||
|
||||
client.emitter.on('sessionChanged', async (newSession) => {
|
||||
if (newSession) {
|
||||
rootLogger.debug('Saving new Mysa session...');
|
||||
await writeFile('session.json', JSON.stringify(newSession));
|
||||
} else {
|
||||
try {
|
||||
rootLogger.debug('Removing Mysa session file...');
|
||||
await rm('session.json');
|
||||
} catch {
|
||||
// Ignore error if file does not exist
|
||||
}
|
||||
}
|
||||
await saveSession(newSession, options.mysaSessionFile, rootLogger);
|
||||
});
|
||||
|
||||
if (!client.isAuthenticated) {
|
||||
rootLogger.info('Logging in...');
|
||||
const username = process.env.MYSA_2_MQTT_USERNAME;
|
||||
const password = process.env.MYSA_2_MQTT_PASSWORD;
|
||||
|
||||
if (!username || !password) {
|
||||
throw new Error('Missing MYSA_2_MQTT_USERNAME or MYSA_2_MQTT_PASSWORD environment variables.');
|
||||
}
|
||||
|
||||
await client.login(username, password);
|
||||
await client.login(options.mysaUsername, options.mysaPassword);
|
||||
}
|
||||
|
||||
const [devices, firmwares] = await Promise.all([client.getDevices(), client.getDeviceFirmwares()]);
|
||||
|
||||
const mqttSettings: MqttSettings = {
|
||||
host: options.mqttHost,
|
||||
port: options.mqttPort,
|
||||
username: options.mqttUsername,
|
||||
password: options.mqttPassword,
|
||||
client_name: options.mqttClientName,
|
||||
state_prefix: options.mqttTopicPrefix
|
||||
};
|
||||
|
||||
const thermostats = Object.entries(devices.DevicesObj).map(
|
||||
([, device]) =>
|
||||
new Thermostat(client, device, rootLogger.child({ module: 'thermostat' }), firmwares.Firmware[device.Id])
|
||||
new Thermostat(
|
||||
client,
|
||||
device,
|
||||
mqttSettings,
|
||||
new PinoLogger(rootLogger.child({ module: 'thermostat', deviceId: device.Id })),
|
||||
firmwares.Firmware[device.Id]
|
||||
)
|
||||
);
|
||||
|
||||
for (const thermostat of thermostats) {
|
||||
|
146
src/options.ts
Normal file
146
src/options.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
mysa2mqtt
|
||||
Copyright (C) 2025 Pascal Bourque
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import { Command, InvalidArgumentError, Option } from 'commander';
|
||||
import { configDotenv } from 'dotenv';
|
||||
import { readFileSync } from 'fs';
|
||||
import { dirname, join } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
configDotenv({
|
||||
path: ['.env', '.env.local'],
|
||||
override: true
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the package version at runtime.
|
||||
*
|
||||
* @returns The package version or 'unknown' if it cannot be read.
|
||||
*/
|
||||
function getPackageVersion(): string {
|
||||
try {
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
const packageJsonPath = join(__dirname, '..', 'package.json');
|
||||
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
|
||||
return packageJson.version || 'unknown';
|
||||
} catch {
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a required integer value.
|
||||
*
|
||||
* @param value - The value to parse.
|
||||
* @returns The parsed integer value.
|
||||
* @throws InvalidArgumentError if the value is not a valid integer.
|
||||
*/
|
||||
function parseRequiredInt(value: string) {
|
||||
const parsedValue = parseInt(value, 10);
|
||||
if (isNaN(parsedValue)) {
|
||||
throw new InvalidArgumentError('Must be a number.');
|
||||
}
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
const extraHelpText = `
|
||||
Copyright (c) 2025 Pascal Bourque
|
||||
Licensed under the MIT License
|
||||
|
||||
Source code and documentation available at: https://github.com/bourquep/mysa2mqtt
|
||||
`;
|
||||
|
||||
export const options = new Command('mysa2mqtt')
|
||||
.version(getPackageVersion())
|
||||
.description('Expose Mysa smart thermostats to home automation platforms via MQTT.')
|
||||
.addHelpText('afterAll', extraHelpText)
|
||||
.addOption(
|
||||
new Option('-l, --log-level <logLevel>', 'log level')
|
||||
.choices(['silent', 'fatal', 'error', 'warn', 'info', 'debug', 'trace'])
|
||||
.env('M2M_LOG_LEVEL')
|
||||
.default('info')
|
||||
.helpGroup('Configuration')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-f, --log-format <logFormat>', 'log format')
|
||||
.choices(['pretty', 'json'])
|
||||
.env('M2M_LOG_FORMAT')
|
||||
.default('pretty')
|
||||
.helpGroup('Configuration')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-H, --mqtt-host <mqttHost>', 'hostname of the MQTT broker')
|
||||
.env('M2M_MQTT_HOST')
|
||||
.makeOptionMandatory()
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-P, --mqtt-port <mqttPort>', 'port of the MQTT broker')
|
||||
.env('M2M_MQTT_PORT')
|
||||
.argParser(parseRequiredInt)
|
||||
.default(1883)
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-U, --mqtt-username <mqttUsername>', 'username of the MQTT broker')
|
||||
.env('M2M_MQTT_USERNAME')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-B, --mqtt-password <mqttPassword>', 'password of the MQTT broker')
|
||||
.env('M2M_MQTT_PASSWORD')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-u, --mysa-username <mysaUsername>', 'Mysa account username')
|
||||
.env('M2M_MYSA_USERNAME')
|
||||
.makeOptionMandatory()
|
||||
.helpGroup('Mysa')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-p, --mysa-password <mysaPassword>', 'Mysa account password')
|
||||
.env('M2M_MYSA_PASSWORD')
|
||||
.makeOptionMandatory()
|
||||
.helpGroup('Mysa')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-s, --mysa-session-file <mysaSessionFile>', 'Mysa session file')
|
||||
.env('M2M_MYSA_SESSION_FILE')
|
||||
.default('session.json')
|
||||
.helpGroup('Configuration')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-N, --mqtt-client-name <mqttClientName>', 'name of the MQTT client')
|
||||
.env('M2M_MQTT_CLIENT_NAME')
|
||||
.default('mysa2mqtt')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.addOption(
|
||||
new Option('-T, --mqtt-topic-prefix <mqttTopicPrefix>', 'prefix of the MQTT topic')
|
||||
.env('M2M_MQTT_TOPIC_PREFIX')
|
||||
.default('mysa2mqtt')
|
||||
.helpGroup('MQTT')
|
||||
)
|
||||
.parse()
|
||||
.opts();
|
69
src/session.ts
Normal file
69
src/session.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
mysa2mqtt
|
||||
Copyright (C) 2025 Pascal Bourque
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import { readFile, rm, writeFile } from 'fs/promises';
|
||||
import { MysaSession } from 'mysa-js-sdk';
|
||||
import pino from 'pino';
|
||||
|
||||
/**
|
||||
* Loads a Mysa session from a file.
|
||||
*
|
||||
* @param filename - The path to the file containing the session data.
|
||||
* @param logger - The logger instance to use for logging.
|
||||
* @returns A promise that resolves to the loaded MysaSession object or undefined if the file is not found or invalid.
|
||||
*/
|
||||
export async function loadSession(filename: string, logger: pino.Logger): Promise<MysaSession | undefined> {
|
||||
try {
|
||||
logger.info('Loading Mysa session...');
|
||||
const sessionJson = await readFile(filename, 'utf8');
|
||||
return JSON.parse(sessionJson);
|
||||
} catch {
|
||||
logger.info('No valid Mysa session file found.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a Mysa session to a file.
|
||||
*
|
||||
* @param session - The MysaSession object to save.
|
||||
* @param filename - The path to the file to save the session data to.
|
||||
* @param logger - The logger instance to use for logging.
|
||||
* @returns A promise that resolves when the session is saved.
|
||||
*/
|
||||
export async function saveSession(
|
||||
session: MysaSession | undefined,
|
||||
filename: string,
|
||||
logger: pino.Logger
|
||||
): Promise<void> {
|
||||
if (session) {
|
||||
logger.info('Saving Mysa session...');
|
||||
await writeFile(filename, JSON.stringify(session));
|
||||
} else {
|
||||
try {
|
||||
logger.debug('Removing Mysa session file...');
|
||||
await rm(filename);
|
||||
} catch {
|
||||
// Ignore error if file does not exist
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,37 +1,51 @@
|
||||
/*
|
||||
mysa2mqtt
|
||||
Copyright (C) 2025 Pascal Bourque
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import { Climate, ClimateAction, DeviceConfiguration, Logger, MqttSettings, Sensor } from 'mqtt2ha';
|
||||
import { DeviceBase, FirmwareDevice, MysaApiClient, MysaDeviceMode, StateChange, Status } from 'mysa-js-sdk';
|
||||
|
||||
export class Thermostat {
|
||||
private isStarted = false;
|
||||
private mqttSettings: MqttSettings;
|
||||
private mqttDevice: DeviceConfiguration;
|
||||
private mqttClimate: Climate;
|
||||
private mqttPower: Sensor;
|
||||
private readonly mqttDevice: DeviceConfiguration;
|
||||
private readonly mqttClimate: Climate;
|
||||
private readonly mqttPower: Sensor;
|
||||
|
||||
private readonly mysaStatusUpdateHandler = this.handleMysaStatusUpdate.bind(this);
|
||||
private readonly mysaStateChangeHandler = this.handleMysaStateChange.bind(this);
|
||||
|
||||
constructor(
|
||||
public client: MysaApiClient,
|
||||
public device: DeviceBase,
|
||||
private logger: Logger,
|
||||
public deviceFirmware?: FirmwareDevice
|
||||
public readonly mysaApiClient: MysaApiClient,
|
||||
public readonly mysaDevice: DeviceBase,
|
||||
private readonly mqttSettings: MqttSettings,
|
||||
private readonly logger: Logger,
|
||||
public readonly mysaDeviceFirmware?: FirmwareDevice
|
||||
) {
|
||||
this.mqttSettings = {
|
||||
host: process.env.MYSA_2_MQTT_BROKER_HOST || 'localhost',
|
||||
port: parseInt(process.env.MYSA_2_MQTT_BROKER_PORT || '1883'),
|
||||
username: process.env.MYSA_2_MQTT_BROKER_USERNAME,
|
||||
password: process.env.MYSA_2_MQTT_BROKER_PASSWORD,
|
||||
client_name: 'mysa2mqtt',
|
||||
state_prefix: 'mysa2mqtt'
|
||||
};
|
||||
|
||||
this.mqttDevice = {
|
||||
identifiers: device.Id,
|
||||
name: device.Name,
|
||||
identifiers: mysaDevice.Id,
|
||||
name: mysaDevice.Name,
|
||||
manufacturer: 'Mysa',
|
||||
model: device.Model,
|
||||
sw_version: deviceFirmware?.InstalledVersion
|
||||
model: mysaDevice.Model,
|
||||
sw_version: mysaDeviceFirmware?.InstalledVersion
|
||||
};
|
||||
|
||||
this.mqttClimate = new Climate(
|
||||
@@ -41,10 +55,10 @@ export class Thermostat {
|
||||
component: {
|
||||
component: 'climate',
|
||||
device: this.mqttDevice,
|
||||
unique_id: `mysa_${device.Id}_climate`,
|
||||
unique_id: `mysa_${mysaDevice.Id}_climate`,
|
||||
name: 'Thermostat',
|
||||
min_temp: device.MinSetpoint,
|
||||
max_temp: device.MaxSetpoint,
|
||||
min_temp: mysaDevice.MinSetpoint,
|
||||
max_temp: mysaDevice.MaxSetpoint,
|
||||
modes: ['off', 'heat'], // TODO: AC
|
||||
precision: 0.1,
|
||||
temp_step: 0.5,
|
||||
@@ -64,16 +78,16 @@ export class Thermostat {
|
||||
async (topic, message) => {
|
||||
switch (topic) {
|
||||
case 'mode_command_topic':
|
||||
this.client.setDeviceState(
|
||||
this.device.Id,
|
||||
this.mysaApiClient.setDeviceState(
|
||||
this.mysaDevice.Id,
|
||||
undefined,
|
||||
message === 'off' ? 'off' : message === 'heat' ? 'heat' : undefined
|
||||
);
|
||||
break;
|
||||
|
||||
case 'power_command_topic':
|
||||
this.client.setDeviceState(
|
||||
this.device.Id,
|
||||
this.mysaApiClient.setDeviceState(
|
||||
this.mysaDevice.Id,
|
||||
undefined,
|
||||
message === 'OFF' ? 'off' : message === 'ON' ? 'heat' : undefined
|
||||
);
|
||||
@@ -81,9 +95,9 @@ export class Thermostat {
|
||||
|
||||
case 'temperature_command_topic':
|
||||
if (message === '') {
|
||||
this.client.setDeviceState(this.device.Id, undefined, undefined);
|
||||
this.mysaApiClient.setDeviceState(this.mysaDevice.Id, undefined, undefined);
|
||||
} else {
|
||||
this.client.setDeviceState(this.device.Id, parseFloat(message), undefined);
|
||||
this.mysaApiClient.setDeviceState(this.mysaDevice.Id, parseFloat(message), undefined);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -96,7 +110,7 @@ export class Thermostat {
|
||||
component: {
|
||||
component: 'sensor',
|
||||
device: this.mqttDevice,
|
||||
unique_id: `mysa_${device.Id}_power`,
|
||||
unique_id: `mysa_${mysaDevice.Id}_power`,
|
||||
device_class: 'power',
|
||||
state_class: 'measurement',
|
||||
unit_of_measurement: 'W',
|
||||
@@ -114,8 +128,8 @@ export class Thermostat {
|
||||
this.isStarted = true;
|
||||
|
||||
try {
|
||||
const deviceStates = await this.client.getDeviceStates();
|
||||
const state = deviceStates.DeviceStatesObj[this.device.Id];
|
||||
const deviceStates = await this.mysaApiClient.getDeviceStates();
|
||||
const state = deviceStates.DeviceStatesObj[this.mysaDevice.Id];
|
||||
|
||||
this.mqttClimate.currentTemperature = state.CorrectedTemp.v;
|
||||
this.mqttClimate.currentHumidity = state.Humidity.v;
|
||||
@@ -129,10 +143,10 @@ export class Thermostat {
|
||||
await this.mqttPower.setState('state_topic', 'None');
|
||||
await this.mqttPower.writeConfig();
|
||||
|
||||
this.client.emitter.on('statusChanged', this.mysaStatusUpdateHandler);
|
||||
this.client.emitter.on('stateChanged', this.mysaStateChangeHandler);
|
||||
this.mysaApiClient.emitter.on('statusChanged', this.mysaStatusUpdateHandler);
|
||||
this.mysaApiClient.emitter.on('stateChanged', this.mysaStateChangeHandler);
|
||||
|
||||
await this.client.startRealtimeUpdates(this.device.Id);
|
||||
await this.mysaApiClient.startRealtimeUpdates(this.mysaDevice.Id);
|
||||
} catch (error) {
|
||||
this.isStarted = false;
|
||||
throw error;
|
||||
@@ -146,16 +160,16 @@ export class Thermostat {
|
||||
|
||||
this.isStarted = false;
|
||||
|
||||
await this.client.stopRealtimeUpdates(this.device.Id);
|
||||
await this.mysaApiClient.stopRealtimeUpdates(this.mysaDevice.Id);
|
||||
|
||||
this.client.emitter.off('statusChanged', this.mysaStatusUpdateHandler);
|
||||
this.client.emitter.off('stateChanged', this.mysaStateChangeHandler);
|
||||
this.mysaApiClient.emitter.off('statusChanged', this.mysaStatusUpdateHandler);
|
||||
this.mysaApiClient.emitter.off('stateChanged', this.mysaStateChangeHandler);
|
||||
|
||||
await this.mqttPower.setState('state_topic', 'None');
|
||||
}
|
||||
|
||||
private async handleMysaStatusUpdate(status: Status) {
|
||||
if (!this.isStarted || status.deviceId !== this.device.Id) {
|
||||
if (!this.isStarted || status.deviceId !== this.mysaDevice.Id) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -165,7 +179,7 @@ export class Thermostat {
|
||||
this.mqttClimate.targetTemperature = this.mqttClimate.currentMode !== 'off' ? status.setPoint : undefined;
|
||||
|
||||
if (status.current != null) {
|
||||
const watts = this.device.Voltage * status.current;
|
||||
const watts = this.mysaDevice.Voltage * status.current;
|
||||
await this.mqttPower.setState('state_topic', watts.toFixed(2));
|
||||
} else {
|
||||
await this.mqttPower.setState('state_topic', 'None');
|
||||
@@ -173,7 +187,7 @@ export class Thermostat {
|
||||
}
|
||||
|
||||
private async handleMysaStateChange(state: StateChange) {
|
||||
if (!this.isStarted || state.deviceId !== this.device.Id) {
|
||||
if (!this.isStarted || state.deviceId !== this.mysaDevice.Id) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user