Skip to content

Commit

Permalink
Handle devices joining the network multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBomholtz authored and rcloran committed Jan 15, 2018
1 parent 655bb3e commit 4cf1237
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 5 additions & 3 deletions bellows/zigbee/application.py
Expand Up @@ -231,14 +231,16 @@ def _handle_join(self, nwk, ieee, device_update, join_dec, parent_nwk):
LOGGER.info("Device 0x%04x (%s) joined the network", nwk, ieee)
if ieee in self.devices:
dev = self.get_device(ieee)
dev.nwk = nwk
if dev.initializing or dev.status == bellows.zigbee.device.Status.ENDPOINTS_INIT:
if dev.nwk != nwk:
LOGGER.debug("Device %s changed id (0x%04x => 0x%04x)", ieee, dev.nwk, nwk)
dev.nwk = nwk
elif dev.initializing or dev.status == bellows.zigbee.device.Status.ENDPOINTS_INIT:
LOGGER.debug("Skip initialization for existing device %s", ieee)
return
else:
dev = self.add_device(ieee, nwk)
self.listener_event('device_joined', dev)

self.listener_event('device_joined', dev)
dev.schedule_initialize()

def _handle_leave(self, nwk, ieee, *args):
Expand Down
8 changes: 6 additions & 2 deletions bellows/zigbee/device.py
Expand Up @@ -36,9 +36,13 @@ def __init__(self, application, ieee, nwk):
self.initializing = False

def schedule_initialize(self):
self.initializing = True
if self.initializing:
LOGGER.debug("Canceling old initialize call")
self._init_handle.cancel()
else:
self.initializing = True
loop = asyncio.get_event_loop()
loop.call_soon(asyncio.async, self._initialize())
self._init_handle = loop.call_soon(asyncio.async, self._initialize())

@asyncio.coroutine
def _initialize(self):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_application.py
Expand Up @@ -221,6 +221,12 @@ def test_join_handler_skip(app, ieee):
assert app.devices[ieee].status == device.Status.ZDO_INIT


def test_join_handler_change_id(app, ieee):
app._handle_join(1, ieee, None, None, None)
app._handle_join(2, ieee, None, None, None)
assert app.devices[ieee].nwk == 2


def test_leave_handler(app, ieee):
app.devices[ieee] = mock.sentinel.device
app.ezsp_callback_handler(
Expand Down

0 comments on commit 4cf1237

Please sign in to comment.