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

Error when connecting P110 to wi-fi #852

Closed
Feirlane opened this issue Apr 20, 2024 · 3 comments · Fixed by #951
Closed

Error when connecting P110 to wi-fi #852

Feirlane opened this issue Apr 20, 2024 · 3 comments · Fixed by #951
Milestone

Comments

@Feirlane
Copy link

Greetings!

I've encountered a couple of problems when trying to connect a new, out-of-the-box, never before connected to the internet P110 to my wi-fi network:

When running the kasa --host 192.168.0.1 wifi join MySSID command, after querying me for the Keytype and Password, the program will error out with Raised error: 'get_device_time':

 $ kasa --host 192.168.0.1 wifi join MySSID
No --type or --device-family and --encrypt-type defined, discovering for 5 seconds..
Keytype: wpa2_psk
Password: 
Asking the device to connect to MySSID..
Raised error: 'get_device_time'
Run with --debug enabled to see stacktrace

Full stacktrace:

 $ kasa --debug --host 192.168.0.1 wifi join asd 
No --type or --device-family and --encrypt-type defined, discovering for 5 seconds..
DEBUG:kasa.discover:[DISCOVERY] 192.168.0.1 >> {'system': {'get_sysinfo': None}}
DEBUG:kasa.discover:Waiting a total of 5 seconds for responses...
DEBUG:kasa.aestransport:Created AES transport for 192.168.0.1
DEBUG:kasa.discover:[DISCOVERY] 192.168.0.1 << {'result': {'device_id': '***', 'owner': '', 'device_type': 'SMART.TAPOPLUG', 'device_model': 'P110(UK)', 'ip': '192.168.0.1', 'mac': '***', 'is_support_iot_cloud': True, 'obd_src': 'tplink', 'factory_default': False, 'mgt_encrypt_schm': {'is_support_https': False, 'encrypt_type': 'AES', 'http_port': 80, 'lv': 2}}, 'error_code': 0}
DEBUG:kasa.device:Initializing 192.168.0.1 of type <class 'kasa.smart.smartdevice.SmartDevice'>
Keytype: wpa2_psk
Password: 
Asking the device to connect to MySSID..
Raised error: 'get_device_time'
Traceback (most recent call last):
  File "/usr/bin/kasa", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 1205, in __call__
    return anyio.run(self._main, main, args, kwargs, **opts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/anyio/_core/_eventloop.py", line 73, in run
    return async_backend.run(func, args, {}, backend_options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2001, in run
    return runner.run(wrapper())
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 1989, in wrapper
    return await func(*args)
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 1208, in _main
    return await main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 1120, in main
    rv = await self.invoke(ctx)
         ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/kasa/cli.py", line 140, in invoke
    _handle_exception(self._debug, exc)
  File "/usr/lib/python3.11/site-packages/kasa/cli.py", line 138, in invoke
    return await super().invoke(ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 1739, in invoke
    return await _process_result(await sub_ctx.command.invoke(sub_ctx))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 1739, in invoke
    return await _process_result(await sub_ctx.command.invoke(sub_ctx))
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 1485, in invoke
    return await ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 824, in invoke
    rv = await rv
         ^^^^^^^^
  File "/usr/lib/python3.11/site-packages/asyncclick/core.py", line 824, in invoke
    rv = await rv
         ^^^^^^^^
  File "/usr/lib/python3.11/site-packages/kasa/cli.py", line 447, in join
    res = await dev.wifi_join(ssid, password, keytype=keytype)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/kasa/smart/smartdevice.py", line 471, in wifi_join
    "time": self.internal_state["get_device_time"],
            ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'get_device_time'

Looks like by the time wifi_join is called, the internal_state map is not filled-up yet. Managed to fix this one by adding the following line right before building the command payload in line 455 of smartdevice.py, although I must admit I'm not super familiar with the code of this package, so I'm not saying this is the right solution or a brute-forced work-around, but it got me going nonetheless :)

await self.update()

Once this was fixed, my plugs were still not connecting to the wifi due to their clock being wildly out of sync (these are brand new devices, never opened before). Managed to fix that one running the following command first:

 $ kasa --host 192.168.0.1 command set_device_time "{\"timestamp\": $(date +"%s"), \"time_diff\": 60, \"region\": \"$(timedatectl show -p Timezone --value)\"}"

If you don't have the timedatectl command in your distro you can replace it with the value of your timezone in the format Europe/Berlin

Hope this helps some people provision these plugs. I know at least for me, this package is the only reason I bought these specific plugs, since this allows me to configure them and use them without the plugs ever connecting to the internet, thanks to all contributors for getting this together!

@rytilahti
Copy link
Member

Hi, and glad that you got it working!

Was this happening on the release version or on the git master branch? I'm just asking as I thought this was already fixed through some changes on how we handle error cases, so this should work out of the box.

That being said, we should probably add a way to set the device time using the cli tool.

@Feirlane
Copy link
Author

I was using the python-kasa-git aur package which builds from source, and according to my install I was using commit 203bd79 from Apr 17th

@rytilahti
Copy link
Member

#951 adds time sync command that can be used to set the time when it gets merged.

@rytilahti rytilahti mentioned this issue Jun 4, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants