Skip to content

Commit 2afb49e

Browse files
refactor: directly test kasa version in TAPO power model
Signed-off-by: Marek Szczypiński <markacy@gmail.com>
1 parent 6b6e589 commit 2afb49e

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

labgrid/driver/power/tapo.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414

1515
import asyncio
1616
import os
17-
import sys
1817

18+
import kasa
1919
from kasa import Credentials, Device, DeviceConfig, DeviceConnectionParameters, DeviceEncryptionType, DeviceFamily
20+
from packaging.version import parse
21+
22+
23+
def _using_old_kasa_api() -> bool:
24+
target_kasa_version = parse("0.10.0")
25+
current_kasa_version = parse(kasa.__version__)
26+
return current_kasa_version < target_kasa_version
2027

2128

2229
def _get_credentials() -> Credentials:
@@ -28,10 +35,8 @@ def _get_credentials() -> Credentials:
2835

2936

3037
def _get_connection_type() -> DeviceConnectionParameters:
31-
# Somewhere between python-kasa 0.7.7 and 0.10.2 the API changed
32-
# Labgrid on Python <= 3.10 uses python-kasa 0.7.7
33-
# Labgrid on Python >= 3.11 uses python-kasa 0.10.2
34-
if sys.version_info < (3, 11):
38+
# API changed between versions 0.9.1 and 0.10.0 of python-kasa
39+
if _using_old_kasa_api():
3540
return DeviceConnectionParameters(
3641
device_family=DeviceFamily.SmartTapoPlug,
3742
encryption_type=DeviceEncryptionType.Klap,
@@ -49,10 +54,10 @@ def _get_connection_type() -> DeviceConnectionParameters:
4954

5055
def _get_device_config(host: str) -> DeviceConfig:
5156
# Same as with `_get_connection_type` - python-kasa API changed
52-
if sys.version_info < (3, 11):
57+
if _using_old_kasa_api():
5358
return DeviceConfig(
5459
host=host, credentials=_get_credentials(), connection_type=_get_connection_type(), uses_http=True
55-
)
60+
) # type: ignore[call-arg]
5661
return DeviceConfig(
5762
host=host, credentials=_get_credentials(), connection_type=_get_connection_type()
5863
)

0 commit comments

Comments
 (0)