|
| 1 | + |
| 2 | +#include "ZigbeeDimmableLight.h" |
| 3 | +#if SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED |
| 4 | + |
| 5 | +#include "esp_zigbee_cluster.h" |
| 6 | + |
| 7 | +ZigbeeDimmableLight::ZigbeeDimmableLight(uint8_t endpoint) : ZigbeeEP(endpoint) { |
| 8 | + _device_id = ESP_ZB_HA_DIMMABLE_LIGHT_DEVICE_ID; |
| 9 | + |
| 10 | + zigbee_dimmable_light_cfg_t light_cfg = ZIGBEE_DEFAULT_DIMMABLE_LIGHT_CONFIG(); |
| 11 | + _cluster_list = zigbee_dimmable_light_clusters_create(&light_cfg); |
| 12 | + |
| 13 | + _ep_config = {.endpoint = _endpoint, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = ESP_ZB_HA_DIMMABLE_LIGHT_DEVICE_ID, .app_device_version = 0}; |
| 14 | + |
| 15 | + // set default values |
| 16 | + _current_state = false; |
| 17 | + _current_level = 255; |
| 18 | +} |
| 19 | + |
| 20 | +// set attribute method -> method overridden in child class |
| 21 | +void ZigbeeDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) { |
| 22 | + // check the data and call right method |
| 23 | + if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF) { |
| 24 | + if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL) { |
| 25 | + if (_current_state != *(bool *)message->attribute.data.value) { |
| 26 | + _current_state = *(bool *)message->attribute.data.value; |
| 27 | + lightChanged(); |
| 28 | + } |
| 29 | + return; |
| 30 | + } else { |
| 31 | + log_w("Received message ignored. Attribute ID: %d not supported for On/Off Light", message->attribute.id); |
| 32 | + } |
| 33 | + } else if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL) { |
| 34 | + if (message->attribute.id == ESP_ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U8) { |
| 35 | + if (_current_level != *(uint8_t *)message->attribute.data.value) { |
| 36 | + _current_level = *(uint8_t *)message->attribute.data.value; |
| 37 | + lightChanged(); |
| 38 | + } |
| 39 | + return; |
| 40 | + } else { |
| 41 | + log_w("Received message ignored. Attribute ID: %d not supported for Level Control", message->attribute.id); |
| 42 | + // TODO: implement more attributes -> includes/zcl/esp_zigbee_zcl_level.h |
| 43 | + } |
| 44 | + } else { |
| 45 | + log_w("Received message ignored. Cluster ID: %d not supported for dimmable Light", message->info.cluster); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +void ZigbeeDimmableLight::lightChanged() { |
| 50 | + if (_on_light_change) { |
| 51 | + _on_light_change(_current_state, _current_level); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void ZigbeeDimmableLight::setLight(bool state, uint8_t level) { |
| 56 | + // Update all attributes |
| 57 | + _current_state = state; |
| 58 | + _current_level = level; |
| 59 | + lightChanged(); |
| 60 | + |
| 61 | + log_v("Updating on/off light state to %d", state); |
| 62 | + /* Update light clusters */ |
| 63 | + esp_zb_lock_acquire(portMAX_DELAY); |
| 64 | + // set on/off state |
| 65 | + esp_zb_zcl_set_attribute_val( |
| 66 | + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_ON_OFF, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID, &_current_state, false |
| 67 | + ); |
| 68 | + // set level |
| 69 | + esp_zb_zcl_set_attribute_val( |
| 70 | + _endpoint, ESP_ZB_ZCL_CLUSTER_ID_LEVEL_CONTROL, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE, ESP_ZB_ZCL_ATTR_LEVEL_CONTROL_CURRENT_LEVEL_ID, &_current_level, false |
| 71 | + ); |
| 72 | + esp_zb_lock_release(); |
| 73 | +} |
| 74 | + |
| 75 | +void ZigbeeDimmableLight::setLightState(bool state) { |
| 76 | + setLight(state, _current_level); |
| 77 | +} |
| 78 | + |
| 79 | +void ZigbeeDimmableLight::setLightLevel(uint8_t level) { |
| 80 | + setLight(_current_state, level); |
| 81 | +} |
| 82 | + |
| 83 | +esp_zb_cluster_list_t *ZigbeeDimmableLight::zigbee_dimmable_light_clusters_create(zigbee_dimmable_light_cfg_t *light_cfg) { |
| 84 | + esp_zb_attribute_list_t *esp_zb_basic_cluster = esp_zb_basic_cluster_create(&light_cfg->basic_cfg); |
| 85 | + esp_zb_attribute_list_t *esp_zb_identify_cluster = esp_zb_identify_cluster_create(&light_cfg->identify_cfg); |
| 86 | + esp_zb_attribute_list_t *esp_zb_groups_cluster = esp_zb_groups_cluster_create(&light_cfg->groups_cfg); |
| 87 | + esp_zb_attribute_list_t *esp_zb_scenes_cluster = esp_zb_scenes_cluster_create(&light_cfg->scenes_cfg); |
| 88 | + esp_zb_attribute_list_t *esp_zb_on_off_cluster = esp_zb_on_off_cluster_create(&light_cfg->on_off_cfg); |
| 89 | + esp_zb_attribute_list_t *esp_zb_level_cluster = esp_zb_level_cluster_create(&light_cfg->level_cfg); |
| 90 | + |
| 91 | + // ------------------------------ Create cluster list ------------------------------ |
| 92 | + esp_zb_cluster_list_t *esp_zb_cluster_list = esp_zb_zcl_cluster_list_create(); |
| 93 | + esp_zb_cluster_list_add_basic_cluster(esp_zb_cluster_list, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 94 | + esp_zb_cluster_list_add_identify_cluster(esp_zb_cluster_list, esp_zb_identify_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 95 | + esp_zb_cluster_list_add_groups_cluster(esp_zb_cluster_list, esp_zb_groups_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 96 | + esp_zb_cluster_list_add_scenes_cluster(esp_zb_cluster_list, esp_zb_scenes_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 97 | + esp_zb_cluster_list_add_on_off_cluster(esp_zb_cluster_list, esp_zb_on_off_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 98 | + esp_zb_cluster_list_add_level_cluster(esp_zb_cluster_list, esp_zb_level_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE); |
| 99 | + |
| 100 | + return esp_zb_cluster_list; |
| 101 | +} |
| 102 | + |
| 103 | +#endif // SOC_IEEE802154_SUPPORTED |
0 commit comments