Skip to content

Commit

Permalink
Delay configureAdaptiveLightning after additional Characteristic Prop…
Browse files Browse the repository at this point in the history
…erties are set
  • Loading branch information
Shaquu committed Jun 18, 2024
1 parent c061760 commit 9030a92
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 54 deletions.
2 changes: 2 additions & 0 deletions src/lib/HAPServiceNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ module.exports = (RED: NodeAPI) => {
self.config
)

ServiceUtils.configureAdaptiveLightning()

if (self.config.isParent) {
BridgeUtils.delayedPublish(self)
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/HAPServiceNode2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ module.exports = (RED: NodeAPI) => {
self.config
)

ServiceUtils.configureAdaptiveLightning()

if (self.config.isParent) {
BridgeUtils.delayedPublish(self)
}
Expand Down
52 changes: 26 additions & 26 deletions src/lib/utils/ServiceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -505,5 +504,6 @@ module.exports = function (node: HAPServiceNodeType) {
onClose,
waitForParent,
handleWaitForSetup,
configureAdaptiveLightning,
}
}
32 changes: 4 additions & 28 deletions src/lib/utils/ServiceUtils2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { logger } from '@nrchkb/logger'
import {
Accessory,
AdaptiveLightingController,
AdaptiveLightingControllerMode,
Characteristic,
CharacteristicChange,
CharacteristicEventTypes,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -558,5 +532,7 @@ module.exports = function (node: HAPService2NodeType) {
onClose,
waitForParent,
handleWaitForSetup,
configureAdaptiveLightning:
ServiceUtilsLegacy.configureAdaptiveLightning,
}
}

0 comments on commit 9030a92

Please sign in to comment.