home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.21k stars 30.58k forks source link

Error while setting up tplink platform #35528

Closed uncled1023 closed 4 years ago

uncled1023 commented 4 years ago

The problem

I have the tplink integration added and it used to work, albeit flaky. I upgraded from 108 -> 109.6 today and now the tp link integration won't load.

Environment

Problem-relevant

configuration.yaml

tplink: !include tplink.yaml

tplink.yaml

discovery: false
dimmer:
    - host: !secret tp_living_room_switch
    - host: !secret tp_kitchen_switch
    - host: !secret tp_entry_switch
switch:
    - host: !secret tp_hallway_switch
    - host: !secret tp_bedroom_switch
    - host: !secret tp_front_porch_switch
    - host: !secret tp_back_porch_switch
strip:
    - host: !secret tp_fountain_strip
    - host: !secret tp_back_patio_strip

Traceback/Error logs

Error while setting up tplink platform for light

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/light.py", line 61, in async_setup_entry
    hass, async_add_entities, hass.data[TPLINK_DOMAIN][CONF_LIGHT], add_entity
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 192, in async_add_entities_retry
    await process_objects_loop(interval.seconds)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 164, in process_objects_loop
    await process_objects()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 180, in process_objects
    callback, add_object, async_add_entities
  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/light.py", line 73, in add_entity
    async_add_entities([TPLinkSmartBulb(device)], update_before_add=True)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 247, in _async_schedule_add_entities
    new_entities, update_before_add=update_before_add
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 314, in async_add_job
    task = self.loop.create_task(target)  # type: ignore
  File "/usr/lib/python3.7/asyncio/base_events.py", line 400, in create_task
    task = tasks.Task(coro, loop=self)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 685, in call_soon
    self._check_thread()
  File "/usr/lib/python3.7/asyncio/base_events.py", line 723, in _check_thread
    "Non-thread-safe operation invoked on an event loop other "
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one

Error while setting up tplink platform for switch

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/switch.py", line 40, in async_setup_entry
    hass, async_add_entities, hass.data[TPLINK_DOMAIN][CONF_SWITCH], add_entity
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 192, in async_add_entities_retry
    await process_objects_loop(interval.seconds)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 164, in process_objects_loop
    await process_objects()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 180, in process_objects
    callback, add_object, async_add_entities
  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/switch.py", line 34, in add_entity
    async_add_entities([SmartPlugSwitch(device)], update_before_add=True)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 247, in _async_schedule_add_entities
    new_entities, update_before_add=update_before_add
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 314, in async_add_job
    task = self.loop.create_task(target)  # type: ignore
  File "/usr/lib/python3.7/asyncio/base_events.py", line 400, in create_task
    task = tasks.Task(coro, loop=self)
  File "/usr/lib/python3.7/asyncio/base_events.py", line 685, in call_soon
    self._check_thread()
  File "/usr/lib/python3.7/asyncio/base_events.py", line 723, in _check_thread
    "Non-thread-safe operation invoked on an event loop other "
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one

Additional information

All of the dimmers, switches, and strips are usable via the KASA app, and were usable before the upgrade.

probot-home-assistant[bot] commented 4 years ago

Hey there @rytilahti, mind taking a look at this issue as its been labeled with a integration (tplink) you are listed as a codeowner for? Thanks! (message by CodeOwnersMention)

uncled1023 commented 4 years ago

After updating to 0.110, I am getting the following error now:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/pyHS100/smartdevice.py", line 117, in _query_helper
    request=request,
  File "/srv/homeassistant/lib/python3.7/site-packages/pyHS100/protocol.py", line 60, in query
    length = struct.unpack(">I", chunk[0:4])[0]
struct.error: unpack requires a buffer of 4 bytes

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/config_entries.py", line 217, in async_setup
    hass, self
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/__init__.py", line 82, in async_setup_entry
    static_devices = get_static_devices(config_data)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/tplink/common.py", line 122, in get_static_devices
    for plug in SmartStrip(host).plugs.values():
  File "/srv/homeassistant/lib/python3.7/site-packages/pyHS100/smartstrip.py", line 43, in __init__
    children = self.sys_info["children"]
  File "/srv/homeassistant/lib/python3.7/site-packages/pyHS100/smartdevice.py", line 186, in sys_info
    return defaultdict(lambda: None, self.get_sysinfo())
  File "/srv/homeassistant/lib/python3.7/site-packages/pyHS100/smartdevice.py", line 196, in get_sysinfo
    return self._query_helper("system", "get_sysinfo")
  File "/srv/homeassistant/lib/python3.7/site-packages/pyHS100/smartdevice.py", line 120, in _query_helper
    raise SmartDeviceException('Communication error') from ex
pyHS100.smartdevice.SmartDeviceException: Communication error

No changes to config or setup besides upgrade.

rytilahti commented 4 years ago

There has been no changes to pyhs100 since some time last year, so I have no idea what could be causing the thread-related errors.

But your latest error is related to connectivity problems with the device. Has anything else changed in your network in the meanwhile? Have you done firmware updates on the devices (tplink may be blocking this api, but the situation is not clear, see https://github.com/python-kasa/python-kasa/issues/42)?

uncled1023 commented 4 years ago

All the devices listed are on, and are available within the Kasa app. I am using the following devices:

uncled1023 commented 4 years ago

Awhile ago I did have a HS105 set up but it's no longer on or in the config. Could it be cached or something and still trying to connect to it?

uncled1023 commented 4 years ago

Ok, so I removed the two outdoor plugs from the config, and I am back to getting the original error messages I described in the initial ticket.

uncled1023 commented 4 years ago

So it seems like my Outdoor plugs (or at least one of them) are not supported?

rytilahti commented 4 years ago

They should be supported, but the integration has known problems with multi-socket devices, especially when combined with spotty networks. When #30719 finally gets done, the situation should improve.

uncled1023 commented 4 years ago

I look forward to testing it once the async fix is finished. They did work on the previous version (108) so maybe the new version of HA is more finicky about connection tries that take too long?

rytilahti commented 4 years ago

It could be, I don't really know. Looking at the stacktrace, it's coming from the retry logic code in common.py, which has not changed in six months.

stale[bot] commented 4 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.