homebridge-tuya-laundry
Version:
Allows washer/dryer cycle completion notifications using Tuya smart plugs with power meter, now using local control.
108 lines • 5.73 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((device, index) => {
this.log.info(`Device ${index + 1}: Name=${device.name}, ID=${device.deviceId}, IP=${device.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;
}
this.ipcServer = new ipcServer_1.IPCServer(this.log, this.config, this.tuyaApiService);
this.ipcServer.start();
if (this.config.laundryDevices) {
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;
}
}
laundryDevice.init();
}
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