Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make connection retries every minute #288

Merged
merged 2 commits into from
Mar 5, 2021
Merged

Make connection retries every minute #288

merged 2 commits into from
Mar 5, 2021

Conversation

postlund
Copy link
Collaborator

@postlund postlund commented Jan 1, 2021

This mimics the re-connect loop previosuly present, but in a simpler
form. Every 60 seconds, a new connection attempt is made as well as
initially when a device is set up.

I will add this an option, enabled by default so we can still support devices that sleep. But I want to get this out for testing.

Copy link

@Marco-Kl Marco-Kl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello postlund,
thanks for the code. I checked the code in both systems and i got errors

2021-01-01 14:52:36 ERROR (MainThread) [custom_components.localtuya.common] [453...66f] Connect to 192.168.200.202 failed
Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 149, 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 457, in exchange
    payload = self._decode_payload(msg.payload)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 559, in _decode_payload
    return json.loads(payload)
  File "/usr/local/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Copy link

@Marco-Kl Marco-Kl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello postlund,
my mistake, old keys configured for some devices. : (
So it is working in my test and prod system without errors.

I can approve, if I am authorized to do so.

@LuckyTriple7
Copy link

When i replace the init.py and restart HASS all Devices are unavailable, Error Messages below:

`Logger: homeassistant
Source: custom_components/localtuya/init.py:191
First occurred: 15:34:48 (1885 occurrences)
Last logged: 15:41:13

Error doing job: Exception in callback _SelectorDatagramTransport._read_ready()
Traceback (most recent call last):
File "/usr/local/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 1021, in _read_ready
self._protocol.datagram_received(data, addr)
File "/config/custom_components/localtuya/discovery.py", line 70, in datagram_received
self.device_found(decoded)
File "/config/custom_components/localtuya/discovery.py", line 79, in device_found
self._callback(device)
File "/config/custom_components/localtuya/init.py", line 191, in _device_discovered
device.async_connect()
AttributeError: 'TuyaDevice' object has no attribute 'async_connect'
Logger: homeassistant
Source: custom_components/localtuya/init.py:209
First occurred: 15:36:43 (6 occurrences)
Last logged: 15:40:47

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/config/custom_components/localtuya/init.py", line 209, in _async_reconnect
device = entry[TUYA_DEVICE]
TypeError: string indices must be integers`

Copy link

@Marco-Kl Marco-Kl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
it is working now, also in my test systems. Great.!!!
But ;-)
If I disconnect or not available for a short time the device it will not reconnect again until i restart HA. The following error apears:

2021-01-01 14:33:00 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/config/custom_components/localtuya/__init__.py", line 216, in _async_reconnect
    device = entry[TUYA_DEVICE]
TypeError: string indices must be integers

Thank you so much

@postlund
Copy link
Collaborator Author

postlund commented Jan 1, 2021

@Marco-Kl I managed to reproduce and hopefully fix that, can you update and try again?

@LuckyTriple7 You must update all changed files, not just one of them.

@LuckyTriple7
Copy link

Updates 2 Files now: commony.py and init.py.
All switch devices are not available after restart HASS

`Logger: homeassistant
Source: custom_components/localtuya/init.py:209
First occurred: 20:26:20 (7 occurrences)
Last logged: 20:31:51

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/config/custom_components/localtuya/init.py", line 209, in _async_reconnect
device = entry[TUYA_DEVICE]
TypeError: string indices must be integers`

@Marco-Kl
Copy link

Marco-Kl commented Jan 1, 2021

Hello postlund,
it is working now. No error message and automatic reconnect. 1 of 5 test was not a success but i think the device was to short disconnected form power. It should powerless more than 10 seconds.

I will test it during this night. I have to improve my Python skills so that I can also provide better support and create integrations

Thank you verry much.

@postlund
Copy link
Collaborator Author

postlund commented Jan 1, 2021

@Marco-Kl Great, thank you! 👍

@LuckyTriple7 I pushed an update, make sure you pull the latest files.

@LuckyTriple7
Copy link

OK my mistake.
No Error Message in Log now, but problem still exists.
All Switch Devices are unavailable. When i reload the integration device comes back until next hass restart.

Debug LOG after reload:
2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Closing connection 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Stopped heartbeat loop 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Connection lost: None 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.switch] [164...da0] Adding switch.airbowl_premium with configuration: {'friendly_name': 'Airbowl Premium', 'id': 1, 'platform': 'switch'} 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.common] [164...da0] Connecting to 192.168.178.132 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Started heartbeat loop 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Sending command heartbeat (device type: type_0a) 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Send payload: b'{}' 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Waiting for sequence number -100 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.common] [164...da0] Retrieving initial state 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Sending command status (device type: type_0a) 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Send payload: b'{"gwId":"16444185ecfabc71bda0","devId":"16444185ecfabc71bda0"}' 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Waiting for sequence number 1 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211) 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Got heartbeat response 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Decrypted payload: {} 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=0, payload=b'l+b$\x14\x84\x82{\x91\xf2Hp4T\xfd\xfc}\xe6\x98\x13\xd9\x9a%h\xc2\xfa\xce\xd1C\x86\xc6\xb0sH<x\xb6\xb7\xd8\xc3\xb8l\xe0Y\xcf\x01G\x8f\xb2\xb8\xd7;W\xda\xc5\xb5\xe4\xf1\x0fw\xed-\xcd_', crc=978406711) 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Dispatching sequence number 1 2021-01-02 06:05:20 DEBUG (MainThread) [custom_components.localtuya.pytuya] [164...da0] Decrypted payload: {"devId":"16444185ecfabc71bda0","dps":{"1":true,"11":0}}

After restart HASS, Device is unavailable and NO Log entries for this Device "Airbowl Premium" with IP 192.168.178.132

@vinhdizzo
Copy link

Is this fix going to be incorporated into v 3.3.0? When will it be released? I am on 3.1.0

@lishan89uc
Copy link

I think I have exact same problem. After Hass restart everything is unavailable and after a reload everything works perfectly. I found a way around it i have an automation that detect whether all the devices are loaded before starting homekit. I added the service to the end of that automation localtuya.reload so that after everything else loads it reloads localtuya. It's so far working flawlessly.

@nkm8
Copy link

nkm8 commented Mar 5, 2021

For what it's worth, I had to pull this PR into my fork which fully resolved my dropped devices issue.

@postlund
Copy link
Collaborator Author

postlund commented Mar 5, 2021

I will take a look at this PR again later today/tonight and make appropriate changes. Then we can merge it and make a release, hopefully it will help a lot of people.

This mimics the re-connect loop previosuly present, but in a simpler
form. Every 60 seconds, a new connection attempt is made as well as
initially when a device is set up.
@postlund
Copy link
Collaborator Author

postlund commented Mar 5, 2021

I have pushed an update, please try out the latest change and once we have confirmation that it works we'll make a new release.

@stratus-ss
Copy link

I was able to pull this down and test it against my HA which is on a different VLAN from the devices in question.

I can confirm for me at least, that this PR allows devices to be available in HA where they previously were not

@postlund postlund self-assigned this Mar 5, 2021
@postlund
Copy link
Collaborator Author

postlund commented Mar 5, 2021

@rospogrigio I think we should merge this (after regular review of course) and make a release. It will help a lot of people. Do you want to handle the release or share your regular "routine" so I can do it?

Copy link
Collaborator

@ultratoto14 ultratoto14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@postlund postlund merged commit 66fdc16 into master Mar 5, 2021
@postlund postlund deleted the simple_reconnect branch March 5, 2021 22:16
@rospogrigio
Copy link
Owner

@rospogrigio I think we should merge this (after regular review of course) and make a release. It will help a lot of people. Do you want to handle the release or share your regular "routine" so I can do it?

Hi @postlund , nice to read back from you! I just go to the Releases menu, press "Draft a new release", add a tag name, title and description and press "Publish release", I'm sure you knew hot to do it though. I'll do it now, the next time feel free to do it yourself if you believe you have an urgent fix you want to publish. Bye!

@jfparis
Copy link

jfparis commented Mar 8, 2021

Looking forward to try this (when I head back home in a month)
Does it still include the passive connection (which require the forwarding of the udp packets/)

@postlund
Copy link
Collaborator Author

postlund commented Mar 8, 2021

@jfparis The passive connection logic is still there, but with this change is built on top of that. So connection attempts are made either if broadcast messages are received or every minute. So it'a not needed.

@postlund
Copy link
Collaborator Author

postlund commented Mar 8, 2021

@rospogrigio I think we should merge this (after regular review of course) and make a release. It will help a lot of people. Do you want to handle the release or share your regular "routine" so I can do it?

Hi @postlund , nice to read back from you! I just go to the Releases menu, press "Draft a new release", add a tag name, title and description and press "Publish release", I'm sure you knew hot to do it though. I'll do it now, the next time feel free to do it yourself if you believe you have an urgent fix you want to publish. Bye!

@rospogrigio Same to you! Too busy nowadays... That sounds simple enough 😊 I think I shall manage!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.