Skip to content

Commit

Permalink
fix: Allow exposes in ModernExtend interface to be a function Koe…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jun 25, 2024
1 parent d0bdb1a commit 059dfb8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,9 @@ function processExtensions(definition: Definition): Definition {
return !allExposes.find((e) => typeof e === 'function');
}
let allExposes: (Expose | DefinitionExposesFunction)[] = [];
const addToAllExposes = (expose: Expose[] | DefinitionExposesFunction) => {
typeof expose === 'function' ? allExposes.push(expose) : allExposes.push(...expose);
if (definitionExposes) {
typeof definitionExposes === 'function' ? allExposes.push(definitionExposes) : allExposes.push(...definitionExposes);
}

if (definitionExposes) addToAllExposes(definitionExposes);
toZigbee = [...toZigbee ?? []];
fromZigbee = [...fromZigbee ?? []];

Expand All @@ -131,7 +129,7 @@ function processExtensions(definition: Definition): Definition {
}
if (ext.toZigbee) toZigbee.push(...ext.toZigbee);
if (ext.fromZigbee) fromZigbee.push(...ext.fromZigbee);
if (ext.exposes) addToAllExposes(ext.exposes);
if (ext.exposes) allExposes.push(...ext.exposes);
if (ext.meta) meta = {...ext.meta, ...meta};
// Filter `undefined` configures, e.g. returned by setupConfigureForReporting.
if (ext.configure) configures.push(...ext.configure.filter((c) => c));
Expand Down
3 changes: 2 additions & 1 deletion src/lib/modernExtend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as globalLegacy from '../lib/legacy';
import {
Fz, Tz, ModernExtend, Range, Zh, DefinitionOta, OnEvent, Access,
KeyValueString, KeyValue, Configure, Expose, DefinitionMeta, KeyValueAny,
DefinitionExposesFunction,
} from './types';
import {zigbeeOTA} from '../lib/ota';
import * as globalStore from '../lib/store';
Expand Down Expand Up @@ -633,7 +634,7 @@ export function occupancy(args?: OccupancyArgs): ModernExtend {
args = {reporting: true, reportingConfig: {min: '10_SECONDS', max: '1_MINUTE', change: 0}, ...args};

const templateExposes: Expose[] = [e.occupancy().withAccess(ea.STATE_GET)];
const exposes: Expose[] = args.endpointNames ?
const exposes: (Expose | DefinitionExposesFunction)[] = args.endpointNames ?
templateExposes.map((exp) => args.endpointNames.map((ep) => exp.withEndpoint(ep))).flat() : templateExposes;

const fromZigbee: Fz.Converter[] = [{
Expand Down
12 changes: 8 additions & 4 deletions src/lib/tuya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import tz from '../converters/toZigbee';
import fz from '../converters/fromZigbee';
import * as utils from './utils';
import * as modernExtend from './modernExtend';
import {Tuya, OnEventType, OnEventData, Zh, KeyValue, Tz, Fz, Expose, OnEvent, ModernExtend, Range, KeyValueNumberString} from './types';
import {
Tuya, OnEventType, OnEventData, Zh, KeyValue, Tz, Fz, Expose, OnEvent, ModernExtend, Range, KeyValueNumberString, DefinitionExposesFunction,
} from './types';
import {logger} from './logger';
// import {Color} from './color';

Expand Down Expand Up @@ -1732,10 +1734,11 @@ const tuyaModernExtend = {
if (args.minBrightness === 'attribute') {
result.fromZigbee.push(tuyaFz.min_brightness_attribute);
result.toZigbee.push(tuyaTz.min_brightness_attribute);
result.exposes = result.exposes.map((e) => utils.isLightExpose(e) ? e.withMinBrightness() : e);
result.exposes = result.exposes.map((e) => typeof e !== 'function' && utils.isLightExpose(e) ? e.withMinBrightness() : e);
} else if (args.minBrightness === 'command') {
result.toZigbee.push(tuyaTz.min_brightness_command);
result.exposes = result.exposes.map((e) => utils.isLightExpose(e) ? e.withMinBrightness().setAccess('min_brightness', ea.STATE_SET) : e);
result.exposes = result.exposes.map((e) => typeof e !== 'function' && utils.isLightExpose(e) ?
e.withMinBrightness().setAccess('min_brightness', ea.STATE_SET) : e);
}

if (args.color) {
Expand All @@ -1750,7 +1753,8 @@ const tuyaModernExtend = {
indicatorMode?: boolean, backlightModeOffNormalInverted?: boolean, backlightModeOffOn?: boolean, electricalMeasurements?: boolean,
electricalMeasurementsFzConverter?: Fz.Converter, childLock?: boolean, switchMode?: boolean, onOffCountdown?: boolean,
}={}): ModernExtend => {
const exposes: Expose[] = args.endpoints ? args.endpoints.map((ee) => e.switch().withEndpoint(ee)) : [e.switch()];
const exposes: (Expose | DefinitionExposesFunction)[] =
args.endpoints ? args.endpoints.map((ee) => e.switch().withEndpoint(ee)) : [e.switch()];
const fromZigbee: Fz.Converter[] = [fz.on_off, fz.ignore_basic_report];
const toZigbee: Tz.Converter[] = [];
if (args.onOffCountdown) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export type OnEvent = (type: OnEventType, data: OnEventData, device: Zh.Device,
export interface ModernExtend {
fromZigbee?: Fz.Converter[],
toZigbee?: Tz.Converter[],
exposes?: Expose[],
exposes?: (Expose | DefinitionExposesFunction)[],
configure?: Configure[],
meta?: DefinitionMeta,
ota?: DefinitionOta,
Expand Down

0 comments on commit 059dfb8

Please sign in to comment.