feat(zigbee): Add method to set/get/report analog output (#11431)

* feat(zigbee): Add methot to set,get,report analog output

* fix(ci): Update json file for example

* fix(zigbee): Add missing keywords

* ci(pre-commit): Apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
Jan Procházka 2025-06-04 16:40:52 +02:00 committed by GitHub
parent cae66e65a3
commit e3018b6719
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 74 additions and 15 deletions

View file

@ -26,8 +26,8 @@
* Modified by Pat Clay
*/
#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#ifndef ZIGBEE_MODE_ZCZR
#error "Zigbee coordinator/router device mode is not selected in Tools->Zigbee mode"
#endif
#include "Zigbee.h"
@ -70,6 +70,7 @@ void setup() {
zbAnalogDevice.addAnalogOutput();
zbAnalogDevice.setAnalogOutputApplication(ESP_ZB_ZCL_AI_RPM_OTHER);
zbAnalogDevice.setAnalogOutputDescription("Fan Speed (RPM)");
zbAnalogDevice.setAnalogOutputResolution(1);
// If analog output cluster is added, set callback function for analog output change
zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange);
@ -99,8 +100,8 @@ void setup() {
Zigbee.addEndpoint(&zbAnalogPercent);
Serial.println("Starting Zigbee...");
// When all EPs are registered, start Zigbee in End Device mode
if (!Zigbee.begin()) {
// When all EPs are registered, start Zigbee in Router Device mode
if (!Zigbee.begin(ZIGBEE_ROUTER)) {
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
@ -151,6 +152,9 @@ void loop() {
Zigbee.factoryReset();
}
}
// For demonstration purposes, increment the analog output value by 100
zbAnalogDevice.setAnalogOutput(zbAnalogDevice.getAnalogOutput() + 100);
zbAnalogDevice.reportAnalogOutput();
}
delay(100);
}

View file

@ -1,7 +1,6 @@
{
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
"fqbn_append": "PartitionScheme=zigbee_zczr,ZigbeeMode=zczr",
"requires": [
"CONFIG_SOC_IEEE802154_SUPPORTED=y",
"CONFIG_ZB_ENABLED=y"
]
}

View file

@ -143,14 +143,21 @@ setSensorType KEYWORD2
setCarbonDioxide KEYWORD2
# ZigbeeAnalog
addAnalogValue KEYWORD2
addAnalogInput KEYWORD2
addAnalogOutput KEYWORD2
onAnalogOutputChange KEYWORD2
setAnalogValue KEYWORD2
setAnalogInput KEYWORD2
setAnalogOutput KEYWORD2
getAnalogOutput KEYWORD2
reportAnalogInput KEYWORD2
reportAnalogOutput KEYWORD2
setAnalogInputReporting KEYWORD2
setAnalogInputApplication KEYWORD2
setAnalogInputDescription KEYWORD2
setAnalogInputResolution KEYWORD2
setAnalogOutputApplication KEYWORD2
setAnalogOutputDescription KEYWORD2
setAnalogOutputResolution KEYWORD2
# ZigbeeCarbonDioxideSensor
setCarbonDioxide KEYWORD2

View file

@ -129,8 +129,8 @@ bool ZigbeeAnalog::setAnalogOutputApplication(uint32_t application_type) {
void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) {
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT) {
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_SINGLE) {
float analog_output = *(float *)message->attribute.data.value;
analogOutputChanged(analog_output);
_output_state = *(float *)message->attribute.data.value;
analogOutputChanged();
} else {
log_w("Received message ignored. Attribute ID: %d not supported for Analog Output", message->attribute.id);
}
@ -139,9 +139,9 @@ void ZigbeeAnalog::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *mes
}
}
void ZigbeeAnalog::analogOutputChanged(float analog_output) {
void ZigbeeAnalog::analogOutputChanged() {
if (_on_analog_output_change) {
_on_analog_output_change(analog_output);
_on_analog_output_change(_output_state);
} else {
log_w("No callback function set for analog output change");
}
@ -166,6 +166,26 @@ bool ZigbeeAnalog::setAnalogInput(float analog) {
return true;
}
bool ZigbeeAnalog::setAnalogOutput(float analog) {
esp_zb_zcl_status_t ret = ESP_ZB_ZCL_STATUS_SUCCESS;
_output_state = analog;
analogOutputChanged();
log_v("Updating analog output to %.2f", analog);
/* Update analog output */
esp_zb_lock_acquire(portMAX_DELAY);
ret = esp_zb_zcl_set_attribute_val(
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID, &_output_state, false
);
esp_zb_lock_release();
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
log_e("Failed to set analog output: 0x%x: %s", ret, esp_zb_zcl_status_to_name(ret));
return false;
}
return true;
}
bool ZigbeeAnalog::reportAnalogInput() {
/* Send report attributes command */
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
@ -187,6 +207,27 @@ bool ZigbeeAnalog::reportAnalogInput() {
return true;
}
bool ZigbeeAnalog::reportAnalogOutput() {
/* Send report attributes command */
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
report_attr_cmd.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
report_attr_cmd.attributeID = ESP_ZB_ZCL_ATTR_ANALOG_OUTPUT_PRESENT_VALUE_ID;
report_attr_cmd.direction = ESP_ZB_ZCL_CMD_DIRECTION_TO_CLI;
report_attr_cmd.clusterID = ESP_ZB_ZCL_CLUSTER_ID_ANALOG_OUTPUT;
report_attr_cmd.zcl_basic_cmd.src_endpoint = _endpoint;
report_attr_cmd.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC;
esp_zb_lock_acquire(portMAX_DELAY);
esp_err_t ret = esp_zb_zcl_report_attr_cmd_req(&report_attr_cmd);
esp_zb_lock_release();
if (ret != ESP_OK) {
log_e("Failed to send Analog Output report: 0x%x: %s", ret, esp_err_to_name(ret));
return false;
}
log_v("Analog Output report sent");
return true;
}
bool ZigbeeAnalog::setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta) {
esp_zb_zcl_reporting_info_t reporting_info;
memset(&reporting_info, 0, sizeof(esp_zb_zcl_reporting_info_t));

View file

@ -46,11 +46,18 @@ public:
_on_analog_output_change = callback;
}
// Set the analog input value
// Set the Analog Input/Output value
bool setAnalogInput(float analog);
bool setAnalogOutput(float analog);
// Report Analog Input value
// Get the Analog Output value
float getAnalogOutput() {
return _output_state;
}
// Report Analog Input/Output
bool reportAnalogInput();
bool reportAnalogOutput();
// Set reporting for Analog Input
bool setAnalogInputReporting(uint16_t min_interval, uint16_t max_interval, float delta);
@ -59,9 +66,10 @@ private:
void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override;
void (*_on_analog_output_change)(float);
void analogOutputChanged(float analog_output);
void analogOutputChanged();
uint8_t _analog_clusters;
float _output_state;
};
#endif // CONFIG_ZB_ENABLED