Working zigbee generic, connected to network and HA

This commit is contained in:
Sam
2025-05-16 15:18:20 +10:00
parent 5585dc0222
commit 78ced50673
42 changed files with 2547 additions and 1552 deletions

View File

@@ -192,19 +192,12 @@ static void esp_zb_task(void *pvParameters)
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG(); // Correct for End Device
esp_zb_init(&zb_nwk_cfg);
// --- Existing On/Off Light (Relay) Endpoint Setup ---
esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
// The esp_zb_ep_list_t should be created ONCE and then have both endpoints added to it.
esp_zb_ep_list_t *multi_ep_list = esp_zb_ep_list_create(); // Create a new list for all endpoints
// esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg);
// The esp_zb_on_off_light_ep_create likely returns an ep_list with one endpoint.
// We need to add its contents to our main multi_ep_list or adapt.
// For simplicity, let's assume esp_zb_on_off_light_ep_create gives us the cluster_list for the light.
// A more robust way is to manually create the clusters for the on/off light as well.
// Let's manually create the clusters for the On/Off Light endpoint for clarity
// This replaces esp_zb_on_off_light_ep_create for more control with multiple EPs
esp_zb_attribute_list_t *on_off_basic_cluster = esp_zb_basic_cluster_create(NULL);
esp_zb_attribute_list_t *on_off_identify_cluster = esp_zb_identify_cluster_create(NULL);
esp_zb_on_off_cluster_cfg_t on_off_cfg_for_relay;
@@ -233,14 +226,11 @@ static void esp_zb_task(void *pvParameters)
};
esp_zb_ep_list_add_ep(multi_ep_list, relay_cluster_list, relay_endpoint_config);
ESP_LOGI(TAG, "On/Off Light (Relay) Endpoint created.");
// --- End of On/Off Light (Relay) Endpoint Setup ---
/* --- Create Occupancy Sensor Endpoint --- */
ESP_LOGI(TAG, "Creating Occupancy Sensor Endpoint (%d)...", MY_OCCUPANCY_SENSOR_ENDPOINT);
esp_zb_attribute_list_t *occupancy_basic_cluster = esp_zb_basic_cluster_create(NULL);
// Add manufacturer info to this endpoint's basic cluster too
esp_zb_basic_cluster_add_attr(occupancy_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, mfg_info.manufacturer_name);
esp_zb_basic_cluster_add_attr(occupancy_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, mfg_info.model_identifier);
@@ -250,9 +240,7 @@ static void esp_zb_task(void *pvParameters)
esp_zb_attribute_list_t *occupancy_sensing_cluster = esp_zb_zcl_attr_list_create(ESP_ZB_ZCL_CLUSTER_ID_OCCUPANCY_SENSING);
uint8_t initial_occupancy_value = 0x00; // Unoccupied
esp_zb_occupancy_sensing_cluster_add_attr(occupancy_sensing_cluster, ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_ID, &initial_occupancy_value);
// Optional: Add sensor type attribute
// uint8_t sensor_type = ESP_ZB_ZCL_OCCUPANCY_SENSOR_TYPE_PIR; // Or a combined type if available
// esp_zb_occupancy_sensing_cluster_add_attr(occupancy_sensing_cluster, ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_TYPE_ID, &sensor_type);
esp_zb_cluster_list_t *occupancy_cluster_list_for_ep = esp_zb_zcl_cluster_list_create();
@@ -268,9 +256,6 @@ static void esp_zb_task(void *pvParameters)
};
esp_zb_ep_list_add_ep(multi_ep_list, occupancy_cluster_list_for_ep, occupancy_endpoint_config);
ESP_LOGI(TAG, "Occupancy Sensor Endpoint created.");
/* --- End of Occupancy Sensor Endpoint Creation --- */
// Register the device with ALL endpoints
esp_zb_device_register(multi_ep_list); // Use the list that contains both endpoints
@@ -282,23 +267,13 @@ ESP_LOGI(TAG, "Configuring reporting for Occupancy sensor...");
.cluster_role = ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
.attr_id = ESP_ZB_ZCL_ATTR_OCCUPANCY_SENSING_OCCUPANCY_ID,
.dst.profile_id = ESP_ZB_AF_HA_PROFILE_ID,
// .dst.addr_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT, // Let stack fill this during binding
// .dst.dst_addr_u.addr_short = 0; // Coordinator address, usually set after binding
.u.send_info.min_interval = 1, // Minimum reporting interval (seconds) - report quickly
.u.send_info.max_interval = 300, // Maximum reporting interval (e.g., 5 minutes)
.u.send_info.min_interval = 1,
.u.send_info.max_interval = 300,
.u.send_info.def_min_interval = 1,
.u.send_info.def_max_interval = 300,
// For a binary attribute like occupancy, reportable_change is usually not critical,
// as any change (0 to 1 or 1 to 0) is significant.
// For analog values, delta is important.
// .u.send_info.delta.u8 = 1; // For boolean/bitmap, delta isn't typically used this way.
// The change itself is the report trigger.
.manuf_code = ESP_ZB_ZCL_ATTR_NON_MANUFACTURER_SPECIFIC,
};
// This function configures the local device to send reports.
// The actual sending of reports often relies on a binding being established
// between this device's occupancy cluster and the coordinator (HA).
esp_err_t report_cfg_err = esp_zb_zcl_update_reporting_info(&occupancy_reporting_info);
if (report_cfg_err == ESP_OK) {
ESP_LOGI(TAG, "Successfully configured reporting for Occupancy attribute.");
@@ -308,6 +283,8 @@ ESP_LOGI(TAG, "Configuring reporting for Occupancy sensor...");
esp_zb_core_action_handler_register(zb_action_handler);
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
ESP_LOGI(TAG, "Starting Zigbee stack to join a network (network steering)...");
ESP_ERROR_CHECK(esp_zb_start(false));