rospogrigio / localtuya

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

HA host restart causes lost devices #705

Open donev77 opened 2 years ago

donev77 commented 2 years ago

The problem

Every time HA Host (HyperV) is restarted, multiple devices (power plugs) disappear (unable to connect). They come back automatically if they are unplugged and plugged back. Reload localtuya service doesn't help. Deleting/re-adding integration, worked once, the second time it kept returning "Unexpected exception" when trying to add the device. The power plugs are different models and brands.

Environment

Windows 11, Hyper V

Steps to reproduce

  1. Reboot host, from Configuration > Add-ons, Backups & Supervisor > System, or restart the VM

Configuration configuration.yaml or config_flow

config_flow ## DP dump

Provide Home Assistant taceback/logs

2022-01-23 12:34:03 ERROR (MainThread) [custom_components.localtuya.common] [bf2...ibg] Connect to 10.0.0.169 failed
Traceback (most recent call last):
File "/config/custom_components/localtuya/common.py", line 145, in _make_connection
self._interface = await pytuya.connect(
File "/config/custom_components/localtuya/pytuya/__init__.py", line 669, 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}')
OSError: [Errno 113] Connect call failed ('10.0.0.169', 6668)

Unplug the socket and plug it back

2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.discovery] Discovered device: {'ip': '10.0.0.169', 'gwId': 'bf2...ibg', 'active': 2, 'ablilty': 0, 'encrypt': True, 'productKey': 'key...han', 'version': '3.3'}
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya] Device bf2...ibg found with IP 10.0.0.169
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.common] [bf2...ibg] Connecting to 10.0.0.169
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Started heartbeat loop
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Sending command heartbeat (device type: type_0a)
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Send payload: b'{}'
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Waiting for sequence number -100
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.common] [bf2...ibg] Retrieving initial state
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Sending command status (device type: type_0a)
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Send payload: b'{"gwId":"bf2...ibg","devId":"bf2...ibg"}'
2022-01-23 12:34:15 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Waiting for sequence number 1
2022-01-23 12:34:16 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2022-01-23 12:34:16 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Got heartbeat response
2022-01-23 12:34:16 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Decrypted payload: {}
2022-01-23 12:34:16 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=0, payload=b'\x80;A\x973\xd6\xc7\xd8x\x98\xeb\xf2A\x8f\xc7z\xd9\x14\x8b\xaa\xf2k\x19\xb7GN\xbe\xae\xec\xc0\xeb\x1a\xad\xb5\xf8\x1d<\xc2\xd0\xd57\xac\xf9\xf0\xe5\x7f\xe4,\xaa\xf7b\x12?\xb8\xed\x94\x96\xd7\xf3\xff\x06\x16\xf0\xbd\x9a$\x85\xa4\xe3yf?\xaf!?\xbd\xc5=\x84\x00\xd5\xd1*\x13V\xaf\xf9\xb1v\x932s\xd9\x1bY\xd4R\xed\xf2\xa8qA\x12@\xc09\x86q6\r\x9e]\xda!G\xed\xfc\xf4F\xa28\xc4\xf0@ \x0bz\xc2\xb2\xb6\x8fU\xd9\x03\x11\x9b\xe2$1\x1db\x18P\xe7', crc=3231281928)
2022-01-23 12:34:16 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Dispatching sequence number 1
2022-01-23 12:34:16 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf2...ibg] Decrypted payload: {"dps":{"1":true,"9":0,"18":0,"19":0,"20":2209,"21":1,"22":0,"23":0,"24":0,"25":0,"26":0,"38":"memory","39":false,"42":"","43":""}}

Additional information

The devices have good wifi signal about -74dBm reported in Smart Life app When the devices are unavailable in HA, I checked them in Smart Life app and they were working fine (power reading, status, on/off). localKey hasn't changed.

samfaitmalomolet commented 2 years ago

Same issue here after updating ha OS to last version ( 7.0 -> 7.2) Rebooting HA host again did not work. Plug works through smart life. Unplug - plug agin the device fixed the issue. Logs: This error originated from a custom integration.

Logger: custom_components.localtuya.common Source: custom_components/localtuya/pytuya/init.py:669 Integration: LocalTuya (documentation, issues) First occurred: 3:21:12 AM (32 occurrences) Last logged: 3:23:50 AM

[bf9...eyu] Connect to 192.168.0.18 failed Traceback (most recent call last): File "/config/custom_components/localtuya/common.py", line 145, in _make_connection self._interface = await pytuya.connect( File "/config/customcomponents/localtuya/pytuya/init.py", line 669, 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}') OSError: [Errno 113] Connect call failed ('192.168.0.18', 6668)

smithbill17 commented 2 years ago

I'm also now getting this same issue (I think) on: Home Assistant Core v2021.12.10 LocalTuya 3.4.0

One of my Tuya SmartPlugs became 'unavailable' in HomeAssistant using LocalTuya, but it is still registered & online in the Tuya IoT Platform, can still be controlled using the 'official' Tuya Integration in HomeAssistant and also using the SmartLife app. So there is nothing wrong with the SmartPlug.

I deleted it (device & entity) from HomeAssistant and after rebooting my network & HA, attempted to re-add it to LocalTuya Integration. The 'device discovery'' dialog appears giving the correct DeviceID & IP address. I enter the correct LocalKey and after a few seconds of the 'busy wheel' the below error appears. It happens everytime. I am now unable to add this one SmartPlug to LocalTuya/HomeAssistant (other Tuya SmartPlugs in LocalTuya appear to be working normally)

2022-01-26 08:28:19 ERROR (MainThread) [custom_components.localtuya.config_flow] Unexpected exception
Traceback (most recent call last):
File "/config/custom_components/localtuya/config_flow.py", line 283, in async_step_basic_info
self.dps_strings = await validate_input(self.hass, user_input)
File "/config/custom_components/localtuya/config_flow.py", line 189, in validate_input
interface = await pytuya.connect(
File "/config/custom_components/localtuya/pytuya/__init__.py", line 669, 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}')
OSError: [Errno 113] Connect call failed ('192.168.68.114', 6668)
smithbill17 commented 2 years ago

Okay - I have finally managed to re-add my SmartPlug device. My problem seems to stem from the fact I have TWO HomeAssistant installations (one on Raspberry Pi & one in a VirtualBox VM) both using LocalTuya. When the VM HA is started with all my LocalTuya SmartPlugs registered, attempting to re-add the missing SmartPlug to my RPi HA fails & results in the error above.

If I restart my VM HA, it seemingly 'releases' the SmartPlugs & I can then add it successfully to my RPi HA.

I have noticed that I have four Tuya SmartPlugs that fail to operate simultaneously in both my HomeAssistant installations - if I restart my VM HA, the SmartPlugs are available to control in my RPi HA, but appear disabled in the VM HA once it's restarted. If I do it the other way around & restart the RPi HA, then suddenly the SmartPlugs are available to control in the VM HA. So I just can't have them working in BOTH HomeAssistants simultaneously.

The only thing these 4 SmartPlugs seem to have in common, is that the SmartLife app indicates they are all on firmware v1.0.0 (and they never get auto-updated). Whereas the rest of my Tuya SmartPlugs that do work simultaneously in both HAs, are all on firmware v.1.0.3 or v1.1.2.

So clearly LocalTuya Integration has some kind of issue with Tuya SmartPlugs stuck on firmware v1.0.0 that prevents it operating in more than a single HomeAssistant installation.

Anton-Ka commented 2 years ago

This is similar to my issue: https://github.com/rospogrigio/localtuya/issues/624