-
-
Notifications
You must be signed in to change notification settings - Fork 263
Fix SMARTCAM Time module and update tests #1659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
23d858f
b2e8547
1a74dcc
36ab4d1
27f48b1
30c8bca
39d734c
cccc701
89ec13c
ead73f5
0e5ded6
aef9930
fe59298
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -488,13 +488,19 @@ async def test_time_set(dev: Device, mocker, runner): | |
| time_mod = dev.modules[Module.Time] | ||
| set_time_mock = mocker.spy(time_mod, "set_time") | ||
| dt = datetime(2024, 10, 15, 8, 15) | ||
| initial_timezone = time_mod.timezone | ||
| res = await runner.invoke( | ||
| time, | ||
| ["set", str(dt.year), str(dt.month), str(dt.day), str(dt.hour), str(dt.minute)], | ||
| obj=dev, | ||
| ) | ||
| set_time_mock.assert_called() | ||
| assert time_mod.time == dt.replace(tzinfo=time_mod.timezone) | ||
| if isinstance(dev, SmartCamDevice): | ||
| assert time_mod.timezone.utcoffset(time_mod.time) == initial_timezone.utcoffset( | ||
| time_mod.time | ||
| ) | ||
| else: | ||
| assert time_mod.time == dt.replace(tzinfo=time_mod.timezone) | ||
|
Comment on lines
+498
to
+503
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no need for special handling in the cli tests. Or it should be covered with a separate, explicit test. |
||
|
|
||
| assert res.exit_code == 0 | ||
| assert "Old time: " in res.output | ||
|
|
@@ -518,7 +524,11 @@ async def test_time_set(dev: Device, mocker, runner): | |
| obj=dev, | ||
| ) | ||
|
|
||
| assert time_mod.time == dt | ||
| if isinstance(dev, SmartCamDevice): | ||
| assert isinstance(time_mod.timezone, ZoneInfo) | ||
| assert time_mod.timezone.key == zone.key | ||
| else: | ||
| assert time_mod.time == dt | ||
|
|
||
| assert res.exit_code == 0 | ||
| assert "Old time: " in res.output | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import importlib | ||
| import inspect | ||
| import logging | ||
| import pkgutil | ||
| import sys | ||
| from datetime import UTC, datetime, timedelta, timezone | ||
|
|
@@ -12,6 +13,7 @@ | |
| import kasa.interfaces | ||
| from kasa import Device, KasaException, LightState, Module, ThermostatState | ||
| from kasa.module import _get_feature_attribute | ||
| from kasa.smartcam import SmartCamDevice | ||
|
|
||
| from .device_fixtures import ( | ||
| bulb_iot, | ||
|
|
@@ -422,7 +424,7 @@ async def test_thermostat(dev: Device, mocker: MockerFixture): | |
| assert therm_mod.temperature_unit == "fahrenheit" | ||
|
|
||
|
|
||
| async def test_set_time(dev: Device): | ||
| async def test_set_time(dev: Device, caplog: pytest.LogCaptureFixture): | ||
| """Test setting the device time.""" | ||
| time_mod = dev.modules[Module.Time] | ||
|
|
||
|
|
@@ -435,9 +437,18 @@ async def test_set_time(dev: Device): | |
| try: | ||
| assert time_mod.time != test_time | ||
|
|
||
| await time_mod.set_time(test_time) | ||
| caplog.clear() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to |
||
| with caplog.at_level(logging.WARNING): | ||
| await time_mod.set_time(test_time) | ||
| await dev.update() | ||
| assert time_mod.time == test_time | ||
| if isinstance(dev, SmartCamDevice): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do really like clean code, so adding a separate test for handling the special behavior would be a better approach.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-worked the tests here to work with smartcam devices only for testing this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nicer to add a separate test for the special "logs a warning" case that to make it explicit, and skip this test for smartcam devices. This way we can cover both paths without having a need for conditionals in tests which are most of the time a bad idea in tests. |
||
| assert ( | ||
| "SmartCam devices do not support setting clock time directly; " | ||
| "only timezone settings are updated." | ||
| ) in caplog.text | ||
| assert time_mod.timezone == test_time.tzinfo | ||
| else: | ||
| assert time_mod.time == test_time | ||
|
|
||
| if ( | ||
| isinstance(original_timezone, ZoneInfo) | ||
|
|
@@ -451,12 +462,18 @@ async def test_set_time(dev: Device): | |
| new_time = time_mod.time.astimezone(test_zonezone) | ||
| await time_mod.set_time(new_time) | ||
| await dev.update() | ||
| assert time_mod.time == new_time | ||
| if isinstance(dev, SmartCamDevice): | ||
| assert time_mod.timezone == new_time.tzinfo | ||
| else: | ||
| assert time_mod.time == new_time | ||
|
Comment on lines
+465
to
+468
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment above; it's better to add separate tests for the special cases than adding conditonals with isinstance. |
||
| finally: | ||
| # Reset back to the original | ||
| await time_mod.set_time(original_time) | ||
| await dev.update() | ||
| assert time_mod.time == original_time | ||
| if isinstance(dev, SmartCamDevice): | ||
| assert time_mod.timezone == original_timezone | ||
| else: | ||
| assert time_mod.time == original_time | ||
|
|
||
|
|
||
| async def test_time_post_update_no_time_uses_utc_unit(monkeypatch: pytest.MonkeyPatch): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests for this function to the relevant test_time.py file with a parametrized set of test cases.