[zigbee] Add power_source option to esp32 (#16062)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
luar123
2026-05-11 20:08:52 +02:00
committed by GitHub
parent ee8ca2a3bf
commit e479e8b641
5 changed files with 18 additions and 7 deletions
+2 -3
View File
@@ -87,9 +87,8 @@ CONFIG_SCHEMA = cv.All(
),
cv.requires_component("nrf52"),
),
cv.OnlyWith(CONF_POWER_SOURCE, "nrf52", default="DC_SOURCE"): cv.All(
cv.enum(POWER_SOURCE, upper=True),
cv.requires_component("nrf52"),
cv.Optional(CONF_POWER_SOURCE, default="DC_SOURCE"): cv.enum(
POWER_SOURCE, upper=True
),
cv.Optional(CONF_IEEE802154_VENDOR_OUI): cv.All(
cv.Any(
+9 -3
View File
@@ -153,7 +153,7 @@ void ZigbeeComponent::add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint
this->attribute_list_[{endpoint_id, cluster_id, role}] = attr_list;
}
void ZigbeeComponent::set_basic_cluster(const char *model, const char *manufacturer) {
void ZigbeeComponent::set_basic_cluster(const char *model, const char *manufacturer, uint8_t power_source) {
char date_buf[16];
time_t time_val = App.get_build_time();
struct tm *timeinfo = localtime(&time_val);
@@ -162,13 +162,14 @@ void ZigbeeComponent::set_basic_cluster(const char *model, const char *manufactu
.model = get_zcl_string(model, 31),
.manufacturer = get_zcl_string(manufacturer, 31),
.date = get_zcl_string(date_buf, 15),
.power_source = power_source,
};
}
esp_zb_attribute_list_t *ZigbeeComponent::create_basic_cluster_() {
esp_zb_basic_cluster_cfg_t basic_cluster_cfg = {
.zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE,
.power_source = 0,
.power_source = this->basic_cluster_data_.power_source,
};
esp_zb_attribute_list_t *attr_list = esp_zb_basic_cluster_create(&basic_cluster_cfg);
esp_zb_basic_cluster_add_attr(attr_list, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID,
@@ -192,7 +193,12 @@ static void esp_zb_task_(void *pvParameters) {
ESP_LOGE(TAG, "Could not setup Zigbee");
vTaskDelete(NULL);
}
esp_zb_set_node_descriptor_power_source(1);
if (global_zigbee->is_battery_powered()) {
ESP_LOGD(TAG, "Battery powered!");
esp_zb_set_node_descriptor_power_source(0);
} else {
esp_zb_set_node_descriptor_power_source(1);
}
esp_zb_stack_main_loop();
}
+3 -1
View File
@@ -43,7 +43,7 @@ class ZigbeeComponent : public Component {
void dump_config() override;
esp_err_t create_endpoint(uint8_t endpoint_id, zb_ha_standard_devs_e device_id,
esp_zb_cluster_list_t *esp_zb_cluster_list);
void set_basic_cluster(const char *model, const char *manufacturer);
void set_basic_cluster(const char *model, const char *manufacturer, uint8_t power_source);
void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role);
void create_default_cluster(uint8_t endpoint_id, zb_ha_standard_devs_e device_id);
@@ -62,6 +62,7 @@ class ZigbeeComponent : public Component {
template<typename F> void add_on_join_callback(F &&cb) { this->join_cb_.add(std::forward<F>(cb)); }
bool is_battery_powered() { return this->basic_cluster_data_.power_source == ESP_ZB_ZCL_BASIC_POWER_SOURCE_BATTERY; }
bool is_started() { return this->started; }
bool is_connected() { return this->connected_; }
std::atomic<bool> started = false;
@@ -73,6 +74,7 @@ class ZigbeeComponent : public Component {
uint8_t *model;
uint8_t *manufacturer;
uint8_t *date;
uint8_t power_source;
} basic_cluster_data_;
bool connected_ = false;
#ifdef ZB_ED_ROLE
@@ -36,9 +36,11 @@ from .const import (
ANALOG_INPUT_APPTYPE,
BACNET_UNIT_NO_UNITS,
BACNET_UNITS,
CONF_POWER_SOURCE,
CONF_REPORT,
CONF_ROUTER,
KEY_ZIGBEE,
POWER_SOURCE,
REPORT,
ZigbeeAttribute,
)
@@ -320,6 +322,7 @@ async def esp32_to_code(config: ConfigType) -> "MockObj":
var.set_basic_cluster(
config[CONF_MODEL],
"esphome",
cg.RawExpression(POWER_SOURCE[config[CONF_POWER_SOURCE]]),
)
)
for ep in ep_list:
@@ -12,6 +12,7 @@ binary_sensor:
zigbee:
model: zigbee_test
router: true
power_source: MAINS_SINGLE_PHASE
on_join:
then:
- logger.log: "Joined network"