Skip to content

Commit

Permalink
Detach capabilities when device is removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwood committed Jan 14, 2024
1 parent 4a0b233 commit 2577797
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 1 addition & 4 deletions implcaps/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ const (
type DetachType int

const (
// Shutdown is used to Detach a capability during the shutdown phase of the ZDA, the network and device should
// be assumed to still existing their established state.
Shutdown DetachType = iota
// DeviceRemoved is used when a device has been removed from the Zigbee network, this has already occurred and it
// should be assumed that no communication is possible.
DeviceRemoved
DeviceRemoved DetachType = iota
// NoLongerEnumerated is used when the enumeration of the node no longer results in this capability existing, or
// it's being replaced by a different implementation. Tidy up via the network may be possible.
NoLongerEnumerated
Expand Down
14 changes: 13 additions & 1 deletion provider_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package zda
import (
"context"
"errors"
"github.com/shimmeringbee/da/capabilities"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/zda/implcaps"
"github.com/shimmeringbee/zigbee"
)

Expand Down Expand Up @@ -49,8 +51,18 @@ func (g *gateway) receiveNodeLeaveEvent(e zigbee.NodeLeaveEvent) {

if n := g.getNode(e.IEEEAddress); n != nil {
for _, d := range g.getDevicesOnNode(n) {
g.logger.LogInfo(g.ctx, "Remove device upon node leaving zigbee network.", logwrap.Datum("Identifier", d.address.String()))
ctx, end := g.logger.Segment(g.ctx, "Device leaving zigbee network.", logwrap.Datum("Identifier", d.address.String()))

g.logger.LogInfo(ctx, "Remove device upon node leaving zigbee network.")
_ = g.removeDevice(d.address)
for cf, impl := range d.capabilities {
g.logger.LogInfo(ctx, "Detaching capability from removed device.", logwrap.Datum("Capability", capabilities.StandardNames[cf]), logwrap.Datum("CapabilityImplementation", impl.ImplName()))
if err := impl.Detach(ctx, implcaps.DeviceRemoved); err != nil {
g.logger.LogWarn(ctx, "Error thrown while detaching capability.", logwrap.Datum("Capability", capabilities.StandardNames[cf]), logwrap.Datum("CapabilityImplementation", impl.ImplName()), logwrap.Err(err))
}
}

end()
}

_ = g.removeNode(e.IEEEAddress)
Expand Down

0 comments on commit 2577797

Please sign in to comment.