zigbee-herdsman 6.0.2 → 6.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (252) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +9 -3
  3. package/.github/ISSUE_TEMPLATE/config.yml +0 -5
  4. package/.github/dependabot.yml +0 -22
  5. package/.github/workflows/ci.yml +0 -69
  6. package/.github/workflows/release-please.yml +0 -18
  7. package/.github/workflows/stale.yml +0 -20
  8. package/.github/workflows/typedoc.yaml +0 -47
  9. package/.release-please-manifest.json +0 -3
  10. package/.vscode/extensions.json +0 -3
  11. package/.vscode/settings.json +0 -11
  12. package/biome.json +0 -98
  13. package/examples/join-and-log.js +0 -24
  14. package/release-please-config.json +0 -9
  15. package/src/adapter/adapter.ts +0 -189
  16. package/src/adapter/adapterDiscovery.ts +0 -666
  17. package/src/adapter/const.ts +0 -12
  18. package/src/adapter/deconz/adapter/deconzAdapter.ts +0 -877
  19. package/src/adapter/deconz/driver/constants.ts +0 -246
  20. package/src/adapter/deconz/driver/driver.ts +0 -1540
  21. package/src/adapter/deconz/driver/frame.ts +0 -11
  22. package/src/adapter/deconz/driver/frameParser.ts +0 -753
  23. package/src/adapter/deconz/driver/parser.ts +0 -45
  24. package/src/adapter/deconz/driver/writer.ts +0 -22
  25. package/src/adapter/deconz/types.d.ts +0 -13
  26. package/src/adapter/ember/adapter/emberAdapter.ts +0 -2265
  27. package/src/adapter/ember/adapter/endpoints.ts +0 -86
  28. package/src/adapter/ember/adapter/oneWaitress.ts +0 -324
  29. package/src/adapter/ember/adapter/tokensManager.ts +0 -782
  30. package/src/adapter/ember/consts.ts +0 -178
  31. package/src/adapter/ember/enums.ts +0 -1746
  32. package/src/adapter/ember/ezsp/buffalo.ts +0 -1392
  33. package/src/adapter/ember/ezsp/consts.ts +0 -148
  34. package/src/adapter/ember/ezsp/enums.ts +0 -1114
  35. package/src/adapter/ember/ezsp/ezsp.ts +0 -9061
  36. package/src/adapter/ember/ezspError.ts +0 -10
  37. package/src/adapter/ember/types.ts +0 -866
  38. package/src/adapter/ember/uart/ash.ts +0 -1960
  39. package/src/adapter/ember/uart/consts.ts +0 -109
  40. package/src/adapter/ember/uart/enums.ts +0 -192
  41. package/src/adapter/ember/uart/parser.ts +0 -48
  42. package/src/adapter/ember/uart/queues.ts +0 -247
  43. package/src/adapter/ember/uart/writer.ts +0 -53
  44. package/src/adapter/ember/utils/initters.ts +0 -58
  45. package/src/adapter/ember/utils/math.ts +0 -73
  46. package/src/adapter/events.ts +0 -21
  47. package/src/adapter/ezsp/adapter/backup.ts +0 -109
  48. package/src/adapter/ezsp/adapter/ezspAdapter.ts +0 -614
  49. package/src/adapter/ezsp/driver/commands.ts +0 -2497
  50. package/src/adapter/ezsp/driver/consts.ts +0 -11
  51. package/src/adapter/ezsp/driver/driver.ts +0 -1002
  52. package/src/adapter/ezsp/driver/ezsp.ts +0 -802
  53. package/src/adapter/ezsp/driver/frame.ts +0 -101
  54. package/src/adapter/ezsp/driver/index.ts +0 -4
  55. package/src/adapter/ezsp/driver/multicast.ts +0 -78
  56. package/src/adapter/ezsp/driver/parser.ts +0 -81
  57. package/src/adapter/ezsp/driver/types/basic.ts +0 -201
  58. package/src/adapter/ezsp/driver/types/index.ts +0 -239
  59. package/src/adapter/ezsp/driver/types/named.ts +0 -2330
  60. package/src/adapter/ezsp/driver/types/struct.ts +0 -844
  61. package/src/adapter/ezsp/driver/uart.ts +0 -460
  62. package/src/adapter/ezsp/driver/utils/crc16ccitt.ts +0 -44
  63. package/src/adapter/ezsp/driver/utils/index.ts +0 -32
  64. package/src/adapter/ezsp/driver/writer.ts +0 -64
  65. package/src/adapter/index.ts +0 -3
  66. package/src/adapter/serialPort.ts +0 -58
  67. package/src/adapter/socketPortUtils.ts +0 -16
  68. package/src/adapter/tstype.ts +0 -78
  69. package/src/adapter/z-stack/adapter/adapter-backup.ts +0 -519
  70. package/src/adapter/z-stack/adapter/adapter-nv-memory.ts +0 -457
  71. package/src/adapter/z-stack/adapter/endpoints.ts +0 -57
  72. package/src/adapter/z-stack/adapter/manager.ts +0 -543
  73. package/src/adapter/z-stack/adapter/tstype.ts +0 -6
  74. package/src/adapter/z-stack/adapter/zStackAdapter.ts +0 -1190
  75. package/src/adapter/z-stack/constants/af.ts +0 -27
  76. package/src/adapter/z-stack/constants/common.ts +0 -285
  77. package/src/adapter/z-stack/constants/dbg.ts +0 -23
  78. package/src/adapter/z-stack/constants/index.ts +0 -11
  79. package/src/adapter/z-stack/constants/mac.ts +0 -128
  80. package/src/adapter/z-stack/constants/sapi.ts +0 -25
  81. package/src/adapter/z-stack/constants/sys.ts +0 -72
  82. package/src/adapter/z-stack/constants/util.ts +0 -82
  83. package/src/adapter/z-stack/constants/utils.ts +0 -14
  84. package/src/adapter/z-stack/constants/zdo.ts +0 -103
  85. package/src/adapter/z-stack/models/startup-options.ts +0 -13
  86. package/src/adapter/z-stack/structs/entries/address-manager-entry.ts +0 -44
  87. package/src/adapter/z-stack/structs/entries/address-manager-table.ts +0 -19
  88. package/src/adapter/z-stack/structs/entries/aps-link-key-data-entry.ts +0 -12
  89. package/src/adapter/z-stack/structs/entries/aps-link-key-data-table.ts +0 -21
  90. package/src/adapter/z-stack/structs/entries/aps-tc-link-key-entry.ts +0 -19
  91. package/src/adapter/z-stack/structs/entries/aps-tc-link-key-table.ts +0 -21
  92. package/src/adapter/z-stack/structs/entries/channel-list.ts +0 -8
  93. package/src/adapter/z-stack/structs/entries/has-configured.ts +0 -16
  94. package/src/adapter/z-stack/structs/entries/index.ts +0 -16
  95. package/src/adapter/z-stack/structs/entries/nib.ts +0 -66
  96. package/src/adapter/z-stack/structs/entries/nwk-key-descriptor.ts +0 -15
  97. package/src/adapter/z-stack/structs/entries/nwk-key.ts +0 -13
  98. package/src/adapter/z-stack/structs/entries/nwk-pan-id.ts +0 -8
  99. package/src/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-entry.ts +0 -20
  100. package/src/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-table.ts +0 -19
  101. package/src/adapter/z-stack/structs/entries/security-manager-entry.ts +0 -33
  102. package/src/adapter/z-stack/structs/entries/security-manager-table.ts +0 -22
  103. package/src/adapter/z-stack/structs/index.ts +0 -4
  104. package/src/adapter/z-stack/structs/serializable-memory-object.ts +0 -14
  105. package/src/adapter/z-stack/structs/struct.ts +0 -367
  106. package/src/adapter/z-stack/structs/table.ts +0 -198
  107. package/src/adapter/z-stack/unpi/constants.ts +0 -33
  108. package/src/adapter/z-stack/unpi/frame.ts +0 -62
  109. package/src/adapter/z-stack/unpi/index.ts +0 -4
  110. package/src/adapter/z-stack/unpi/parser.ts +0 -56
  111. package/src/adapter/z-stack/unpi/writer.ts +0 -21
  112. package/src/adapter/z-stack/utils/channel-list.ts +0 -40
  113. package/src/adapter/z-stack/utils/index.ts +0 -2
  114. package/src/adapter/z-stack/utils/network-options.ts +0 -26
  115. package/src/adapter/z-stack/znp/buffaloZnp.ts +0 -175
  116. package/src/adapter/z-stack/znp/definition.ts +0 -2713
  117. package/src/adapter/z-stack/znp/index.ts +0 -2
  118. package/src/adapter/z-stack/znp/parameterType.ts +0 -22
  119. package/src/adapter/z-stack/znp/tstype.ts +0 -44
  120. package/src/adapter/z-stack/znp/utils.ts +0 -10
  121. package/src/adapter/z-stack/znp/znp.ts +0 -342
  122. package/src/adapter/z-stack/znp/zpiObject.ts +0 -148
  123. package/src/adapter/zboss/adapter/zbossAdapter.ts +0 -526
  124. package/src/adapter/zboss/commands.ts +0 -1184
  125. package/src/adapter/zboss/consts.ts +0 -9
  126. package/src/adapter/zboss/driver.ts +0 -422
  127. package/src/adapter/zboss/enums.ts +0 -360
  128. package/src/adapter/zboss/frame.ts +0 -227
  129. package/src/adapter/zboss/reader.ts +0 -65
  130. package/src/adapter/zboss/types.ts +0 -0
  131. package/src/adapter/zboss/uart.ts +0 -428
  132. package/src/adapter/zboss/utils.ts +0 -58
  133. package/src/adapter/zboss/writer.ts +0 -49
  134. package/src/adapter/zigate/adapter/patchZdoBuffaloBE.ts +0 -27
  135. package/src/adapter/zigate/adapter/zigateAdapter.ts +0 -618
  136. package/src/adapter/zigate/driver/LICENSE +0 -17
  137. package/src/adapter/zigate/driver/buffaloZiGate.ts +0 -212
  138. package/src/adapter/zigate/driver/commandType.ts +0 -418
  139. package/src/adapter/zigate/driver/constants.ts +0 -150
  140. package/src/adapter/zigate/driver/frame.ts +0 -197
  141. package/src/adapter/zigate/driver/messageType.ts +0 -287
  142. package/src/adapter/zigate/driver/parameterType.ts +0 -32
  143. package/src/adapter/zigate/driver/ziGateObject.ts +0 -146
  144. package/src/adapter/zigate/driver/zigate.ts +0 -423
  145. package/src/adapter/zoh/adapter/utils.ts +0 -27
  146. package/src/adapter/zoh/adapter/zohAdapter.ts +0 -838
  147. package/src/buffalo/buffalo.ts +0 -342
  148. package/src/buffalo/index.ts +0 -1
  149. package/src/controller/controller.ts +0 -1022
  150. package/src/controller/database.ts +0 -124
  151. package/src/controller/events.ts +0 -52
  152. package/src/controller/greenPower.ts +0 -603
  153. package/src/controller/helpers/index.ts +0 -1
  154. package/src/controller/helpers/installCodes.ts +0 -107
  155. package/src/controller/helpers/request.ts +0 -96
  156. package/src/controller/helpers/requestQueue.ts +0 -125
  157. package/src/controller/helpers/zclFrameConverter.ts +0 -47
  158. package/src/controller/helpers/zclTransactionSequenceNumber.ts +0 -19
  159. package/src/controller/index.ts +0 -6
  160. package/src/controller/model/device.ts +0 -1249
  161. package/src/controller/model/endpoint.ts +0 -1105
  162. package/src/controller/model/entity.ts +0 -23
  163. package/src/controller/model/group.ts +0 -424
  164. package/src/controller/model/index.ts +0 -5
  165. package/src/controller/model/zigbeeEntity.ts +0 -30
  166. package/src/controller/touchlink.ts +0 -189
  167. package/src/controller/tstype.ts +0 -274
  168. package/src/index.ts +0 -12
  169. package/src/models/backup-storage-legacy.ts +0 -48
  170. package/src/models/backup-storage-unified.ts +0 -47
  171. package/src/models/backup.ts +0 -37
  172. package/src/models/index.ts +0 -5
  173. package/src/models/network-options.ts +0 -11
  174. package/src/utils/backup.ts +0 -152
  175. package/src/utils/index.ts +0 -5
  176. package/src/utils/logger.ts +0 -20
  177. package/src/utils/patchBigIntSerialization.ts +0 -8
  178. package/src/utils/queue.ts +0 -76
  179. package/src/utils/types.d.ts +0 -3
  180. package/src/utils/utils.ts +0 -19
  181. package/src/utils/wait.ts +0 -5
  182. package/src/utils/waitress.ts +0 -96
  183. package/src/zspec/consts.ts +0 -84
  184. package/src/zspec/enums.ts +0 -22
  185. package/src/zspec/index.ts +0 -3
  186. package/src/zspec/tstypes.ts +0 -18
  187. package/src/zspec/utils.ts +0 -247
  188. package/src/zspec/zcl/buffaloZcl.ts +0 -1220
  189. package/src/zspec/zcl/definition/cluster.ts +0 -5915
  190. package/src/zspec/zcl/definition/clusters-typegen.ts +0 -588
  191. package/src/zspec/zcl/definition/clusters-types.ts +0 -7331
  192. package/src/zspec/zcl/definition/consts.ts +0 -24
  193. package/src/zspec/zcl/definition/enums.ts +0 -203
  194. package/src/zspec/zcl/definition/foundation.ts +0 -329
  195. package/src/zspec/zcl/definition/manufacturerCode.ts +0 -729
  196. package/src/zspec/zcl/definition/status.ts +0 -69
  197. package/src/zspec/zcl/definition/tstype.ts +0 -377
  198. package/src/zspec/zcl/index.ts +0 -11
  199. package/src/zspec/zcl/utils.ts +0 -321
  200. package/src/zspec/zcl/zclFrame.ts +0 -356
  201. package/src/zspec/zcl/zclHeader.ts +0 -102
  202. package/src/zspec/zcl/zclStatusError.ts +0 -10
  203. package/src/zspec/zdo/buffaloZdo.ts +0 -2336
  204. package/src/zspec/zdo/definition/clusters.ts +0 -722
  205. package/src/zspec/zdo/definition/consts.ts +0 -16
  206. package/src/zspec/zdo/definition/enums.ts +0 -99
  207. package/src/zspec/zdo/definition/status.ts +0 -105
  208. package/src/zspec/zdo/definition/tstypes.ts +0 -1062
  209. package/src/zspec/zdo/index.ts +0 -7
  210. package/src/zspec/zdo/utils.ts +0 -76
  211. package/src/zspec/zdo/zdoStatusError.ts +0 -10
  212. package/test/adapter/adapter.test.ts +0 -1062
  213. package/test/adapter/ember/ash.test.ts +0 -337
  214. package/test/adapter/ember/consts.ts +0 -131
  215. package/test/adapter/ember/emberAdapter.test.ts +0 -3449
  216. package/test/adapter/ember/ezsp.test.ts +0 -385
  217. package/test/adapter/ember/ezspBuffalo.test.ts +0 -93
  218. package/test/adapter/ember/ezspError.test.ts +0 -12
  219. package/test/adapter/ember/math.test.ts +0 -206
  220. package/test/adapter/ezsp/frame.test.ts +0 -30
  221. package/test/adapter/ezsp/uart.test.ts +0 -181
  222. package/test/adapter/z-stack/adapter.test.ts +0 -3984
  223. package/test/adapter/z-stack/constants.test.ts +0 -33
  224. package/test/adapter/z-stack/structs.test.ts +0 -115
  225. package/test/adapter/z-stack/unpi.test.ts +0 -213
  226. package/test/adapter/z-stack/znp.test.ts +0 -1284
  227. package/test/adapter/zboss/fixZdoResponse.test.ts +0 -179
  228. package/test/adapter/zigate/patchZdoBuffaloBE.test.ts +0 -81
  229. package/test/adapter/zigate/zdo.test.ts +0 -187
  230. package/test/adapter/zoh/utils.test.ts +0 -36
  231. package/test/adapter/zoh/zohAdapter.test.ts +0 -1307
  232. package/test/benchOptions.ts +0 -14
  233. package/test/buffalo.test.ts +0 -431
  234. package/test/controller.bench.ts +0 -214
  235. package/test/controller.test.ts +0 -8702
  236. package/test/greenpower.test.ts +0 -1408
  237. package/test/mockAdapters.ts +0 -65
  238. package/test/mockDevices.ts +0 -598
  239. package/test/requests.bench.ts +0 -229
  240. package/test/testUtils.ts +0 -20
  241. package/test/tsconfig.json +0 -9
  242. package/test/utils/math.ts +0 -19
  243. package/test/utils.test.ts +0 -279
  244. package/test/vitest.config.mts +0 -26
  245. package/test/zcl.test.ts +0 -2831
  246. package/test/zspec/utils.test.ts +0 -68
  247. package/test/zspec/zcl/buffalo.test.ts +0 -1374
  248. package/test/zspec/zcl/frame.test.ts +0 -960
  249. package/test/zspec/zcl/utils.test.ts +0 -273
  250. package/test/zspec/zdo/buffalo.test.ts +0 -1850
  251. package/test/zspec/zdo/utils.test.ts +0 -241
  252. package/tsconfig.json +0 -24
@@ -1,1105 +0,0 @@
1
- import assert from "node:assert";
2
- import type {Events as AdapterEvents} from "../../adapter";
3
- import {logger} from "../../utils/logger";
4
- import * as ZSpec from "../../zspec";
5
- import {BroadcastAddress} from "../../zspec/enums";
6
- import type {Eui64} from "../../zspec/tstypes";
7
- import * as Zcl from "../../zspec/zcl";
8
- import type {TFoundation} from "../../zspec/zcl/definition/clusters-types";
9
- import type * as ZclTypes from "../../zspec/zcl/definition/tstype";
10
- import * as Zdo from "../../zspec/zdo";
11
- import Request from "../helpers/request";
12
- import RequestQueue from "../helpers/requestQueue";
13
- import * as ZclFrameConverter from "../helpers/zclFrameConverter";
14
- import zclTransactionSequenceNumber from "../helpers/zclTransactionSequenceNumber";
15
- import type {
16
- ClusterOrRawAttributeKeys,
17
- ClusterOrRawAttributes,
18
- ClusterOrRawPayload,
19
- ClusterOrRawWriteAttributes,
20
- FoundationOrRawPayload,
21
- KeyValue,
22
- PartialClusterOrRawWriteAttributes,
23
- SendPolicy,
24
- TCustomCluster,
25
- } from "../tstype";
26
- import Device from "./device";
27
- import Entity from "./entity";
28
- import Group from "./group";
29
- import {ZigbeeEntity} from "./zigbeeEntity";
30
-
31
- const NS = "zh:controller:endpoint";
32
-
33
- export interface ConfigureReportingItem<Cl extends string | number, Custom extends TCustomCluster | undefined = undefined> {
34
- attribute: ClusterOrRawAttributeKeys<Cl, Custom>[number] | {ID: number; type: number};
35
- minimumReportInterval: number;
36
- maximumReportInterval: number;
37
- reportableChange: number;
38
- }
39
-
40
- interface Options {
41
- manufacturerCode?: number;
42
- disableDefaultResponse?: boolean;
43
- disableResponse?: boolean;
44
- timeout?: number;
45
- direction?: Zcl.Direction;
46
- srcEndpoint?: number;
47
- reservedBits?: number;
48
- transactionSequenceNumber?: number;
49
- disableRecovery?: boolean;
50
- writeUndiv?: boolean;
51
- sendPolicy?: SendPolicy;
52
- }
53
-
54
- interface OptionsWithDefaults extends Options {
55
- disableDefaultResponse: boolean;
56
- disableResponse: boolean;
57
- timeout: number;
58
- direction: Zcl.Direction;
59
- reservedBits: number;
60
- disableRecovery: boolean;
61
- writeUndiv: boolean;
62
- }
63
-
64
- interface Clusters {
65
- [cluster: string]: {
66
- attributes: {[attribute: string]: number | string};
67
- };
68
- }
69
-
70
- interface BindInternal {
71
- cluster: number;
72
- type: "endpoint" | "group";
73
- deviceIeeeAddress?: string;
74
- endpointID?: number;
75
- groupID?: number;
76
- }
77
-
78
- interface Bind {
79
- cluster: ZclTypes.Cluster;
80
- target: Endpoint | Group;
81
- }
82
-
83
- interface ConfiguredReportingInternal {
84
- cluster: number;
85
- attrId: number;
86
- minRepIntval: number;
87
- maxRepIntval: number;
88
- repChange: number;
89
- manufacturerCode?: number | undefined;
90
- }
91
-
92
- interface ConfiguredReporting {
93
- cluster: ZclTypes.Cluster;
94
- attribute: ZclTypes.Attribute;
95
- minimumReportInterval: number;
96
- maximumReportInterval: number;
97
- reportableChange: number;
98
- }
99
-
100
- export class Endpoint extends ZigbeeEntity {
101
- public deviceID?: number;
102
- public inputClusters: number[];
103
- public outputClusters: number[];
104
- public profileID?: number;
105
- // biome-ignore lint/style/useNamingConvention: cross-repo impact
106
- public readonly ID: number;
107
- public readonly clusters: Clusters;
108
- public deviceIeeeAddress: string;
109
- public deviceNetworkAddress: number;
110
- private _binds: BindInternal[];
111
- private _configuredReportings: ConfiguredReportingInternal[];
112
- public meta: KeyValue;
113
- private pendingRequests: RequestQueue;
114
-
115
- // Getters/setters
116
- get binds(): Bind[] {
117
- const binds: Bind[] = [];
118
-
119
- for (const bind of this._binds) {
120
- // XXX: properties assumed valid when associated to `type`
121
- const target: Group | Endpoint | undefined =
122
- // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
123
- bind.type === "endpoint" ? Device.byIeeeAddr(bind.deviceIeeeAddress!)?.getEndpoint(bind.endpointID!) : Group.byGroupID(bind.groupID!);
124
-
125
- if (target) {
126
- binds.push({target, cluster: this.getCluster(bind.cluster)});
127
- }
128
- }
129
-
130
- return binds;
131
- }
132
-
133
- get configuredReportings(): ConfiguredReporting[] {
134
- const device = this.getDevice();
135
-
136
- return this._configuredReportings.map((entry, index) => {
137
- const cluster = Zcl.Utils.getCluster(entry.cluster, entry.manufacturerCode, device.customClusters);
138
- const attribute: ZclTypes.Attribute = cluster.getAttribute(entry.attrId) ?? {
139
- ID: entry.attrId,
140
- name: `attr${index}`,
141
- type: Zcl.DataType.UNKNOWN,
142
- manufacturerCode: undefined,
143
- };
144
-
145
- return {
146
- cluster,
147
- attribute,
148
- minimumReportInterval: entry.minRepIntval,
149
- maximumReportInterval: entry.maxRepIntval,
150
- reportableChange: entry.repChange,
151
- };
152
- });
153
- }
154
-
155
- private constructor(
156
- id: number,
157
- profileID: number | undefined,
158
- deviceID: number | undefined,
159
- inputClusters: number[],
160
- outputClusters: number[],
161
- deviceNetworkAddress: number,
162
- deviceIeeeAddress: string,
163
- clusters: Clusters,
164
- binds: BindInternal[],
165
- configuredReportings: ConfiguredReportingInternal[],
166
- meta: KeyValue,
167
- ) {
168
- super();
169
- this.ID = id;
170
- this.profileID = profileID;
171
- this.deviceID = deviceID;
172
- this.inputClusters = inputClusters;
173
- this.outputClusters = outputClusters;
174
- this.deviceNetworkAddress = deviceNetworkAddress;
175
- this.deviceIeeeAddress = deviceIeeeAddress;
176
- this.clusters = clusters;
177
- this._binds = binds;
178
- this._configuredReportings = configuredReportings;
179
- this.meta = meta;
180
- this.pendingRequests = new RequestQueue(this);
181
- }
182
-
183
- /**
184
- * Get device of this endpoint
185
- */
186
- public getDevice(): Device {
187
- const device = Device.byIeeeAddr(this.deviceIeeeAddress);
188
-
189
- if (!device) {
190
- logger.error(`Tried to get unknown/deleted device ${this.deviceIeeeAddress} from endpoint ${this.ID}.`, NS);
191
- // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
192
- logger.debug(new Error().stack!, NS);
193
- }
194
-
195
- // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
196
- return device!;
197
- }
198
-
199
- /**
200
- * @param {number|string} clusterKey
201
- * @returns {boolean}
202
- */
203
- public supportsInputCluster(clusterKey: number | string): boolean {
204
- const cluster = this.getCluster(clusterKey);
205
- return this.inputClusters.includes(cluster.ID);
206
- }
207
-
208
- /**
209
- * @param {number|string} clusterKey
210
- * @returns {boolean}
211
- */
212
- public supportsOutputCluster(clusterKey: number | string): boolean {
213
- const cluster = this.getCluster(clusterKey);
214
- return this.outputClusters.includes(cluster.ID);
215
- }
216
-
217
- /**
218
- * @returns {ZclTypes.Cluster[]}
219
- */
220
- public getInputClusters(): ZclTypes.Cluster[] {
221
- return this.clusterNumbersToClusters(this.inputClusters);
222
- }
223
-
224
- /**
225
- * @returns {ZclTypes.Cluster[]}
226
- */
227
- public getOutputClusters(): ZclTypes.Cluster[] {
228
- return this.clusterNumbersToClusters(this.outputClusters);
229
- }
230
-
231
- private clusterNumbersToClusters(clusterNumbers: number[]): ZclTypes.Cluster[] {
232
- return clusterNumbers.map((c) => this.getCluster(c));
233
- }
234
-
235
- /*
236
- * CRUD
237
- */
238
-
239
- public static fromDatabaseRecord(record: KeyValue, deviceNetworkAddress: number, deviceIeeeAddress: string): Endpoint {
240
- // Migrate attrs to attributes
241
- for (const entryKey in record.clusters) {
242
- const entry = record.clusters[entryKey];
243
-
244
- if (entry.attrs != null) {
245
- entry.attributes = entry.attrs;
246
- delete entry.attrs;
247
- }
248
- }
249
-
250
- return new Endpoint(
251
- record.epId,
252
- record.profId,
253
- record.devId,
254
- record.inClusterList,
255
- record.outClusterList,
256
- deviceNetworkAddress,
257
- deviceIeeeAddress,
258
- record.clusters,
259
- record.binds || [],
260
- record.configuredReportings || [],
261
- record.meta || {},
262
- );
263
- }
264
-
265
- public toDatabaseRecord(): KeyValue {
266
- return {
267
- profId: this.profileID,
268
- epId: this.ID,
269
- devId: this.deviceID,
270
- inClusterList: this.inputClusters,
271
- outClusterList: this.outputClusters,
272
- clusters: this.clusters,
273
- binds: this._binds,
274
- configuredReportings: this._configuredReportings,
275
- meta: this.meta,
276
- };
277
- }
278
-
279
- public static create(
280
- id: number,
281
- profileID: number | undefined,
282
- deviceID: number | undefined,
283
- inputClusters: number[],
284
- outputClusters: number[],
285
- deviceNetworkAddress: number,
286
- deviceIeeeAddress: string,
287
- ): Endpoint {
288
- return new Endpoint(id, profileID, deviceID, inputClusters, outputClusters, deviceNetworkAddress, deviceIeeeAddress, {}, [], [], {});
289
- }
290
-
291
- public saveClusterAttributeKeyValue(clusterKey: number | string, list: KeyValue): void {
292
- const cluster = this.getCluster(clusterKey);
293
-
294
- if (!this.clusters[cluster.name]) {
295
- this.clusters[cluster.name] = {attributes: {}};
296
- }
297
-
298
- for (const attribute in list) {
299
- this.clusters[cluster.name].attributes[attribute] = list[attribute];
300
- }
301
- }
302
-
303
- public getClusterAttributeValue(clusterKey: number | string, attributeKey: number | string): number | string | undefined {
304
- const cluster = this.getCluster(clusterKey);
305
-
306
- if (this.clusters[cluster.name] && this.clusters[cluster.name].attributes) {
307
- // XXX: used to throw (behavior changed in #1455)
308
- const attribute = cluster.getAttribute(attributeKey);
309
-
310
- if (attribute) {
311
- return this.clusters[cluster.name].attributes[attribute.name];
312
- }
313
- }
314
-
315
- return undefined;
316
- }
317
-
318
- public hasPendingRequests(): boolean {
319
- return this.pendingRequests.size > 0;
320
- }
321
-
322
- public async sendPendingRequests(fastPolling: boolean): Promise<void> {
323
- return await this.pendingRequests.send(fastPolling);
324
- }
325
-
326
- private async sendRequest(frame: Zcl.Frame, options: OptionsWithDefaults): Promise<AdapterEvents.ZclPayload>;
327
- private async sendRequest<Type>(frame: Zcl.Frame, options: OptionsWithDefaults, func: () => Promise<Type>): Promise<Type>;
328
- private async sendRequest<Type>(
329
- frame: Zcl.Frame,
330
- options: OptionsWithDefaults,
331
- func: () => Promise<Type> = (): Promise<Type> => {
332
- return Entity.adapter.sendZclFrameToEndpoint(
333
- this.deviceIeeeAddress,
334
- this.deviceNetworkAddress,
335
- this.ID,
336
- frame,
337
- options.timeout,
338
- options.disableResponse,
339
- options.disableRecovery,
340
- options.srcEndpoint,
341
- ) as Promise<Type>;
342
- },
343
- ): Promise<Type> {
344
- const logPrefix = `Request Queue (${this.deviceIeeeAddress}/${this.ID}): `;
345
- const device = this.getDevice();
346
- const request = new Request(func, frame, device.pendingRequestTimeout, options.sendPolicy);
347
-
348
- if (request.sendPolicy !== "bulk") {
349
- // Check if such a request is already in the queue and remove the old one(s) if necessary
350
- this.pendingRequests.filter(request);
351
- }
352
-
353
- // send without queueing if sendPolicy is 'immediate' or if the device has no timeout set
354
- if (request.sendPolicy === "immediate" || !device.pendingRequestTimeout) {
355
- if (device.pendingRequestTimeout > 0) {
356
- logger.debug(`${logPrefix}send ${frame.command.name} request immediately (sendPolicy=${options.sendPolicy})`, NS);
357
- }
358
- return await request.send();
359
- }
360
- // If this is a bulk message, we queue directly.
361
- if (request.sendPolicy === "bulk") {
362
- logger.debug(`${logPrefix}queue request (${this.pendingRequests.size})`, NS);
363
- return await this.pendingRequests.queue(request);
364
- }
365
-
366
- try {
367
- logger.debug(`${logPrefix}send request`, NS);
368
- return await request.send();
369
- } catch (error) {
370
- // If we got a failed transaction, the device is likely sleeping.
371
- // Queue for transmission later.
372
- logger.debug(`${logPrefix}queue request (transaction failed) (${error})`, NS);
373
- return await this.pendingRequests.queue(request);
374
- }
375
- }
376
-
377
- /*
378
- * Zigbee functions
379
- */
380
- private checkStatus(payload: [{status: Zcl.Status}] | {cmdId: number; statusCode: number}): void {
381
- const codes = Array.isArray(payload) ? payload.map((i) => i.status) : [payload.statusCode];
382
- const invalid = codes.find((c) => c !== Zcl.Status.SUCCESS);
383
- if (invalid) throw new Zcl.StatusError(invalid);
384
- }
385
-
386
- public async report<Cl extends number | string, Custom extends TCustomCluster | undefined = undefined>(
387
- clusterKey: Cl,
388
- attributes: PartialClusterOrRawWriteAttributes<Cl, Custom>,
389
- options?: Options,
390
- ): Promise<void> {
391
- const cluster = this.getCluster(clusterKey, undefined, options?.manufacturerCode);
392
- const payload: TFoundation["report"] = [];
393
-
394
- for (const nameOrID in attributes) {
395
- const attribute = cluster.getAttribute(nameOrID);
396
-
397
- if (attribute) {
398
- payload.push({attrId: attribute.ID, attrData: attributes[nameOrID], dataType: attribute.type});
399
- } else if (!Number.isNaN(Number(nameOrID))) {
400
- const value = attributes[nameOrID];
401
-
402
- payload.push({attrId: Number(nameOrID), attrData: value.value, dataType: value.type});
403
- } else {
404
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
405
- }
406
- }
407
-
408
- await this.zclCommand(cluster, "report", payload, options, attributes);
409
- }
410
-
411
- public async write<Cl extends number | string, Custom extends TCustomCluster | undefined = undefined>(
412
- clusterKey: Cl,
413
- attributes: PartialClusterOrRawWriteAttributes<Cl, Custom>,
414
- options?: Options,
415
- ): Promise<void> {
416
- const cluster = this.getCluster(clusterKey, undefined, options?.manufacturerCode);
417
- const optionsWithDefaults = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
418
- optionsWithDefaults.manufacturerCode = this.ensureManufacturerCodeIsUniqueAndGet<Cl, Custom>(
419
- cluster,
420
- Object.keys(attributes),
421
- optionsWithDefaults.manufacturerCode,
422
- "write",
423
- );
424
- const payload: TFoundation["write"] = [];
425
-
426
- for (const nameOrID in attributes) {
427
- const attribute = cluster.getAttribute(nameOrID);
428
-
429
- if (attribute) {
430
- payload.push({attrId: attribute.ID, attrData: attributes[nameOrID], dataType: attribute.type});
431
- } else if (!Number.isNaN(Number(nameOrID))) {
432
- const value = attributes[nameOrID];
433
-
434
- payload.push({attrId: Number(nameOrID), attrData: value.value, dataType: value.type});
435
- } else {
436
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
437
- }
438
- }
439
-
440
- await this.zclCommand(cluster, optionsWithDefaults.writeUndiv ? "writeUndiv" : "write", payload, optionsWithDefaults, attributes, true);
441
- }
442
-
443
- public async writeResponse<Cl extends number | string, Custom extends TCustomCluster | undefined = undefined>(
444
- clusterKey: Cl,
445
- transactionSequenceNumber: number,
446
- attributes: Partial<Record<ClusterOrRawAttributeKeys<Cl, Custom>[number], TFoundation["writeRsp"][number]>> &
447
- Record<number, TFoundation["writeRsp"][number]>,
448
- options?: Options,
449
- ): Promise<void> {
450
- assert(options?.transactionSequenceNumber === undefined, "Use parameter");
451
- const cluster = this.getCluster(clusterKey, undefined, options?.manufacturerCode);
452
- const payload: TFoundation["writeRsp"] = [];
453
-
454
- for (const nameOrID in attributes) {
455
- // biome-ignore lint/style/noNonNullAssertion: from loop
456
- const value = attributes[nameOrID]!;
457
-
458
- if (value.status !== undefined) {
459
- const attribute = cluster.getAttribute(nameOrID);
460
-
461
- if (attribute) {
462
- payload.push({attrId: attribute.ID, status: value.status});
463
- } else if (!Number.isNaN(Number(nameOrID))) {
464
- payload.push({attrId: Number(nameOrID), status: value.status});
465
- } else {
466
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
467
- }
468
- } else {
469
- throw new Error(`Missing attribute 'status'`);
470
- }
471
- }
472
-
473
- await this.zclCommand(
474
- cluster,
475
- "writeRsp",
476
- payload,
477
- {direction: Zcl.Direction.SERVER_TO_CLIENT, ...options, transactionSequenceNumber},
478
- attributes,
479
- );
480
- }
481
-
482
- // XXX: ideally, the return type should limit to the contents of the `attributes` param
483
- public async read<Cl extends number | string, Custom extends TCustomCluster | undefined = undefined>(
484
- clusterKey: Cl,
485
- attributes: ClusterOrRawAttributeKeys<Cl, Custom>,
486
- options?: Options,
487
- ): Promise<ClusterOrRawAttributes<Cl, Custom>> {
488
- const device = this.getDevice();
489
- const cluster = this.getCluster(clusterKey, device, options?.manufacturerCode);
490
- const optionsWithDefaults = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
491
- optionsWithDefaults.manufacturerCode = this.ensureManufacturerCodeIsUniqueAndGet<Cl, Custom>(
492
- cluster,
493
- attributes,
494
- optionsWithDefaults.manufacturerCode,
495
- "read",
496
- );
497
- const payload: TFoundation["read"] = [];
498
-
499
- for (const attribute of attributes) {
500
- if (typeof attribute === "number") {
501
- payload.push({attrId: attribute});
502
- } else {
503
- const attr = cluster.getAttribute(attribute);
504
-
505
- if (attr) {
506
- payload.push({attrId: attr.ID});
507
- } else {
508
- logger.warning(`Ignoring unknown attribute ${attribute} in cluster ${cluster.name}`, NS);
509
- }
510
- }
511
- }
512
-
513
- const resultFrame = await this.zclCommand(cluster, "read", payload, optionsWithDefaults, attributes, true);
514
-
515
- return resultFrame
516
- ? ZclFrameConverter.attributeKeyValue<Cl, Custom>(resultFrame, device.manufacturerID, device.customClusters)
517
- : ({} as ClusterOrRawWriteAttributes<Cl, Custom>);
518
- }
519
-
520
- public async readResponse<Cl extends number | string, Custom extends TCustomCluster | undefined = undefined>(
521
- clusterKey: Cl,
522
- transactionSequenceNumber: number,
523
- attributes: PartialClusterOrRawWriteAttributes<Cl, Custom>,
524
- options?: Options,
525
- ): Promise<void> {
526
- assert(options?.transactionSequenceNumber === undefined, "Use parameter");
527
-
528
- const cluster = this.getCluster(clusterKey, undefined, options?.manufacturerCode);
529
- const payload: TFoundation["readRsp"] = [];
530
-
531
- for (const nameOrID in attributes) {
532
- const attribute = cluster.getAttribute(nameOrID);
533
-
534
- if (attribute) {
535
- payload.push({attrId: attribute.ID, attrData: attributes[nameOrID], dataType: attribute.type, status: 0});
536
- } else if (!Number.isNaN(Number(nameOrID))) {
537
- const value = attributes[nameOrID];
538
-
539
- payload.push({attrId: Number(nameOrID), attrData: value.value, dataType: value.type, status: 0});
540
- } else {
541
- throw new Error(`Unknown attribute '${nameOrID}', specify either an existing attribute or a number`);
542
- }
543
- }
544
-
545
- await this.zclCommand(
546
- cluster,
547
- "readRsp",
548
- payload,
549
- {direction: Zcl.Direction.SERVER_TO_CLIENT, ...options, transactionSequenceNumber},
550
- attributes,
551
- );
552
- }
553
-
554
- public async updateSimpleDescriptor(): Promise<void> {
555
- const clusterId = Zdo.ClusterId.SIMPLE_DESCRIPTOR_REQUEST;
556
- const zdoPayload = Zdo.Buffalo.buildRequest(Entity.adapter.hasZdoMessageOverhead, clusterId, this.deviceNetworkAddress, this.ID);
557
- const response = await Entity.adapter.sendZdo(this.deviceIeeeAddress, this.deviceNetworkAddress, clusterId, zdoPayload, false);
558
-
559
- if (!Zdo.Buffalo.checkStatus<Zdo.ClusterId.SIMPLE_DESCRIPTOR_RESPONSE>(response)) {
560
- throw new Zdo.StatusError(response[0]);
561
- }
562
-
563
- const simpleDescriptor = response[1];
564
-
565
- this.profileID = simpleDescriptor.profileId;
566
- this.deviceID = simpleDescriptor.deviceId;
567
- this.inputClusters = simpleDescriptor.inClusterList;
568
- this.outputClusters = simpleDescriptor.outClusterList;
569
- }
570
-
571
- public hasBind(clusterId: number, target: Endpoint | Group): boolean {
572
- return this.getBindIndex(clusterId, target) !== -1;
573
- }
574
-
575
- public getBindIndex(clusterId: number, target: Endpoint | Group): number {
576
- return this.binds.findIndex((b) => b.cluster.ID === clusterId && b.target === target);
577
- }
578
-
579
- public addBinding(clusterKey: number | string, target: Endpoint | Group | number): void {
580
- const cluster = this.getCluster(clusterKey);
581
-
582
- if (typeof target === "number") {
583
- target = Group.byGroupID(target) || Group.create(target);
584
- }
585
-
586
- this.addBindingInternal(cluster, target);
587
- }
588
-
589
- private addBindingInternal(cluster: ZclTypes.Cluster, target: Endpoint | Group): void {
590
- if (!this.hasBind(cluster.ID, target)) {
591
- if (target instanceof Group) {
592
- this._binds.push({cluster: cluster.ID, groupID: target.groupID, type: "group"});
593
- } else {
594
- this._binds.push({
595
- cluster: cluster.ID,
596
- type: "endpoint",
597
- deviceIeeeAddress: target.deviceIeeeAddress,
598
- endpointID: target.ID,
599
- });
600
- }
601
-
602
- this.save();
603
- }
604
- }
605
-
606
- public async bind(clusterKey: number | string, target: Endpoint | Group | number): Promise<void> {
607
- const cluster = this.getCluster(clusterKey);
608
-
609
- if (typeof target === "number") {
610
- target = Group.byGroupID(target) || Group.create(target);
611
- }
612
-
613
- const destinationAddress = target instanceof Endpoint ? target.deviceIeeeAddress : target.groupID;
614
-
615
- const log = `Bind ${this.deviceIeeeAddress}/${this.ID} ${cluster.name} from '${target instanceof Endpoint ? `${destinationAddress}/${target.ID}` : destinationAddress}'`;
616
- logger.debug(log, NS);
617
-
618
- try {
619
- const zdoClusterId = Zdo.ClusterId.BIND_REQUEST;
620
- const zdoPayload = Zdo.Buffalo.buildRequest(
621
- Entity.adapter.hasZdoMessageOverhead,
622
- zdoClusterId,
623
- this.deviceIeeeAddress as Eui64,
624
- this.ID,
625
- cluster.ID,
626
- target instanceof Endpoint ? Zdo.UNICAST_BINDING : Zdo.MULTICAST_BINDING,
627
- target instanceof Endpoint ? (target.deviceIeeeAddress as Eui64) : ZSpec.BLANK_EUI64,
628
- target instanceof Group ? target.groupID : 0,
629
- target instanceof Endpoint ? target.ID : 0xff,
630
- );
631
-
632
- const response = await Entity.adapter.sendZdo(this.deviceIeeeAddress, this.deviceNetworkAddress, zdoClusterId, zdoPayload, false);
633
-
634
- if (!Zdo.Buffalo.checkStatus<Zdo.ClusterId.BIND_RESPONSE>(response)) {
635
- throw new Zdo.StatusError(response[0]);
636
- }
637
-
638
- this.addBindingInternal(cluster, target);
639
- } catch (error) {
640
- const err = error as Error;
641
- err.message = `${log} failed (${err.message})`;
642
- // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
643
- logger.debug(err.stack!, NS);
644
- throw error;
645
- }
646
- }
647
-
648
- public save(): void {
649
- this.getDevice().save();
650
- }
651
-
652
- public async unbind(clusterKey: number | string, target: Endpoint | Group | number): Promise<void> {
653
- const cluster = this.getCluster(clusterKey);
654
- const action = `Unbind ${this.deviceIeeeAddress}/${this.ID} ${cluster.name}`;
655
-
656
- if (typeof target === "number") {
657
- const groupTarget = Group.byGroupID(target);
658
-
659
- if (!groupTarget) {
660
- throw new Error(`${action} invalid target '${target}' (no group with this ID exists).`);
661
- }
662
-
663
- target = groupTarget;
664
- }
665
-
666
- const destinationAddress = target instanceof Endpoint ? target.deviceIeeeAddress : target.groupID;
667
- const log = `${action} from '${target instanceof Endpoint ? `${destinationAddress}/${target.ID}` : destinationAddress}'`;
668
- const index = this.getBindIndex(cluster.ID, target);
669
-
670
- if (index === -1) {
671
- logger.debug(`${log} no bind present, skipping.`, NS);
672
- return;
673
- }
674
-
675
- logger.debug(log, NS);
676
-
677
- try {
678
- const zdoClusterId = Zdo.ClusterId.UNBIND_REQUEST;
679
- const zdoPayload = Zdo.Buffalo.buildRequest(
680
- Entity.adapter.hasZdoMessageOverhead,
681
- zdoClusterId,
682
- this.deviceIeeeAddress as Eui64,
683
- this.ID,
684
- cluster.ID,
685
- target instanceof Endpoint ? Zdo.UNICAST_BINDING : Zdo.MULTICAST_BINDING,
686
- target instanceof Endpoint ? (target.deviceIeeeAddress as Eui64) : ZSpec.BLANK_EUI64,
687
- target instanceof Group ? target.groupID : 0,
688
- target instanceof Endpoint ? target.ID : 0xff,
689
- );
690
-
691
- const response = await Entity.adapter.sendZdo(this.deviceIeeeAddress, this.deviceNetworkAddress, zdoClusterId, zdoPayload, false);
692
-
693
- if (!Zdo.Buffalo.checkStatus<Zdo.ClusterId.UNBIND_RESPONSE>(response)) {
694
- if (response[0] === Zdo.Status.NO_ENTRY) {
695
- logger.debug(`${log} no entry on device, removing entry from database.`, NS);
696
- } else {
697
- throw new Zdo.StatusError(response[0]);
698
- }
699
- }
700
-
701
- this._binds.splice(index, 1);
702
- this.save();
703
- } catch (error) {
704
- const err = error as Error;
705
- err.message = `${log} failed (${err.message})`;
706
- // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
707
- logger.debug(err.stack!, NS);
708
- throw error;
709
- }
710
- }
711
-
712
- public async defaultResponse(
713
- commandID: number,
714
- status: number,
715
- clusterID: number,
716
- transactionSequenceNumber: number,
717
- options?: Options,
718
- ): Promise<void> {
719
- assert(options?.transactionSequenceNumber === undefined, "Use parameter");
720
- const payload = {cmdId: commandID, statusCode: status};
721
- await this.zclCommand(clusterID, "defaultRsp", payload, {direction: Zcl.Direction.SERVER_TO_CLIENT, ...options, transactionSequenceNumber});
722
- }
723
-
724
- public async configureReporting<Cl extends number | string, Custom extends TCustomCluster | undefined = undefined>(
725
- clusterKey: Cl,
726
- items: ConfigureReportingItem<Cl, Custom>[],
727
- options?: Options,
728
- ): Promise<void> {
729
- const cluster = this.getCluster(clusterKey, undefined, options?.manufacturerCode);
730
- const optionsWithDefaults = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
731
- optionsWithDefaults.manufacturerCode = this.ensureManufacturerCodeIsUniqueAndGet<Cl, Custom>(
732
- cluster,
733
- items,
734
- optionsWithDefaults.manufacturerCode,
735
- "configureReporting",
736
- );
737
-
738
- const payload = items.map((item): TFoundation["configReport"][number] => {
739
- let dataType: number;
740
- let attrId: number;
741
-
742
- if (typeof item.attribute === "object") {
743
- dataType = item.attribute.type;
744
- attrId = item.attribute.ID;
745
- } else {
746
- const attribute = cluster.getAttribute(item.attribute);
747
-
748
- if (attribute) {
749
- dataType = attribute.type;
750
- attrId = attribute.ID;
751
- } else {
752
- throw new Error(`Invalid attribute '${item.attribute}' for cluster '${clusterKey}'`);
753
- }
754
- }
755
-
756
- return {
757
- direction: Zcl.Direction.CLIENT_TO_SERVER,
758
- attrId,
759
- dataType,
760
- minRepIntval: item.minimumReportInterval,
761
- maxRepIntval: item.maximumReportInterval,
762
- repChange: item.reportableChange,
763
- };
764
- });
765
-
766
- await this.zclCommand(cluster, "configReport", payload, optionsWithDefaults, items, true);
767
-
768
- for (const e of payload) {
769
- this._configuredReportings = this._configuredReportings.filter(
770
- (c) =>
771
- !(
772
- c.attrId === e.attrId &&
773
- c.cluster === cluster.ID &&
774
- (!("manufacturerCode" in c) || c.manufacturerCode === optionsWithDefaults.manufacturerCode)
775
- ),
776
- );
777
- }
778
-
779
- for (const entry of payload) {
780
- if (entry.maxRepIntval !== 0xffff) {
781
- this._configuredReportings.push({
782
- cluster: cluster.ID,
783
- attrId: entry.attrId,
784
- minRepIntval: entry.minRepIntval as number,
785
- maxRepIntval: entry.maxRepIntval as number,
786
- // expects items[].attribute to always point to a number DataType
787
- repChange: entry.repChange as number,
788
- manufacturerCode: optionsWithDefaults.manufacturerCode,
789
- });
790
- }
791
- }
792
-
793
- this.save();
794
- }
795
-
796
- public async writeStructured<Cl extends number | string>(
797
- clusterKey: Cl,
798
- payload: TFoundation["writeStructured"],
799
- options?: Options,
800
- ): Promise<void> {
801
- await this.zclCommand(clusterKey, "writeStructured", payload, options);
802
- // TODO: support `writeStructuredResponse`
803
- }
804
-
805
- public async command<Cl extends number | string, Co extends number | string, Custom extends TCustomCluster | undefined = undefined>(
806
- clusterKey: Cl,
807
- commandKey: Co,
808
- payload: ClusterOrRawPayload<Cl, Co, Custom>,
809
- options?: Options,
810
- ): Promise<undefined | KeyValue> {
811
- const frame = await this.zclCommand(clusterKey, commandKey, payload, options, undefined, false, Zcl.FrameType.SPECIFIC);
812
- if (frame) {
813
- return frame.payload;
814
- }
815
- }
816
-
817
- public async commandResponse<Cl extends number | string, Co extends number | string, Custom extends TCustomCluster | undefined = undefined>(
818
- clusterKey: Cl,
819
- commandKey: Co,
820
- payload: ClusterOrRawPayload<Cl, Co, Custom>,
821
- options?: Options,
822
- transactionSequenceNumber?: number,
823
- ): Promise<void> {
824
- assert(options?.transactionSequenceNumber === undefined, "Use parameter");
825
-
826
- const device = this.getDevice();
827
- const cluster = this.getCluster(clusterKey, device, options?.manufacturerCode);
828
- const command = cluster.getCommandResponse(commandKey);
829
- transactionSequenceNumber = transactionSequenceNumber || zclTransactionSequenceNumber.next();
830
- const optionsWithDefaults = this.getOptionsWithDefaults(options, true, Zcl.Direction.SERVER_TO_CLIENT, cluster.manufacturerCode);
831
-
832
- const frame = Zcl.Frame.create(
833
- Zcl.FrameType.SPECIFIC,
834
- optionsWithDefaults.direction,
835
- optionsWithDefaults.disableDefaultResponse,
836
- optionsWithDefaults.manufacturerCode,
837
- transactionSequenceNumber,
838
- command,
839
- cluster,
840
- payload,
841
- device.customClusters,
842
- optionsWithDefaults.reservedBits,
843
- );
844
-
845
- const createLogMessage = (): string =>
846
- `CommandResponse ${this.deviceIeeeAddress}/${this.ID} ` +
847
- `${cluster.name}.${command.name}(${JSON.stringify(payload)}, ${JSON.stringify(optionsWithDefaults)})`;
848
- logger.debug(createLogMessage, NS);
849
-
850
- try {
851
- // Broadcast Green Power responses
852
- if (this.ID === 242) {
853
- await this.sendRequest(frame, optionsWithDefaults, async () => {
854
- await Entity.adapter.sendZclFrameToAll(242, frame, 242, BroadcastAddress.RX_ON_WHEN_IDLE);
855
- });
856
- } else {
857
- await this.sendRequest(frame, optionsWithDefaults);
858
- }
859
- } catch (error) {
860
- const err = error as Error;
861
- err.message = `${createLogMessage()} failed (${err.message})`;
862
- // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
863
- logger.debug(err.stack!, NS);
864
- throw error;
865
- }
866
- }
867
-
868
- public waitForCommand(
869
- clusterKey: number | string,
870
- commandKey: number | string,
871
- transactionSequenceNumber: number | undefined,
872
- timeout: number,
873
- ): {promise: Promise<{header: Zcl.Header; payload: KeyValue}>; cancel: () => void} {
874
- const device = this.getDevice();
875
- const cluster = this.getCluster(clusterKey, device);
876
- const command = cluster.getCommand(commandKey);
877
- const waiter = Entity.adapter.waitFor(
878
- this.deviceNetworkAddress,
879
- this.ID,
880
- Zcl.FrameType.SPECIFIC,
881
- Zcl.Direction.CLIENT_TO_SERVER,
882
- transactionSequenceNumber,
883
- cluster.ID,
884
- command.ID,
885
- timeout,
886
- );
887
-
888
- const promise = new Promise<{header: Zcl.Header; payload: KeyValue}>((resolve, reject) => {
889
- waiter.promise.then(
890
- (payload) => {
891
- try {
892
- const frame = Zcl.Frame.fromBuffer(payload.clusterID, payload.header, payload.data, device.customClusters);
893
- resolve({header: frame.header, payload: frame.payload});
894
- } catch (error) {
895
- reject(error);
896
- }
897
- },
898
- (error) => reject(error),
899
- );
900
- });
901
-
902
- return {promise, cancel: waiter.cancel};
903
- }
904
-
905
- private getOptionsWithDefaults(
906
- options: Options | undefined,
907
- disableDefaultResponse: boolean,
908
- direction: Zcl.Direction,
909
- manufacturerCode: number | undefined,
910
- ): OptionsWithDefaults {
911
- return {
912
- timeout: 10000,
913
- disableResponse: false,
914
- disableRecovery: false,
915
- disableDefaultResponse,
916
- direction,
917
- srcEndpoint: undefined,
918
- reservedBits: 0,
919
- manufacturerCode,
920
- transactionSequenceNumber: undefined,
921
- writeUndiv: false,
922
- ...(options || {}),
923
- };
924
- }
925
-
926
- private ensureManufacturerCodeIsUniqueAndGet<Cl extends string | number, Custom extends TCustomCluster | undefined = undefined>(
927
- cluster: ZclTypes.Cluster,
928
- attributes: (string | number)[] | ConfigureReportingItem<Cl, Custom>[],
929
- fallbackManufacturerCode: number | undefined, // XXX: problematic undefined for a "fallback"?
930
- caller: string,
931
- ): number | undefined {
932
- const manufacturerCodes = new Set(
933
- attributes.map((nameOrID): number | undefined => {
934
- let attributeID: number | string;
935
-
936
- if (typeof nameOrID === "object") {
937
- // ConfigureReportingItem
938
- if (typeof nameOrID.attribute !== "object") {
939
- attributeID = nameOrID.attribute;
940
- } else {
941
- return fallbackManufacturerCode;
942
- }
943
- } else {
944
- // string || number
945
- attributeID = nameOrID;
946
- }
947
-
948
- // we fall back to caller|cluster provided manufacturerCode
949
- const attribute = cluster.getAttribute(attributeID);
950
-
951
- if (attribute) {
952
- return attribute.manufacturerCode === undefined ? fallbackManufacturerCode : attribute.manufacturerCode;
953
- }
954
-
955
- // unknown attribute, we should not fail on this here
956
- return fallbackManufacturerCode;
957
- }),
958
- );
959
-
960
- if (manufacturerCodes.size === 1) {
961
- return manufacturerCodes.values().next().value;
962
- }
963
-
964
- throw new Error(`Cannot have attributes with different manufacturerCode in single '${caller}' call`);
965
- }
966
-
967
- public async addToGroup(group: Group): Promise<void> {
968
- await this.command("genGroups", "add", {groupid: group.groupID, groupname: ""});
969
- group.addMember(this);
970
- }
971
-
972
- private getCluster(
973
- clusterKey: number | string,
974
- device: Device | undefined = undefined,
975
- manufacturerCode: number | undefined = undefined,
976
- ): ZclTypes.Cluster {
977
- if (!device) {
978
- device = this.getDevice();
979
- }
980
-
981
- return Zcl.Utils.getCluster(clusterKey, manufacturerCode ?? device.manufacturerID, device.customClusters);
982
- }
983
-
984
- /**
985
- * Remove endpoint from a group, accepts both a Group and number as parameter.
986
- * The number parameter type should only be used when removing from a group which is not known
987
- * to zigbee-herdsman.
988
- */
989
- public async removeFromGroup(group: Group | number): Promise<void> {
990
- await this.command("genGroups", "remove", {groupid: group instanceof Group ? group.groupID : group});
991
- if (group instanceof Group) {
992
- group.removeMember(this);
993
- }
994
- }
995
-
996
- public async removeFromAllGroups(): Promise<void> {
997
- await this.command("genGroups", "removeAll", {}, {disableDefaultResponse: true});
998
- this.removeFromAllGroupsDatabase();
999
- }
1000
-
1001
- public removeFromAllGroupsDatabase(): void {
1002
- for (const group of Group.allIterator()) {
1003
- if (group.hasMember(this)) {
1004
- group.removeMember(this);
1005
- }
1006
- }
1007
- }
1008
-
1009
- public async zclCommand<Cl extends number | string, Co extends number | string, Custom extends TCustomCluster | undefined = undefined>(
1010
- clusterKey: Cl | ZclTypes.Cluster,
1011
- commandKey: Co | ZclTypes.Command,
1012
- payload: ClusterOrRawPayload<Cl, Co, Custom> | FoundationOrRawPayload<Co>,
1013
- options?: Options,
1014
- logPayload?: KeyValue,
1015
- checkStatus = false,
1016
- frameType: Zcl.FrameType = Zcl.FrameType.GLOBAL,
1017
- ): Promise<undefined | Zcl.Frame> {
1018
- const device = this.getDevice();
1019
- const cluster = typeof clusterKey === "object" ? clusterKey : this.getCluster(clusterKey, device, options?.manufacturerCode);
1020
- const command =
1021
- typeof commandKey === "object"
1022
- ? commandKey
1023
- : frameType === Zcl.FrameType.GLOBAL
1024
- ? Zcl.Utils.getGlobalCommand(commandKey)
1025
- : cluster.getCommand(commandKey);
1026
- const hasResponse = frameType === Zcl.FrameType.GLOBAL ? true : command.response !== undefined;
1027
- const optionsWithDefaults = this.getOptionsWithDefaults(options, hasResponse, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
1028
-
1029
- const frame = Zcl.Frame.create(
1030
- frameType,
1031
- optionsWithDefaults.direction,
1032
- optionsWithDefaults.disableDefaultResponse,
1033
- optionsWithDefaults.manufacturerCode,
1034
- optionsWithDefaults.transactionSequenceNumber ?? zclTransactionSequenceNumber.next(),
1035
- command,
1036
- cluster,
1037
- payload,
1038
- device.customClusters,
1039
- optionsWithDefaults.reservedBits,
1040
- );
1041
-
1042
- const createLogMessage = (): string =>
1043
- `ZCL command ${this.deviceIeeeAddress}/${this.ID} ` +
1044
- `${cluster.name}.${command.name}(${JSON.stringify(logPayload ? logPayload : payload)}, ${JSON.stringify(optionsWithDefaults)})`;
1045
- logger.debug(createLogMessage, NS);
1046
-
1047
- try {
1048
- const result = await this.sendRequest(frame, optionsWithDefaults);
1049
-
1050
- if (result) {
1051
- const resultFrame = Zcl.Frame.fromBuffer(result.clusterID, result.header, result.data, device.customClusters);
1052
- if (result && checkStatus && !optionsWithDefaults.disableResponse) {
1053
- this.checkStatus(resultFrame.payload);
1054
- }
1055
- return resultFrame;
1056
- }
1057
- } catch (error) {
1058
- const err = error as Error;
1059
- err.message = `${createLogMessage()} failed (${err.message})`;
1060
- // biome-ignore lint/style/noNonNullAssertion: ignored using `--suppress`
1061
- logger.debug(err.stack!, NS);
1062
- throw error;
1063
- }
1064
- }
1065
-
1066
- public async zclCommandBroadcast<Cl extends number | string, Co extends number | string, Custom extends TCustomCluster | undefined = undefined>(
1067
- endpoint: number,
1068
- destination: BroadcastAddress,
1069
- clusterKey: Cl,
1070
- commandKey: Co,
1071
- payload: ClusterOrRawPayload<Cl, Co, Custom> | FoundationOrRawPayload<Co>,
1072
- options?: Options,
1073
- ): Promise<void> {
1074
- const device = this.getDevice();
1075
- const cluster = this.getCluster(clusterKey, device, options?.manufacturerCode);
1076
- const command = cluster.getCommand(commandKey);
1077
- const optionsWithDefaults = this.getOptionsWithDefaults(options, true, Zcl.Direction.CLIENT_TO_SERVER, cluster.manufacturerCode);
1078
- const sourceEndpoint = optionsWithDefaults.srcEndpoint ?? this.ID;
1079
-
1080
- const frame = Zcl.Frame.create(
1081
- Zcl.FrameType.SPECIFIC,
1082
- optionsWithDefaults.direction,
1083
- true,
1084
- optionsWithDefaults.manufacturerCode,
1085
- optionsWithDefaults.transactionSequenceNumber ?? zclTransactionSequenceNumber.next(),
1086
- command,
1087
- cluster,
1088
- payload,
1089
- device.customClusters,
1090
- optionsWithDefaults.reservedBits,
1091
- );
1092
-
1093
- logger.debug(
1094
- () =>
1095
- `ZCL command broadcast ${this.deviceIeeeAddress}/${sourceEndpoint} to ${destination}/${endpoint} ` +
1096
- `${cluster.name}.${command.name}(${JSON.stringify({payload, optionsWithDefaults})})`,
1097
- NS,
1098
- );
1099
-
1100
- // if endpoint===0xFF ("broadcast endpoint"), deliver to all endpoints supporting cluster, should be avoided whenever possible
1101
- await Entity.adapter.sendZclFrameToAll(endpoint, frame, sourceEndpoint, destination);
1102
- }
1103
- }
1104
-
1105
- export default Endpoint;