Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Per-Light Settings for Maximum Brightness Adjustment #1205

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@
"description": "ID of the Homebridge Hue2 migration resource link.",
"type": "string"
},
"lightSettings": {
"type": "array",
"title": "Light Settings",
"description": "Define limits for brightness using the light's serial number.",
"items": {
"type": "object",
"properties": {
"serialNumber": {
"type": "string",
"title": "Serial Number",
"description": "The serial number of the light."
},
"limit": {
"type": "integer",
"title": "Brightness Limit (%)",
"description": "The maximum brightness allowed (1-100%).",
"minimum": 1,
"maximum": 100
}
},
"required": [
"serialNumber",
"limit"
]
}
},
"lights": {
"description": "Expose lights.",
"type": "boolean"
Expand Down Expand Up @@ -376,6 +402,31 @@
"title": "Advanced Settings",
"description": "Don't change these, unless you understand what you're doing.",
"items": [
{
"type": "fieldset",
"expandable": true,
"title": "Light Brightness Limits",
"description": "Define brightness limits for specific lights using their serial numbers.",
"items": [
{
"key": "lightSettings",
"type": "array",
"addButtonTitle": "Add Light Limit",
"items": [
{
"key": "lightSettings[].serialNumber",
"title": "Serial Number",
"description": "Enter the unique serial number of the light."
},
{
"key": "lightSettings[].limit",
"title": "Brightness Limit (%)",
"description": "Enter the maximum brightness percentage (1-100)."
}
]
}
]
},
{
"key": "brightnessAdjustment",
"condition": {
Expand Down
95 changes: 66 additions & 29 deletions lib/HueLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -1170,41 +1170,78 @@ class HueLight {
// if (bri === this.hk.bri) {
// return callback()
// }
const lightConfig = this.bridge.platform.config.lightSettings.find(
(setting) => setting.serialNumber === this.serialNumber
);

const originalBri = bri; // Store original brightness for logging.

if (lightConfig && lightConfig.limit) {
const maxBrightness = lightConfig.limit;
bri = Math.round((bri * maxBrightness) / 100);
this.log.info(
'%s: Adjusting brightness from %s%% to %s%% of limit (%s%%)',
this.name,
originalBri,
bri,
maxBrightness
);
}

this.log.info(
'%s: homekit brightness changed from %s%% to %s%%', this.name,
this.hk.bri, bri
)
const oldBri = this.hk.bri
this.hk.bri = bri
this.checkAdaptiveLighting()
const newBri = Math.round(this.hk.bri * 254.0 / 100.0)
this.put({ bri: newBri }).then(() => {
this.obj.state.bri = newBri
callback()
}).catch((error) => {
this.hk.bri = oldBri
callback(error)
})
'%s: homekit brightness changed from %s%% to %s%%',
this.name,
this.hk.bri,
bri
);
const oldBri = this.hk.bri;
this.hk.bri = bri;
this.checkAdaptiveLighting();
const newBri = Math.round(this.hk.bri * 254.0 / 100.0);
this.put({ bri: newBri })
.then(() => {
this.obj.state.bri = newBri;
callback();
})
.catch((error) => {
this.hk.bri = oldBri;
callback(error);
});
}

setBriChange (delta, callback) {
delta = Math.round(delta)
const lightConfig = this.bridge.platform.config.lightSettings.find(
(setting) => setting.serialNumber === this.serialNumber
);

if (lightConfig && lightConfig.limit) {
const maxBrightness = lightConfig.limit;
delta = Math.round((delta * maxBrightness) / 100);
this.log.info(
'%s: Adjusting brightness change by %s%% of limit (%s%%)',
this.name,
delta,
maxBrightness
);
}

delta = Math.round(delta);
if (delta === 0) {
return callback()
return callback();
}
this.log.info(
'%s: homekit brightness change by %s%%', this.name,
delta
)
const briDelta = Math.round(delta * 254.0 / 100.0)
this.put({ bri_inc: briDelta }).then((obj) => {
setTimeout(() => {
this.service.setCharacteristic(my.Characteristics.BrightnessChange, 0)
}, this.config.resetTimeout)
callback()
}).catch((error) => {
callback(error)
})

this.log.info('%s: homekit brightness change by %s%%', this.name, delta);
const briDelta = Math.round(delta * 254.0 / 100.0);
this.put({ bri_inc: briDelta })
.then(() => {
setTimeout(() => {
this.service.setCharacteristic(my.Characteristics.BrightnessChange, 0);
}, this.config.resetTimeout);
callback();
})
.catch((error) => {
callback(error);
});
}

setCt (ct, callback) {
Expand Down
2 changes: 2 additions & 0 deletions lib/HuePlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class HuePlatform {
.boolKey('hueDimmerRepeat')
.boolKey('hueMotionTemperatureHistory')
.stringKey('homebridgeHue2')
.arrayKey('lightSettings')
.boolKey('lights')
.boolKey('lightSensors')
.boolKey('linkButton')
Expand Down Expand Up @@ -142,6 +143,7 @@ class HuePlatform {
}
}
}
this.config.lightSettings = this.config.lightSettings || [];
this.config.brightnessAdjustment /= 100
const excludeSensorTypes = this.config.excludeSensorTypes
this.config.excludeSensorTypes = {}
Expand Down