Skip to content

Error when connecting P110 to wi-fi #852

@Feirlane

Description

@Feirlane

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions