-
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
21 changed files
with
316 additions
and
393 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
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/implcaps" | ||
"github.com/shimmeringbee/zigbee" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var _ implcaps.ZDACapability = (*Implementation)(nil) | ||
var _ capabilities.DeviceWorkarounds = (*Implementation)(nil) | ||
|
||
func NewDeviceWorkaround(zi implcaps.ZDAInterface) *Implementation { | ||
return &Implementation{zi: zi, m: &sync.RWMutex{}} | ||
} | ||
|
||
type Implementation struct { | ||
s persistence.Section | ||
d da.Device | ||
zi implcaps.ZDAInterface | ||
|
||
m *sync.RWMutex | ||
workaroundsEnabled []string | ||
} | ||
|
||
func (i *Implementation) Capability() da.Capability { | ||
return capabilities.DeviceWorkaroundsFlag | ||
} | ||
|
||
func (i *Implementation) Name() string { | ||
return capabilities.StandardNames[capabilities.DeviceWorkaroundsFlag] | ||
} | ||
|
||
func (i *Implementation) Init(d da.Device, s persistence.Section) { | ||
i.d = d | ||
i.s = s | ||
} | ||
|
||
func (i *Implementation) Load(ctx context.Context) (bool, error) { | ||
i.m.Lock() | ||
defer i.m.Unlock() | ||
|
||
i.workaroundsEnabled = i.s.Section("Workarounds").SectionKeys() | ||
|
||
for _, workaround := range i.workaroundsEnabled { | ||
if err := i.loadWorkaround(ctx, workaround); err != nil { | ||
return false, err | ||
} | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (i *Implementation) Enumerate(ctx context.Context, m map[string]any) (bool, error) { | ||
var workarounds []string | ||
|
||
for k, _ := range m { | ||
if strings.HasPrefix(k, "Enable") { | ||
workarounds = append(workarounds, k) | ||
} | ||
} | ||
|
||
i.m.Lock() | ||
i.workaroundsEnabled = workarounds | ||
i.m.Unlock() | ||
|
||
for _, workaround := range workarounds { | ||
if err := i.enumerateWorkaround(ctx, m, workaround); err != nil { | ||
return false, err | ||
} | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (i *Implementation) Detach(ctx context.Context, detachType implcaps.DetachType) error { | ||
i.m.Lock() | ||
defer i.m.Unlock() | ||
|
||
for _, workaround := range i.workaroundsEnabled { | ||
if err := i.detachWorkaround(ctx, detachType, workaround); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Implementation) ImplName() string { | ||
return "GenericDeviceWorkarounds" | ||
} | ||
|
||
func (i *Implementation) Enabled(_ context.Context) []string { | ||
i.m.RLock() | ||
defer i.m.RUnlock() | ||
|
||
return i.workaroundsEnabled | ||
} | ||
|
||
func (i *Implementation) loadWorkaround(ctx context.Context, workaround string) error { | ||
return nil | ||
} | ||
|
||
func (i *Implementation) detachWorkaround(ctx context.Context, detachType implcaps.DetachType, workaround string) error { | ||
return nil | ||
} | ||
|
||
func (i *Implementation) enumerateWorkaround(ctx context.Context, m map[string]any, workaround string) error { | ||
switch workaround { | ||
case "EnableZCLReportingKeepAlive": | ||
return i.enumerateZCLReportingKeepAlive(ctx, m) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *Implementation) enumerateZCLReportingKeepAlive(ctx context.Context, m map[string]any) error { | ||
remoteEndpoint := implcaps.Get(m, "ZigbeeEndpoint", zigbee.Endpoint(1)) | ||
|
||
ieeeAddress, localEndpoint, ack, seq := i.zi.TransmissionLookup(i.d, zigbee.ProfileHomeAutomation) | ||
|
||
if err := i.zi.NodeBinder().BindNodeToController(ctx, ieeeAddress, localEndpoint, remoteEndpoint, zcl.BasicId); err != nil { | ||
return err | ||
} | ||
|
||
if err := i.zi.ZCLCommunicator().ConfigureReporting(ctx, ieeeAddress, ack, zcl.BasicId, zigbee.NoManufacturer, localEndpoint, remoteEndpoint, seq, basic.ZCLVersion, zcl.TypeUnsignedInt8, uint16(60), uint16(240), 0); err != nil { | ||
return err | ||
} | ||
|
||
i.s.Section("Workarounds", "ZCLReportingKeepAlive") | ||
|
||
return nil | ||
} |
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,63 @@ | ||
package device_workaround | ||
|
||
import ( | ||
"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/implcaps" | ||
mocks2 "github.com/shimmeringbee/zda/mocks" | ||
"github.com/shimmeringbee/zigbee" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"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.DeviceWorkaroundsFlag, i.Capability()) | ||
assert.Equal(t, capabilities.StandardNames[capabilities.DeviceWorkaroundsFlag], i.Name()) | ||
assert.Equal(t, "GenericDeviceWorkarounds", i.ImplName()) | ||
}) | ||
} | ||
|
||
func TestImplementation_EnableZCLReportingKeepAlive(t *testing.T) { | ||
t.Run("configures reporting for ZCLVersion on Enumeration", func(t *testing.T) { | ||
mzi := &implcaps.MockZDAInterface{} | ||
defer mzi.AssertExpectations(t) | ||
|
||
mnb := &zigbee.MockProvider{} | ||
defer mnb.AssertExpectations(t) | ||
|
||
md := &mocks.MockDevice{} | ||
defer md.AssertExpectations(t) | ||
|
||
mzc := &mocks2.MockZCLCommunicator{} | ||
defer mzc.AssertExpectations(t) | ||
|
||
mzi.On("NodeBinder").Return(mnb) | ||
mzi.On("ZCLCommunicator").Return(mzc) | ||
|
||
ieee := zigbee.GenerateLocalAdministeredIEEEAddress() | ||
|
||
mzi.On("TransmissionLookup", md, zigbee.ProfileHomeAutomation).Return(ieee, zigbee.Endpoint(2), false, 4) | ||
|
||
mnb.On("BindNodeToController", mock.Anything, ieee, zigbee.Endpoint(2), zigbee.Endpoint(3), zcl.BasicId).Return(nil) | ||
|
||
mzc.On("ConfigureReporting", mock.Anything, ieee, false, zcl.BasicId, zigbee.NoManufacturer, zigbee.Endpoint(2), zigbee.Endpoint(3), uint8(4), basic.ZCLVersion, zcl.TypeUnsignedInt8, uint16(60), uint16(240), 0).Return(nil) | ||
|
||
s := memory.New() | ||
i := NewDeviceWorkaround(mzi) | ||
i.Init(md, s) | ||
|
||
attached, err := i.Enumerate(nil, map[string]any{"EnableZCLReportingKeepAlive": true, "ZigbeeEndpoint": zigbee.Endpoint(3)}) | ||
assert.NoError(t, err) | ||
assert.True(t, attached) | ||
|
||
ws := s.Section("Workarounds") | ||
assert.Contains(t, ws.SectionKeys(), "ZCLReportingKeepAlive") | ||
}) | ||
} |
Oops, something went wrong.