-
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.
Add DeviceRemoval, move creation of all device capabilties to table.go.
- Loading branch information
Showing
8 changed files
with
145 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package zda | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/shimmeringbee/da" | ||
"github.com/shimmeringbee/da/capabilities" | ||
"github.com/shimmeringbee/logwrap" | ||
"github.com/shimmeringbee/zigbee" | ||
) | ||
|
||
type deviceRemoval struct { | ||
node *node | ||
logger logwrap.Logger | ||
nodeRemover zigbee.NodeRemover | ||
} | ||
|
||
func (z deviceRemoval) Capability() da.Capability { | ||
return capabilities.DeviceRemovalFlag | ||
} | ||
|
||
func (z deviceRemoval) Name() string { | ||
return capabilities.StandardNames[z.Capability()] | ||
} | ||
|
||
func (z deviceRemoval) Remove(ctx context.Context, removalType capabilities.RemovalType) error { | ||
switch removalType { | ||
case capabilities.Request: | ||
z.logger.LogInfo(ctx, "Requesting removal of device from zigbee provider.", logwrap.Datum("IEEEAddress", z.node.address.String())) | ||
return z.nodeRemover.RequestNodeLeave(ctx, z.node.address) | ||
case capabilities.Force: | ||
z.logger.LogInfo(ctx, "Requesting forced removal of device from zigbee provider.", logwrap.Datum("IEEEAddress", z.node.address.String())) | ||
return z.nodeRemover.ForceNodeLeave(ctx, z.node.address) | ||
default: | ||
z.logger.LogError(ctx, "Request removal called with unknown removal type.", logwrap.Datum("IEEEAddress", z.node.address.String()), logwrap.Datum("removalType", removalType)) | ||
return fmt.Errorf("remove device called with unknown removal type: %v", removalType) | ||
} | ||
} | ||
|
||
var _ capabilities.DeviceRemoval = (*deviceRemoval)(nil) | ||
var _ da.BasicCapability = (*deviceRemoval)(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,50 @@ | ||
package zda | ||
|
||
import ( | ||
"context" | ||
"github.com/shimmeringbee/da/capabilities" | ||
lw "github.com/shimmeringbee/logwrap" | ||
"github.com/shimmeringbee/logwrap/impl/discard" | ||
"github.com/shimmeringbee/zigbee" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"testing" | ||
) | ||
|
||
func TestZigbeeDeviceRemoval_Remove(t *testing.T) { | ||
t.Run("successfully calls RequestNodeLeave on provider with devices ieee and Request flag", func(t *testing.T) { | ||
mockProvider := zigbee.MockProvider{} | ||
defer mockProvider.AssertExpectations(t) | ||
|
||
expectedIEEE := zigbee.GenerateLocalAdministeredIEEEAddress() | ||
|
||
zed := deviceRemoval{ | ||
nodeRemover: &mockProvider, | ||
logger: lw.New(discard.Discard()), | ||
node: &node{address: expectedIEEE}, | ||
} | ||
|
||
mockProvider.On("RequestNodeLeave", mock.Anything, expectedIEEE).Return(nil) | ||
|
||
err := zed.Remove(context.Background(), capabilities.Request) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("successfully calls ForceNodeLeave on provider with devices ieee and Force flag", func(t *testing.T) { | ||
mockProvider := zigbee.MockProvider{} | ||
defer mockProvider.AssertExpectations(t) | ||
|
||
expectedIEEE := zigbee.GenerateLocalAdministeredIEEEAddress() | ||
|
||
zed := deviceRemoval{ | ||
nodeRemover: &mockProvider, | ||
logger: lw.New(discard.Discard()), | ||
node: &node{address: expectedIEEE}, | ||
} | ||
|
||
mockProvider.On("ForceNodeLeave", mock.Anything, expectedIEEE).Return(nil) | ||
|
||
err := zed.Remove(context.Background(), capabilities.Force) | ||
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
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