Skip to content
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

Add time sync command #951

Merged
merged 7 commits into from
Jun 17, 2024
Merged

Add time sync command #951

merged 7 commits into from
Jun 17, 2024

Conversation

rytilahti
Copy link
Member

@rytilahti rytilahti commented Jun 4, 2024

Allows setting the device time (on SMART devices) to the current time.
Fixes also setting the time which was previously completely broken.

TBD:

  • Add tests
  • Update docs to instruct to set the time when getting errors on non-provisioned devices

Fixes #852
Fixes #818

Fixes also setting the time.
kasa/cli.py Outdated Show resolved Hide resolved
Comment on lines +59 to +60
"timestamp": int(unixtime),
"time_diff": int(diff),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not ever working previously due to incorrect types.

@rytilahti rytilahti added enhancement New feature or request labels Jun 4, 2024
@rytilahti
Copy link
Member Author

rytilahti commented Jun 4, 2024

I'll mark this for 0.7.0, as it would be a good idea to get this in (even when it'd be limited to smart devices) as having time set is necessary for many features to function. Instructing users with issues is easier when a single command acts as a quick fix, instead of guiding how to use kasa command to execute set_device_time manually.

Copy link

codecov bot commented Jun 5, 2024

Codecov Report

Attention: Patch coverage is 86.36364% with 3 lines in your changes missing coverage. Please review.

Project coverage is 91.49%. Comparing base (2234738) to head (e8d4a68).
Report is 23 commits behind head on master.

Files Patch % Lines
kasa/cli.py 85.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #951      +/-   ##
==========================================
+ Coverage   91.36%   91.49%   +0.13%     
==========================================
  Files          83       83              
  Lines        5514     5564      +50     
  Branches     1343     1357      +14     
==========================================
+ Hits         5038     5091      +53     
+ Misses        374      368       -6     
- Partials      102      105       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rytilahti rytilahti requested a review from sdb9696 June 14, 2024 21:42
@rytilahti rytilahti marked this pull request as ready for review June 14, 2024 21:42
Copy link
Collaborator

@sdb9696 sdb9696 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this is setting the time based on the devices existing region? Why wouldn’t region be synced with the region of the device calling sync? Which I think would be obtained using datetime.now(timezone.utc).astime() but I can’t check right now.

@sdb9696
Copy link
Collaborator

sdb9696 commented Jun 15, 2024

If the device is connected to the cloud does this still work? If it works will it get overwritten with a ntp time shortly afterwards? Depending on the answers maybe a message from the cli telling the user it’s a noop if cloud connected or similar.

@rytilahti
Copy link
Member Author

IIUC this is setting the time based on the devices existing region? Why wouldn’t region be synced with the region of the device calling sync?

It's setting it based on the UTC time, as we currently do not have proper information about the timezone (when it's not defined). I think doing this properly would require us to introduce dependency on zoneinfo, perhaps we should do just that already here? I.e., install the backports version for <3.9 and do a try-except hack to have it available on all supported versions.

After cherry-picking the idx-timezone mapping from the schedule PR, we can properly use proper timezones everywhere throughout the code base.

If the device is connected to the cloud does this still work? If it works will it get overwritten with a ntp time shortly afterwards? Depending on the answers maybe a message from the cli telling the user it’s a noop if cloud connected or similar.

I'd guess it'd be overwritten using the NTP, but I think that this is generally only useful for those people who do not ever want to connect their devices to the cloud.

@rytilahti
Copy link
Member Author

You are right, datetime.now().astimezone().tzinfo delivers the current timezone information so now my devices are showing their times correctly & I updated the PR accordingly :-)

Copy link
Collaborator

@sdb9696 sdb9696 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sdb9696 sdb9696 merged commit 867b7b8 into master Jun 17, 2024
26 of 27 checks passed
@sdb9696 sdb9696 deleted the feat/set_time_smart branch June 17, 2024 08:37
@sdb9696 sdb9696 mentioned this pull request Jun 17, 2024
sdb9696 added a commit that referenced this pull request Jun 17, 2024
## [0.7.0.dev5](https://github.com/python-kasa/python-kasa/tree/0.7.0.dev5) (2024-06-17)

[Full Changelog](0.7.0.dev4...0.7.0.dev5)

**Implemented enhancements:**

- Add timezone to on\_since attributes [\#978](#978) (@sdb9696)
- Add common energy module and deprecate device emeter attributes [\#976](#976) (@sdb9696)
- Add type hints to feature set\_value [\#974](#974) (@sdb9696)
- Handle unknown light effect names and only calculate effect list once [\#973](#973) (@sdb9696)
- Add time sync command [\#951](#951) (@rytilahti)

**Fixed bugs:**

- Disallow non-targeted device commands [\#982](#982) (@rytilahti)
- Add supported check to light transition module [\#971](#971) (@sdb9696)

**Project maintenance:**

- Add fixture for L920-5\(EU\) 1.0.7 [\#972](#972) (@rytilahti)
@sdb9696 sdb9696 mentioned this pull request Jun 21, 2024
sdb9696 added a commit that referenced this pull request Jun 23, 2024
## [0.7.0](https://github.com/python-kasa/python-kasa/tree/0.7.0) (2024-06-23)

[Full Changelog](0.6.2.1...0.7.0)

We have been working hard behind the scenes to make this major release possible.
This release brings a major refactoring of the library to serve the ever-growing list of supported devices and paves the way for the future, yet unsupported devices.
The library now exposes device features through generic module and feature interfaces, that allows easy extension for future improvements.

With almost 180 merged pull requests, over 200 changed files and  since the last release, this release includes lots of goodies for everyone:
* Support for multi-functional devices like the dimmable fan KS240.
* Initial support for hubs and hub-connected devices like thermostats and sensors.
* Both IOT (legacy kasa) and SMART (tapo and newer kasa) devices now expose features and share common API.
* Modules to allow controlling new devices and functions such as light presets, fan controls, thermostats, humidity sensors, firmware updates and alarms.
* The common APIs allow dynamic introspection of available device features, making it easy to create dynamic interfaces.
* Improved documentation.

Hope you enjoy the release, feel free to leave a comment and feedback!

If you have a device that works, but is not listed in our supported devices list, feel free to [contribute fixture files](https://python-kasa.readthedocs.io/en/latest/contribute.html#contributing-fixture-files) to help us to make the library even better!

> git diff 0.6.2.1..HEAD|diffstat
> 214 files changed, 26960 insertions(+), 6310 deletions(-)

For more information on the changes please checkout our [documentation on the API changes](https://python-kasa.readthedocs.io/en/latest/deprecated.html)

**Breaking changes:**

- Add common energy module and deprecate device emeter attributes [\#976](#976) (@sdb9696)
- Move SmartBulb into SmartDevice [\#874](#874) (@sdb9696)
- Change state\_information to return feature values [\#804](#804) (@rytilahti)
- Remove SmartPlug in favor of SmartDevice [\#781](#781) (@rytilahti)
- Add generic interface for accessing device features [\#741](#741) (@rytilahti)

**Implemented enhancements:**

- Cleanup cli output [\#1000](#1000) (@rytilahti)
- Improve autooff name and unit [\#997](#997) (@rytilahti)
- Update mode, time, rssi and report\_interval feature names/units [\#995](#995) (@sdb9696)
- Add unit\_getter for feature [\#993](#993) (@rytilahti)
- Add timezone to on\_since attributes [\#978](#978) (@sdb9696)
- Add type hints to feature set\_value [\#974](#974) (@sdb9696)
- Handle unknown light effect names and only calculate effect list once [\#973](#973) (@sdb9696)
- Support smart child modules queries [\#967](#967) (@sdb9696)
- Do not expose child modules on parent devices [\#964](#964) (@sdb9696)
- Do not add parent only modules to strip sockets [\#963](#963) (@sdb9696)
- Add time sync command [\#951](#951) (@rytilahti)
- Make device initialisation easier by reducing required imports [\#936](#936) (@sdb9696)
- Fix set\_state for common light modules [\#929](#929) (@sdb9696)
- Add state feature for iot devices [\#924](#924) (@rytilahti)
- Add post update hook to module and use in smart LightEffect [\#921](#921) (@sdb9696)
- Add LightEffect module for smart light strips [\#918](#918) (@sdb9696)
- Add light presets common module to devices. [\#907](#907) (@sdb9696)
- Improve categorization of features [\#904](#904) (@rytilahti)
- Create common interfaces for remaining device types [\#895](#895) (@sdb9696)
- Make get\_module return typed module [\#892](#892) (@sdb9696)
- Add LightEffectModule for dynamic light effects on SMART bulbs [\#887](#887) (@sdb9696)
- Implement choice feature type [\#880](#880) (@rytilahti)
- Add Fan interface for SMART devices [\#873](#873) (@sdb9696)
- Improve temperature controls [\#872](#872) (@rytilahti)
- Add precision\_hint to feature [\#871](#871) (@rytilahti)
- Be more lax on unknown SMART devices [\#863](#863) (@rytilahti)
- Handle paging of partial responses of lists like child\_device\_info [\#862](#862) (@sdb9696)
- Better firmware module support for devices not connected to the internet [\#854](#854) (@sdb9696)
- Re-query missing responses after multi request errors [\#850](#850) (@sdb9696)
- Implement action feature [\#849](#849) (@rytilahti)
- Add temperature control module for smart [\#848](#848) (@rytilahti)
- Implement feature categories [\#846](#846) (@rytilahti)
- Expose IOT emeter info as features [\#844](#844) (@rytilahti)
- Add support for feature units [\#843](#843) (@rytilahti)
- Add ColorModule for smart devices [\#840](#840) (@sdb9696)
- Add colortemp feature for iot devices [\#827](#827) (@rytilahti)
- Add support for firmware module v1 [\#821](#821) (@sdb9696)
- Add colortemp module [\#814](#814) (@rytilahti)
- Add iot brightness feature [\#808](#808) (@sdb9696)
- Revise device initialization and subsequent updates [\#807](#807) (@rytilahti)
- Add brightness module [\#806](#806) (@rytilahti)
- Support multiple child requests [\#795](#795) (@sdb9696)
- Support for on\_off\_gradually v2+ [\#793](#793) (@rytilahti)
- Improve smartdevice update module [\#791](#791) (@rytilahti)
- Add --child option to feature command [\#789](#789) (@rytilahti)
- Add temperature\_unit feature to t315 [\#788](#788) (@rytilahti)
- Add feature for ambient light sensor [\#787](#787) (@shifty35)
- Add initial support for H100 and T315 [\#776](#776) (@rytilahti)
- Generalize smartdevice child support [\#775](#775) (@rytilahti)
- Raise CLI errors in debug mode [\#771](#771) (@sdb9696)
- Add cloud module for smartdevice [\#767](#767) (@rytilahti)
- Add firmware module for smartdevice [\#766](#766) (@rytilahti)
- Add fan module [\#764](#764) (@rytilahti)
- Add smartdevice module for led controls [\#761](#761) (@rytilahti)
- Auto auto-off module for smartdevice [\#760](#760) (@rytilahti)
- Add smartdevice module for smooth transitions [\#759](#759) (@rytilahti)
- Initial implementation for modularized smartdevice [\#757](#757) (@rytilahti)
- Let caller handle SMART errors on multi-requests [\#754](#754) (@sdb9696)
- Add 'shell' command to cli [\#738](#738) (@rytilahti)

**Fixed bugs:**

- Fix smart led status to report rule status [\#1002](#1002) (@sdb9696)
- Demote device\_time back to debug [\#1001](#1001) (@rytilahti)
- Fix to call update when only --device-family passed to cli [\#987](#987) (@sdb9696)
- Disallow non-targeted device commands [\#982](#982) (@rytilahti)
- Add supported check to light transition module [\#971](#971) (@sdb9696)
- Fix switching off light effects for iot lights strips [\#961](#961) (@sdb9696)
- Add state features to iot strip sockets [\#960](#960) (@sdb9696)
- Ensure http delay logic works during default login attempt [\#959](#959) (@sdb9696)
- Fix fan speed level when off and derive smart fan module from common fan interface [\#957](#957) (@sdb9696)
- Require update in cli for wifi commands [\#956](#956) (@rytilahti)
- Do not raise on multi-request errors on child devices [\#949](#949) (@rytilahti)
- Do not show a zero error code when cli exits from showing help [\#935](#935) (@rytilahti)
- Initialize autooff features only when data is available [\#933](#933) (@rytilahti)
- Fix P100 errors on multi-requests [\#930](#930) (@sdb9696)
- Fix potential infinite loop if incomplete lists returned [\#920](#920) (@sdb9696)
- Add 'battery\_percentage' only when it's available [\#906](#906) (@rytilahti)
- Add missing alarm volume 'normal' [\#899](#899) (@rytilahti)
- Use Path.save for saving the fixtures [\#894](#894) (@rytilahti)
- Fix wifi scan re-querying error [\#891](#891) (@sdb9696)
- Fix --help on subcommands [\#886](#886) (@rytilahti)
- Fix smartprotocol response list handler to handle null reponses [\#884](#884) (@sdb9696)
- Improve feature setter robustness [\#870](#870) (@rytilahti)
- smartbulb: Limit brightness range to 1-100 [\#829](#829) (@rytilahti)
- Fix energy module calling get\_current\_power [\#798](#798) (@sdb9696)
- Fix auto update switch [\#786](#786) (@rytilahti)
- Retry query on 403 after successful handshake [\#785](#785) (@sdb9696)
- Ensure connections are closed when cli is finished [\#752](#752) (@sdb9696)
- Fix for P100 on fw 1.1.3 login\_version none [\#751](#751) (@sdb9696)
- Pass timeout parameters to discover\_single [\#744](#744) (@sdb9696)
- Reduce AuthenticationExceptions raising from transports [\#740](#740) (@sdb9696)
- Do not crash cli on missing discovery info [\#735](#735) (@rytilahti)
- Fix port-override for aes&klap transports [\#734](#734) (@rytilahti)
- Fix discovery cli to print devices not printed during discovery timeout [\#670](#670) (@sdb9696)

**Added support for devices:**

- Add fixture for L920-5\(EU\) 1.0.7 [\#972](#972) (@rytilahti)
- Add P115 fixture [\#950](#950) (@rytilahti)
- Add some device fixtures [\#948](#948) (@rytilahti)
- Add fixture for S505D [\#947](#947) (@rytilahti)
- Add fixture for p300 1.0.15 [\#915](#915) (@rytilahti)
- Add H100 1.5.10 and KE100 2.4.0 fixtures [\#905](#905) (@rytilahti)
- Add fixture for waterleak sensor T300 [\#897](#897) (@rytilahti)
- Add support for contact sensor \(T110\) [\#877](#877) (@rytilahti)
- Add support for waterleak sensor \(T300\) [\#876](#876) (@rytilahti)
- Add support for KH100 hub [\#847](#847) (@Adriandorr)
- Support for new ks240 fan/light wall switch [\#839](#839) (@sdb9696)
- Add P100 fw 1.4.0 fixture [\#820](#820) (@sdb9696)
- Add fixture for P110 sw 1.0.7 [\#801](#801) (@rytilahti)
- Add updated l530 fixture 1.1.6 [\#792](#792) (@rytilahti)
- Fix devtools for P100 and add fixture [\#753](#753) (@sdb9696)
- Add H100 fixtures [\#737](#737) (@rytilahti)

**Documentation updates:**

- Cleanup README to use the new cli format [\#999](#999) (@rytilahti)
- Add 0.7 api changes section to docs [\#996](#996) (@sdb9696)
- Update README to be more approachable for new users [\#994](#994) (@rytilahti)
- Update docs with more howto examples [\#968](#968) (@sdb9696)
- Update documentation structure and start migrating to markdown [\#934](#934) (@sdb9696)
- Add tutorial doctest module and enable top level await [\#919](#919) (@sdb9696)
- Add warning about tapo watchdog [\#902](#902) (@rytilahti)
- Move contribution instructions into docs [\#901](#901) (@rytilahti)
- Add rust tapo link to README [\#857](#857) (@rytilahti)
- Enable shell extra for installing ptpython and rich [\#782](#782) (@sdb9696)
- Add WallSwitch device type and autogenerate supported devices docs [\#758](#758) (@sdb9696)

**Project maintenance:**

- Drop python3.8 support [\#992](#992) (@rytilahti)
- Remove anyio dependency from pyproject.toml [\#990](#990) (@sdb9696)
- Configure mypy to run in virtual environment and fix resulting issues [\#989](#989) (@sdb9696)
- Better checking of child modules not supported by parent device [\#966](#966) (@sdb9696)
- Use freezegun for testing aes http client delays [\#954](#954) (@sdb9696)
- Fix passing custom port for dump\_devinfo [\#938](#938) (@rytilahti)
- Update release playbook [\#932](#932) (@rytilahti)
- Deprecate device level light, effect and led attributes [\#916](#916) (@sdb9696)
- Update cli to use common modules and remove iot specific cli testing [\#913](#913) (@sdb9696)
- Deprecate is\_something attributes [\#912](#912) (@sdb9696)
- Make Light and Fan a common module interface [\#911](#911) (@sdb9696)
- Rename bulb interface to light and move fan and light interface to interfaces [\#910](#910) (@sdb9696)
- Make module names consistent and remove redundant module casting [\#909](#909) (@sdb9696)
- Add child devices from hubs to generated list of supported devices [\#898](#898) (@sdb9696)
- Update interfaces so they all inherit from Device [\#893](#893) (@sdb9696)
- Update ks240 fixture with child device query info [\#890](#890) (@sdb9696)
- Use pydantic.v1 namespace on all pydantic versions [\#883](#883) (@rytilahti)
- Update dump\_devinfo to print original exception stack on errors. [\#882](#882) (@sdb9696)
- Put modules back on children for wall switches [\#881](#881) (@sdb9696)
- Fix pypy39 CI cache on macos [\#868](#868) (@sdb9696)
- Do not try coverage upload for pypy [\#867](#867) (@sdb9696)
- Add runner.arch to cache-key in CI [\#866](#866) (@sdb9696)
- Fix broken CI due to missing python version on macos-latest [\#864](#864) (@sdb9696)
- Fix incorrect state updates in FakeTestProtocols [\#861](#861) (@sdb9696)
- Embed FeatureType inside Feature [\#860](#860) (@rytilahti)
- Include component\_nego with child fixtures [\#858](#858) (@sdb9696)
- Use brightness module for smartbulb [\#853](#853) (@rytilahti)
- Ignore system environment variables for tests [\#851](#851) (@rytilahti)
- Remove mock fixtures [\#845](#845) (@rytilahti)
- Enable and convert to future annotations [\#838](#838) (@sdb9696)
- Update poetry locks and pre-commit hooks [\#837](#837) (@sdb9696)
- Cache pipx in CI and add custom setup action [\#835](#835) (@sdb9696)
- Fix non python 3.8 compliant test [\#832](#832) (@sdb9696)
- Fix CI issue with python version used by pipx to install poetry [\#831](#831) (@sdb9696)
- Refactor split smartdevice tests to test\_{iot,smart}device [\#822](#822) (@rytilahti)
- Add pre-commit caching and fix poetry extras cache [\#817](#817) (@sdb9696)
- Fix slow aestransport and cli tests [\#816](#816) (@sdb9696)
- Do not run coverage on pypy and cache poetry envs [\#812](#812) (@sdb9696)
- Update test framework for dynamic parametrization [\#810](#810) (@sdb9696)
- Put child fixtures in subfolder [\#809](#809) (@sdb9696)
- Simplify device \_\_repr\_\_ [\#805](#805) (@rytilahti)
- Add T315 fixture, tests for humidity&temperature modules [\#802](#802) (@rytilahti)
- Do not fail fast on pypy CI jobs [\#799](#799) (@sdb9696)
- Update dump\_devinfo to collect child device info [\#796](#796) (@sdb9696)
- Refactor test framework [\#794](#794) (@sdb9696)
- Add missing firmware module import [\#774](#774) (@rytilahti)
- Fix dump\_devinfo scrubbing for ks240 [\#765](#765) (@rytilahti)
- Rename and deprecate exception classes [\#739](#739) (@sdb9696)
- Refactor devices into subpackages and deprecate old names [\#716](#716) (@sdb9696)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error when connecting P110 to wi-fi Error when trying to discover new Tapo P110 plug
2 participants