Skip to content

Commit e0f466a

Browse files
authored
added template helper to get mandatory attribute count (#1642)
1 parent 37ba4da commit e0f466a

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

docs/api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9111,10 +9111,19 @@ This module contains the API for templating. For more detailed instructions, rea
91119111

91129112

91139113
* [Templating API: Attribute helpers](#module_Templating API_ Attribute helpers)
9114+
* [~count_mandatory_matter_attributes()](#module_Templating API_ Attribute helpers..count_mandatory_matter_attributes)
91149115
* [~featureBits(options)](#module_Templating API_ Attribute helpers..featureBits) ⇒
91159116
* [~attributeDefault()](#module_Templating API_ Attribute helpers..attributeDefault) ⇒
91169117
* [~as_underlying_atomic_identifier_for_attribute_id(attributeId)](#module_Templating API_ Attribute helpers..as_underlying_atomic_identifier_for_attribute_id)
91179118

9119+
<a name="module_Templating API_ Attribute helpers..count_mandatory_matter_attributes"></a>
9120+
9121+
### Templating API: Attribute helpers~count\_mandatory\_matter\_attributes()
9122+
Counts the number of mandatory attributes (not optional, non-global) for the current cluster context.
9123+
Usage: {{count_mandatory_matter_attributes side="server"}}
9124+
If side is not provided or invalid, counts all attributes.
9125+
9126+
**Kind**: inner method of [<code>Templating API: Attribute helpers</code>](#module_Templating API_ Attribute helpers)
91189127
<a name="module_Templating API_ Attribute helpers..featureBits"></a>
91199128

91209129
### Templating API: Attribute helpers~featureBits(options) ⇒

docs/helpers.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,19 @@ This module contains the API for templating. For more detailed instructions, rea
138138

139139

140140
* [Templating API: Attribute helpers](#module_Templating API_ Attribute helpers)
141+
* [~count_mandatory_matter_attributes()](#module_Templating API_ Attribute helpers..count_mandatory_matter_attributes)
141142
* [~featureBits(options)](#module_Templating API_ Attribute helpers..featureBits) ⇒
142143
* [~attributeDefault()](#module_Templating API_ Attribute helpers..attributeDefault) ⇒
143144
* [~as_underlying_atomic_identifier_for_attribute_id(attributeId)](#module_Templating API_ Attribute helpers..as_underlying_atomic_identifier_for_attribute_id)
144145

146+
<a name="module_Templating API_ Attribute helpers..count_mandatory_matter_attributes"></a>
147+
148+
### Templating API: Attribute helpers~count\_mandatory\_matter\_attributes()
149+
Counts the number of mandatory attributes (not optional, non-global) for the current cluster context.
150+
Usage: {{count_mandatory_matter_attributes side="server"}}
151+
If side is not provided or invalid, counts all attributes.
152+
153+
**Kind**: inner method of [<code>Templating API: Attribute helpers</code>](#module_Templating API_ Attribute helpers)
145154
<a name="module_Templating API_ Attribute helpers..featureBits"></a>
146155

147156
### Templating API: Attribute helpers~featureBits(options) ⇒

src-electron/generator/helper-attribute.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ const templateUtil = require('./template-util')
2727
const zclUtil = require('../util/zcl-util')
2828
const dbEnum = require('../../src-shared/db-enum')
2929

30+
/**
31+
* Counts the number of mandatory attributes (not optional, non-global) for the current cluster context.
32+
* Usage: {{count_mandatory_matter_attributes side="server"}}
33+
* If side is not provided or invalid, counts all attributes.
34+
*/
35+
async function count_mandatory_matter_attributes(options) {
36+
if (!('id' in this))
37+
throw new Error(
38+
'count_mandatory_matter_attributes requires a cluster id inside the context.'
39+
)
40+
const packageIds = await templateUtil.ensureZclPackageIds(this)
41+
let side = options?.hash?.side
42+
if (typeof side === 'string') side = side.toLowerCase()
43+
let attributes
44+
if (side === dbEnum.side.server || side === dbEnum.side.client) {
45+
attributes =
46+
await queryZcl.selectAttributesByClusterIdAndSideIncludingGlobal(
47+
this.global.db,
48+
this.id,
49+
packageIds,
50+
side
51+
)
52+
} else {
53+
// Get all attributes for this cluster (both sides)
54+
attributes = await queryZcl.selectAttributesByClusterIdIncludingGlobal(
55+
this.global.db,
56+
this.id,
57+
packageIds
58+
)
59+
}
60+
// Count mandatory attributes (not optional, non-global)
61+
return attributes.filter((a) => a.clusterRef && !a.isOptional).length
62+
}
63+
3064
/**
3165
* Get feature bits from the given context.
3266
* @param {*} options
@@ -172,3 +206,4 @@ exports.global_attribute_default = attributeDefault
172206
exports.feature_bits = featureBits
173207
exports.as_underlying_atomic_identifier_for_attribute_id =
174208
as_underlying_atomic_identifier_for_attribute_id
209+
exports.count_mandatory_matter_attributes = count_mandatory_matter_attributes

test/gen-matter-1.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ test(
275275
)
276276
)
277277

278+
// Testing count_mandatory_matter_attributes helper
279+
expect(
280+
sdkExt.includes(
281+
'// mandatory attribute count for cluster Network Commissioning: 6'
282+
)
283+
).toBeTruthy()
284+
278285
// Testing promisedHandlebars when we have {{#if (promise)}} in a template
279286
// Eg: {{#zcl_clusters}}
280287
// {{#zcl_commands_source_server}}

test/gen-template/matter/sdk-ext.zapt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
------------------- Clusters -------------------
55
{{#zcl_clusters}}
66
// cluster: {{asHex code 4}} {{label}}, text extension: '{{cluster_extension property="testClusterExtension"}}'
7+
// mandatory attribute count for cluster {{label}}: {{count_mandatory_matter_attributes}}
78
{{/zcl_clusters}}
89

910
------------------- By-role Clusters -------------------

0 commit comments

Comments
 (0)