Skip to content

Commit 99e627e

Browse files
committed
Improve logging and use Future to mark completion
Instead of using an Event use a Future to mark completion of the update. This allows to set an Exception in case we see update state transitions which are unexpected (specifically kQuerying -> kIdle).
1 parent 6428e60 commit 99e627e

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

matter_server/server/device_controller.py

+5
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,9 @@ async def update_node(
910910
notify the node about the new update.
911911
"""
912912

913+
node_logger = LOGGER.getChild(f"node_{node_id}")
914+
node_logger.info("Update to software version %r", software_version)
915+
913916
update = await self._check_node_update(node_id, software_version)
914917
if update is None:
915918
raise UpdateCheckError(
@@ -923,6 +926,7 @@ async def update_node(
923926

924927
await ota_provider.initialize()
925928

929+
node_logger.info("Downloading update from '%s'", update["otaUrl"])
926930
await ota_provider.download_update(update)
927931

928932
self._attribute_update_callbacks.setdefault(node_id, []).append(
@@ -931,6 +935,7 @@ async def update_node(
931935

932936
try:
933937
# Make sure any previous instances get stopped
938+
node_logger.info("Starting update using OTA Provider.")
934939
await ota_provider.start_update(
935940
self._chip_device_controller,
936941
node_id,

matter_server/server/ota/provider.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def __init__(self, vendor_id: int, ota_provider_dir: Path) -> None:
7575
self._ota_file_path: Path | None = None
7676
self._ota_provider_proc: Process | None = None
7777
self._ota_provider_task: asyncio.Task | None = None
78-
self._ota_done: asyncio.Event = asyncio.Event()
78+
self._ota_done: asyncio.Future = asyncio.Future()
7979
self._ota_target_node_id: int | None = None
8080

8181
async def initialize(self) -> None:
@@ -161,11 +161,9 @@ async def _commission_ota_provider(
161161
"Failed writing adjusted OTA Provider App ACL: Status %s.",
162162
str(write_result[0].Status),
163163
)
164-
await self.stop()
165164
raise UpdateError("Error while setting up OTA Provider.")
166165
except ChipStackError as ex:
167166
logging.exception("Failed adjusting OTA Provider App ACL.", exc_info=ex)
168-
await self.stop()
169167
raise UpdateError("Error while setting up OTA Provider.") from ex
170168

171169
async def start_update(
@@ -239,8 +237,11 @@ async def start_update(
239237
"Error while announcing OTA Provider to node."
240238
) from ex
241239

242-
await self._ota_done.wait()
240+
LOGGER.info("Waiting for target node update state change")
241+
await self._ota_done
242+
LOGGER.info("OTA update finished successfully")
243243
finally:
244+
LOGGER.info("Cleaning up OTA provider")
244245
await self.stop()
245246
self._ota_target_node_id = None
246247

@@ -345,30 +346,28 @@ async def check_update_state(
345346
) -> None:
346347
"""Check the update state of a node and take appropriate action."""
347348

348-
LOGGER.info("Update state changed: %s, %s %s", str(path), old_value, new_value)
349349
if str(path) != OTA_SOFTWARE_UPDATE_REQUESTOR_UPDATE_STATE_ATTRIBUTE_PATH:
350350
return
351351

352-
update_state = cast(
353-
Clusters.OtaSoftwareUpdateRequestor.Enums.UpdateStateEnum, new_value
354-
)
352+
UpdateState = Clusters.OtaSoftwareUpdateRequestor.Enums.UpdateStateEnum # noqa: N806
355353

356-
old_update_state = cast(
357-
Clusters.OtaSoftwareUpdateRequestor.Enums.UpdateStateEnum, old_value
354+
new_update_state = UpdateState(new_value)
355+
old_update_state = UpdateState(old_value)
356+
357+
LOGGER.info(
358+
"Update state changed from %r to %r",
359+
old_update_state,
360+
new_update_state,
358361
)
359362

360363
# Update state of target node changed, check if update is done.
361-
if (
362-
update_state
363-
== Clusters.OtaSoftwareUpdateRequestor.Enums.UpdateStateEnum.kIdle
364-
):
365-
if (
366-
old_update_state
367-
== Clusters.OtaSoftwareUpdateRequestor.Enums.UpdateStateEnum.kQuerying
368-
):
369-
raise UpdateError("Target node did not process the update file")
364+
if new_update_state == UpdateState.kIdle:
365+
if old_update_state == UpdateState.kQuerying:
366+
self._ota_done.set_exception(
367+
UpdateError("Target node did not process the update file")
368+
)
370369

371370
LOGGER.info(
372371
"Node %d update state idle, assuming done.", self._ota_target_node_id
373372
)
374-
self._ota_done.set()
373+
self._ota_done.set_result(None)

0 commit comments

Comments
 (0)