ESPHome 2026.1.4
Loading...
Searching...
No Matches
zigbee_switch_zephyr.cpp
Go to the documentation of this file.
2#if defined(USE_ZIGBEE) && defined(USE_NRF52) && defined(USE_SWITCH)
3#include "esphome/core/log.h"
4#include <zephyr/settings/settings.h>
5
6extern "C" {
7#include <zboss_api.h>
8#include <zboss_api_addons.h>
9#include <zb_nrf_platform.h>
10#include <zigbee/zigbee_app_utils.h>
11#include <zb_error_to_string.h>
12}
13
14namespace esphome::zigbee {
15
16static const char *const TAG = "zigbee_on_off.switch";
17
19 ESP_LOGCONFIG(TAG,
20 "Zigbee Switch\n"
21 " Endpoint: %d, present_value %u",
23}
24
26 this->parent_->add_callback(this->endpoint_, [this](zb_bufid_t bufid) { this->zcl_device_cb_(bufid); });
27 this->switch_->add_on_state_callback([this](bool state) {
28 this->cluster_attributes_->present_value = state ? ZB_TRUE : ZB_FALSE;
29 ESP_LOGD(TAG, "Set attribute endpoint: %d, present_value %d", this->endpoint_,
31 ZB_ZCL_SET_ATTRIBUTE(this->endpoint_, ZB_ZCL_CLUSTER_ID_BINARY_OUTPUT, ZB_ZCL_CLUSTER_SERVER_ROLE,
33 ZB_FALSE);
34 this->parent_->flush();
35 });
36}
37
38void ZigbeeSwitch::zcl_device_cb_(zb_bufid_t bufid) {
39 zb_zcl_device_callback_param_t *p_device_cb_param = ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t);
40 zb_zcl_device_callback_id_t device_cb_id = p_device_cb_param->device_cb_id;
41 zb_uint16_t cluster_id = p_device_cb_param->cb_param.set_attr_value_param.cluster_id;
42 zb_uint16_t attr_id = p_device_cb_param->cb_param.set_attr_value_param.attr_id;
43
44 p_device_cb_param->status = RET_OK;
45
46 switch (device_cb_id) {
47 /* ZCL set attribute value */
48 case ZB_ZCL_SET_ATTR_VALUE_CB_ID:
49 if (cluster_id == ZB_ZCL_CLUSTER_ID_BINARY_OUTPUT) {
50 uint8_t value = p_device_cb_param->cb_param.set_attr_value_param.values.data8;
51 ESP_LOGI(TAG, "Binary output attribute setting to %hd", value);
53 this->defer([this, value]() {
54 this->cluster_attributes_->present_value = value ? ZB_TRUE : ZB_FALSE;
55 this->switch_->publish_state(value);
56 });
57 }
58 } else {
59 /* other clusters attribute handled here */
60 ESP_LOGI(TAG, "Unhandled cluster attribute id: %d", cluster_id);
61 }
62 break;
63 default:
64 p_device_cb_param->status = RET_ERROR;
65 break;
66 }
67
68 ESP_LOGD(TAG, "%s status: %hd", __func__, p_device_cb_param->status);
69}
70
72
73static zb_ret_t check_value_binary_output_server(zb_uint16_t attr_id, zb_uint8_t endpoint,
74 zb_uint8_t *value) { // NOLINT(readability-non-const-parameter)
75 zb_ret_t ret = RET_OK;
76 ZVUNUSED(endpoint);
77
78 switch (attr_id) {
81 ret = ZB_ZCL_CHECK_BOOL_VALUE(*value) ? RET_OK : RET_ERROR;
82 break;
83
86 ret = RET_ERROR;
87 }
88 break;
89
90 default:
91 break;
92 }
93
94 return ret;
95}
96
97} // namespace esphome::zigbee
98
100 zb_zcl_add_cluster_handlers(ZB_ZCL_CLUSTER_ID_BINARY_OUTPUT, ZB_ZCL_CLUSTER_SERVER_ROLE,
101 esphome::zigbee::check_value_binary_output_server,
102 (zb_zcl_cluster_write_attr_hook_t) NULL, (zb_zcl_cluster_handler_t) NULL);
103}
104
106 zb_zcl_add_cluster_handlers(ZB_ZCL_CLUSTER_ID_BINARY_OUTPUT, ZB_ZCL_CLUSTER_CLIENT_ROLE,
107 (zb_zcl_cluster_check_value_t) NULL, (zb_zcl_cluster_write_attr_hook_t) NULL,
108 (zb_zcl_cluster_handler_t) NULL);
109}
110
111#endif
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:492
void add_on_state_callback(std::function< void(bool)> &&callback)
Set callback for state changes.
Definition switch.cpp:73
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:57
void add_callback(zb_uint8_t endpoint, std::function< void(zb_bufid_t bufid)> &&cb)
void zcl_device_cb_(zb_bufid_t bufid)
bool state
Definition fan.h:0
const char *const TAG
Definition spi.cpp:7
const zb_uint8_t ZB_ZCL_BINARY_OUTPUT_STATUS_FLAG_MAX_VALUE
void zb_zcl_binary_output_init_client()
void zb_zcl_binary_output_init_server()
@ ZB_ZCL_ATTR_BINARY_OUTPUT_OUT_OF_SERVICE_ID
@ ZB_ZCL_ATTR_BINARY_OUTPUT_STATUS_FLAG_ID
@ ZB_ZCL_ATTR_BINARY_OUTPUT_PRESENT_VALUE_ID