Skip to content

Commit b8b9c1a

Browse files
authored
Deprecate wifi_led in favor of led (#1342)
* Deprecate wifi_led in favor of led Doing this makes all devices with leds to have a common interface * Ignore deprecation warnings in Device.__repr__
1 parent 670ecba commit b8b9c1a

5 files changed

Lines changed: 69 additions & 17 deletions

File tree

miio/chuangmi_plug.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ def load_power(self) -> Optional[float]:
7878
return float(self.data["load_power"])
7979
return None
8080

81-
@property
81+
@property # type: ignore
82+
@deprecated("Use led()")
8283
def wifi_led(self) -> Optional[bool]:
84+
"""True if the wifi led is turned on."""
85+
return self.led
86+
87+
@property
88+
def led(self) -> Optional[bool]:
8389
"""True if the wifi led is turned on."""
8490
if "wifi_led" in self.data and self.data["wifi_led"] is not None:
8591
return self.data["wifi_led"] == "on"
@@ -142,6 +148,7 @@ def usb_off(self):
142148
"""Power off."""
143149
return self.send("set_usb_off")
144150

151+
@deprecated("Use set_led instead of set_wifi_led")
145152
@command(
146153
click.argument("wifi_led", type=bool),
147154
default_output=format_output(
@@ -152,6 +159,16 @@ def usb_off(self):
152159
)
153160
def set_wifi_led(self, wifi_led: bool):
154161
"""Set the wifi led on/off."""
162+
self.set_led(wifi_led)
163+
164+
@command(
165+
click.argument("wifi_led", type=bool),
166+
default_output=format_output(
167+
lambda wifi_led: "Turning on LED" if wifi_led else "Turning off LED"
168+
),
169+
)
170+
def set_led(self, wifi_led: bool):
171+
"""Set the led on/off."""
155172
if wifi_led:
156173
return self.send("set_wifi_led", ["on"])
157174
else:

miio/device.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
import logging
3+
import warnings
34
from enum import Enum
45
from pprint import pformat as pf
56
from typing import Any, List, Optional # noqa: F401
@@ -35,7 +36,9 @@ def __repr__(self):
3536
for prop_tuple in props:
3637
name, prop = prop_tuple
3738
try:
38-
prop_value = prop.fget(self)
39+
# ignore deprecation warnings
40+
with warnings.catch_warnings():
41+
prop_value = prop.fget(self)
3942
except Exception as ex:
4043
prop_value = ex.__class__.__name__
4144

miio/powerstrip.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .click_common import EnumType, command, format_output
99
from .device import Device, DeviceStatus
1010
from .exceptions import DeviceException
11+
from .utils import deprecated
1112

1213
_LOGGER = logging.getLogger(__name__)
1314

@@ -97,8 +98,14 @@ def mode(self) -> Optional[PowerMode]:
9798
return PowerMode(self.data["mode"])
9899
return None
99100

100-
@property
101+
@property # type: ignore
102+
@deprecated("Use led instead of wifi_led")
101103
def wifi_led(self) -> Optional[bool]:
104+
"""True if the wifi led is turned on."""
105+
return self.led
106+
107+
@property
108+
def led(self) -> Optional[bool]:
102109
"""True if the wifi led is turned on."""
103110
if "wifi_led" in self.data and self.data["wifi_led"] is not None:
104111
return self.data["wifi_led"] == "on"
@@ -182,13 +189,24 @@ def set_power_mode(self, mode: PowerMode):
182189
# green, normal
183190
return self.send("set_power_mode", [mode.value])
184191

192+
@deprecated("use set_led instead of set_wifi_led")
185193
@command(
186194
click.argument("led", type=bool),
187195
default_output=format_output(
188196
lambda led: "Turning on WiFi LED" if led else "Turning off WiFi LED"
189197
),
190198
)
191199
def set_wifi_led(self, led: bool):
200+
"""Set the wifi led on/off."""
201+
self.set_led(led)
202+
203+
@command(
204+
click.argument("led", type=bool),
205+
default_output=format_output(
206+
lambda led: "Turning on LED" if led else "Turning off LED"
207+
),
208+
)
209+
def set_led(self, led: bool):
192210
"""Set the wifi led on/off."""
193211
if led:
194212
return self.send("set_wifi_led", ["on"])

miio/tests/test_chuangmi_plug.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,22 @@ def test_usb_off(self):
164164
self.device.usb_off()
165165
assert self.device.status().usb_power is False
166166

167-
def test_set_wifi_led(self):
168-
def wifi_led():
169-
return self.device.status().wifi_led
167+
def test_led(self):
168+
def led():
169+
return self.device.status().led
170170

171-
self.device.set_wifi_led(True)
172-
assert wifi_led() is True
171+
self.device.set_led(True)
172+
assert led() is True
173173

174-
self.device.set_wifi_led(False)
175-
assert wifi_led() is False
174+
self.device.set_led(False)
175+
assert led() is False
176+
177+
def test_wifi_led_deprecation(self):
178+
with pytest.deprecated_call():
179+
self.device.set_wifi_led(True)
180+
181+
with pytest.deprecated_call():
182+
self.device.status().wifi_led
176183

177184

178185
class DummyChuangmiPlugM1(DummyDevice, ChuangmiPlug):

miio/tests/test_powerstrip.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,22 @@ def mode():
199199
self.device.set_power_mode(PowerMode.Normal)
200200
assert mode() == PowerMode.Normal
201201

202-
def test_set_wifi_led(self):
203-
def wifi_led():
204-
return self.device.status().wifi_led
202+
def test_set_led(self):
203+
def led():
204+
return self.device.status().led
205205

206-
self.device.set_wifi_led(True)
207-
assert wifi_led() is True
206+
self.device.set_led(True)
207+
assert led() is True
208208

209-
self.device.set_wifi_led(False)
210-
assert wifi_led() is False
209+
self.device.set_led(False)
210+
assert led() is False
211+
212+
def test_set_wifi_led_deprecation(self):
213+
with pytest.deprecated_call():
214+
self.device.set_wifi_led(True)
215+
216+
with pytest.deprecated_call():
217+
self.device.status().wifi_led
211218

212219
def test_set_power_price(self):
213220
def power_price():

0 commit comments

Comments
 (0)