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

Improve multi-endpoint support #8031

Merged
merged 2 commits into from
Sep 27, 2024
Merged
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
12 changes: 6 additions & 6 deletions src/lib/modernExtend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
assertNumber,
batteryVoltageToPercentage,
configureSetPowerSourceWhenUnknown,
exposeEndpoints,
flatten,
getEndpointName,
getFromLookup,
Expand Down Expand Up @@ -469,7 +470,7 @@ export interface OnOffArgs {
export function onOff(args?: OnOffArgs): ModernExtend {
args = {powerOnBehavior: true, skipDuplicateTransaction: false, configureReporting: true, ...args};

const exposes: Expose[] = args.endpointNames ? args.endpointNames.map((ep) => e.switch().withEndpoint(ep)) : [e.switch()];
const exposes: Expose[] = exposeEndpoints(e.switch(), args.endpointNames);

const fromZigbee: Fz.Converter[] = [args.skipDuplicateTransaction ? fz.on_off_skip_duplicate_transaction : fz.on_off];
const toZigbee: Tz.Converter[] = [tz.on_off];
Expand Down Expand Up @@ -943,9 +944,7 @@ export function light(args?: LightArgs): ModernExtend {
}
: false;

const lightExpose = args.endpointNames
? args.endpointNames.map((ep) => e.light().withBrightness().withEndpoint(ep))
: [e.light().withBrightness()];
const lightExpose = exposeEndpoints(e.light().withBrightness(), args.endpointNames);

const fromZigbee: Fz.Converter[] = [fz.on_off, fz.brightness, fz.ignore_basic_report, fz.level_config];
const toZigbee: Tz.Converter[] = [
Expand Down Expand Up @@ -1001,12 +1000,13 @@ export function light(args?: LightArgs): ModernExtend {
if (args.color) {
effects.values.push('colorloop', 'stop_colorloop');
}
exposes.push(effects);

exposes.push(...exposeEndpoints(effects, args.endpointNames));
toZigbee.push(tz.effect);
}

if (args.powerOnBehavior) {
exposes.push(e.power_on_behavior(['off', 'on', 'toggle', 'previous']));
exposes.push(...exposeEndpoints(e.power_on_behavior(['off', 'on', 'toggle', 'previous']), args.endpointNames));
fromZigbee.push(fz.power_on_behavior);
toZigbee.push(tz.power_on_behavior);
}
Expand Down
19 changes: 11 additions & 8 deletions src/lib/philips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as ota from './ota';
import * as globalStore from './store';
import {Fz, KeyValue, KeyValueAny, Tz} from './types';
import * as utils from './utils';
import {isObject} from './utils';
import {exposeEndpoints, isObject} from './utils';

const NS = 'zhc:philips';
const ea = exposes.access;
Expand Down Expand Up @@ -71,12 +71,15 @@ export function philipsLight(args?: modernExtend.LightArgs & {hueEffect?: boolea
}
result.exposes.push(
// gradient_scene is deprecated, use gradient instead
e.enum('gradient_scene', ea.SET, Object.keys(gradientScenes)),
e
.list('gradient', ea.ALL, e.text('hex', ea.ALL).withDescription('Color in RGB HEX format (eg #663399)'))
.withLengthMin(1)
.withLengthMax(9)
.withDescription('List of RGB HEX colors'),
...exposeEndpoints(e.enum('gradient_scene', ea.SET, Object.keys(gradientScenes)), args.endpointNames),
...exposeEndpoints(
e
.list('gradient', ea.ALL, e.text('hex', ea.ALL).withDescription('Color in RGB HEX format (eg #663399)'))
.withLengthMin(1)
.withLengthMax(9)
.withDescription('List of RGB HEX colors'),
args.endpointNames,
),
);
result.configure.push(async (device, coordinatorEndpoint, definition) => {
for (const ep of device.endpoints.filter((ep) => ep.supportsInputCluster('manuSpecificPhilips2'))) {
Expand All @@ -85,7 +88,7 @@ export function philipsLight(args?: modernExtend.LightArgs & {hueEffect?: boolea
});
}
effects.push('finish_effect', 'stop_effect', 'stop_hue_effect');
result.exposes.push(e.enum('effect', ea.SET, effects));
result.exposes.push(...exposeEndpoints(e.enum('effect', ea.SET, effects), args.endpointNames));
}
return result;
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ export function postfixWithEndpointName(value: string, msg: Fz.Message, definiti
return value;
}

export function exposeEndpoints<T extends Expose>(expose: T, endpointNames?: string[]): T[] {
return endpointNames ? (endpointNames.map((ep) => expose.clone().withEndpoint(ep)) as T[]) : [expose];
}

export function enforceEndpoint(entity: Zh.Endpoint, key: string, meta: Tz.Meta) {
// @ts-expect-error ignore
const multiEndpointEnforce: {[s: string]: number} = getMetaValue(entity, meta.mapped, 'multiEndpointEnforce', 'allEqual', []);
Expand Down
11 changes: 10 additions & 1 deletion test/modernExtend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,16 @@ describe('ModernExtend', () => {
'osram_set_transition',
'osram_remember_state',
],
exposes: ['action', 'effect', 'light_l1(state,brightness)', 'light_l2(state,brightness)', 'light_s1(state,brightness)', 'linkquality'],
exposes: [
'action',
'effect',
'effect',
'effect',
'light_l1(state,brightness)',
'light_l2(state,brightness)',
'light_s1(state,brightness)',
'linkquality',
],
bind: {
10: ['genOnOff', 'genLevelCtrl'],
11: ['genOnOff', 'genLevelCtrl'],
Expand Down
Loading