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 a max brightness field for each dimmer in config file #149 #200

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ The `defaults` has these properties:

- `id` The name or id for the device that is registered in the Android/iOS App. When matching on ID please provide the `Tuya ID` as shown during Homebridge boot.
- `device_type` The `device_type` to be overruled. This can be useful for dimmers that are reported as `light` by the Tuya API and don't support hue and saturation or for outlets that are reported as `switch`.
- `max_brightness` The maximum value of your dimmer light brightness (It seems that not all dimmers has a the same value), you can get this value by setting your dimmer to 100% in Tuya\Smartlife app, then go into your homebridge main log screen and open the homekit app, once the accessories load you can view the output and see the current value of each light, this output value is your accessories maximum brightness.
milo526 marked this conversation as resolved.
Show resolved Hide resolved

[14/02/2021, 19:26:34] [TuyaWebPlatform] [office light] Characteristic.Brightness - [GET] 255
milo526 marked this conversation as resolved.
Show resolved Hide resolved

> Note: After overriding the device type, it might appear duplicated in both HomeBridge (Accessories Tab) and the Home App. To solve this issue, go to the Homebridge settings (top right corner) and remove the device using the `Remove Single Cached Accessory` option.

Expand Down
9 changes: 9 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
"description": "Can be found in your platform app (device info -> virtual ID) and is exposed during plugin boot (use the Tuya ID)",
"required": true
},
"max_brightness": {
"title": "The Device max brigthness",
milo526 marked this conversation as resolved.
Show resolved Hide resolved
"type": "number",
"default": "255",
"description": "Each dimmer light has different max value, find it and set it here"
milo526 marked this conversation as resolved.
Show resolved Hide resolved
},
"device_type": {
"title": "Device type",
"type": "string",
Expand Down Expand Up @@ -260,6 +266,9 @@
{
"key": "defaults[].id"
},
{
"key": "defaults[].max_brigthness"
},
{
"key": "defaults[].device_type"
},
Expand Down
16 changes: 16 additions & 0 deletions src/accessories/DimmerAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
OnCharacteristic,
} from "./characteristics";
import { TuyaDevice } from "../api/response";
import { TuyaDeviceDefaults } from "../config";

export class DimmerAccessory extends BaseAccessory {
constructor(
Expand Down Expand Up @@ -42,4 +43,19 @@ export class DimmerAccessory extends BaseAccessory {

return super.deviceSupportedCharacteristics;
}

validateConfigOverwrites(config: TuyaDeviceDefaults): string[] {
const errors = super.validateConfigOverwrites(config);
if (config?.max_brigthness) {
const maxBrigthness = Number(config.max_brigthness);
if (!maxBrigthness) {
errors.push(
"Wrong value configured for `max_brigthness`, should be a number"
);
} else {
config.max_brigthness = maxBrigthness;
}
}
return errors;
}
}
16 changes: 16 additions & 0 deletions src/accessories/LightAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "./characteristics";
import { ColorAccessory } from "./ColorAccessory";
import { TuyaDevice } from "../api/response";
import { TuyaDeviceDefaults } from "../config";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some warnings here, could you please take a look at those.


export class LightAccessory extends ColorAccessory {
constructor(
Expand Down Expand Up @@ -59,4 +60,19 @@ export class LightAccessory extends ColorAccessory {

return super.deviceSupportedCharacteristics;
}

validateConfigOverwrites(config: TuyaDeviceDefaults): string[] {
const errors = super.validateConfigOverwrites(config);
if (config?.max_brigthness) {
const maxBrigthness = Number(config.max_brigthness);
if (!maxBrigthness) {
errors.push(
"Wrong value configured for `max_brigthness`, should be a number"
);
} else {
config.max_brigthness = maxBrigthness;
}
}
return errors;
}
}
6 changes: 4 additions & 2 deletions src/accessories/characteristics/brightness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ export class BrightnessCharacteristic extends TuyaWebCharacteristic {
data?.color?.brightness !== undefined
) {
stateValue = Number(data.color.brightness);
} else if (data?.brightness) {
stateValue = Math.round((Number(data.brightness) / 255) * 100);
} else if (data?.brightness && data?.max_brightness) {
stateValue = Math.round(
(Number(data.brightness) / data.max_brightness) * 100
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data?.max_brightness will not be defined in case the user does not enter device defaults information.
Please use the old value (255) as the default value In this case.

Suggested change
} else if (data?.brightness && data?.max_brightness) {
stateValue = Math.round(
(Number(data.brightness) / data.max_brightness) * 100
);
} else if (data?.brightness) {
const max_brightness = data?.max_brightness ?? 255
stateValue = Math.round(
(Number(data.brightness) / max_brightness) * 100
);

This will use 255 as fallback incase the max_brightness is not set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I set the default value for max brightness to be 255 in the config file, then I read that value weather its provided or not, good enough? (check out latests commit)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you only set the "default" value in the ui config. This has nothing to do with the actual config file. Just when users use config-ui-x. Furthermore they can still remove the default value, making it undefined. Or not add the device config at all. Therefor I believe the proposed changes are still necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data?.max_brightness is null for me, before the change I read the value from the config file, where is the connection between data.max_brightness and config.max_brightness should be set?

}

if (stateValue) {
Expand Down
1 change: 1 addition & 0 deletions src/api/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TuyaDeviceDefaults } from "../config";
export type ExtendedBoolean = boolean | "true" | "false" | "True" | "False";

type TuyaProperties = Partial<{
max_brightness: number;
milo526 marked this conversation as resolved.
Show resolved Hide resolved
brightness: number | string;
color: Partial<{ hue: string; saturation: string; brightness: string }>;
color_mode: ColorModes;
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TuyaDeviceType } from "./api/response";
export type TuyaDeviceDefaults = {
id: string;
device_type: TuyaDeviceType;
max_brigthness: number;
min_temper: string | number;
max_temper: string | number;
current_temperature_factor: string | number;
Expand Down