diff --git a/components/zigbee/__init__.py b/components/zigbee/__init__.py index 7e83761..dbd55b0 100644 --- a/components/zigbee/__init__.py +++ b/components/zigbee/__init__.py @@ -51,6 +51,8 @@ CONF_SCALE = "scale" CONF_ATTRIBUTE_ID = "attribute_id" CONF_ZIGBEE_ID = "zigbee_id" +CONF_TRUST_CENTER_KEY = "trust_center_key" +CONF_DEVICE_VERSION = "device_version" zigbee_ns = cg.esphome_ns.namespace("zigbee") ZigBeeComponent = zigbee_ns.class_("ZigBeeComponent", cg.Component) @@ -172,6 +174,8 @@ def final_validate(config): cv.Optional(CONF_POWER_SUPPLY, default=0): cv.int_, # make enum cv.Optional(CONF_VERSION, default=0): cv.int_, cv.Optional(CONF_AREA, default=0): cv.int_, # make enum + cv.Optional(CONF_TRUST_CENTER_KEY): cv.bind_key, + cv.Optional(CONF_DEVICE_VERSION, default=0): cv.int_, cv.Required(CONF_ENDPOINTS): cv.ensure_list( cv.Schema( { @@ -304,6 +308,10 @@ async def to_code(config): ) if CONF_IDENT_TIME in config: cg.add(var.set_ident_time(config[CONF_IDENT_TIME])) + if CONF_TRUST_CENTER_KEY in config: + cg.add_define("CONF_TRUST_CENTER_KEY", config[CONF_TRUST_CENTER_KEY]) + if CONF_DEVICE_VERSION in config: + cg.add_define("CONF_DEVICE_VERSION", config[CONF_DEVICE_VERSION]) for ep in config[CONF_ENDPOINTS]: cg.add( var.create_default_cluster(ep[CONF_NUM], DEVICE_ID[ep[CONF_DEVICE_TYPE]]) diff --git a/components/zigbee/zigbee.cpp b/components/zigbee/zigbee.cpp index 4f23566..157c88e 100644 --- a/components/zigbee/zigbee.cpp +++ b/components/zigbee/zigbee.cpp @@ -381,7 +381,7 @@ esp_err_t ZigBeeComponent::create_endpoint(uint8_t endpoint_id, esp_zb_ha_standa esp_zb_endpoint_config_t endpoint_config = {.endpoint = endpoint_id, .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID, .app_device_id = device_id, - .app_device_version = 0}; + .app_device_version = CONF_DEVICE_VERSION}; return esp_zb_ep_list_add_ep(this->esp_zb_ep_list_, esp_zb_cluster_list, endpoint_config); } @@ -425,6 +425,22 @@ void ZigBeeComponent::setup() { zb_nwk_cfg.nwk_cfg.zed_cfg = zb_zed_cfg; esp_zb_init(&zb_nwk_cfg); +#ifdef CONF_TRUST_CENTER_KEY + uint8_t trustkey_[16]; + memset(trustkey_, 0, 16); + + char temp[3] = {0}; + for (int i = 0; i < 16; i++) { + strncpy(temp, &(CONF_TRUST_CENTER_KEY[i * 2]), 2); + trustkey_[i] = std::strtoul(temp, nullptr, 16); + } + + ESP_LOGE(TAG, "Zigbee trust center key: %s", format_hex_pretty(trustkey_, 16).c_str()); + + esp_zb_enable_joining_to_distributed(true); + esp_zb_secur_TC_standard_distributed_key_set(trustkey_); +#endif + esp_err_t ret; // clusters diff --git a/example_esp32c6_hue.yaml b/example_esp32c6_hue.yaml new file mode 100644 index 0000000..f9aa8cb --- /dev/null +++ b/example_esp32c6_hue.yaml @@ -0,0 +1,113 @@ +esphome: + name: zb-example-c6 + +external_components: + - source: components + components: [zigbee] + +esp32: + board: esp32-c6-devkitc-1 + #flash_size: 4MB + partitions: partitions_zb.csv + framework: + type: esp-idf + #sdkconfig_options: + #CONFIG_ESPTOOLPY_FLASHSIZE_4MB: y + +# Enable logging +logger: + hardware_uart: UART0 + +globals: + - id: color_x + type: float + restore_value: no + initial_value: '0' + - id: color_y + type: float + restore_value: no + initial_value: '0' + +zigbee: + id: "zb" + trust_center_key: "8142xxxxxxxxxxxxxxxxxxxxxxxxxxB8" # replace with real trustcenter key + device_version: 1 + endpoints: + - num: 1 + device_type: COLOR_DIMMABLE_LIGHT + clusters: + - id: ON_OFF + attributes: + - attribute_id: 0 + type: bool + on_value: + then: + - light.control: + id: light_1 + state: !lambda "return (bool)x;" + - attribute_id: 0x4000 # global_scene_control + type: bool + - attribute_id: 0x4001 # on_off_time + type: U16 + - id: LEVEL_CONTROL + attributes: + - attribute_id: 0 + type: U8 + value: 255 + on_value: + then: + - light.control: + id: light_1 + brightness: !lambda "return ((float)x)/255;" + - id: COLOR_CONTROL + attributes: + - attribute_id: 3 + type: U16 + on_value: + then: + - lambda: id(color_x) = (float)x/65536; + - light.control: + id: light_1 + red: !lambda "return zigbee::get_r_from_xy(id(color_x), id(color_y));" + green: !lambda "return zigbee::get_g_from_xy(id(color_x), id(color_y));" + blue: !lambda "return zigbee::get_b_from_xy(id(color_x), id(color_y));" + - attribute_id: 4 + type: U16 + on_value: + then: + - lambda: id(color_y) = (float)x/65536; + - light.control: + id: light_1 + red: !lambda "return zigbee::get_r_from_xy(id(color_x), id(color_y));" + green: !lambda "return zigbee::get_g_from_xy(id(color_x), id(color_y));" + blue: !lambda "return zigbee::get_b_from_xy(id(color_x), id(color_y));" + on_join: + then: + - logger.log: "Joined network" + +light: + - platform: esp32_rmt_led_strip + rgb_order: GRB + pin: 8 + num_leds: 1 + rmt_channel: 0 + chipset: ws2812 + id: light_1 + +binary_sensor: + - platform: gpio + pin: + number: 9 + mode: + input: true + pullup: true + inverted: true + id: button_1 + on_press: + then: + - zigbee.report: zb + on_click: + min_length: 5s + max_length: 20s + then: + - zigbee.reset: zb