homebridge-tuya-laundry
Version:
Allows washer/dryer cycle completion notifications using Tuya smart plugs with power meter, now using local control.
115 lines • 6.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TuyaLaundryNotifyPlatform = void 0;
const settings_1 = require("./settings");
const laundryDeviceTracker_1 = require("./lib/laundryDeviceTracker");
const messageGateway_1 = require("./lib/messageGateway");
const configManager_1 = require("./lib/configManager");
const ipcServer_1 = require("./lib/ipcServer");
const tuyaApiService_1 = require("./lib/tuyaApiService");
const smartPlugService_1 = require("./lib/smartPlugService");
class TuyaLaundryNotifyPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.accessories = [];
this.laundryDevices = [];
this.log.info('TuyaLaundryNotifyPlatform initialized.');
const configManager = new configManager_1.ConfigManager(this.config);
this.log.info('Configuration Manager initialized. Fetching configuration...');
const { laundryDevices, tuyaApiCredentials } = configManager.getConfig();
if (laundryDevices && laundryDevices.length > 0) {
this.log.info('Laundry Devices Found: ${laundryDevices.length}');
laundryDevices.forEach((dev, i) => {
this.log.info(`Device ${i + 1}: Name=${dev.name}, ID=${dev.deviceId}, IP=${dev.ipAddress}`);
});
}
else {
this.log.info('No Laundry Devices found.');
}
if (tuyaApiCredentials) {
this.log.info('Tuya API Credentials:');
this.log.info('Access ID: ${tuyaApiCredentials.accessId}');
this.log.info('Access Key: ${tuyaApiCredentials.accessKey}');
this.log.info('Username: ${tuyaApiCredentials.username}');
this.log.info('Country Code: ${tuyaApiCredentials.countryCode}');
this.log.info('App Schema: ${tuyaApiCredentials.appSchema}');
this.log.info('Endpoint: ${tuyaApiCredentials.endpoint}');
}
else {
this.log.error('No Tuya API credentials found in configuration.');
}
const messageGateway = new messageGateway_1.MessageGateway(log, this.config, api);
const smartPlugService = new smartPlugService_1.SmartPlugService(this.tuyaApiService, log);
if (laundryDevices) {
for (const laundryDevice of laundryDevices) {
this.laundryDevices.push(new laundryDeviceTracker_1.LaundryDeviceTracker(log, messageGateway, laundryDevice, api, smartPlugService));
}
}
this.api.on('didFinishLaunching', async () => {
this.log.info('Homebridge has started, beginning initialization.');
if (!tuyaApiCredentials) {
this.log.error('Tuya API credentials are missing. Authentication cannot proceed.');
return;
}
this.tuyaApiService = tuyaApiService_1.TuyaApiService.getInstance(tuyaApiCredentials, this.log);
await this.tuyaApiService.authenticate();
const apiInstance = this.tuyaApiService.getApiInstance();
if (apiInstance) {
this.log.info('Tuya API successfully authenticated.');
}
else {
this.log.error('Failed to authenticate with Tuya API.');
return;
}
const enableIpcServer = this.config.enableIpcServer !== false;
if (enableIpcServer) {
this.ipcServer = new ipcServer_1.IPCServer(this.log, this.config, this.tuyaApiService);
this.ipcServer.start();
this.log.info('CLI tool is active. When you\'re done with setup, consider disabling it in plugin settings ' +
'(enableIpcServer: false) to close the IPC socket.');
}
if (this.config.laundryDevices) {
// Discover devices on the LAN once and share the results between trackers
const localDevices = await smartPlugService.discoverLocalDevices();
for (const laundryDevice of this.laundryDevices) {
try {
const uuid = this.api.hap.uuid.generate(laundryDevice.config.name || laundryDevice.config.deviceId);
const cachedAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (laundryDevice.config.exposeStateSwitch) {
if (!cachedAccessory) {
const accessory = new this.api.platformAccessory(laundryDevice.config.name || laundryDevice.config.deviceId, uuid);
laundryDevice.accessory = accessory;
if (laundryDevice.accessory) {
laundryDevice.accessory.addService(this.api.hap.Service.Switch, laundryDevice.config.name);
this.accessories.push(laundryDevice.accessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [laundryDevice.accessory]);
}
}
else {
laundryDevice.accessory = cachedAccessory;
}
}
await laundryDevice.init(localDevices);
}
catch (error) {
this.log.error(`Failed to init ${laundryDevice.config.name}`, error);
}
}
}
});
}
configureAccessory(accessory) {
const deviceName = this.config.name || this.config.deviceId;
const existingDevice = this.laundryDevices.find(() => this.api.hap.uuid.generate(deviceName) === accessory.UUID);
if (!existingDevice || !existingDevice.config.exposeStateSwitch) {
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
else {
this.accessories.push(accessory);
}
}
}
exports.TuyaLaundryNotifyPlatform = TuyaLaundryNotifyPlatform;
//# sourceMappingURL=platform.js.map