-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
289 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package device_workaround | ||
|
||
import ( | ||
"context" | ||
"github.com/shimmeringbee/da" | ||
"github.com/shimmeringbee/da/capabilities" | ||
"github.com/shimmeringbee/persistence" | ||
"github.com/shimmeringbee/zcl" | ||
"github.com/shimmeringbee/zcl/commands/local/basic" | ||
"github.com/shimmeringbee/zda/attribute" | ||
"github.com/shimmeringbee/zda/implcaps" | ||
"github.com/shimmeringbee/zigbee" | ||
"time" | ||
) | ||
|
||
var _ implcaps.ZDACapability = (*Implementation)(nil) | ||
|
||
func NewDeviceWorkaround(zi implcaps.ZDAInterface) *Implementation { | ||
return &Implementation{zi: zi} | ||
} | ||
|
||
type Implementation struct { | ||
s persistence.Section | ||
d da.Device | ||
am attribute.Monitor | ||
zi implcaps.ZDAInterface | ||
} | ||
|
||
func (i *Implementation) Capability() da.Capability { | ||
return capabilities.DeviceWorkaroundFlag | ||
} | ||
|
||
func (i *Implementation) Name() string { | ||
return capabilities.StandardNames[capabilities.DeviceWorkaroundFlag] | ||
} | ||
|
||
func (i *Implementation) Init(d da.Device, s persistence.Section) { | ||
i.d = d | ||
i.s = s | ||
|
||
i.am = i.zi.NewAttributeMonitor() | ||
i.am.Init(s.Section("AttributeMonitor", "ZCLVersion"), d, i.update) | ||
} | ||
|
||
func (i *Implementation) Load(ctx context.Context) (bool, error) { | ||
if err := i.am.Load(ctx); err != nil { | ||
return false, err | ||
} else { | ||
return true, nil | ||
} | ||
} | ||
|
||
func (i *Implementation) Enumerate(ctx context.Context, m map[string]any) (bool, error) { | ||
endpoint := implcaps.Get(m, "ZigbeeEndpoint", zigbee.Endpoint(1)) | ||
|
||
reporting := attribute.ReportingConfig{ | ||
Mode: attribute.AttemptConfigureReporting, | ||
MinimumInterval: 1 * time.Minute, | ||
MaximumInterval: 2 * time.Minute, | ||
ReportableChange: uint(0), | ||
} | ||
|
||
polling := attribute.PollingConfig{ | ||
Mode: attribute.NeverPoll, | ||
} | ||
|
||
if err := i.am.Attach(ctx, endpoint, zcl.BasicId, basic.ZCLVersion, zcl.TypeUnsignedInt8, reporting, polling); err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (i *Implementation) Detach(ctx context.Context, detachType implcaps.DetachType) error { | ||
if err := i.am.Detach(ctx, detachType == implcaps.NoLongerEnumerated); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Implementation) ImplName() string { | ||
return "ProprietaryTiRouterDeviceWorkaround" | ||
} | ||
|
||
func (i *Implementation) update(_ zcl.AttributeID, _ zcl.AttributeDataTypeValue) { | ||
|
||
} |
125 changes: 125 additions & 0 deletions
125
implcaps/proprietary/tirouter/device_workaround/impl_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package device_workaround | ||
|
||
import ( | ||
"context" | ||
"github.com/shimmeringbee/da/capabilities" | ||
"github.com/shimmeringbee/da/mocks" | ||
"github.com/shimmeringbee/persistence/impl/memory" | ||
"github.com/shimmeringbee/zcl" | ||
"github.com/shimmeringbee/zcl/commands/local/basic" | ||
"github.com/shimmeringbee/zda/attribute" | ||
"github.com/shimmeringbee/zda/implcaps" | ||
"github.com/shimmeringbee/zigbee" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestImplementation_BaseFunctions(t *testing.T) { | ||
t.Run("basic static functions respond correctly", func(t *testing.T) { | ||
i := NewDeviceWorkaround(nil) | ||
|
||
assert.Equal(t, capabilities.DeviceWorkaroundFlag, i.Capability()) | ||
assert.Equal(t, capabilities.StandardNames[capabilities.DeviceWorkaroundFlag], i.Name()) | ||
assert.Equal(t, "ProprietaryTiRouterDeviceWorkaround", i.ImplName()) | ||
}) | ||
} | ||
|
||
func TestImplementation_Init(t *testing.T) { | ||
t.Run("constructs a new attribute monitor correctly initialising it", func(t *testing.T) { | ||
mzi := &implcaps.MockZDAInterface{} | ||
defer mzi.AssertExpectations(t) | ||
|
||
mm := &attribute.MockMonitor{} | ||
defer mm.AssertExpectations(t) | ||
|
||
mzi.On("NewAttributeMonitor").Return(mm) | ||
|
||
md := &mocks.MockDevice{} | ||
defer md.AssertExpectations(t) | ||
|
||
s := memory.New() | ||
es := s.Section("AttributeMonitor", implcaps.ReadingKey) | ||
|
||
mm.On("Init", es, md, mock.Anything) | ||
|
||
i := NewDeviceWorkaround(mzi) | ||
i.Init(md, s) | ||
}) | ||
} | ||
|
||
func TestImplementation_Load(t *testing.T) { | ||
t.Run("loads attribute monitor functionality, returning true if successful", func(t *testing.T) { | ||
mm := &attribute.MockMonitor{} | ||
defer mm.AssertExpectations(t) | ||
|
||
mm.On("Load", mock.Anything).Return(nil) | ||
|
||
i := NewDeviceWorkaround(nil) | ||
i.am = mm | ||
attached, err := i.Load(context.TODO()) | ||
|
||
assert.True(t, attached) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("loads attribute monitor functionality, returning false if error", func(t *testing.T) { | ||
mm := &attribute.MockMonitor{} | ||
defer mm.AssertExpectations(t) | ||
|
||
mm.On("Load", mock.Anything).Return(io.EOF) | ||
|
||
i := NewDeviceWorkaround(nil) | ||
i.am = mm | ||
attached, err := i.Load(context.TODO()) | ||
|
||
assert.False(t, attached) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestImplementation_Enumerate(t *testing.T) { | ||
t.Run("attaches to the attribute monitor", func(t *testing.T) { | ||
mm := &attribute.MockMonitor{} | ||
defer mm.AssertExpectations(t) | ||
|
||
mm.On("Attach", mock.Anything, zigbee.Endpoint(0x01), zcl.BasicId, basic.ZCLVersion, zcl.TypeUnsignedInt8, mock.Anything, mock.Anything).Return(nil) | ||
|
||
i := NewDeviceWorkaround(nil) | ||
i.am = mm | ||
attached, err := i.Enumerate(context.TODO(), make(map[string]any)) | ||
|
||
assert.True(t, attached) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("fails if attach to the attribute monitor fails", func(t *testing.T) { | ||
mm := &attribute.MockMonitor{} | ||
defer mm.AssertExpectations(t) | ||
|
||
mm.On("Attach", mock.Anything, zigbee.Endpoint(0x01), zcl.BasicId, basic.ZCLVersion, zcl.TypeUnsignedInt8, mock.Anything, mock.Anything).Return(io.EOF) | ||
|
||
i := NewDeviceWorkaround(nil) | ||
i.am = mm | ||
attached, err := i.Enumerate(context.TODO(), make(map[string]any)) | ||
|
||
assert.False(t, attached) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestImplementation_Detach(t *testing.T) { | ||
t.Run("detached attribute monitor on detach", func(t *testing.T) { | ||
mm := &attribute.MockMonitor{} | ||
defer mm.AssertExpectations(t) | ||
|
||
mm.On("Detach", mock.Anything, true).Return(nil) | ||
|
||
i := NewDeviceWorkaround(nil) | ||
i.am = mm | ||
|
||
err := i.Detach(context.TODO(), implcaps.NoLongerEnumerated) | ||
assert.NoError(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"Name": "proprietary", | ||
"Rules": [ | ||
{ | ||
"Description": "TI Routers", | ||
"Filter": "Product[Self].Name == 'ti.router'", | ||
"Actions": { | ||
"Capabilities": { | ||
"Add": { | ||
"ProprietaryTiRouterDeviceWorkaround": { | ||
"ZigbeeEndpoint": "Fn.Endpoint(Self)" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} |