Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions sensor/base/on_off.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import (
type OnOff struct {
*Base `yaml:",inline"`
Pin types.Pin
// Button will be a on/off physical switch somewhere.
// For example a button on the board.
// This will also change the state of this OnOff.
//
// Take an example of a power socket:
// you may want to control it with Home Assistant,
// but you also sure want to control it from the device itself.
ControlButton string `yaml:"control_button"`
}

func (*OnOff) String() string {
Expand Down
2 changes: 1 addition & 1 deletion templates/src/extenders/sensors/on_off.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ if (err != 0) {
}
{{end}}

{{ define "component_constructor_argument_names"}} {{.Sensor.Pin.Label}} {{end}}
{{ define "component_constructor_argument_names"}} {{.Sensor.Pin.Label}} , {{ if not (eq .Sensor.ControlButton "")}} BUTTON_BIT({{.Sensor.ControlButton}}) {{ else }} 0 {{ end }} {{end}}
78 changes: 77 additions & 1 deletion templates/src/types_on_off.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,74 @@
#include <zephyr/drivers/gpio.h>
#include <zephyr/logging/log.h>

#ifdef __cplusplus
extern "C"
{
#endif
#include <zboss_api.h>
#include <zb_zcl_common.h>
#include <zcl/zb_zcl_on_off.h>
#include <zigbee/zigbee_error_handler.h>
#ifdef __cplusplus
}
#endif

#include "types.hpp"
#include "types_on_off.hpp"
#include "types_button_handler.hpp"

LOG_MODULE_REGISTER(on_off, LOG_LEVEL_DBG);

namespace zbhome
{
namespace components
{
bool OnOff::setup()
{
if (m_button != 0)
{
const auto endpoint = getEndpoint();
const auto output_pin = m_pin;

const auto handler = [output_pin, endpoint](uint32_t button_bit, bool newState)
{
// Only run this if button is pressed down, not released.
// Otherwise we will be tied to button state.
if (!newState)
{
return;
}

// Technically we could get this from `dev_ctx`,
// but we don't import `device.hpp`.
// Also, by calling this function we are not tied
// to any specific way the attributes are defined,
// or even if `dev_ctx` will be renamed.
const zb_zcl_attr_t *attr = zb_zcl_get_attr_desc_a(endpoint,
ZB_ZCL_CLUSTER_ID_ON_OFF,
ZB_ZCL_CLUSTER_SERVER_ROLE,
ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID);
bool current_state = *(bool *)attr->data_p;
// This is for readability only.
bool next_state = !current_state;

// This value should be next state, not current.
zb_uint16_t next_state_arg = next_state;
next_state_arg = (endpoint << 1) | next_state_arg;

gpio_pin_set_dt(&output_pin, next_state);

/* Allocate output buffer and set new state. */
zb_ret_t zb_err_code = zb_buf_get_out_delayed_ext(
OnOff::change_state, next_state_arg, 0);
ZB_ERROR_CHECK(zb_err_code);
};

zbhome::addButtonHandler(m_button, std::move(handler));
}

return zbhome::types::Component::setup();
}

void OnOff::zclSetAttrValue(zb_zcl_set_attr_value_param_t *setAttrValueParam)
{
if (setAttrValueParam->cluster_id != ZB_ZCL_CLUSTER_ID_ON_OFF ||
Expand All @@ -21,5 +80,22 @@ namespace zbhome
bool value = (bool)setAttrValueParam->values.data8;
gpio_pin_set_dt(&m_pin, value);
}

void OnOff::change_state(zb_bufid_t bufid, zb_uint16_t data)
{
zb_uint8_t state_val = data & 1;
const zb_uint8_t endpoint = data >> 1;

auto status = zb_zcl_set_attr_val(endpoint,
ZB_ZCL_CLUSTER_ID_ON_OFF,
ZB_ZCL_CLUSTER_SERVER_ROLE,
ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID,
&state_val,
ZB_FALSE);
if (status != 0)
{
LOG_DBG("set attr status: %d", status);
}
}
}
}
6 changes: 5 additions & 1 deletion templates/src/types_on_off.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ namespace zbhome
class OnOff : public zbhome::types::ZCLCommandHandler
{
public:
OnOff(const struct gpio_dt_spec pin) : m_pin(pin) {};
OnOff(const struct gpio_dt_spec pin, const uint32_t button) : m_pin(pin), m_button(button) {};
bool setup() override;
// This function will be called when callback is received with the endpoint
// that this components is for. Otherwise cluster/attribute id's are not
// checked and are up to the implementation to validate.
void zclSetAttrValue(zb_zcl_set_attr_value_param_t *setAttrValueParam);

private:
const struct gpio_dt_spec m_pin = {};
const uint32_t m_button = 0;

static void change_state(zb_bufid_t bufid, zb_uint16_t new_state);
};
}
}