Tplink device unavailable after reboot respberry pi

After I added Tplink device ins HA, it worked great. However, when I rebooted the raspberry pi, the tplink devices became unavailable. To make them available, I have to restart the HA service. By checking the log, it shows below error. Is it because when the tplink setup, the network is still unavailable? If yes, it looks like the setup should be performed when the network is available or at least it should have retry logic. Any idea?

Error setting up entry TP-Link Smart Home for tplink
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/config_entries.py", line 215, in async_setup
    hass, self
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/__init__.py", line 89, in async_setup_entry
    discovered_devices = await async_discover_devices(hass, static_devices)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 73, in async_discover_devices
    devices = await async_get_discoverable_devices(hass)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 65, in async_get_discoverable_devices
    return await hass.async_add_executor_job(discover)
  File "/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 62, in discover
    devs = Discover.discover()
  File "/srv/homeassistant/lib/python3.7/site-packages/pyHS100/discover.py", line 45, in discover
    sock.sendto(encrypted_req[4:], (target, port))
OSError: [Errno 101] Network is unreachable

There are several threads on this. Here’s one:

To summarize: You do want to make sure they have a strong network connection, and your network isn’t saturated. You do want to set up IP address reservations in your DHCP server. You may or may not want to configure the integration to auto-connect. (With me, it worked better enabled. Others report disabling that option works for them.)

Of note is the fact that the “Kasa” app doesn’t often have this problem, when used with the exact same devices on the exact same network. My assumption is that the app is designed to assume these things sometimes don’t respond right away, and either gives them more time or retries before giving up, while the HA integration is more rigid and reports unavailable devices immediately or after the first try.

Hi Tom,

Thanks for the reply. I think my case is different. My TPLink devices are online all the time and because I did not restart it, the IP address should not be changed as well. Once the devices are available it is quite stable.

The issues happens at every time I reboot the raspberry pi. I can easily fix the issue by restarting the HA manually in configuration after HA is alive.

It looks for me that the issue is because when the setup code is executing, the WiFi network is not available yet (just rebooted). To fix this, the setup code should be executed once the WiFi is connected or have some retry logic.

1 Like

I resolved this issue by adding the following lines in system.d file.

[Service]
ExecStartPre=/bin/sleep 30

It is not very nice but it works.

thanks - is this a one off, or does it need to be added after each upgrade?

You will have to redo it after every reflash. It is just a workaround.

It would be nice that home asssistant could add this feature in or enable the retry function of setup process.