Correct Quality Issues

- Fix: Decrease log message severity in `feature.py`.
- Fix: Rename `_set_range_cli` to `_set_range_from_str`.
- Add: Basic tests for string to Motion Range value converter function.
This commit is contained in:
Ryan Nitcher 2024-12-06 19:09:30 -07:00
parent 28080067db
commit 5dd71dde39
4 changed files with 53 additions and 6 deletions

View File

@ -279,7 +279,7 @@ class Feature:
if self.type == Feature.Type.Choice:
if not isinstance(choices, list):
_LOGGER.critical(
_LOGGER.error(
"Choices are not properly defined for %s (%s). Type: <%s> Value: %s", # noqa: E501
self.name,
self.id,

View File

@ -124,7 +124,7 @@ class Motion(IotModule):
name="Motion Sensor Range",
icon="mdi:motion-sensor",
attribute_getter="range",
attribute_setter="_set_range_cli",
attribute_setter="_set_range_from_str",
type=Feature.Type.Choice,
choices_getter="ranges",
category=Feature.Category.Config,
@ -336,7 +336,7 @@ class Motion(IotModule):
)
return Range[value]
async def _set_range_cli(self, input: str) -> dict:
async def _set_range_from_str(self, input: str) -> dict:
value = self._parse_range_value(input)
return await self.set_range(range=value)

View File

@ -1,6 +1,7 @@
import pytest
from pytest_mock import MockerFixture
from kasa import Module
from kasa import KasaException, Module
from kasa.iot import IotDimmer
from kasa.iot.modules.motion import Motion, Range
@ -45,6 +46,33 @@ async def test_motion_range(dev: IotDimmer, mocker: MockerFixture):
)
@dimmer_iot
async def test_motion_range_from_string(dev: IotDimmer, mocker: MockerFixture):
motion: Motion = dev.modules[Module.IotMotion]
query_helper = mocker.patch("kasa.iot.IotDimmer._query_helper")
ranges_good = {
"near": Range.Near,
"MID": Range.Mid,
"fAr": Range.Far,
" Custom ": Range.Custom,
}
for range_str, range in ranges_good.items():
await motion._set_range_from_str(range_str)
query_helper.assert_called_with(
"smartlife.iot.PIR",
"set_trigger_sens",
{"index": range.value},
)
query_helper = mocker.patch("kasa.iot.IotDimmer._query_helper")
ranges_bad = ["near1", "MD", "F\nAR", "Custom Near", '"FAR"', "'FAR'"]
for range_str in ranges_bad:
with pytest.raises(KasaException):
await motion._set_range_from_str(range_str)
query_helper.assert_not_called()
@dimmer_iot
async def test_motion_threshold(dev: IotDimmer, mocker: MockerFixture):
motion: Motion = dev.modules[Module.IotMotion]

View File

@ -1124,7 +1124,7 @@ async def test_feature_set_unquoted(mocker, runner):
dummy_device = await get_device_for_fixture_protocol(
"ES20M(US)_1.0_1.0.11.json", "IOT"
)
range_setter = mocker.patch("kasa.iot.modules.motion.Motion._set_range_cli")
range_setter = mocker.patch("kasa.iot.modules.motion.Motion._set_range_from_str")
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
res = await runner.invoke(
@ -1143,7 +1143,7 @@ async def test_feature_set_badquoted(mocker, runner):
dummy_device = await get_device_for_fixture_protocol(
"ES20M(US)_1.0_1.0.11.json", "IOT"
)
range_setter = mocker.patch("kasa.iot.modules.motion.Motion._set_range_cli")
range_setter = mocker.patch("kasa.iot.modules.motion.Motion._set_range_from_str")
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
res = await runner.invoke(
@ -1157,6 +1157,25 @@ async def test_feature_set_badquoted(mocker, runner):
assert res.exit_code != 0
async def test_feature_set_goodquoted(mocker, runner):
"""Test feature command's set value."""
dummy_device = await get_device_for_fixture_protocol(
"ES20M(US)_1.0_1.0.11.json", "IOT"
)
range_setter = mocker.patch("kasa.iot.modules.motion.Motion._set_range_from_str")
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
res = await runner.invoke(
cli,
["--host", "127.0.0.123", "--debug", "feature", "pir_range", "'Far'"],
catch_exceptions=False,
)
range_setter.assert_called()
assert "Error: Invalid value: " not in res.output
assert res.exit_code == 0
async def test_cli_child_commands(
dev: Device, runner: CliRunner, mocker: MockerFixture
):