Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions kasa/cli/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from __future__ import annotations

import asyncio
import json
import re
import sys
from collections.abc import Callable
from contextlib import contextmanager
from functools import singledispatch, update_wrapper, wraps
from gettext import gettext
from typing import TYPE_CHECKING, Any, Final

import asyncclick as click
Expand Down Expand Up @@ -238,4 +240,19 @@
except Exception as exc:
_handle_exception(self._debug, exc)

def __call__(self, *args, **kwargs):
"""Run the coroutine in the event loop and print any exceptions.

python click catches KeyboardInterrupt in main, raises Abort()
and does sys.exit. asyncclick doesn't properly handle a coroutine
receiving CancelledError on a KeyboardInterrupt, so we catch the
KeyboardInterrupt here once asyncio.run has re-raised it. This
avoids large stacktraces when a user presses Ctrl-C.
"""
try:
asyncio.run(self.main(*args, **kwargs))
except KeyboardInterrupt:
click.echo(gettext("\nAborted!"), file=sys.stderr)
sys.exit(1)

Check warning on line 256 in kasa/cli/common.py

View check run for this annotation

Codecov / codecov/patch

kasa/cli/common.py#L252-L256

Added lines #L252 - L256 were not covered by tests

return _CommandCls
2 changes: 1 addition & 1 deletion kasa/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ async def discover(
try:
_LOGGER.debug("Waiting %s seconds for responses...", discovery_timeout)
await protocol.wait_for_discovery_to_complete()
except KasaException as ex:
except (KasaException, asyncio.CancelledError) as ex:
for device in protocol.discovered_devices.values():
await device.protocol.close()
raise ex
Expand Down
Loading