Skip to content

Commit dd1d3b8

Browse files
committed
feat(zigbee): Add callback for config change
1 parent 254fd8d commit dd1d3b8

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

Diff for: libraries/Zigbee/examples/Zigbee_Occupancy_Sensor/Zigbee_Occupancy_Sensor.ino

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ void setup() {
6262
// Optional: Set unoccupied to occupied threshold (if your sensor supports it) to PIR sensor as its set by setSensorType
6363
zbOccupancySensor.setUnoccupiedToOccupiedThreshold(ZIGBEE_OCCUPANCY_SENSOR_TYPE_PIR, 1); // 1 movement event threshold
6464

65+
// Optional: Set callback function for occupancy config change
66+
zbOccupancySensor.onOccupancyConfigChange(occupancyConfigChange);
67+
6568
// Add endpoint to Zigbee Core
6669
Zigbee.addEndpoint(&zbOccupancySensor);
6770

@@ -113,3 +116,9 @@ void loop() {
113116
}
114117
delay(100);
115118
}
119+
120+
// Callback function for occupancy config change
121+
void occupancyConfigChange(ZigbeeOccupancySensorType sensor_type, uint16_t occ_to_unocc_delay, uint16_t unocc_to_occ_delay, uint8_t unocc_to_occ_threshold) {
122+
// Handle sensor configuration here
123+
Serial.printf("Occupancy config change: sensor type: %d, occ to unocc delay: %d, unocc to occ delay: %d, unocc to occ threshold: %d\n", sensor_type, occ_to_unocc_delay, unocc_to_occ_delay, unocc_to_occ_threshold);
124+
}

Diff for: libraries/Zigbee/src/ep/ZigbeeOccupancySensor.cpp

+23-10
Original file line numberDiff line numberDiff line change
@@ -226,35 +226,35 @@ void ZigbeeOccupancySensor::zbAttributeSet(const esp_zb_zcl_set_attr_value_messa
226226
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING) {
227227
//PIR
228228
if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_PIR_OCC_TO_UNOCC_DELAY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
229-
uint16_t pir_occ_to_unocc_delay = *(uint16_t *)message->attribute.data.value;
229+
_pir_occ_to_unocc_delay = *(uint16_t *)message->attribute.data.value;
230230
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_PIR);
231231
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_PIR_UNOCC_TO_OCC_DELAY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
232-
uint16_t pir_unocc_to_occ_delay = *(uint16_t *)message->attribute.data.value;
232+
_pir_unocc_to_occ_delay = *(uint16_t *)message->attribute.data.value;
233233
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_PIR);
234234
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_PIR_UNOCC_TO_OCC_THRESHOLD_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U8) {
235-
uint8_t pir_unocc_to_occ_threshold = *(uint8_t *)message->attribute.data.value;
235+
_pir_unocc_to_occ_threshold = *(uint8_t *)message->attribute.data.value;
236236
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_PIR);
237237
}
238238
//Ultrasonic
239239
else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_ULTRASONIC_OCCUPIED_TO_UNOCCUPIED_DELAY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
240-
uint16_t ultrasonic_occ_to_unocc_delay = *(uint16_t *)message->attribute.data.value;
240+
_ultrasonic_occ_to_unocc_delay = *(uint16_t *)message->attribute.data.value;
241241
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_ULTRASONIC);
242242
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_ULTRASONIC_UNOCCUPIED_TO_OCCUPIED_DELAY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
243-
uint16_t ultrasonic_unocc_to_occ_delay = *(uint16_t *)message->attribute.data.value;
243+
_ultrasonic_unocc_to_occ_delay = *(uint16_t *)message->attribute.data.value;
244244
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_ULTRASONIC);
245245
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_ULTRASONIC_UNOCCUPIED_TO_OCCUPIED_THRESHOLD_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U8) {
246-
uint8_t ultrasonic_unocc_to_occ_threshold = *(uint8_t *)message->attribute.data.value;
246+
_ultrasonic_unocc_to_occ_threshold = *(uint8_t *)message->attribute.data.value;
247247
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_ULTRASONIC);
248248
}
249249
//Physical Contact
250250
else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_PHYSICAL_CONTACT_OCCUPIED_TO_UNOCCUPIED_DELAY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
251-
uint16_t physical_contact_occ_to_unocc_delay = *(uint16_t *)message->attribute.data.value;
251+
_physical_contact_occ_to_unocc_delay = *(uint16_t *)message->attribute.data.value;
252252
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_PHYSICAL_CONTACT);
253253
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_PHYSICAL_CONTACT_UNOCCUPIED_TO_OCCUPIED_DELAY_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
254-
uint16_t physical_contact_unocc_to_occ_delay = *(uint16_t *)message->attribute.data.value;
254+
_physical_contact_unocc_to_occ_delay = *(uint16_t *)message->attribute.data.value;
255255
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_PHYSICAL_CONTACT);
256256
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_PHYSICAL_CONTACT_UNOCCUPIED_TO_OCCUPIED_THRESHOLD_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U8) {
257-
uint8_t physical_contact_unocc_to_occ_threshold = *(uint8_t *)message->attribute.data.value;
257+
_physical_contact_unocc_to_occ_threshold = *(uint8_t *)message->attribute.data.value;
258258
occupancyConfigChanged(ZIGBEE_OCCUPANCY_SENSOR_TYPE_PHYSICAL_CONTACT);
259259
} else {
260260
log_w("Received message ignored. Attribute ID: %d not supported for Occupancy Sensor endpoint", message->attribute.id);
@@ -266,7 +266,20 @@ void ZigbeeOccupancySensor::zbAttributeSet(const esp_zb_zcl_set_attr_value_messa
266266

267267
void ZigbeeOccupancySensor::occupancyConfigChanged(ZigbeeOccupancySensorType sensor_type) {
268268
if (_on_occupancy_config_change) {
269-
_on_occupancy_config_change(sensor_type); //sensor type, delay, delay, threshold
269+
switch (sensor_type) {
270+
case ZIGBEE_OCCUPANCY_SENSOR_TYPE_PIR:
271+
_on_occupancy_config_change(sensor_type, _pir_occ_to_unocc_delay, _pir_unocc_to_occ_delay, _pir_unocc_to_occ_threshold);
272+
break;
273+
case ZIGBEE_OCCUPANCY_SENSOR_TYPE_ULTRASONIC:
274+
_on_occupancy_config_change(sensor_type, _ultrasonic_occ_to_unocc_delay, _ultrasonic_unocc_to_occ_delay, _ultrasonic_unocc_to_occ_threshold);
275+
break;
276+
case ZIGBEE_OCCUPANCY_SENSOR_TYPE_PHYSICAL_CONTACT:
277+
_on_occupancy_config_change(sensor_type, _physical_contact_occ_to_unocc_delay, _physical_contact_unocc_to_occ_delay, _physical_contact_unocc_to_occ_threshold);
278+
break;
279+
default:
280+
log_e("Invalid sensor type for occupancy config change: 0x%x", sensor_type);
281+
break;
282+
}
270283
} else {
271284
log_w("No callback function set for occupancy config change");
272285
}

Diff for: libraries/Zigbee/src/ep/ZigbeeOccupancySensor.h

+15
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ class ZigbeeOccupancySensor : public ZigbeeEP {
8585

8686
void (*_on_occupancy_config_change)(bool);
8787
void occupancyConfigChanged(ZigbeeOccupancySensorType sensor_type);
88+
89+
// PIR sensor configuration
90+
uint16_t _pir_occ_to_unocc_delay;
91+
uint16_t _pir_unocc_to_occ_delay;
92+
uint8_t _pir_unocc_to_occ_threshold;
93+
94+
// Ultrasonic sensor configuration
95+
uint16_t _ultrasonic_occ_to_unocc_delay;
96+
uint16_t _ultrasonic_unocc_to_occ_delay;
97+
uint8_t _ultrasonic_unocc_to_occ_threshold;
98+
99+
// Physical contact sensor configuration
100+
uint16_t _physical_contact_occ_to_unocc_delay;
101+
uint16_t _physical_contact_unocc_to_occ_delay;
102+
uint8_t _physical_contact_unocc_to_occ_threshold;
88103
};
89104

90105
#endif // CONFIG_ZB_ENABLED

0 commit comments

Comments
 (0)