Skip to content

Commit 167fb6c

Browse files
P-R-O-C-H-YCopilotpre-commit-ci-lite[bot]
authored
feat(zibgee): Add IASZone enroll request and restore + error check fixes (#11990)
* feat(zibgee): Add requestIASZoneEnroll + error check fixes * fix: Spelling Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(zigbee): Remove transaction sequence logging * feat(zigbee): Add restoreIASZone from flash method * docs(zigbee): Add docs for new methods * feat(zigbee): Update checking logic * fix: Fix memcpy and update docs * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 731aee2 commit 167fb6c

File tree

12 files changed

+390
-35
lines changed

12 files changed

+390
-35
lines changed

docs/en/zigbee/ep_contact_switch.rst

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ Sets the contact switch to open state.
6363
6464
This function will return ``true`` if successful, ``false`` otherwise.
6565

66+
report
67+
^^^^^^
68+
69+
Manually reports the current contact state.
70+
71+
.. code-block:: arduino
72+
73+
bool report();
74+
75+
This function will return ``true`` if successful, ``false`` otherwise.
76+
6677
setIASClientEndpoint
6778
^^^^^^^^^^^^^^^^^^^^
6879

@@ -74,16 +85,38 @@ Sets the IAS Client endpoint number (default is 1).
7485
7586
* ``ep_number`` - IAS Client endpoint number
7687

77-
report
78-
^^^^^^
88+
requestIASZoneEnroll
89+
^^^^^^^^^^^^^^^^^^^^
7990

80-
Manually reports the current contact state.
91+
Requests a new IAS Zone enrollment. Can be called to enroll a new device or to re-enroll an already enrolled device.
8192

8293
.. code-block:: arduino
8394
84-
bool report();
95+
bool requestIASZoneEnroll();
8596
86-
This function will return ``true`` if successful, ``false`` otherwise.
97+
This function will return ``true`` if the enrollment request was sent successfully, ``false`` otherwise. The actual enrollment status should be checked using the ``enrolled()`` method after waiting for the enrollment response.
98+
99+
restoreIASZoneEnroll
100+
^^^^^^^^^^^^^^^^^^^^
101+
102+
Restores IAS Zone enrollment from stored attributes. This method should be called after rebooting an already enrolled device. It restores the enrollment information from flash memory, which is faster for sleepy devices compared to requesting a new enrollment.
103+
104+
.. code-block:: arduino
105+
106+
bool restoreIASZoneEnroll();
107+
108+
This function will return ``true`` if the enrollment was successfully restored, ``false`` otherwise. The enrollment information (zone ID and IAS CIE address) must be available in the device's stored attributes for this to succeed.
109+
110+
enrolled
111+
^^^^^^^^
112+
113+
Checks if the device is currently enrolled in the IAS Zone.
114+
115+
.. code-block:: arduino
116+
117+
bool enrolled();
118+
119+
This function returns ``true`` if the device is enrolled, ``false`` otherwise. Use this method to check the enrollment status after calling ``requestIASZoneEnroll()`` or ``restoreIASZoneEnroll()``.
87120

88121
Example
89122
-------

docs/en/zigbee/ep_door_window_handle.rst

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ Sets the door/window handle to tilted position.
6767
6868
This function will return ``true`` if successful, ``false`` otherwise.
6969

70+
report
71+
^^^^^^
72+
73+
Manually reports the current handle position.
74+
75+
.. code-block:: arduino
76+
77+
bool report();
78+
79+
This function will return ``true`` if successful, ``false`` otherwise.
80+
7081
setIASClientEndpoint
7182
^^^^^^^^^^^^^^^^^^^^
7283

@@ -78,16 +89,38 @@ Sets the IAS Client endpoint number (default is 1).
7889
7990
* ``ep_number`` - IAS Client endpoint number
8091

81-
report
82-
^^^^^^
92+
requestIASZoneEnroll
93+
^^^^^^^^^^^^^^^^^^^^
8394

84-
Manually reports the current handle position.
95+
Requests a new IAS Zone enrollment. Can be called to enroll a new device or to re-enroll an already enrolled device.
8596

8697
.. code-block:: arduino
8798
88-
bool report();
99+
bool requestIASZoneEnroll();
89100
90-
This function will return ``true`` if successful, ``false`` otherwise.
101+
This function will return ``true`` if the enrollment request was sent successfully, ``false`` otherwise. The actual enrollment status should be checked using the ``enrolled()`` method after waiting for the enrollment response.
102+
103+
restoreIASZoneEnroll
104+
^^^^^^^^^^^^^^^^^^^^
105+
106+
Restores IAS Zone enrollment from stored attributes. This method should be called after rebooting an already enrolled device. It restores the enrollment information from flash memory, which is faster for sleepy devices compared to requesting a new enrollment.
107+
108+
.. code-block:: arduino
109+
110+
bool restoreIASZoneEnroll();
111+
112+
This function will return ``true`` if the enrollment was successfully restored, ``false`` otherwise. The enrollment information (zone ID and IAS CIE address) must be available in the device's stored attributes for this to succeed.
113+
114+
enrolled
115+
^^^^^^^^
116+
117+
Checks if the device is currently enrolled in the IAS Zone.
118+
119+
.. code-block:: arduino
120+
121+
bool enrolled();
122+
123+
This function returns ``true`` if the device is enrolled, ``false`` otherwise. Use this method to check the enrollment status after calling ``requestIASZoneEnroll()`` or ``restoreIASZoneEnroll()``.
91124

92125
Example
93126
-------

docs/en/zigbee/ep_vibration_sensor.rst

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,42 @@ Manually reports the current vibration state.
7373

7474
.. code-block:: arduino
7575
76-
void report();
76+
bool report();
7777
78-
This function does not return a value.
78+
This function will return ``true`` if successful, ``false`` otherwise.
79+
80+
requestIASZoneEnroll
81+
^^^^^^^^^^^^^^^^^^^^
82+
83+
Requests a new IAS Zone enrollment. Can be called to enroll a new device or to re-enroll an already enrolled device.
84+
85+
.. code-block:: arduino
86+
87+
bool requestIASZoneEnroll();
88+
89+
This function will return ``true`` if the enrollment request was sent successfully, ``false`` otherwise. The actual enrollment status should be checked using the ``enrolled()`` method after waiting for the enrollment response.
90+
91+
restoreIASZoneEnroll
92+
^^^^^^^^^^^^^^^^^^^^
93+
94+
Restores IAS Zone enrollment from stored attributes. This method should be called after rebooting an already enrolled device. It restores the enrollment information from flash memory, which is faster for sleepy devices compared to requesting a new enrollment.
95+
96+
.. code-block:: arduino
97+
98+
bool restoreIASZoneEnroll();
99+
100+
This function will return ``true`` if the enrollment was successfully restored, ``false`` otherwise. The enrollment information (zone ID and IAS CIE address) must be available in the device's stored attributes for this to succeed.
101+
102+
enrolled
103+
^^^^^^^^
104+
105+
Checks if the device is currently enrolled in the IAS Zone.
106+
107+
.. code-block:: arduino
108+
109+
bool enrolled();
110+
111+
This function returns ``true`` if the device is enrolled, ``false`` otherwise. Use this method to check the enrollment status after calling ``requestIASZoneEnroll()`` or ``restoreIASZoneEnroll()``.
79112

80113
Example
81114
-------

libraries/Zigbee/examples/Zigbee_Contact_Switch/Zigbee_Contact_Switch.ino

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,25 @@
3131
#endif
3232

3333
#include "Zigbee.h"
34+
#include <Preferences.h>
3435

3536
/* Zigbee contact sensor configuration */
36-
#define CONTACT_SWITCH_ENDPOINT_NUMBER 10
37+
#define CONTACT_SWITCH_ENDPOINT_NUMBER 1
3738
uint8_t button = BOOT_PIN;
3839
uint8_t sensor_pin = 4;
3940

4041
ZigbeeContactSwitch zbContactSwitch = ZigbeeContactSwitch(CONTACT_SWITCH_ENDPOINT_NUMBER);
4142

43+
/* Preferences for storing ENROLLED flag to persist across reboots */
44+
Preferences preferences;
45+
4246
void setup() {
4347
Serial.begin(115200);
4448

49+
preferences.begin("Zigbee", false); // Save ENROLLED flag in flash so it persists across reboots
50+
bool enrolled = preferences.getBool("ENROLLED"); // Get ENROLLED flag from preferences
51+
preferences.end();
52+
4553
// Init button + switch
4654
pinMode(button, INPUT_PULLUP);
4755
pinMode(sensor_pin, INPUT_PULLUP);
@@ -67,6 +75,31 @@ void setup() {
6775
delay(100);
6876
}
6977
Serial.println();
78+
79+
// Check if device has been enrolled before restarting - if so, restore IAS Zone enroll, otherwise request new IAS Zone enroll
80+
if (enrolled) {
81+
Serial.println("Device has been enrolled before - restoring IAS Zone enrollment");
82+
zbContactSwitch.restoreIASZoneEnroll();
83+
} else {
84+
Serial.println("Device is factory new - first time joining network - requesting new IAS Zone enrollment");
85+
zbContactSwitch.requestIASZoneEnroll();
86+
}
87+
88+
while (!zbContactSwitch.enrolled()) {
89+
Serial.print(".");
90+
delay(100);
91+
}
92+
Serial.println();
93+
Serial.println("Zigbee enrolled successfully!");
94+
95+
// Store ENROLLED flag only if this was a new enrollment (previous flag was false)
96+
// Skip writing if we just restored enrollment (flag was already true)
97+
if (!enrolled) {
98+
preferences.begin("Zigbee", false);
99+
preferences.putBool("ENROLLED", true); // set ENROLLED flag to true
100+
preferences.end();
101+
Serial.println("ENROLLED flag saved to preferences");
102+
}
70103
}
71104

72105
void loop() {
@@ -91,6 +124,11 @@ void loop() {
91124
if ((millis() - startTime) > 3000) {
92125
// If key pressed for more than 3secs, factory reset Zigbee and reboot
93126
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
127+
// Clear the ENROLLED flag from preferences
128+
preferences.begin("Zigbee", false);
129+
preferences.putBool("ENROLLED", false); // set ENROLLED flag to false
130+
preferences.end();
131+
Serial.println("ENROLLED flag cleared from preferences");
94132
delay(1000);
95133
Zigbee.factoryReset();
96134
}

libraries/Zigbee/examples/Zigbee_Vibration_Sensor/Zigbee_Vibration_Sensor.ino

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,25 @@
3131
#endif
3232

3333
#include "Zigbee.h"
34+
#include <Preferences.h>
3435

3536
/* Zigbee vibration sensor configuration */
36-
#define VIBRATION_SENSOR_ENDPOINT_NUMBER 10
37+
#define VIBRATION_SENSOR_ENDPOINT_NUMBER 1
3738
uint8_t button = BOOT_PIN;
3839
uint8_t sensor_pin = 4;
3940

4041
ZigbeeVibrationSensor zbVibrationSensor = ZigbeeVibrationSensor(VIBRATION_SENSOR_ENDPOINT_NUMBER);
4142

43+
/* Preferences for storing ENROLLED flag to persist across reboots */
44+
Preferences preferences;
45+
4246
void setup() {
4347
Serial.begin(115200);
4448

49+
preferences.begin("Zigbee", false); // Save ENROLLED flag in flash so it persists across reboots
50+
bool enrolled = preferences.getBool("ENROLLED"); // Get ENROLLED flag from preferences
51+
preferences.end();
52+
4553
// Init button + sensor
4654
pinMode(button, INPUT_PULLUP);
4755
pinMode(sensor_pin, INPUT);
@@ -67,6 +75,31 @@ void setup() {
6775
delay(100);
6876
}
6977
Serial.println();
78+
79+
// Check if device has been enrolled before restarting - if so, restore IAS Zone enroll, otherwise request new IAS Zone enroll
80+
if (enrolled) {
81+
Serial.println("Device has been enrolled before - restoring IAS Zone enrollment");
82+
zbVibrationSensor.restoreIASZoneEnroll();
83+
} else {
84+
Serial.println("Device is factory new - first time joining network - requesting new IAS Zone enrollment");
85+
zbVibrationSensor.requestIASZoneEnroll();
86+
}
87+
88+
while (!zbVibrationSensor.enrolled()) {
89+
Serial.print(".");
90+
delay(100);
91+
}
92+
Serial.println();
93+
Serial.println("Zigbee enrolled successfully!");
94+
95+
// Store ENROLLED flag only if this was a new enrollment (previous flag was false)
96+
// Skip writing if we just restored enrollment (flag was already true)
97+
if (!enrolled) {
98+
preferences.begin("Zigbee", false);
99+
preferences.putBool("ENROLLED", true); // set ENROLLED flag to true
100+
preferences.end();
101+
Serial.println("ENROLLED flag saved to preferences");
102+
}
70103
}
71104

72105
void loop() {
@@ -95,6 +128,11 @@ void loop() {
95128
if ((millis() - startTime) > 3000) {
96129
// If key pressed for more than 3secs, factory reset Zigbee and reboot
97130
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
131+
// Clear the ENROLLED flag from preferences
132+
preferences.begin("Zigbee", false);
133+
preferences.putBool("ENROLLED", false); // set ENROLLED flag to false
134+
preferences.end();
135+
Serial.println("ENROLLED flag cleared from preferences");
98136
delay(1000);
99137
Zigbee.factoryReset();
100138
}

libraries/Zigbee/keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ setIASClientEndpoint KEYWORD2
209209
setClosed KEYWORD2
210210
setOpen KEYWORD2
211211
setTilted KEYWORD2
212+
requestIASZoneEnroll KEYWORD2
213+
restoreIASZoneEnroll KEYWORD2
214+
enrolled KEYWORD2
212215

213216
# ZigbeeVibrationSensor
214217
setVibration KEYWORD2

0 commit comments

Comments
 (0)