Deprecate device level light, effect and led attributes (#916)

Deprecates the attributes at device level for light, light effects, and led. i.e. device.led, device.is_color. Will continue to support consumers using these attributes and emit a warning.
This commit is contained in:
Steven B
2024-05-15 18:49:08 +01:00
committed by GitHub
parent 133a839f22
commit a2e8d2c4e8
10 changed files with 232 additions and 172 deletions

View File

@@ -247,7 +247,7 @@ class IotBulb(IotDevice):
@property # type: ignore
@requires_update
def valid_temperature_range(self) -> ColorTempRange:
def _valid_temperature_range(self) -> ColorTempRange:
"""Return the device-specific white temperature range (in Kelvin).
:return: White temperature range in Kelvin (minimum, maximum)
@@ -284,7 +284,7 @@ class IotBulb(IotDevice):
@property # type: ignore
@requires_update
def has_effects(self) -> bool:
def _has_effects(self) -> bool:
"""Return True if the device supports effects."""
return "lighting_effect_state" in self.sys_info
@@ -347,7 +347,7 @@ class IotBulb(IotDevice):
@property # type: ignore
@requires_update
def hsv(self) -> HSV:
def _hsv(self) -> HSV:
"""Return the current HSV state of the bulb.
:return: hue, saturation and value (degrees, %, %)
@@ -364,7 +364,7 @@ class IotBulb(IotDevice):
return HSV(hue, saturation, value)
@requires_update
async def set_hsv(
async def _set_hsv(
self,
hue: int,
saturation: int,
@@ -404,7 +404,7 @@ class IotBulb(IotDevice):
@property # type: ignore
@requires_update
def color_temp(self) -> int:
def _color_temp(self) -> int:
"""Return color temperature of the device in kelvin."""
if not self._is_variable_color_temp:
raise KasaException("Bulb does not support colortemp.")
@@ -413,7 +413,7 @@ class IotBulb(IotDevice):
return int(light_state["color_temp"])
@requires_update
async def set_color_temp(
async def _set_color_temp(
self, temp: int, *, brightness=None, transition: int | None = None
) -> dict:
"""Set the color temperature of the device in kelvin.
@@ -444,7 +444,7 @@ class IotBulb(IotDevice):
@property # type: ignore
@requires_update
def brightness(self) -> int:
def _brightness(self) -> int:
"""Return the current brightness in percentage."""
if not self._is_dimmable: # pragma: no cover
raise KasaException("Bulb is not dimmable.")
@@ -453,7 +453,7 @@ class IotBulb(IotDevice):
return int(light_state["brightness"])
@requires_update
async def set_brightness(
async def _set_brightness(
self, brightness: int, *, transition: int | None = None
) -> dict:
"""Set the brightness in percentage.

View File

@@ -91,7 +91,7 @@ class IotDimmer(IotPlug):
@property # type: ignore
@requires_update
def brightness(self) -> int:
def _brightness(self) -> int:
"""Return current brightness on dimmers.
Will return a range between 0 - 100.
@@ -103,7 +103,7 @@ class IotDimmer(IotPlug):
return int(sys_info["brightness"])
@requires_update
async def set_brightness(self, brightness: int, *, transition: int | None = None):
async def _set_brightness(self, brightness: int, *, transition: int | None = None):
"""Set the new dimmer brightness level in percentage.
:param int transition: transition duration in milliseconds.
@@ -222,3 +222,13 @@ class IotDimmer(IotPlug):
"""Whether the switch supports brightness changes."""
sys_info = self.sys_info
return "brightness" in sys_info
@property
def _is_variable_color_temp(self) -> bool:
"""Whether the device supports variable color temp."""
return False
@property
def _is_color(self) -> bool:
"""Whether the device supports color."""
return False

View File

@@ -6,9 +6,8 @@ from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..module import Module
from ..protocol import BaseProtocol
from .effects import EFFECT_NAMES_V1
from .iotbulb import IotBulb
from .iotdevice import KasaException, requires_update
from .iotdevice import requires_update
from .modules.lighteffect import LightEffect
@@ -70,68 +69,3 @@ class IotLightStrip(IotBulb):
def length(self) -> int:
"""Return length of the strip."""
return self.sys_info["length"]
@property # type: ignore
@requires_update
def effect(self) -> dict:
"""Return effect state.
Example:
{'brightness': 50,
'custom': 0,
'enable': 0,
'id': '',
'name': ''}
"""
# LightEffectModule returns the current effect name
# so return the dict here for backwards compatibility
return self.sys_info["lighting_effect_state"]
@property # type: ignore
@requires_update
def effect_list(self) -> list[str] | None:
"""Return built-in effects list.
Example:
['Aurora', 'Bubbling Cauldron', ...]
"""
# LightEffectModule returns effect names along with a LIGHT_EFFECTS_OFF value
# so return the original effect names here for backwards compatibility
return EFFECT_NAMES_V1 if self.has_effects else None
@requires_update
async def set_effect(
self,
effect: str,
*,
brightness: int | None = None,
transition: int | None = None,
) -> None:
"""Set an effect on the device.
If brightness or transition is defined,
its value will be used instead of the effect-specific default.
See :meth:`effect_list` for available effects,
or use :meth:`set_custom_effect` for custom effects.
:param str effect: The effect to set
:param int brightness: The wanted brightness
:param int transition: The wanted transition time
"""
await self.modules[Module.LightEffect].set_effect(
effect, brightness=brightness, transition=transition
)
@requires_update
async def set_custom_effect(
self,
effect_dict: dict,
) -> None:
"""Set a custom effect on the device.
:param str effect_dict: The custom effect dict to set
"""
if not self.has_effects:
raise KasaException("Bulb does not support effects.")
await self.modules[Module.LightEffect].set_custom_effect(effect_dict)

View File

@@ -79,16 +79,6 @@ class IotPlug(IotDevice):
"""Turn the switch off."""
return await self._query_helper("system", "set_relay_state", {"state": 0})
@property # type: ignore
@requires_update
def led(self) -> bool:
"""Return the state of the led."""
return self.modules[Module.Led].led
async def set_led(self, state: bool):
"""Set the state of the led (night mode)."""
return await self.modules[Module.Led].set_led(state)
class IotWallSwitch(IotPlug):
"""Representation of a TP-Link Smart Wall Switch."""

View File

@@ -147,17 +147,6 @@ class IotStrip(IotDevice):
return max(plug.on_since for plug in self.children if plug.on_since is not None)
@property # type: ignore
@requires_update
def led(self) -> bool:
"""Return the state of the led."""
sys_info = self.sys_info
return bool(1 - sys_info["led_off"])
async def set_led(self, state: bool):
"""Set the state of the led (night mode)."""
await self._query_helper("system", "set_led_off", {"off": int(not state)})
async def current_consumption(self) -> float:
"""Get the current power consumption in watts."""
return sum([await plug.current_consumption() for plug in self.children])

View File

@@ -30,7 +30,7 @@ class Light(IotModule, LightInterface):
super()._initialize_features()
device = self._device
if self._device.is_dimmable:
if self._device._is_dimmable:
self._add_feature(
Feature(
device,
@@ -45,7 +45,7 @@ class Light(IotModule, LightInterface):
category=Feature.Category.Primary,
)
)
if self._device.is_variable_color_temp:
if self._device._is_variable_color_temp:
self._add_feature(
Feature(
device=device,
@@ -59,7 +59,7 @@ class Light(IotModule, LightInterface):
type=Feature.Type.Number,
)
)
if self._device.is_color:
if self._device._is_color:
self._add_feature(
Feature(
device=device,
@@ -96,7 +96,7 @@ class Light(IotModule, LightInterface):
@property # type: ignore
def brightness(self) -> int:
"""Return the current brightness in percentage."""
return self._device.brightness
return self._device._brightness
async def set_brightness(
self, brightness: int, *, transition: int | None = None
@@ -106,7 +106,7 @@ class Light(IotModule, LightInterface):
:param int brightness: brightness in percent
:param int transition: transition in milliseconds.
"""
return await self._device.set_brightness(brightness, transition=transition)
return await self._device._set_brightness(brightness, transition=transition)
@property
def is_color(self) -> bool:
@@ -127,7 +127,7 @@ class Light(IotModule, LightInterface):
"""Return True if the device supports effects."""
if (bulb := self._get_bulb_device()) is None:
return False
return bulb.has_effects
return bulb._has_effects
@property
def hsv(self) -> HSV:
@@ -137,7 +137,7 @@ class Light(IotModule, LightInterface):
"""
if (bulb := self._get_bulb_device()) is None or not bulb._is_color:
raise KasaException("Light does not support color.")
return bulb.hsv
return bulb._hsv
async def set_hsv(
self,
@@ -158,7 +158,7 @@ class Light(IotModule, LightInterface):
"""
if (bulb := self._get_bulb_device()) is None or not bulb._is_color:
raise KasaException("Light does not support color.")
return await bulb.set_hsv(hue, saturation, value, transition=transition)
return await bulb._set_hsv(hue, saturation, value, transition=transition)
@property
def valid_temperature_range(self) -> ColorTempRange:
@@ -170,7 +170,7 @@ class Light(IotModule, LightInterface):
bulb := self._get_bulb_device()
) is None or not bulb._is_variable_color_temp:
raise KasaException("Light does not support colortemp.")
return bulb.valid_temperature_range
return bulb._valid_temperature_range
@property
def color_temp(self) -> int:
@@ -179,7 +179,7 @@ class Light(IotModule, LightInterface):
bulb := self._get_bulb_device()
) is None or not bulb._is_variable_color_temp:
raise KasaException("Light does not support colortemp.")
return bulb.color_temp
return bulb._color_temp
async def set_color_temp(
self, temp: int, *, brightness=None, transition: int | None = None
@@ -195,6 +195,6 @@ class Light(IotModule, LightInterface):
bulb := self._get_bulb_device()
) is None or not bulb._is_variable_color_temp:
raise KasaException("Light does not support colortemp.")
return await bulb.set_color_temp(
return await bulb._set_color_temp(
temp, brightness=brightness, transition=transition
)

View File

@@ -94,3 +94,29 @@ class LightEffect(IotModule, LightEffectInterface):
def query(self):
"""Return the base query."""
return {}
@property # type: ignore
def _deprecated_effect(self) -> dict:
"""Return effect state.
Example:
{'brightness': 50,
'custom': 0,
'enable': 0,
'id': '',
'name': ''}
"""
# LightEffectModule returns the current effect name
# so return the dict here for backwards compatibility
return self.data["lighting_effect_state"]
@property # type: ignore
def _deprecated_effect_list(self) -> list[str] | None:
"""Return built-in effects list.
Example:
['Aurora', 'Bubbling Cauldron', ...]
"""
# LightEffectModule returns effect names along with a LIGHT_EFFECTS_OFF value
# so return the original effect names here for backwards compatibility
return EFFECT_NAMES_V1