fishbigger / TapoP100

A module for controlling the TP-Link Tapo P100 Plugs
MIT License
566 stars 138 forks source link

Connection issues after running for awhile #93

Closed MandolinMicke closed 1 year ago

MandolinMicke commented 1 year ago

First of all, thanks for a nice project!

I can connect and run without a problem when testing, but after running for a while, I start to get issues with connections. I get errors like: RemoteDisconnected('Remote end closed connection without response') ConnectionResetError(104, 'Connection reset by peer')) (these errors I enabled to see the responce in the package itself)

Has anyone else experienced this?

My code looks something like this:


class TpPlugController():
    """ used for P110 plugs

    """
    def __init__(self, plug_name):

        if plug_name not in tpplugs:
            raise ValueError('not a correct plug name')
        self.plug_name = plug_name
        self._plug = PyP110.P110(tpplugs[plug_name], "some_email", tp_pass)
        self._plug.handshake()
        self._plug.login()
        self._max_tries = 3
    def turn_on(self):
        keep_running = True
        counter = 0
        while keep_running and counter < self._max_tries:
            try:
                if not self._plug.getDeviceInfo()['result']['device_on']:
                    self._plug.turnOn()
                    keep_running = False
                break
            except Exception as e:
                print(self.plug_name, ' something went wrong')
                counter += 1

Added the loop in order to try again, it works sometimes but not far from always.

Next step for me is to try to destroy self._plug each time but that shouldn't be needed.

MandolinMicke commented 1 year ago

For future reference, this is finally how I solved it to make it stable and fast.


class TpPlugController():
    """ used for P110 plugs

    """
    def __init__(self, plug_name):

        if plug_name not in tpplugs:
            raise ValueError('not a correct plug name')
        self.plug_name = plug_name
        self.email = "someemail"

        self._max_tries = 3
        self.setup_plug()
    def setup_plug(self):
        tries = 0
        while tries < 3:
            try:
                self._plug = PyP110.P110(tpplugs[self.plug_name], self.email, tp_pass)
                self._plug.handshake()
                self._plug.login()
                break
            except Exception as e:

                tries +=1
    def turn_on(self):
        keep_running = True
        counter = 0

        while keep_running and counter < self._max_tries:
            try:
                if not self._plug.getDeviceInfo()['result']['device_on']:
                    self._plug.turnOn()
                    keep_running = False
                break
            except Exception as e:
                print(self.plug_name, ' something went wrong')
                print(e)
                self.setup_plug()
                counter += 1