-
Notifications
You must be signed in to change notification settings - Fork 1
/
fromzigbee.go
112 lines (97 loc) · 3.16 KB
/
fromzigbee.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package deviceadapters
import (
"fmt"
"log"
"github.com/davecgh/go-spew/spew"
"github.com/function61/hautomo/pkg/ezstack"
"github.com/function61/hautomo/pkg/ezstack/ezhub/hubtypes"
"github.com/function61/hautomo/pkg/ezstack/zcl"
"github.com/function61/hautomo/pkg/ezstack/zcl/cluster"
)
// updates attributes from ReportAttributesCommand/ZoneStatusChangeNotificationCommand/etc.
func ZclIncomingMessageToAttributes(
message *zcl.ZclIncomingMessage,
actx *hubtypes.AttrsCtx,
device *ezstack.Device,
) error {
adapter := AdapterForModel(device.Model)
switch cmd := message.Data.Command.(type) {
case *cluster.DefaultResponseCommand:
return nil // no-op, because this was already synchronously handled
case *cluster.ReportAttributesCommand:
// AttributeReports contains attributes a Zigbee sensor wants to report to us,
// e.g. Temperature=21.3 °C, Humidity=40 %
for _, report := range cmd.AttributeReports {
// this will copy the reported attribute value to device current attributes, along
// with the current timestamp
if err := AttributeReportToAttributes(
report,
message.ClusterID,
device,
adapter,
actx,
); err != nil {
return err
}
}
return nil
default:
if err := adapter.HandleCommand(cmd, actx); err != nil {
if err == errUnhandledCommand {
return fmt.Errorf("unsupported command; got %s", spew.Sdump(message.Data.Command))
} else {
return err
}
}
return nil
}
}
// parses Zigbee AttributeReport message into structured sane attributes, taking into account
// manufacturer-specific "peculiarities"...
func AttributeReportToAttributes(
report *cluster.AttributeReport,
clusterId cluster.ClusterId,
dev *ezstack.Device,
adapter Adapter,
actx *hubtypes.AttrsCtx,
) error {
rxAttributeId := cluster.AttributeId(report.AttributeID)
attrReport := report.Attribute // shorthand
clDefinition := cluster.FindDefinition(clusterId)
if clDefinition == nil {
return fmt.Errorf("unknown cluster: %d", clusterId)
}
// TODO: clarify docs that definition datatypes are mainly needed for TX?
attrDef := clDefinition.Attribute(rxAttributeId)
// validate that received attr datatype is same as in definition. this is strictly not
// necessary, as we can still meaningfully decode the value, but is a good sanity check
// if this doesn't break any real-world use cases
if attrDef != nil && attrDef.Type != attrReport.DataType {
return fmt.Errorf(
"device %s sent %s.%s with ZCL type %d (expected %d)",
dev.NetworkAddress,
clDefinition.Name(),
attrDef.Name,
attrReport.DataType,
attrDef.Type)
}
// "genBasic.unknown(65281)" | "genBasic.modelId"
keyDisplay := func() string {
if attrDef == nil {
return fmt.Sprintf("%s.unknown(%d)", clDefinition.Name(), report.AttributeID)
} else {
return fmt.Sprintf("%s.%s", clDefinition.Name(), attrDef.Name)
}
}()
if parser := adapter.ParserForAttribute(keyDisplay); parser != nil {
return parser(attrReport, actx)
} else {
val := cluster.SerializeMagicValue(attrReport.DataType, attrReport.Value)
log.Printf(
"AttributeReport from %s: unimplemented cluster %s (value %s)",
dev.NetworkAddress,
keyDisplay,
val)
return nil
}
}