Skip to content

Instantly share code, notes, and snippets.

@nayelyzarazua-bluetrail
Created April 7, 2022 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayelyzarazua-bluetrail/79ab9f092a2b57bf5bf5e20e444f639c to your computer and use it in GitHub Desktop.
Save nayelyzarazua-bluetrail/79ab9f092a2b57bf5bf5e20e444f639c to your computer and use it in GitHub Desktop.
WriteAttributeCommand-Zigbee
local read_attribute = require "st.zigbee.zcl.global_commands.read_attribute"
local write_attribute = require "st.zigbee.zcl.global_commands.write_attribute"
local messages = require "st.zigbee.messages"
local zb_const = require "st.zigbee.constants"
local zcl_messages = require "st.zigbee.zcl"
local data_types = require "st.zigbee.data_types"
local messageHandler = {}
messageHandler.build_zcl_message = function(device,cluster_id,zclh,body_message,mfg_specific_code)
local addrh = messages.AddressHeader(
zb_const.HUB.ADDR,
zb_const.HUB.ENDPOINT,
device:get_short_address(),
device:get_endpoint(cluster_id),
zb_const.HA_PROFILE_ID,
cluster_id
)
local message_body = zcl_messages.ZclMessageBody({
zcl_header = zclh,
zcl_body = body_message
})
local txMessage = messages.ZigbeeMessageTx({
address_header = addrh,
body = message_body
})
--Add the manufacturer info if exists
if mfg_specific_code then
txMessage.body.zcl_header.frame_ctrl:set_mfg_specific()
txMessage.body.zcl_header.mfg_code = data_types.validate_or_build_type(mfg_specific_code, data_types.Uint16, "mfg_code")
end
device:send(txMessage)
end
--Build Tx messages to read attributes
messageHandler.build_read_tx_message = function(device, cluster_id, body_message, mfg_specific_code)
local zclh = zcl_messages.ZclHeader({
cmd = data_types.ZCLCommandId(read_attribute.ReadAttribute.ID)
})
messageHandler.build_zcl_message(device,cluster_id,zclh,body_message,mfg_specific_code)
end
messageHandler.build_write_tx_message = function(device, cluster_id, attr_id, mfg_specific_code,attr_type, attr_value)
local zclh = zcl_messages.ZclHeader({
cmd = data_types.ZCLCommandId(write_attribute.WriteAttribute.ID)
})
local cie_attr_write = write_attribute.WriteAttributeAttributeRecord(
data_types.AttributeId(attr_id),
data_types.ZigbeeDataType(data_types[attr_type].ID),
data_types[attr_type](attr_value)
)
local write_body = write_attribute.WriteAttribute({ cie_attr_write })
messageHandler.build_zcl_message(device,cluster_id,zclh,write_body,mfg_specific_code)
end
return messageHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment