UNPKG

@homebridge-plugins/homebridge-smarthq

Version:

The SmartHQ plugin allows you to interact with SmartHQ Devices in HomeKit and with Siri.

110 lines 4.92 kB
import { Buffer } from 'node:buffer'; import { OpalDeviceBase } from '../OpalDeviceBase.js'; import { ERD_TYPES } from '../../../index.js'; export class OpalProgressSvcManager extends OpalDeviceBase { platform; accessory; device; advancedOptionQueryStrs = [ 'device=opal&label=Production_Duration_Minutes&indicator=opalProductionLimit&type=number&defaultValue=0&placeholder=Number._0_for_Infinite', 'device=opal&label=HKC_Progress_Complete_Notification_Path&indicator=oplHKCProgressCompleteNotificationPath', ]; serviceName = 'Opal Progress'; service = null; configuredName = 'Ice Progress'; constructor(platform, accessory, device) { super(platform, accessory, device); this.platform = platform; this.accessory = accessory; this.device = device; if (platform.config.deviceOptions?.opal?.opalProductionLimit) { this.createService(); } else { // If the service exists but condition is false, remove it const existingService = this.accessory.getService(this.serviceName); if (existingService) { this.accessory.removeService(existingService); } } } // Create the service and configure it createService() { // Check if service already exists const existingService = this.accessory.getService(this.serviceName); // Remove existing service if it exists if (existingService) { this.accessory.removeService(existingService); } // Create new service this.service = this.accessory.addService(this.platform.Service.Fanv2, this.serviceName); this.service .getCharacteristic(this.platform.Characteristic.ConfiguredName) .onGet(() => this.configuredName) .setValue(this.configuredName); // Configure Active characteristic this.service.getCharacteristic(this.platform.Characteristic.Active) .setProps({ perms: ['ev', 'pr'], }); // Configure RotationSpeed characteristic this.service.getCharacteristic(this.platform.Characteristic.RotationSpeed) .setProps({ format: "int" /* Formats.INT */, unit: "percentage" /* Units.PERCENTAGE */, minStep: 1, minValue: 0, maxValue: 100, perms: ['ev', 'pr'], }) .removeOnGet() .removeOnSet() .on('change', async (chg) => { if (chg.oldValue !== 100 && chg.newValue === 100) { const notificationPath = this.platform.config.deviceOptions?.opal?.oplHKCProgressCompleteNotificationPath; if (notificationPath) { await this.sendHomeKitControllerNotification(notificationPath); } } }); } // Check if service exists hasService() { return this.service !== null; } async processProductionProgress() { try { const [productionValueProgressBar, productionValueMinutes] = await this.getProductionValue(); this.service?.updateCharacteristic(this.platform.Characteristic.RotationSpeed, Math.min(productionValueProgressBar, 100)); this.service?.updateCharacteristic(this.platform.Characteristic.Active, productionValueMinutes > 0 ? 1 : 0); return productionValueMinutes; } catch (err) { const typedErr = err; this.platform.errorLog(`Monitor error: ${typedErr.message}`); return 0; } } // Get current production value async getProductionValue() { try { const erdVal = await this.readErd(ERD_TYPES.OIM_PRODUCTION); const productionValueMinutes = Buffer.from(erdVal, 'hex').readUInt8(0); const completionistMsg = productionValueMinutes > 100 ? `, Completion: ${productionValueMinutes}` : ''; this.platform.debugSuccessLog(`Production: ${productionValueMinutes}, Limit: ${this.platform.config.deviceOptions?.opal?.opalProductionLimit}${completionistMsg}`); const opalProductionLimit = this.platform.config.deviceOptions?.opal?.opalProductionLimit; let productionValueProgressBar = 0; if (typeof opalProductionLimit === 'number' && opalProductionLimit > 0) { productionValueProgressBar = Math.floor((100 / opalProductionLimit) * productionValueMinutes); } return [productionValueProgressBar, productionValueMinutes]; } catch (error) { const typedErr = error; this.platform.errorLog(`Failed to read production value: ${typedErr.message}`); // Default to 0 if there's an error return [0, 0]; } } } //# sourceMappingURL=OpalProgressSvcManager.js.map