Skip to content

Commit

Permalink
Merge pull request #466 from puddly/puddly/coordinator-db-group-membe…
Browse files Browse the repository at this point in the history
…r-fix

Load group information from existing DB device
  • Loading branch information
puddly authored Jul 13, 2022
2 parents 766a6d6 + 332b8b0 commit d73b2f0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bellows/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ async def start_network(self):
self.controller_event.set()
self._watchdog_task = asyncio.create_task(self._watchdog())

try:
db_device = self.get_device(ieee=self.state.node_info.ieee)
except KeyError:
db_device = None

ezsp_device = EZSPCoordinator(
application=self,
ieee=self.state.node_info.ieee,
Expand All @@ -198,6 +203,10 @@ async def start_network(self):
ezsp_device.manufacturer = ezsp_device.endpoints[1].manufacturer
await ezsp_device.schedule_initialize()

# Group membership is stored in the database for EZSP coordinators
if db_device is not None:
ezsp_device.endpoints[1].member_of.update(db_device.endpoints[1].member_of)

await self.multicast.startup(ezsp_device)

async def load_network_info(self, *, load_devices=False) -> None:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1539,3 +1539,47 @@ async def test_ensure_network_running_not_joined_success(app):

ezsp.networkState.assert_called_once()
ezsp.networkInit.assert_called_once()


async def test_startup_coordinator_existing_groups_joined(app, ieee):
"""Coordinator joins groups loaded from the database."""

app._ensure_network_running = AsyncMock()
app._ezsp.update_policies = AsyncMock()
app.load_network_info = AsyncMock()

app._multicast = bellows.multicast.Multicast(app._ezsp)
app.state.node_info.ieee = ieee

db_device = app.add_device(ieee, 0x0000)
db_ep = db_device.add_endpoint(1)

app.groups.add_group(0x1234, "Group Name", suppress_event=True)
app.groups[0x1234].add_member(db_ep, suppress_event=True)

p1 = patch.object(bellows.multicast.Multicast, "_initialize")
p2 = patch.object(bellows.multicast.Multicast, "subscribe")

with p1 as p1, p2 as p2:
await app.start_network()

p2.assert_called_once_with(0x1234)


async def test_startup_new_coordinator_no_groups_joined(app, ieee):
"""Coordinator freshy added to the database has no groups to join."""

app._ensure_network_running = AsyncMock()
app._ezsp.update_policies = AsyncMock()
app.load_network_info = AsyncMock()

app._multicast = bellows.multicast.Multicast(app._ezsp)
app.state.node_info.ieee = ieee

p1 = patch.object(bellows.multicast.Multicast, "_initialize")
p2 = patch.object(bellows.multicast.Multicast, "subscribe")

with p1 as p1, p2 as p2:
await app.start_network()

p2.assert_not_called()

0 comments on commit d73b2f0

Please sign in to comment.