Skip to content

feat(zigbee): Add battery voltage attribute support #11210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ void setup() {
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C)
zbTempSensor.setTolerance(1);

// Set power source to battery and set battery percentage to measured value (now 100% for demonstration)
// The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) anytime
zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100);
// Set power source to battery, battery percentage and battery voltage (now 100% and 3.5V for demonstration)
// The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) or zbTempSensor.setBatteryVoltage(voltage) anytime after Zigbee.begin()
zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100, 35);

// Add humidity cluster to the temperature sensor device with min, max and tolerance values
zbTempSensor.addHumiditySensor(0, 100, 1);
Expand Down
22 changes: 21 additions & 1 deletion libraries/Zigbee/src/ZigbeeEP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool ZigbeeEP::setManufacturerAndModel(const char *name, const char *model) {
return ret_name == ESP_OK && ret_model == ESP_OK;
}

bool ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t battery_percentage) {
bool ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t battery_percentage, uint8_t battery_voltage) {
esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
esp_err_t ret = esp_zb_cluster_update_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_POWER_SOURCE_ID, (void *)&power_source);
if (ret != ESP_OK) {
Expand All @@ -88,6 +88,11 @@ bool ZigbeeEP::setPowerSource(zb_power_source_t power_source, uint8_t battery_pe
log_e("Failed to add battery percentage attribute: 0x%x: %s", ret, esp_err_to_name(ret));
return false;
}
ret = esp_zb_power_config_cluster_add_attr(power_config_cluster, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_VOLTAGE_ID, (void *)&battery_voltage);
if (ret != ESP_OK) {
log_e("Failed to add battery voltage attribute: 0x%x: %s", ret, esp_err_to_name(ret));
return false;
}
ret = esp_zb_cluster_list_add_power_config_cluster(_cluster_list, power_config_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
if (ret != ESP_OK) {
log_e("Failed to add power config cluster: 0x%x: %s", ret, esp_err_to_name(ret));
Expand Down Expand Up @@ -120,6 +125,21 @@ bool ZigbeeEP::setBatteryPercentage(uint8_t percentage) {
return true;
}

bool ZigbeeEP::setBatteryVoltage(uint8_t voltage) {
esp_zb_zcl_status_t ret = ESP_ZB_ZCL_STATUS_SUCCESS;
esp_zb_lock_acquire(portMAX_DELAY);
ret = esp_zb_zcl_set_attribute_val(
_endpoint, ESP_ZB_ZCL_CLUSTER_ID_POWER_CONFIG, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_POWER_CONFIG_BATTERY_VOLTAGE_ID, &voltage, false
);
esp_zb_lock_release();
if (ret != ESP_ZB_ZCL_STATUS_SUCCESS) {
log_e("Failed to set battery voltage: 0x%x: %s", ret, esp_zb_zcl_status_to_name(ret));
return false;
}
log_v("Battery voltage updated");
return true;
}

bool ZigbeeEP::reportBatteryPercentage() {
/* Send report attributes command */
esp_zb_zcl_report_attr_cmd_t report_attr_cmd;
Expand Down
7 changes: 4 additions & 3 deletions libraries/Zigbee/src/ZigbeeEP.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ class ZigbeeEP {
char *readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_addr_t ieee_addr);

// Set Power source and battery percentage for battery powered devices
bool setPowerSource(zb_power_source_t power_source, uint8_t percentage = 255);
bool setBatteryPercentage(uint8_t percentage);
bool reportBatteryPercentage();
bool setPowerSource(zb_power_source_t power_source, uint8_t percentage = 0xff, uint8_t voltage = 0xff); // voltage in 100mV
bool setBatteryPercentage(uint8_t percentage); // 0-100 %
bool setBatteryVoltage(uint8_t voltage); // voltage in 100mV (example value 35 for 3.5V)
bool reportBatteryPercentage(); // battery voltage is not reportable attribute

// Set time
bool addTimeCluster(tm time = {}, int32_t gmt_offset = 0); // gmt offset in seconds
Expand Down
Loading