From dee2cc985a199876006058ddd39b3ad584c69c0e Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 4 Jun 2024 00:34:50 +0200 Subject: [PATCH 1/4] Follow rename of ERROR_CATEGORY_* to LOG_CATEGORY_* --- matter_server/server/stack.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/matter_server/server/stack.py b/matter_server/server/stack.py index 6ffe947b..7c09de12 100644 --- a/matter_server/server/stack.py +++ b/matter_server/server/stack.py @@ -8,10 +8,11 @@ from chip.ChipStack import ChipStack import chip.logging from chip.logging import ( - ERROR_CATEGORY_DETAIL, - ERROR_CATEGORY_ERROR, - ERROR_CATEGORY_NONE, - ERROR_CATEGORY_PROGRESS, + LOG_CATEGORY_AUTOMATION, + LOG_CATEGORY_DETAIL, + LOG_CATEGORY_ERROR, + LOG_CATEGORY_NONE, + LOG_CATEGORY_PROGRESS, ) from chip.logging.library_handle import _GetLoggingLibraryHandle from chip.logging.types import LogRedirectCallback_t @@ -45,13 +46,13 @@ def _redirect_to_python_logging( # unknown/None as critical. level = logging.CRITICAL - if category == ERROR_CATEGORY_ERROR: + if category == LOG_CATEGORY_ERROR: level = CHIP_ERROR - elif category == ERROR_CATEGORY_PROGRESS: + elif category == LOG_CATEGORY_PROGRESS: level = CHIP_PROGRESS - elif category == ERROR_CATEGORY_DETAIL: + elif category == LOG_CATEGORY_DETAIL: level = CHIP_DETAIL - elif category == 4: # TODO: Add automation level to upstream Python bindings + elif category == LOG_CATEGORY_AUTOMATION: level = CHIP_AUTOMATION logger.log(level, "%s", message) @@ -62,15 +63,15 @@ def init_logging(category: str) -> None: _LOGGER.info("Initializing CHIP/Matter Logging...") global _category_num # pylint: disable=global-statement # noqa: PLW0603 - _category_num = ERROR_CATEGORY_NONE + _category_num = LOG_CATEGORY_NONE if category == "ERROR": - _category_num = ERROR_CATEGORY_ERROR + _category_num = LOG_CATEGORY_ERROR elif category == "PROGRESS": - _category_num = ERROR_CATEGORY_PROGRESS + _category_num = LOG_CATEGORY_PROGRESS elif category == "DETAIL": - _category_num = ERROR_CATEGORY_DETAIL + _category_num = LOG_CATEGORY_DETAIL elif category == "AUTOMATION": - _category_num = 4 + _category_num = LOG_CATEGORY_AUTOMATION logging.addLevelName(CHIP_ERROR, "CHIP_ERROR") logging.addLevelName(CHIP_PROGRESS, "CHIP_PROGRESS") From 829b4feaf89c8f61f9c7b29fd93833225fb0aafd Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 4 Jun 2024 00:35:31 +0200 Subject: [PATCH 2/4] Remove dropped installDefaultLogHandler in ChipStack() --- matter_server/server/stack.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matter_server/server/stack.py b/matter_server/server/stack.py index 7c09de12..212fb412 100644 --- a/matter_server/server/stack.py +++ b/matter_server/server/stack.py @@ -107,7 +107,6 @@ def __init__( self._chip_stack = ChipStack( persistentStoragePath=storage_file, - installDefaultLogHandler=False, enableServerInteractions=False, ) From 2728546033b031be18ad1c35410a4b7253463d61 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Fri, 7 Jun 2024 18:20:14 +0200 Subject: [PATCH 3/4] Update to Matter SDK wheels 2024.6.0 --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d33f90de..3cdee720 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ dependencies = [ "async-timeout", "coloredlogs", "orjson", - "home-assistant-chip-clusters==2024.5.2", + "home-assistant-chip-clusters==2024.6.0", ] description = "Python Matter WebSocket Server" license = {text = "Apache-2.0"} @@ -39,7 +39,7 @@ server = [ "cryptography==42.0.8", "orjson==3.10.3", "zeroconf==0.132.2", - "home-assistant-chip-core==2024.5.2", + "home-assistant-chip-core==2024.6.0", ] test = [ "codespell==2.3.0", From 39f9d164db6fae4aeb199b1b85488b72e93918bc Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Fri, 7 Jun 2024 18:32:56 +0200 Subject: [PATCH 4/4] Call TriggerResubscribeIfScheduled on mDNS re-discovery If we have active subscriptions, and we re-discover the device through mDNS, we can ask the SDK to re-subscribe to the device immediately. This should make device to be available quicker when they come back online. --- matter_server/server/device_controller.py | 9 +++++++++ matter_server/server/sdk.py | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/matter_server/server/device_controller.py b/matter_server/server/device_controller.py index a8d0c6be..2a55a0b4 100644 --- a/matter_server/server/device_controller.py +++ b/matter_server/server/device_controller.py @@ -1301,6 +1301,15 @@ def _on_mdns_operational_node_state( elif (now - last_seen) > NODE_MDNS_BACKOFF: # node came back online after being offline for a while or restarted logger.info("Node %s re-discovered on MDNS", node_id) + elif state_change == ServiceStateChange.Added: + # Trigger node re-subscriptions when mDNS entry got added + logger.info("Node %s activity on MDNS, trigger resubscribe", node_id) + asyncio.create_task( + self._chip_device_controller.trigger_resubscribe_if_scheduled( + node_id, "mDNS state change detected" + ) + ) + return else: # ignore all other cases return diff --git a/matter_server/server/sdk.py b/matter_server/server/sdk.py index 38478804..a2a54e2f 100644 --- a/matter_server/server/sdk.py +++ b/matter_server/server/sdk.py @@ -396,3 +396,14 @@ async def shutdown_subscription(self, node_id: int) -> None: def node_has_subscription(self, node_id: int) -> bool: """Check if a node has an active subscription.""" return node_id in self._subscriptions + + async def trigger_resubscribe_if_scheduled(self, node_id: int, reason: str) -> None: + """Trigger resubscribe now if a resubscribe is scheduled. + + If the ReadClient currently has a resubscription attempt scheduled, This + function allows to trigger that attempt immediately. This is useful + when the server side is up and communicating, and it's a good time to + try to resubscribe. + """ + if sub := self._subscriptions.get(node_id, None): + await sub.TriggerResubscribeIfScheduled(reason)