Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support v2 manifests and Simplicity SDK builds #17

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

tl-sl
Copy link

@tl-sl tl-sl commented Nov 3, 2024

Bump universal-silabs-flasher == 0.0.24 and make various adjustments to enums for new manifest formats.

This allows the web flasher to work with v2 manifests from template builder and Simplicity SDK builds.

NB: Zigpy is pulling in sqlite3, not sure that is really needed or can just be mocked?

@puddly
Copy link
Collaborator

puddly commented Nov 3, 2024

You can mock out sqlite3, we don't use it.

@puddly
Copy link
Collaborator

puddly commented Nov 3, 2024

We've also adjusted the serial API to properly close ports asynchronously. Untested:

diff --git a/src/webserial_transport.py b/src/webserial_transport.py
index 02da299..0d80c7e 100644
--- a/src/webserial_transport.py
+++ b/src/webserial_transport.py
@@ -21,7 +21,7 @@ except ImportError:
 
 
 _SERIAL_PORT = None
-_SERIAL_PORT_CLOSING_QUEUE = []
+_SERIAL_PORT_CLOSING_TASKS = []
 
 _LOGGER = logging.getLogger(__name__)
 
@@ -106,14 +106,23 @@ class WebSerialTransport(asyncio.Transport):
             self._js_writer.releaseLock()
             self._js_writer = None
 
+        closing_task = None
+
         if self._port is not None:
-            _SERIAL_PORT_CLOSING_QUEUE.append(
-                asyncio.create_task(close_port(self._port))
-            )
+            closing_task = asyncio.create_task(close_port(self._port))
             self._port = None
 
         if self._protocol is not None:
-            self._protocol.connection_lost(exception)
+            if closing_task is None:
+                self._protocol.connection_lost(exception)
+            else:
+                closing_task.add_done_callback(
+                    lambda _: self._protocol.connection_lost(exception)
+                )
+                closing_task.add_done_callback(
+                    lambda _: _SERIAL_PORT_CLOSING_TASKS.remove(closing_task)
+                )
+
             self._protocol = None
 
     def close(self) -> None:
@@ -136,11 +145,12 @@ async def create_serial_connection(
     rtscts=False,
     xonxoff=False,
 ) -> tuple[WebSerialTransport, asyncio.Protocol]:
-    # XXX: Since asyncio's `transport.close` is synchronous but JavaScript's is not, we
-    # must delegate closing to a task and then "block" at the next asynchronous entry
-    # point to allow the serial port to be re-opened immediately after being closed
-    while _SERIAL_PORT_CLOSING_QUEUE:
-        await _SERIAL_PORT_CLOSING_QUEUE.pop()
+    while _SERIAL_PORT_CLOSING_TASKS:
+        _LOGGER.warning(
+            "Serial connection was not closed before a new one was opened!"
+            " Waiting before opening a new one."
+        )
+        await _SERIAL_PORT_CLOSING_TASKS.pop()
 
     # `url` is ignored, `_SERIAL_PORT` is used instead
     await _SERIAL_PORT.open(

@tl-sl
Copy link
Author

tl-sl commented Nov 3, 2024

I have mocked sqlite3 and imported your patch with minor fixes, seems to be working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants