Skip to content

Commit

Permalink
Deprecate wifi_led in favor of led (#1342)
Browse files Browse the repository at this point in the history
* 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__
  • Loading branch information
rytilahti authored Mar 3, 2022
1 parent 670ecba commit b8b9c1a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
19 changes: 18 additions & 1 deletion miio/chuangmi_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ def load_power(self) -> Optional[float]:
return float(self.data["load_power"])
return None

@property
@property # type: ignore
@deprecated("Use led()")
def wifi_led(self) -> Optional[bool]:
"""True if the wifi led is turned on."""
return self.led

@property
def led(self) -> Optional[bool]:
"""True if the wifi led is turned on."""
if "wifi_led" in self.data and self.data["wifi_led"] is not None:
return self.data["wifi_led"] == "on"
Expand Down Expand Up @@ -142,6 +148,7 @@ def usb_off(self):
"""Power off."""
return self.send("set_usb_off")

@deprecated("Use set_led instead of set_wifi_led")
@command(
click.argument("wifi_led", type=bool),
default_output=format_output(
Expand All @@ -152,6 +159,16 @@ def usb_off(self):
)
def set_wifi_led(self, wifi_led: bool):
"""Set the wifi led on/off."""
self.set_led(wifi_led)

@command(
click.argument("wifi_led", type=bool),
default_output=format_output(
lambda wifi_led: "Turning on LED" if wifi_led else "Turning off LED"
),
)
def set_led(self, wifi_led: bool):
"""Set the led on/off."""
if wifi_led:
return self.send("set_wifi_led", ["on"])
else:
Expand Down
5 changes: 4 additions & 1 deletion miio/device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import logging
import warnings
from enum import Enum
from pprint import pformat as pf
from typing import Any, List, Optional # noqa: F401
Expand Down Expand Up @@ -35,7 +36,9 @@ def __repr__(self):
for prop_tuple in props:
name, prop = prop_tuple
try:
prop_value = prop.fget(self)
# ignore deprecation warnings
with warnings.catch_warnings():
prop_value = prop.fget(self)
except Exception as ex:
prop_value = ex.__class__.__name__

Expand Down
20 changes: 19 additions & 1 deletion miio/powerstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .click_common import EnumType, command, format_output
from .device import Device, DeviceStatus
from .exceptions import DeviceException
from .utils import deprecated

_LOGGER = logging.getLogger(__name__)

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

@property
@property # type: ignore
@deprecated("Use led instead of wifi_led")
def wifi_led(self) -> Optional[bool]:
"""True if the wifi led is turned on."""
return self.led

@property
def led(self) -> Optional[bool]:
"""True if the wifi led is turned on."""
if "wifi_led" in self.data and self.data["wifi_led"] is not None:
return self.data["wifi_led"] == "on"
Expand Down Expand Up @@ -182,13 +189,24 @@ def set_power_mode(self, mode: PowerMode):
# green, normal
return self.send("set_power_mode", [mode.value])

@deprecated("use set_led instead of set_wifi_led")
@command(
click.argument("led", type=bool),
default_output=format_output(
lambda led: "Turning on WiFi LED" if led else "Turning off WiFi LED"
),
)
def set_wifi_led(self, led: bool):
"""Set the wifi led on/off."""
self.set_led(led)

@command(
click.argument("led", type=bool),
default_output=format_output(
lambda led: "Turning on LED" if led else "Turning off LED"
),
)
def set_led(self, led: bool):
"""Set the wifi led on/off."""
if led:
return self.send("set_wifi_led", ["on"])
Expand Down
21 changes: 14 additions & 7 deletions miio/tests/test_chuangmi_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,22 @@ def test_usb_off(self):
self.device.usb_off()
assert self.device.status().usb_power is False

def test_set_wifi_led(self):
def wifi_led():
return self.device.status().wifi_led
def test_led(self):
def led():
return self.device.status().led

self.device.set_wifi_led(True)
assert wifi_led() is True
self.device.set_led(True)
assert led() is True

self.device.set_wifi_led(False)
assert wifi_led() is False
self.device.set_led(False)
assert led() is False

def test_wifi_led_deprecation(self):
with pytest.deprecated_call():
self.device.set_wifi_led(True)

with pytest.deprecated_call():
self.device.status().wifi_led


class DummyChuangmiPlugM1(DummyDevice, ChuangmiPlug):
Expand Down
21 changes: 14 additions & 7 deletions miio/tests/test_powerstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,22 @@ def mode():
self.device.set_power_mode(PowerMode.Normal)
assert mode() == PowerMode.Normal

def test_set_wifi_led(self):
def wifi_led():
return self.device.status().wifi_led
def test_set_led(self):
def led():
return self.device.status().led

self.device.set_wifi_led(True)
assert wifi_led() is True
self.device.set_led(True)
assert led() is True

self.device.set_wifi_led(False)
assert wifi_led() is False
self.device.set_led(False)
assert led() is False

def test_set_wifi_led_deprecation(self):
with pytest.deprecated_call():
self.device.set_wifi_led(True)

with pytest.deprecated_call():
self.device.status().wifi_led

def test_set_power_price(self):
def power_price():
Expand Down

0 comments on commit b8b9c1a

Please sign in to comment.