Skip to content

Commit d5f2cff

Browse files
fix(tapo): add pylint disables for python-kasa version compatibility
Suppress unexpected-keyword-arg warnings for uses_http and http_port parameters that exist in different python-kasa API versions. Additionally tapo.py and test_tapo.py are now ruff formatted. Signed-off-by: Marek Szczypiński <markacy@gmail.com>
1 parent 0cd1482 commit d5f2cff

2 files changed

Lines changed: 41 additions & 43 deletions

File tree

labgrid/driver/power/tapo.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def _get_connection_type() -> DeviceConnectionParameters:
4343
https=False,
4444
login_version=2,
4545
)
46+
# http_port parameter exists in new kasa (>= 0.10.0) API but not in old versions (<= 0.9.1)
47+
# pylint: disable-next=unexpected-keyword-arg
4648
return DeviceConnectionParameters(
4749
device_family=DeviceFamily.SmartTapoPlug,
4850
encryption_type=DeviceEncryptionType.Klap,
@@ -55,12 +57,12 @@ def _get_connection_type() -> DeviceConnectionParameters:
5557
def _get_device_config(host: str) -> DeviceConfig:
5658
# Same as with `_get_connection_type` - python-kasa API changed
5759
if _using_old_kasa_api():
60+
# uses_http parameter exists in old kasa (<= 0.9.1) API but not in new versions (>= 0.10.0)
61+
# pylint: disable-next=unexpected-keyword-arg
5862
return DeviceConfig(
5963
host=host, credentials=_get_credentials(), connection_type=_get_connection_type(), uses_http=True
6064
) # type: ignore[call-arg]
61-
return DeviceConfig(
62-
host=host, credentials=_get_credentials(), connection_type=_get_connection_type()
63-
)
65+
return DeviceConfig(host=host, credentials=_get_credentials(), connection_type=_get_connection_type())
6466

6567

6668
async def _power_set(host: str, port: str, index: str, value: bool) -> None:

tests/test_tapo.py

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
@pytest.fixture
1010
def mock_device_strip():
1111
device = AsyncMock()
12-
device.children = [
13-
AsyncMock(is_on=True),
14-
AsyncMock(is_on=False),
15-
AsyncMock(is_on=True)
16-
]
12+
device.children = [AsyncMock(is_on=True), AsyncMock(is_on=False), AsyncMock(is_on=True)]
1713
return device
1814

1915

@@ -26,117 +22,117 @@ def mock_device_single_plug():
2622

2723
@pytest.fixture
2824
def mock_env():
29-
os.environ['KASA_USERNAME'] = 'test_user'
30-
os.environ['KASA_PASSWORD'] = 'test_pass'
25+
os.environ["KASA_USERNAME"] = "test_user"
26+
os.environ["KASA_PASSWORD"] = "test_pass"
3127
yield
32-
del os.environ['KASA_USERNAME']
33-
del os.environ['KASA_PASSWORD']
28+
del os.environ["KASA_USERNAME"]
29+
del os.environ["KASA_PASSWORD"]
3430

3531

3632
class TestTapoPowerDriver:
3733
def test_get_credentials_should_raise_value_error_when_credentials_missing(self):
3834
# Save existing environment variables
39-
saved_username = os.environ.pop('KASA_USERNAME', None)
40-
saved_password = os.environ.pop('KASA_PASSWORD', None)
35+
saved_username = os.environ.pop("KASA_USERNAME", None)
36+
saved_password = os.environ.pop("KASA_PASSWORD", None)
4137

4238
try:
4339
with pytest.raises(EnvironmentError, match="KASA_USERNAME or KASA_PASSWORD environment variable not set"):
4440
_get_credentials()
4541
finally:
4642
# Restore environment variables if they existed
4743
if saved_username is not None:
48-
os.environ['KASA_USERNAME'] = saved_username
44+
os.environ["KASA_USERNAME"] = saved_username
4945
if saved_password is not None:
50-
os.environ['KASA_PASSWORD'] = saved_password
46+
os.environ["KASA_PASSWORD"] = saved_password
5147

5248
def test_credentials_valid(self, mock_env):
5349
creds = _get_credentials()
54-
assert creds.username == 'test_user'
55-
assert creds.password == 'test_pass'
50+
assert creds.username == "test_user"
51+
assert creds.password == "test_pass"
5652

5753
@pytest.mark.asyncio
5854
async def test_power_get_single_plug_turn_on(self, mock_device_single_plug, mock_env):
5955
mock_device_single_plug.is_on = True
6056

61-
with patch('kasa.Device.connect', return_value=mock_device_single_plug):
62-
result = await _power_get('192.168.1.100', None, "0")
57+
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
58+
result = await _power_get("192.168.1.100", None, "0")
6359
assert result is True
6460

6561
@pytest.mark.asyncio
6662
async def test_power_get_single_plug_turn_off(self, mock_device_single_plug, mock_env):
6763
mock_device_single_plug.is_on = False
6864

69-
with patch('kasa.Device.connect', return_value=mock_device_single_plug):
70-
result = await _power_get('192.168.1.100', None, "0")
65+
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
66+
result = await _power_get("192.168.1.100", None, "0")
7167
assert result is False
7268

7369
@pytest.mark.asyncio
7470
async def test_power_get_single_plug_should_not_care_for_index(self, mock_device_single_plug, mock_env):
7571
invalid_index_ignored = "7"
7672
mock_device_single_plug.is_on = True
7773

78-
with patch('kasa.Device.connect', return_value=mock_device_single_plug):
79-
result = await _power_get('192.168.1.100', None, invalid_index_ignored)
74+
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
75+
result = await _power_get("192.168.1.100", None, invalid_index_ignored)
8076
assert result is True
8177

8278
@pytest.mark.asyncio
8379
async def test_power_set_single_plug_turn_on(self, mock_device_single_plug, mock_env):
8480
mock_device_single_plug.is_on = False
85-
with patch('kasa.Device.connect', return_value=mock_device_single_plug):
86-
await _power_set('192.168.1.100', None, "0", True)
81+
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
82+
await _power_set("192.168.1.100", None, "0", True)
8783
mock_device_single_plug.turn_on.assert_called_once()
8884

8985
@pytest.mark.asyncio
9086
async def test_power_set_single_plug_turn_off(self, mock_device_single_plug, mock_env):
9187
mock_device_single_plug.is_on = True
92-
with patch('kasa.Device.connect', return_value=mock_device_single_plug):
93-
await _power_set('192.168.1.100', None, "0", False)
88+
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
89+
await _power_set("192.168.1.100", None, "0", False)
9490
mock_device_single_plug.turn_off.assert_called_once()
9591

9692
@pytest.mark.asyncio
9793
async def test_power_get_strip_valid_socket(self, mock_device_strip, mock_env):
98-
with patch('kasa.Device.connect', return_value=mock_device_strip):
94+
with patch("kasa.Device.connect", return_value=mock_device_strip):
9995
# Test first outlet (on)
100-
result = await _power_get('192.168.1.100', None, "0")
96+
result = await _power_get("192.168.1.100", None, "0")
10197
assert result is True
10298

10399
# Test second outlet (off)
104-
result = await _power_get('192.168.1.100', None, "1")
100+
result = await _power_get("192.168.1.100", None, "1")
105101
assert result is False
106102

107103
# Test third outlet (on)
108-
result = await _power_get('192.168.1.100', None, "2")
104+
result = await _power_get("192.168.1.100", None, "2")
109105
assert result is True
110106

111107
@pytest.mark.asyncio
112108
async def test_power_set_strip_valid_socket(self, mock_device_strip, mock_env):
113-
with patch('kasa.Device.connect', return_value=mock_device_strip):
114-
await _power_set('192.168.1.100', None, "0", False)
109+
with patch("kasa.Device.connect", return_value=mock_device_strip):
110+
await _power_set("192.168.1.100", None, "0", False)
115111
mock_device_strip.children[0].turn_off.assert_called_once()
116112

117-
await _power_set('192.168.1.100', None, "1", True)
113+
await _power_set("192.168.1.100", None, "1", True)
118114
mock_device_strip.children[1].turn_on.assert_called_once()
119115

120116
def test_power_get_should_raise_assertion_error_when_invalid_index_strip(self, mock_device_strip, mock_env):
121117
invalid_socket = "5"
122-
with patch('kasa.Device.connect', return_value=mock_device_strip):
118+
with patch("kasa.Device.connect", return_value=mock_device_strip):
123119
with pytest.raises(AssertionError, match="Trying to access non-existant plug socket"):
124-
power_get('192.168.1.100', None, invalid_socket)
120+
power_get("192.168.1.100", None, invalid_socket)
125121

126122
@pytest.mark.asyncio
127123
async def test_power_set_should_raise_assertion_error_when_invalid_index_strip(self, mock_device_strip, mock_env):
128124
invalid_socket = "5"
129-
with patch('kasa.Device.connect', return_value=mock_device_strip):
125+
with patch("kasa.Device.connect", return_value=mock_device_strip):
130126
with pytest.raises(AssertionError, match="Trying to access non-existant plug socket"):
131-
await _power_set('192.168.1.100', None, invalid_socket, True)
127+
await _power_set("192.168.1.100", None, invalid_socket, True)
132128

133129
def test_port_not_none_strip(self, mock_device_strip):
134-
with patch('kasa.Device.connect', return_value=mock_device_strip):
130+
with patch("kasa.Device.connect", return_value=mock_device_strip):
135131
with pytest.raises(AssertionError):
136-
power_get('192.168.1.100', '8080', "0")
132+
power_get("192.168.1.100", "8080", "0")
137133

138134
def test_port_not_none_single_socket(self, mock_device_single_plug):
139135
mock_device_single_plug.is_on = True
140-
with patch('kasa.Device.connect', return_value=mock_device_single_plug):
136+
with patch("kasa.Device.connect", return_value=mock_device_single_plug):
141137
with pytest.raises(AssertionError):
142-
power_get('192.168.1.100', '8080', "0")
138+
power_get("192.168.1.100", "8080", "0")

0 commit comments

Comments
 (0)