Skip to content

Commit

Permalink
Move MatterDeviceController instantiation to start() (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
agners authored Jun 10, 2024
1 parent 36ceb2e commit a938271
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions matter_server/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import asyncio
from functools import partial
from functools import cached_property, partial
import ipaddress
import logging
import os
Expand Down Expand Up @@ -125,19 +125,17 @@ def __init__(
self.loop: asyncio.AbstractEventLoop | None = None
# Instantiate the Matter Stack using the SDK using the given storage path
self.stack = MatterStack(self)
# Initialize our (intermediate) device controller which keeps track
# of Matter devices and their subscriptions.
self._device_controller = MatterDeviceController(self, self.paa_root_cert_dir)
self.storage = StorageController(self)
self.vendor_info = VendorInfo(self)
# we dynamically register command handlers
self.command_handlers: dict[str, APICommandHandler] = {}
self._device_controller: MatterDeviceController | None = None
self._subscribers: set[EventCallBackType] = set()
self._register_api_commands()

@property
@cached_property
def device_controller(self) -> MatterDeviceController:
"""Return the main Matter device controller."""
assert self._device_controller
return self._device_controller

async def start(self) -> None:
Expand All @@ -156,6 +154,11 @@ async def start(self) -> None:
# NOTE: this must be done before initializing the controller
await fetch_certificates(self.paa_root_cert_dir)

# Initialize our (intermediate) device controller which keeps track
# of Matter devices and their subscriptions.
self._device_controller = MatterDeviceController(self, self.paa_root_cert_dir)
self._register_api_commands()

await self._device_controller.initialize()
await self.storage.start()
await self._device_controller.start()
Expand Down Expand Up @@ -197,7 +200,7 @@ async def stop(self) -> None:
await self._runner.cleanup()
await self.app.shutdown()
await self.app.cleanup()
await self._device_controller.stop()
await self.device_controller.stop()
await self.storage.stop()
self.stack.shutdown()
self.logger.debug("Cleanup complete")
Expand All @@ -220,6 +223,7 @@ def unsub() -> None:
@api_command(APICommand.SERVER_INFO)
def get_info(self) -> ServerInfoMessage:
"""Return (version)info of the Matter Server."""
assert self._device_controller
assert self._device_controller.compressed_fabric_id is not None
return ServerInfoMessage(
fabric_id=self.fabric_id,
Expand All @@ -236,8 +240,8 @@ def get_diagnostics(self) -> ServerDiagnostics:
"""Return a full dump of the server (for diagnostics)."""
return ServerDiagnostics(
info=self.get_info(),
nodes=self._device_controller.get_nodes(),
events=list(self._device_controller.event_history),
nodes=self.device_controller.get_nodes(),
events=list(self.device_controller.event_history),
)

def signal_event(self, evt: EventType, data: Any = None) -> None:
Expand Down

0 comments on commit a938271

Please sign in to comment.