Deprecate is_something attributes (#912)

Deprecates the is_something attributes like is_bulb and is_dimmable in favour of the modular approach.
This commit is contained in:
Steven B
2024-05-13 18:52:08 +01:00
committed by GitHub
parent 33d839866e
commit ef49f44eac
8 changed files with 142 additions and 93 deletions

View File

@@ -226,21 +226,21 @@ class IotBulb(IotDevice):
@property # type: ignore
@requires_update
def is_color(self) -> bool:
def _is_color(self) -> bool:
"""Whether the bulb supports color changes."""
sys_info = self.sys_info
return bool(sys_info["is_color"])
@property # type: ignore
@requires_update
def is_dimmable(self) -> bool:
def _is_dimmable(self) -> bool:
"""Whether the bulb supports brightness changes."""
sys_info = self.sys_info
return bool(sys_info["is_dimmable"])
@property # type: ignore
@requires_update
def is_variable_color_temp(self) -> bool:
def _is_variable_color_temp(self) -> bool:
"""Whether the bulb supports color temperature changes."""
sys_info = self.sys_info
return bool(sys_info["is_variable_color_temp"])
@@ -252,7 +252,7 @@ class IotBulb(IotDevice):
:return: White temperature range in Kelvin (minimum, maximum)
"""
if not self.is_variable_color_temp:
if not self._is_variable_color_temp:
raise KasaException("Color temperature not supported")
for model, temp_range in TPLINK_KELVIN.items():
@@ -352,7 +352,7 @@ class IotBulb(IotDevice):
:return: hue, saturation and value (degrees, %, %)
"""
if not self.is_color:
if not self._is_color:
raise KasaException("Bulb does not support color.")
light_state = cast(dict, self.light_state)
@@ -379,7 +379,7 @@ class IotBulb(IotDevice):
:param int value: value in percentage [0, 100]
:param int transition: transition in milliseconds.
"""
if not self.is_color:
if not self._is_color:
raise KasaException("Bulb does not support color.")
if not isinstance(hue, int) or not (0 <= hue <= 360):
@@ -406,7 +406,7 @@ class IotBulb(IotDevice):
@requires_update
def color_temp(self) -> int:
"""Return color temperature of the device in kelvin."""
if not self.is_variable_color_temp:
if not self._is_variable_color_temp:
raise KasaException("Bulb does not support colortemp.")
light_state = self.light_state
@@ -421,7 +421,7 @@ class IotBulb(IotDevice):
:param int temp: The new color temperature, in Kelvin
:param int transition: transition in milliseconds.
"""
if not self.is_variable_color_temp:
if not self._is_variable_color_temp:
raise KasaException("Bulb does not support colortemp.")
valid_temperature_range = self.valid_temperature_range
@@ -446,7 +446,7 @@ class IotBulb(IotDevice):
@requires_update
def brightness(self) -> int:
"""Return the current brightness in percentage."""
if not self.is_dimmable: # pragma: no cover
if not self._is_dimmable: # pragma: no cover
raise KasaException("Bulb is not dimmable.")
light_state = self.light_state
@@ -461,7 +461,7 @@ class IotBulb(IotDevice):
:param int brightness: brightness in percent
:param int transition: transition in milliseconds.
"""
if not self.is_dimmable: # pragma: no cover
if not self._is_dimmable: # pragma: no cover
raise KasaException("Bulb is not dimmable.")
self._raise_for_invalid_brightness(brightness)

View File

@@ -96,7 +96,7 @@ class IotDimmer(IotPlug):
Will return a range between 0 - 100.
"""
if not self.is_dimmable:
if not self._is_dimmable:
raise KasaException("Device is not dimmable.")
sys_info = self.sys_info
@@ -109,7 +109,7 @@ class IotDimmer(IotPlug):
:param int transition: transition duration in milliseconds.
Using a transition will cause the dimmer to turn on.
"""
if not self.is_dimmable:
if not self._is_dimmable:
raise KasaException("Device is not dimmable.")
if not isinstance(brightness, int):
@@ -218,7 +218,7 @@ class IotDimmer(IotPlug):
@property # type: ignore
@requires_update
def is_dimmable(self) -> bool:
def _is_dimmable(self) -> bool:
"""Whether the switch supports brightness changes."""
sys_info = self.sys_info
return "brightness" in sys_info

View File

@@ -4,6 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, cast
from ...device_type import DeviceType
from ...exceptions import KasaException
from ...feature import Feature
from ...interfaces.light import HSV, ColorTempRange
@@ -78,14 +79,19 @@ class Light(IotModule, LightInterface):
return {}
def _get_bulb_device(self) -> IotBulb | None:
if self._device.is_bulb or self._device.is_light_strip:
"""For type checker this gets an IotBulb.
IotDimmer is not a subclass of IotBulb and using isinstance
here at runtime would create a circular import.
"""
if self._device.device_type in {DeviceType.Bulb, DeviceType.LightStrip}:
return cast("IotBulb", self._device)
return None
@property # type: ignore
def is_dimmable(self) -> int:
"""Whether the bulb supports brightness changes."""
return self._device.is_dimmable
return self._device._is_dimmable
@property # type: ignore
def brightness(self) -> int:
@@ -107,14 +113,14 @@ class Light(IotModule, LightInterface):
"""Whether the light supports color changes."""
if (bulb := self._get_bulb_device()) is None:
return False
return bulb.is_color
return bulb._is_color
@property
def is_variable_color_temp(self) -> bool:
"""Whether the bulb supports color temperature changes."""
if (bulb := self._get_bulb_device()) is None:
return False
return bulb.is_variable_color_temp
return bulb._is_variable_color_temp
@property
def has_effects(self) -> bool:
@@ -129,7 +135,7 @@ class Light(IotModule, LightInterface):
:return: hue, saturation and value (degrees, %, %)
"""
if (bulb := self._get_bulb_device()) is None or not bulb.is_color:
if (bulb := self._get_bulb_device()) is None or not bulb._is_color:
raise KasaException("Light does not support color.")
return bulb.hsv
@@ -150,7 +156,7 @@ class Light(IotModule, LightInterface):
:param int value: value in percentage [0, 100]
:param int transition: transition in milliseconds.
"""
if (bulb := self._get_bulb_device()) is None or not bulb.is_color:
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)
@@ -160,14 +166,18 @@ class Light(IotModule, LightInterface):
:return: White temperature range in Kelvin (minimum, maximum)
"""
if (bulb := self._get_bulb_device()) is None or not bulb.is_variable_color_temp:
if (
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
@property
def color_temp(self) -> int:
"""Whether the bulb supports color temperature changes."""
if (bulb := self._get_bulb_device()) is None or not bulb.is_variable_color_temp:
if (
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
@@ -181,7 +191,9 @@ class Light(IotModule, LightInterface):
:param int temp: The new color temperature, in Kelvin
:param int transition: transition in milliseconds.
"""
if (bulb := self._get_bulb_device()) is None or not bulb.is_variable_color_temp:
if (
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(
temp, brightness=brightness, transition=transition