mirror of
https://github.com/bourquep/mysa2mqtt.git
synced 2025-10-22 23:48:07 +00:00
feat: Expose device serial number and origin (#7)
This commit is contained in:
19
src/main.ts
19
src/main.ts
@@ -64,8 +64,24 @@ async function main() {
|
||||
await client.login(options.mysaUsername, options.mysaPassword);
|
||||
}
|
||||
|
||||
rootLogger.debug('Fetching devices and firmwares...');
|
||||
const [devices, firmwares] = await Promise.all([client.getDevices(), client.getDeviceFirmwares()]);
|
||||
|
||||
rootLogger.debug('Fetching serial numbers...');
|
||||
const serialNumbers = new Map<string, string>();
|
||||
for (const [deviceId] of Object.entries(devices.DevicesObj)) {
|
||||
try {
|
||||
const serial = await client.getDeviceSerialNumber(deviceId);
|
||||
if (serial) {
|
||||
serialNumbers.set(deviceId, serial);
|
||||
}
|
||||
} catch (error) {
|
||||
rootLogger.error(`Failed to retrieve serial number for device ${deviceId}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
rootLogger.debug('Initializing MQTT entities...');
|
||||
|
||||
const mqttSettings: MqttSettings = {
|
||||
host: options.mqttHost,
|
||||
port: options.mqttPort,
|
||||
@@ -82,7 +98,8 @@ async function main() {
|
||||
device,
|
||||
mqttSettings,
|
||||
new PinoLogger(rootLogger.child({ module: 'thermostat', deviceId: device.Id })),
|
||||
firmwares.Firmware[device.Id]
|
||||
firmwares.Firmware[device.Id],
|
||||
serialNumbers.get(device.Id)
|
||||
)
|
||||
);
|
||||
|
||||
|
@@ -64,6 +64,8 @@ function parseRequiredInt(value: string) {
|
||||
return parsedValue;
|
||||
}
|
||||
|
||||
export const version = getPackageVersion();
|
||||
|
||||
const extraHelpText = `
|
||||
Copyright (c) 2025 Pascal Bourque
|
||||
Licensed under the MIT License
|
||||
@@ -72,7 +74,7 @@ Source code and documentation available at: https://github.com/bourquep/mysa2mqt
|
||||
`;
|
||||
|
||||
export const options = new Command('mysa2mqtt')
|
||||
.version(getPackageVersion())
|
||||
.version(version)
|
||||
.description('Expose Mysa smart thermostats to home automation platforms via MQTT.')
|
||||
.addHelpText('afterAll', extraHelpText)
|
||||
.addOption(
|
||||
|
@@ -21,12 +21,22 @@ 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 {
|
||||
Climate,
|
||||
ClimateAction,
|
||||
DeviceConfiguration,
|
||||
Logger,
|
||||
MqttSettings,
|
||||
OriginConfiguration,
|
||||
Sensor
|
||||
} from 'mqtt2ha';
|
||||
import { DeviceBase, FirmwareDevice, MysaApiClient, MysaDeviceMode, StateChange, Status } from 'mysa-js-sdk';
|
||||
import { version } from './options';
|
||||
|
||||
export class Thermostat {
|
||||
private isStarted = false;
|
||||
private readonly mqttDevice: DeviceConfiguration;
|
||||
private readonly mqttOrigin: OriginConfiguration;
|
||||
private readonly mqttClimate: Climate;
|
||||
private readonly mqttTemperature: Sensor;
|
||||
private readonly mqttHumidity: Sensor;
|
||||
@@ -40,14 +50,22 @@ export class Thermostat {
|
||||
public readonly mysaDevice: DeviceBase,
|
||||
private readonly mqttSettings: MqttSettings,
|
||||
private readonly logger: Logger,
|
||||
public readonly mysaDeviceFirmware?: FirmwareDevice
|
||||
public readonly mysaDeviceFirmware?: FirmwareDevice,
|
||||
public readonly mysaDeviceSerialNumber?: string
|
||||
) {
|
||||
this.mqttDevice = {
|
||||
identifiers: mysaDevice.Id,
|
||||
name: mysaDevice.Name,
|
||||
manufacturer: 'Mysa',
|
||||
model: mysaDevice.Model,
|
||||
sw_version: mysaDeviceFirmware?.InstalledVersion
|
||||
sw_version: mysaDeviceFirmware?.InstalledVersion,
|
||||
serial_number: mysaDeviceSerialNumber
|
||||
};
|
||||
|
||||
this.mqttOrigin = {
|
||||
name: 'mysa2mqtt',
|
||||
sw_version: version,
|
||||
support_url: 'https://github.com/bourquep/mysa2mqtt'
|
||||
};
|
||||
|
||||
this.mqttClimate = new Climate(
|
||||
@@ -57,6 +75,7 @@ export class Thermostat {
|
||||
component: {
|
||||
component: 'climate',
|
||||
device: this.mqttDevice,
|
||||
origin: this.mqttOrigin,
|
||||
unique_id: `mysa_${mysaDevice.Id}_climate`,
|
||||
name: 'Thermostat',
|
||||
min_temp: mysaDevice.MinSetpoint,
|
||||
@@ -112,6 +131,7 @@ export class Thermostat {
|
||||
component: {
|
||||
component: 'sensor',
|
||||
device: this.mqttDevice,
|
||||
origin: this.mqttOrigin,
|
||||
unique_id: `mysa_${mysaDevice.Id}_temperature`,
|
||||
name: 'Current temperature',
|
||||
device_class: 'temperature',
|
||||
@@ -128,6 +148,7 @@ export class Thermostat {
|
||||
component: {
|
||||
component: 'sensor',
|
||||
device: this.mqttDevice,
|
||||
origin: this.mqttOrigin,
|
||||
unique_id: `mysa_${mysaDevice.Id}_humidity`,
|
||||
name: 'Current humidity',
|
||||
device_class: 'humidity',
|
||||
@@ -144,6 +165,7 @@ export class Thermostat {
|
||||
component: {
|
||||
component: 'sensor',
|
||||
device: this.mqttDevice,
|
||||
origin: this.mqttOrigin,
|
||||
unique_id: `mysa_${mysaDevice.Id}_power`,
|
||||
name: 'Current power',
|
||||
device_class: 'power',
|
||||
|
Reference in New Issue
Block a user