Skip to content

Commit

Permalink
Move more sync functions to executor (#159)
Browse files Browse the repository at this point in the history
* Move more sync functions to executor

* Drop explicit default Python version from CI

* Oops

* Patch the default pin factory in `test_connect`
  • Loading branch information
puddly authored Oct 25, 2024
1 parent f650390 commit 93c7358
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
with:
CODE_FOLDER: zigpy_zigate
CACHE_VERSION: 2
PYTHON_VERSION_DEFAULT: 3.8.14
PRE_COMMIT_CACHE_PATH: ~/.cache/pre-commit
MINIMUM_COVERAGE_PERCENTAGE: 46
secrets:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def gw():
("/dev/null", "pizigate:/dev/ttyAMA0"),
)
async def test_connect(port, monkeypatch):
monkeypatch.setattr(gpiozero.Device, "_default_pin_factory", MagicMock())

api = MagicMock()

async def mock_conn(loop, protocol_factory, url, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion zigpy_zigate/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def async_run_in_executor(function):
"""Decorator to make a sync function async."""

async def replacement(*args):
return asyncio.get_running_loop().run_in_executor(None, function, *args)
return await asyncio.get_running_loop().run_in_executor(None, function, *args)

replacement._sync_func = function

Expand All @@ -174,3 +174,5 @@ async def replacement(*args):
async_set_pizigate_flashing_mode = async_run_in_executor(set_pizigate_flashing_mode)
async_set_zigatedin_running_mode = async_run_in_executor(set_zigatedin_running_mode)
async_set_zigatedin_flashing_mode = async_run_in_executor(set_zigatedin_flashing_mode)
async_is_pizigate = async_run_in_executor(is_pizigate)
async_is_zigate_din = async_run_in_executor(is_zigate_din)
4 changes: 2 additions & 2 deletions zigpy_zigate/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ async def connect(device_config: Dict[str, Any], api, loop=None):
if port == "auto":
port = await loop.run_in_executor(None, c.discover_port)

if c.is_pizigate(port):
if await c.async_is_pizigate(port):
LOGGER.debug("PiZiGate detected")
await c.async_set_pizigate_running_mode()
# in case of pizigate:/dev/ttyAMA0 syntax
if port.startswith("pizigate:"):
port = port.replace("pizigate:", "", 1)
elif c.is_zigate_din(port):
elif await c.async_is_zigate_din(port):
LOGGER.debug("ZiGate USB DIN detected")
await c.async_set_zigatedin_running_mode()
elif c.is_zigate_wifi(port):
Expand Down
4 changes: 2 additions & 2 deletions zigpy_zigate/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ async def load_network_info(self, *, load_devices: bool = False):

if c.is_zigate_wifi(port):
model = "ZiGate WiFi"
elif c.is_pizigate(port):
elif await c.async_is_pizigate(port):
model = "PiZiGate"
elif c.is_zigate_din(port):
elif await c.async_is_zigate_din(port):
model = "ZiGate USB-DIN"
else:
model = "ZiGate USB-TTL"
Expand Down

0 comments on commit 93c7358

Please sign in to comment.