Skip to content

Commit 2bee085

Browse files
committed
feat(Zigbee): Add Zigbee Dimmable light endpoint class
Add a endpoint type class for a dimmable light. Based on a copy of color dimmable light.
1 parent fb6e977 commit 2bee085

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

Diff for: libraries/Zigbee/src/Zigbee.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// Endpoints
1010
#include "ep/ZigbeeLight.h"
1111
#include "ep/ZigbeeSwitch.h"
12+
#include "ep/ZigbeeDimmableLight.h"
1213
#include "ep/ZigbeeColorDimmableLight.h"
1314
#include "ep/ZigbeeColorDimmerSwitch.h"
1415
#include "ep/ZigbeeTempSensor.h"

Diff for: libraries/Zigbee/src/ep/ZigbeeDimmableLight.cpp

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

Diff for: libraries/Zigbee/src/ep/ZigbeeDimmableLight.h

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Class of Zigbee On/Off Light endpoint inherited from common EP class */
2+
3+
#pragma once
4+
5+
#include "soc/soc_caps.h"
6+
#if SOC_IEEE802154_SUPPORTED
7+
8+
#include "ZigbeeEP.h"
9+
#include "ha/esp_zigbee_ha_standard.h"
10+
11+
/**
12+
* @brief Zigbee HA standard dimmable light device clusters.
13+
* Added here as not supported by ESP Zigbee library.
14+
*
15+
*
16+
*/
17+
typedef struct esp_zb_dimmable_light_cfg_s
18+
{
19+
esp_zb_basic_cluster_cfg_t basic_cfg; /*!< Basic cluster configuration, @ref esp_zb_basic_cluster_cfg_s */
20+
esp_zb_identify_cluster_cfg_t identify_cfg; /*!< Identify cluster configuration, @ref esp_zb_identify_cluster_cfg_s */
21+
esp_zb_groups_cluster_cfg_t groups_cfg; /*!< Groups cluster configuration, @ref esp_zb_groups_cluster_cfg_s */
22+
esp_zb_scenes_cluster_cfg_t scenes_cfg; /*!< Scenes cluster configuration, @ref esp_zb_scenes_cluster_cfg_s */
23+
esp_zb_on_off_cluster_cfg_t on_off_cfg; /*!< On off cluster configuration, @ref esp_zb_on_off_cluster_cfg_s */
24+
esp_zb_level_cluster_cfg_t level_cfg; /*!< Level cluster configuration, @ref esp_zb_level_cluster_cfg_s */
25+
} esp_zb_dimmable_light_cfg_t;
26+
27+
/**
28+
* @brief Zigbee HA standard dimmable light device default config value.
29+
* Added here as not supported by ESP Zigbee library.
30+
*
31+
*/
32+
#define ESP_ZB_DEFAULT_DIMMABLE_LIGHT_CONFIG() \
33+
{ \
34+
.basic_cfg = \
35+
{ \
36+
.zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE, \
37+
.power_source = ESP_ZB_ZCL_BASIC_POWER_SOURCE_DEFAULT_VALUE, \
38+
}, \
39+
.identify_cfg = \
40+
{ \
41+
.identify_time = ESP_ZB_ZCL_IDENTIFY_IDENTIFY_TIME_DEFAULT_VALUE, \
42+
}, \
43+
.groups_cfg = \
44+
{ \
45+
.groups_name_support_id = ESP_ZB_ZCL_GROUPS_NAME_SUPPORT_DEFAULT_VALUE, \
46+
}, \
47+
.scenes_cfg = \
48+
{ \
49+
.scenes_count = ESP_ZB_ZCL_SCENES_SCENE_COUNT_DEFAULT_VALUE, \
50+
.current_scene = ESP_ZB_ZCL_SCENES_CURRENT_SCENE_DEFAULT_VALUE, \
51+
.current_group = ESP_ZB_ZCL_SCENES_CURRENT_GROUP_DEFAULT_VALUE, \
52+
.scene_valid = ESP_ZB_ZCL_SCENES_SCENE_VALID_DEFAULT_VALUE, \
53+
.name_support = ESP_ZB_ZCL_SCENES_NAME_SUPPORT_DEFAULT_VALUE, \
54+
}, \
55+
.on_off_cfg = \
56+
{ \
57+
.on_off = ESP_ZB_ZCL_ON_OFF_ON_OFF_DEFAULT_VALUE, \
58+
}, \
59+
.level_cfg = \
60+
{ \
61+
.current_level = ESP_ZB_ZCL_LEVEL_CONTROL_CURRENT_LEVEL_DEFAULT_VALUE, \
62+
}, \
63+
}
64+
65+
class ZigbeeDimmableLight : public ZigbeeEP
66+
{
67+
public:
68+
ZigbeeDimmableLight(uint8_t endpoint);
69+
~ZigbeeDimmableLight();
70+
71+
void onLightChange(void (*callback)(bool, uint8_t))
72+
{
73+
_on_light_change = callback;
74+
}
75+
void restoreLight()
76+
{
77+
lightChanged();
78+
}
79+
80+
private:
81+
void zbAttributeSet(const esp_zb_zcl_set_attr_value_message_t *message) override;
82+
83+
void lightChanged();
84+
// callback function to be called on light change (State, Level)
85+
void (*_on_light_change)(bool, uint8_t);
86+
87+
/**
88+
* @brief Create a standard HA dimmable light cluster list.
89+
* Added here as not supported by ESP Zigbee library.
90+
*
91+
* @note This contains basic, identify, groups, scenes, on-off, level, as server side.
92+
* @param[in] light_cfg Configuration parameters for this cluster lists defined by @ref esp_zb_dimmable_light_cfg_t
93+
*
94+
* @return Pointer to cluster list @ref esp_zb_cluster_list_s
95+
*
96+
*/
97+
esp_zb_cluster_list_t *esp_zb_dimmable_light_clusters_create(esp_zb_dimmable_light_cfg_t *light_cfg);
98+
99+
bool _current_state;
100+
uint8_t _current_level;
101+
};
102+
103+
#endif // SOC_IEEE802154_SUPPORTED

0 commit comments

Comments
 (0)