rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.72k stars 532 forks source link

Can't work on two instances at the same time #624

Open Anton-Ka opened 2 years ago

Anton-Ka commented 2 years ago

The problem

I can only control my socket from one Home Assistant server. The device on the second server becomes unavailable. But when the server is rebooted, where the devices were available, another server manages to "take control" and the devices become available to it, but are no longer available to the rebooted server after it boots.

Environment

Steps to reproduce

  1. Install Local Tuya on two servers.
  2. Connect the same device on both servers.
  3. There should be a problem with controlling the device from one server while the device is being controlled from the second server.

Configuration configuration.yaml or config_flow

none

DP dump

All i know. Name | Brand | Product Key | Firmware | Type | ID(on/off) | Current | Power | Voltage -- | -- | -- | -- | -- | -- | -- | -- | -- NX-SM200 | Nanxin | -| - | smart | 1 | 4 | 5 | 6

Provide Home Assistant taceback/logs

Эта ошибка возникла в кастомной интеграции.

Logger: custom_components.localtuya.common
Source: custom_components/localtuya/pytuya/__init__.py:637
Integration: LocalTuya integration (documentation, issues)
First occurred: 20:18:09 (1 occurrences)
Last logged: 20:18:09

[631...faf] Connect to 192.168.10.3 failed
Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 139, in _make_connection
    self._interface = await pytuya.connect(
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 637, in connect
    _, protocol = await loop.create_connection(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1056, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1041, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 955, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 502, in sock_connect
    return await fut
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 537, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionResetError: [Errno 104] Connect call failed ('192.168.10.3', 6668)
Эта ошибка возникла в кастомной интеграции.

Logger: custom_components.localtuya.common
Source: custom_components/localtuya/pytuya/__init__.py:637
Integration: LocalTuya integration (documentation, issues)
First occurred: 20:18:09 (1 occurrences)
Last logged: 20:18:09

[631...faf] Connect to 192.168.10.3 failed
Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 139, in _make_connection
    self._interface = await pytuya.connect(
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 637, in connect
    _, protocol = await loop.create_connection(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1056, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1041, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 955, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 502, in sock_connect
    return await fut
  File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 537, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionResetError: [Errno 104] Connect call failed ('192.168.10.3', 6668)

Additional information

image

image

smithbill17 commented 2 years ago

This appears to be the same issue as detailed in issue #631

However, I don't appear to be having this issue on ALL my Tuya SmartPlugs - some are available to control in BOTH my Home Assistants (VM & RaspberryPi) without any problems. It always seems to be the same 4 SmartPlugs in my setup that can only be operated in one HomeAssistant. There's nothing obvious that's 'common' to those 4 SmartPlugs as they are different models to each other & other similar model SmartPlugs don't exhibit this 'one HA only' issue.

Anton-Ka commented 2 years ago

This appears to be the same issue as detailed in issue #631

However, I don't appear to be having this issue on ALL my Tuya SmartPlugs - some are available to control in BOTH my Home Assistants (VM & RaspberryPi) without any problems. It always seems to be the same 4 SmartPlugs in my setup that can only be operated in one HomeAssistant. There's nothing obvious that's 'common' to those 4 SmartPlugs as they are different models to each other & other similar model SmartPlugs don't exhibit this 'one HA only' issue.

I assume that this behavior does not occur with all devices, as much with certain models.

smithbill17 commented 2 years ago

I don't think the cause is certain models, I have a total of 15 SmartPlugs of various models - some of the same model don't exhibit this problem. And of the 4 SmartPlugs that do exhibit the problem, they aren't all the same model.

I had thought it was something to do with which particular router in my Mesh LAN that the SmartPlugs were connected to and if they were using a different router to the HomeAssistant (I use a 3 router TP-Link Deco S4 Mesh network). However, I'm not sure this has proved to be relevant to this issue.

Anton-Ka commented 2 years ago

I don't think the cause is certain models, I have a total of 15 SmartPlugs of various models - some of the same model don't exhibit this problem. And of the 4 SmartPlugs that do exhibit the problem, they aren't all the same model.

I had thought it was something to do with which particular router in my Mesh LAN that the SmartPlugs were connected to and if they were using a different router to the HomeAssistant (I use a 3 router TP-Link Deco S4 Mesh network). However, I'm not sure this has proved to be relevant to this issue.

I only have one router. I only have one device and it has this problem. But I think that you know that the same models of Tuya devices can be assembled in different factories and have quite large differences inside with the same cases.

smithbill17 commented 2 years ago

I've been looking at my 15 SmartPlugs in the SmartLife app, and have noticed that the 4 SmartPlugs that I have a problem with, are all using firmware Main Module & MCU Module v1.0.0 or v1.0.1.

Whereas, the rest of my SmartPlugs that don't exhibit this problem are using firmware v1.0.3 or v.1.1.2.

Unfortunately, there doesn't seem to be any way to upgrade the SmartPlugs on v1.0.0 or v1.0.1 as when you try switching on 'Automatic Updates' it never updates them.

Anton-Ka commented 2 years ago

My smart plug have firmware version is v.1.0.4. Maybe problem with protocol version 3.1?

smithbill17 commented 2 years ago

I would imagine that your firmware v1.0.4 is 'newer' than my v1.0.3, but my SmartPlugs with firmware v1.0.3 work fine. It's only those with v1.0.0 and v1.0.1 that have a problem that prevents them working with two different HomeAssistants.

But possibly in your country, your v1.0.4 firmware is the same as my v1.0.0 or v1.0.1 (I'm in the UK).

Anton-Ka commented 2 years ago

But possibly in your country, your v1.0.4 firmware is the same as my v1.0.0 or v1.0.1 (I'm in the UK).

Perhaps you `re right. I'm from Russia. I just checked, changed protocol version 3.1 to 3.3 - devices stopped connecting to any of the servers at all. It looks like protocol version 3.3 is not compatible with my socket.

smithbill17 commented 2 years ago

I'm using protocol 3.3 on all my devices. I'll see if I can change to 3.1 on the 4 SmartPlugs I have a problem with (maybe that'll match better with the older firmware those 4 SmartPlugs seem to have).

smithbill17 commented 2 years ago

Switching to protocol 3.1 on my problem devices didn't seem to make any difference. So I've changed back to protocol 3.3 and my 4 devices still only work in one HomeAssistant.

Anton-Ka commented 2 years ago

After updating the integration, the problem remained. I see an error in my log:

Эта ошибка возникла в кастомной интеграции.

Logger: custom_components.localtuya.common
Source: custom_components/localtuya/pytuya/__init__.py:240
Integration: LocalTuya integration (documentation, issues)
First occurred: 10:56:54 (19 occurrences)
Last logged: 11:14:55

[631...faf] Connect to 192.168.10.3 failed
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/locks.py", line 413, in acquire
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/asyncio/tasks.py", line 492, in wait_for
    fut.result()
asyncio.exceptions.CancelledError

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

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 151, in _make_connection
    status = await self._interface.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 472, in status
    status = await self.exchange(STATUS)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 451, in exchange
    msg = await self.dispatcher.wait_for(seqno)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 240, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.9/asyncio/tasks.py", line 494, in wait_for
    raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
ejpenney commented 2 years ago

For what it's worth, I'm also having this issue when I use the Tuya Smartplug Plugin on OctoPrint at the same time as localtuya on my Home Assistant. Seems only one of the two is capable of controlling the plug at any time. I've tried going back and forth, if I deregister one, the other starts working, and vice versa. Can't get them both working simultaneously.

Anton-Ka commented 2 years ago

I am still seeing this issue. Has there been any solution?

smithbill17 commented 2 years ago

I also still get this issue on HA 2022.4.4 and LocalTuya v3.5.0

I gave up on there being any solution.

Anton-Ka commented 2 years ago

A great solution would be the ability to deactivate the Tuya integration and activate it again in a second using automation. And make the availability of the second server an automation trigger. But there is no such service in Home Assistant. Just restarting the integration doesn't always help.