Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Sometimes "Connection Refused" occurs #31

Open
LucidSkyWalker opened this issue Jul 20, 2018 · 4 comments
Open

Sometimes "Connection Refused" occurs #31

LucidSkyWalker opened this issue Jul 20, 2018 · 4 comments

Comments

@LucidSkyWalker
Copy link

Hi together,

I am using python-tuya in my Snips Home Automation.
It works fine, but sometimes I get a "Connection Refused".
I know that it is blocked when the App is open (you think there is a possiblity work around?),
but also if the Mobile App is closed I sometimes get a "Connection Refused".

When I reopen the App, turn off and on the light switches and then close the app
it is working again...

Do you have any idea or solution for this?

Thank you for your python library!!! Most of the time it works really nice :))

@BillSobel
Copy link

BillSobel commented Jul 21, 2018 via email

@t0ny-peng
Copy link

@BillSobel
Hi Bill,
I also have this connection refused problem with my plugs. So you mean, the mobile application Tuya has two methods to communicate with the devices:

  1. via TCP if Tuya app and plug are in the same LAN.
  2. via a MQTT broker provided by Tuya. App publishes a message to the broker topic which is listened by the plug.

Is that right? If so, is there any possible way to configure the MQTT broker address inside the plug? I assume it's fixed in the firmware.

@BillSobel
Copy link

BillSobel commented Oct 29, 2018 via email

@caffeinatedMike
Copy link

@clach04 I also receive this error on a occasion & the device's state remains unchanged when it occurs. Typically, if I retry toggling the switch the device's state does successfully change (after a second or third try of toggling). So, I have a proposition, I think it'd be beneficial to add a try/except within a loop with range(3) to try changing the state up to three times to minimize these issues. Does that change seem like something you would allow?

For reference, I think this function is what we'd need to implement the proposed solution to.

    def _send_receive(self, payload):
        """
        Send single buffer `payload` and receive a single buffer.
        
        Args:
            payload(bytes): Data to send.
        """
        for i in range(3):
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                s.settimeout(self.connection_timeout)
                s.connect((self.address, self.port))
                s.send(payload)
                data = s.recv(1024)
                s.close()
                return data
            except socket.error:
                continue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants