mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-11-15 06:38:42 +00:00
Ensure connections are closed when cli is finished (#752)
* Ensure connections are closed when cli is finished * Test for close calls on error and success
This commit is contained in:
10
kasa/cli.py
10
kasa/cli.py
@@ -5,6 +5,7 @@ import json
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
from functools import singledispatch, wraps
|
||||
from pprint import pformat as pf
|
||||
from typing import Any, Dict, cast
|
||||
@@ -365,7 +366,14 @@ async def cli(
|
||||
if ctx.invoked_subcommand not in SKIP_UPDATE_COMMANDS and not device_family:
|
||||
await dev.update()
|
||||
|
||||
ctx.obj = dev
|
||||
@asynccontextmanager
|
||||
async def async_wrapped_device(device: Device):
|
||||
try:
|
||||
yield device
|
||||
finally:
|
||||
await device.disconnect()
|
||||
|
||||
ctx.obj = await ctx.with_async_resource(async_wrapped_device(dev))
|
||||
|
||||
if ctx.invoked_subcommand is None:
|
||||
return await ctx.invoke(state)
|
||||
|
||||
Reference in New Issue
Block a user