Deprecate legacy light module is_capability checks (#1297)

Deprecate the `is_color`, `is_dimmable`, `is_variable_color_temp`, `valid_temperate_range`,
and `has_effects` attributes from the `Light` module, as consumers should use
`has_feature("hsv")`, `has_feature("brightness")`, `has_feature("color_temp")`,
`get_feature("color_temp").range`, and `Module.LightEffect in dev.modules` respectively.

Calling the deprecated attributes will emit a `DeprecationWarning`
and type checkers will fail them.
This commit is contained in:
Steven B.
2025-01-22 10:26:37 +00:00
committed by GitHub
parent a03a4b1d63
commit fa0f7157c6
5 changed files with 161 additions and 109 deletions

View File

@@ -1,5 +1,9 @@
from __future__ import annotations
import re
from collections.abc import Callable
from contextlib import nullcontext
import pytest
from kasa import Device, DeviceType, KasaException, Module
@@ -180,3 +184,67 @@ async def test_non_variable_temp(dev: Device):
@bulb
def test_device_type_bulb(dev: Device):
assert dev.device_type in {DeviceType.Bulb, DeviceType.LightStrip}
@pytest.mark.parametrize(
("attribute", "use_msg", "use_fn"),
[
pytest.param(
"is_color",
'use has_feature("hsv") instead',
lambda device, mod: mod.has_feature("hsv"),
id="is_color",
),
pytest.param(
"is_dimmable",
'use has_feature("brightness") instead',
lambda device, mod: mod.has_feature("brightness"),
id="is_dimmable",
),
pytest.param(
"is_variable_color_temp",
'use has_feature("color_temp") instead',
lambda device, mod: mod.has_feature("color_temp"),
id="is_variable_color_temp",
),
pytest.param(
"has_effects",
"check `Module.LightEffect in device.modules` instead",
lambda device, mod: Module.LightEffect in device.modules,
id="has_effects",
),
],
)
@bulb
async def test_deprecated_light_is_has_attributes(
dev: Device, attribute: str, use_msg: str, use_fn: Callable[[Device, Module], bool]
):
light = dev.modules.get(Module.Light)
assert light
msg = f"{attribute} is deprecated, {use_msg}"
with pytest.deprecated_call(match=(re.escape(msg))):
result = getattr(light, attribute)
assert result == use_fn(dev, light)
@bulb
async def test_deprecated_light_valid_temperature_range(dev: Device):
light = dev.modules.get(Module.Light)
assert light
color_temp = light.has_feature("color_temp")
dep_msg = (
"valid_temperature_range is deprecated, use "
'get_feature("color_temp") minimum_value '
" and maximum_value instead"
)
exc_context = pytest.raises(KasaException, match="Color temperature not supported")
expected_context = nullcontext() if color_temp else exc_context
with (
expected_context,
pytest.deprecated_call(match=(re.escape(dep_msg))),
):
assert light.valid_temperature_range # type: ignore[attr-defined]