Skip to content

Commit

Permalink
fix: Define endpoint in definition.toZigbee containing key 'state' (#…
Browse files Browse the repository at this point in the history
…8190)

* Define endpoint in definition.toZigbee containing key 'state'
To allow the selection of the proper converter with multi endpoints divice
at zigbee2mqtt-master\lib\extension\publish.ts at  line "const converter =
converters.find...c.endpoint == endpointName"
This change corrects issue Koenkk/zigbee2mqtt#24352

* Code format with Prettier

* Simplify Tz.Converter.endpoints type to string[] array only
  • Loading branch information
Zitrium31 authored Nov 1, 2024
1 parent 0012650 commit f54366e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/lib/modernExtend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export function onOff(args?: OnOffArgs): ModernExtend {
: 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];
const toZigbee: Tz.Converter[] = [{...tz.on_off, endpoints: args?.endpointNames}];

if (args.powerOnBehavior) {
exposes.push(...exposeEndpoints(e.power_on_behavior(['off', 'on', 'toggle', 'previous']), args.endpointNames));
Expand Down Expand Up @@ -1005,7 +1005,7 @@ export function light(args?: LightArgs): ModernExtend {

const fromZigbee: Fz.Converter[] = [fz.on_off, fz.brightness, fz.ignore_basic_report, fz.level_config];
const toZigbee: Tz.Converter[] = [
tz.light_onoff_brightness,
{...tz.light_onoff_brightness, endpoints: args?.endpointNames},
tz.ignore_transition,
tz.level_config,
tz.ignore_rate,
Expand Down Expand Up @@ -1287,12 +1287,19 @@ export function commandsColorCtrl(args?: CommandsColorCtrl): ModernExtend {

export interface LockArgs {
pinCodeCount: number;
endpointNames?: string[];
}
export function lock(args?: LockArgs): ModernExtend {
args = {...args};

const fromZigbee = [fz.lock, fz.lock_operation_event, fz.lock_programming_event, fz.lock_pin_code_response, fz.lock_user_status_response];
const toZigbee = [tz.lock, tz.pincode_lock, tz.lock_userstatus, tz.lock_auto_relock_time, tz.lock_sound_volume];
const toZigbee = [
{...tz.lock, endpoints: args?.endpointNames},
tz.pincode_lock,
tz.lock_userstatus,
tz.lock_auto_relock_time,
tz.lock_sound_volume,
];
const exposes = [
e.lock(),
e.pincode(),
Expand Down Expand Up @@ -1326,7 +1333,7 @@ export function windowCovering(args: WindowCoveringArgs): ModernExtend {
const exposes: Expose[] = [coverExpose];

const fromZigbee: Fz.Converter[] = [fz.cover_position_tilt];
const toZigbee: Tz.Converter[] = [tz.cover_state, tz.cover_position_tilt];
const toZigbee: Tz.Converter[] = [{...tz.cover_state, endpoints: args?.endpointNames}, tz.cover_position_tilt];

const result: ModernExtend = {exposes, fromZigbee, toZigbee, isModernExtend: true};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/tuya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ export function getHandlersForDP(
: [
{
key: [name],
endpoint: endpoint,
endpoints: [endpoint],
convertSet: async (entity, key, value, meta) => {
// A set converter is only called once; therefore we need to loop
const state: KeyValue = {};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export namespace Tz {
export interface Converter {
key?: string[];
options?: Option[] | ((definition: Definition) => Option[]);
endpoint?: string;
endpoints?: string[];

This comment has been minimized.

Copy link
@LaurentChardin

LaurentChardin Nov 2, 2024

Contributor

This changes is causing an error when we compile z2m in the publish module:

lib/extension/publish.ts:262:107 - error TS2551: Property 'endpoint' does not exist on type 'Converter'. Did you mean 'endpoints'?

262             const converter = converters.find((c) => (!c.key || c.key.includes(key)) && (!c.endpoint || c.endpoint == endpointName));
                                                                                                              ~~~~~~~~

  ../zigbee-herdsman-converters/lib/types.d.ts:333:9
    333         endpoints?: string[];
                ~~~~~~~~~
    'endpoints' is declared here.

@Zitrium31 i guess we are waiting your PR into z2m now, will have to manually patch z2m or zhc until it is done :(

Koenkk/zigbee2mqtt#24525

This comment has been minimized.

Copy link
@Zitrium31

Zitrium31 Nov 2, 2024

Author Contributor

Sorry for the inconvenience.
Fix of issue 24352 needs also PR #24525 (in addition to PR #8190.)
@Koenkk is aware of these dual PRs. I believe he will review the second one on z2m side soon.

convertSet?: (entity: Zh.Endpoint | Zh.Group, key: string, value: unknown, meta: Tz.Meta) => Promise<ConvertSetResult>;
convertGet?: (entity: Zh.Endpoint | Zh.Group, key: string, meta: Tz.Meta) => Promise<void>;
}
Expand Down

0 comments on commit f54366e

Please sign in to comment.