mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-11 18:08:02 +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:
@@ -49,6 +49,20 @@ async def connect(*, host: Optional[str] = None, config: DeviceConfig) -> "Devic
|
||||
if host:
|
||||
config = DeviceConfig(host=host)
|
||||
|
||||
if (protocol := get_protocol(config=config)) is None:
|
||||
raise UnsupportedDeviceException(
|
||||
f"Unsupported device for {config.host}: "
|
||||
+ f"{config.connection_type.device_family.value}"
|
||||
)
|
||||
|
||||
try:
|
||||
return await _connect(config, protocol)
|
||||
except:
|
||||
await protocol.close()
|
||||
raise
|
||||
|
||||
|
||||
async def _connect(config: DeviceConfig, protocol: BaseProtocol) -> "Device":
|
||||
debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
|
||||
if debug_enabled:
|
||||
start_time = time.perf_counter()
|
||||
@@ -63,12 +77,6 @@ async def connect(*, host: Optional[str] = None, config: DeviceConfig) -> "Devic
|
||||
)
|
||||
start_time = time.perf_counter()
|
||||
|
||||
if (protocol := get_protocol(config=config)) is None:
|
||||
raise UnsupportedDeviceException(
|
||||
f"Unsupported device for {config.host}: "
|
||||
+ f"{config.connection_type.device_family.value}"
|
||||
)
|
||||
|
||||
device_class: Optional[Type[Device]]
|
||||
device: Optional[Device] = None
|
||||
|
||||
|
Reference in New Issue
Block a user