diff --git a/src/lib/HAPServiceNode.ts b/src/lib/HAPServiceNode.ts index df2badc5..75892404 100644 --- a/src/lib/HAPServiceNode.ts +++ b/src/lib/HAPServiceNode.ts @@ -254,6 +254,8 @@ module.exports = (RED: NodeAPI) => { self.config ) + ServiceUtils.configureAdaptiveLightning() + if (self.config.isParent) { BridgeUtils.delayedPublish(self) } diff --git a/src/lib/HAPServiceNode2.ts b/src/lib/HAPServiceNode2.ts index 5b45e245..10f743e3 100644 --- a/src/lib/HAPServiceNode2.ts +++ b/src/lib/HAPServiceNode2.ts @@ -256,6 +256,8 @@ module.exports = (RED: NodeAPI) => { self.config ) + ServiceUtils.configureAdaptiveLightning() + if (self.config.isParent) { BridgeUtils.delayedPublish(self) } diff --git a/src/lib/utils/ServiceUtils.ts b/src/lib/utils/ServiceUtils.ts index 7558097b..e7e6ac60 100644 --- a/src/lib/utils/ServiceUtils.ts +++ b/src/lib/utils/ServiceUtils.ts @@ -365,32 +365,6 @@ module.exports = function (node: HAPServiceNodeType) { service = newService } else { service = accessory.addService(newService) - - if ( - serviceInformation.serviceName === 'Lightbulb' && - serviceInformation.config.adaptiveLightingOptionsEnable - ) { - try { - const adaptiveLightingController = - new AdaptiveLightingController(service, { - controllerMode: - serviceInformation.config - .adaptiveLightingOptionsMode ?? - AdaptiveLightingControllerMode.AUTOMATIC, - customTemperatureAdjustment: - serviceInformation.config - .adaptiveLightingOptionsCustomTemperatureAdjustment, - }) - - accessory.configureController( - adaptiveLightingController - ) - } catch (error) { - log.error( - `Failed to configure adaptive lightning due to ${error}` - ) - } - } } } else { // if a service with the same UUID and subtype was found it will @@ -496,6 +470,31 @@ module.exports = function (node: HAPServiceNodeType) { } } + const configureAdaptiveLightning = () => { + if ( + node.service.name === 'Lightbulb' && + node.config.adaptiveLightingOptionsEnable + ) { + try { + const adaptiveLightingController = + new AdaptiveLightingController(node.service, { + controllerMode: + node.config.adaptiveLightingOptionsMode ?? + AdaptiveLightingControllerMode.AUTOMATIC, + customTemperatureAdjustment: + node.config + .adaptiveLightingOptionsCustomTemperatureAdjustment, + }) + + node.accessory.configureController(adaptiveLightingController) + } catch (error) { + log.error( + `Failed to configure adaptive lightning due to ${error}` + ) + } + } + } + return { getOrCreate, onCharacteristicGet, @@ -505,5 +504,6 @@ module.exports = function (node: HAPServiceNodeType) { onClose, waitForParent, handleWaitForSetup, + configureAdaptiveLightning, } } diff --git a/src/lib/utils/ServiceUtils2.ts b/src/lib/utils/ServiceUtils2.ts index 26bf5c72..b12f1b92 100644 --- a/src/lib/utils/ServiceUtils2.ts +++ b/src/lib/utils/ServiceUtils2.ts @@ -1,8 +1,6 @@ import { logger } from '@nrchkb/logger' import { Accessory, - AdaptiveLightingController, - AdaptiveLightingControllerMode, Characteristic, CharacteristicChange, CharacteristicEventTypes, @@ -27,6 +25,8 @@ import HAPService2NodeType from '../types/HAPService2NodeType' module.exports = function (node: HAPService2NodeType) { const log = logger('NRCHKB', 'ServiceUtils2', node.config.name, node) + const ServiceUtilsLegacy = require('./ServiceUtils')(node) + const HapNodeJS = require('hap-nodejs') const Service = HapNodeJS.Service const Characteristic = HapNodeJS.Characteristic @@ -418,32 +418,6 @@ module.exports = function (node: HAPService2NodeType) { service = newService } else { service = accessory.addService(newService) - - if ( - serviceInformation.serviceName === 'Lightbulb' && - serviceInformation.config.adaptiveLightingOptionsEnable - ) { - try { - const adaptiveLightingController = - new AdaptiveLightingController(service, { - controllerMode: - serviceInformation.config - .adaptiveLightingOptionsMode ?? - AdaptiveLightingControllerMode.AUTOMATIC, - customTemperatureAdjustment: - serviceInformation.config - .adaptiveLightingOptionsCustomTemperatureAdjustment, - }) - - accessory.configureController( - adaptiveLightingController - ) - } catch (error) { - log.error( - `Failed to configure adaptive lightning due to ${error}` - ) - } - } } } else { // if a service with the same UUID and subtype was found it will @@ -558,5 +532,7 @@ module.exports = function (node: HAPService2NodeType) { onClose, waitForParent, handleWaitForSetup, + configureAdaptiveLightning: + ServiceUtilsLegacy.configureAdaptiveLightning, } }