Skip to content

Commit c5341f6

Browse files
Add some workaround logic to poll devices that are unavailable for a longer period of time (#359)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
1 parent e736c47 commit c5341f6

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

matter_server/server/device_controller.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
LOGGER = logging.getLogger(__name__)
5353
INTERVIEW_TASK_LIMIT = 5
54+
MAX_POLL_INTERVAL = 600
5455

5556
# a list of attributes we should always watch on all nodes
5657
DEFAULT_SUBSCRIBE_ATTRIBUTES: set[tuple[int | str, int | str, int | str]] = {
@@ -698,6 +699,18 @@ def resubscription_attempted(
698699
if node.available:
699700
node.available = False
700701
self.server.signal_event(EventType.NODE_UPDATED, node)
702+
if nextResubscribeIntervalMsec / 1000 > MAX_POLL_INTERVAL:
703+
# workaround to handle devices that are unplugged
704+
# from power for a longer period of time
705+
# cancel subscription and add this node to our node polling job
706+
# TODO: fix this once OerationalNodeDiscovery is available:
707+
# https://github.com/project-chip/connectedhomeip/pull/26718
708+
sub.Shutdown()
709+
self._subscriptions.pop(node_id)
710+
assert self.server.loop
711+
self.server.loop.create_task(
712+
self._check_interview_and_subscription(node_id, MAX_POLL_INTERVAL)
713+
)
701714

702715
def resubscription_succeeded(
703716
transaction: Attribute.SubscriptionTransaction,
@@ -757,8 +770,9 @@ def reschedule() -> None:
757770
asyncio.create_task,
758771
self._check_interview_and_subscription(
759772
node_id,
760-
# increase interval at each attempt with maximum of 10 minutes
761-
min(reschedule_interval + 10, 600),
773+
# increase interval at each attempt with maximum of
774+
# MAX_POLL_INTERVAL seconds (= 10 minutes)
775+
min(reschedule_interval + 10, MAX_POLL_INTERVAL),
762776
),
763777
)
764778

@@ -794,6 +808,8 @@ def reschedule() -> None:
794808
"will retry later in the background.",
795809
node_id,
796810
)
811+
# TODO: fix this once OperationalNodeDiscovery is available:
812+
# https://github.com/project-chip/connectedhomeip/pull/26718
797813
reschedule()
798814

799815
@staticmethod
@@ -845,7 +861,7 @@ async def _resolve_node(self, node_id: int, retries: int = 3) -> None:
845861
raise RuntimeError("Device Controller not initialized.")
846862
try:
847863
async with node_lock, self._resolve_lock:
848-
LOGGER.info("Attempting to resolve node %s...", node_id)
864+
LOGGER.debug("Attempting to resolve node %s...", node_id)
849865
await self._call_sdk(
850866
self.chip_controller.ResolveNode,
851867
nodeid=node_id,

0 commit comments

Comments
 (0)