tuya / tuya-iot-python-sdk

Tuya IoT Python SDK for Tuya Open API.
MIT License
117 stars 47 forks source link

Intermittent failures in Home Assistant - "error while get mqtt config" #50

Open alexanv1 opened 2 years ago

alexanv1 commented 2 years ago

I am seeing intermittent issues in Home Assistant that started 2-3 days ago. The logs show the following errors:

2021-12-15` 07:39:57 ERROR (Thread-5411) [tuya_iot] Unexpected disconnection.7
2021-12-15 07:40:01 ERROR (Thread-5411) [tuya_iot] Unexpected disconnection.5
2021-12-15 07:49:04 ERROR (Thread-5411) [tuya_iot] Unexpected disconnection.7
2021-12-15 11:37:53 ERROR (Thread-34) [tuya_iot] error while get mqtt config

Once this occurs, devices no longer reflect their current state in Home Assistant and changing the state (turning on\off\etc) does not work either. Reloading the integration in Home Assistant works fine for a few hours which implies that the issue is intermittent but seems to put tuya_iot in a bad state until reloaded.

I've already checked the project on Tuya IoT to ensure all needed APIs are enabled as per instructions (also it was working fine for many weeks until the issues started a couple of days ago).

Given that the integration reload in Home Assistant fixes the issue, it seems that tuya_iot should be able to handle this as well by retrying\reconnecting\etc.

sharoltd commented 2 years ago

Many people with such problem last few days, but no way to fix it ((

vdbosch commented 2 years ago

Same issues here. All devices seems to lose connection, after restarting Tuya integrations it works again for some time (a few hours). It's absolutely not stable at this moment. Problem with 11.5 and with 2021.1.12. It looks that there are problems with the Tuya API/ Tuya Cloud and has nothing to do with the version of Home Assistant nor OS-version. I have same problems with my test-box (used old version and new version and different versions of OS).

bpfoster commented 2 years ago

I'm also having the same issue all of a sudden. Added in some logging to the openmqtt method _get_mqtt_config() to see what the error from the API is:

{'code': 1010, 'msg': 'token is expired', 'success': False, 't': 1639929045868}

So it would appear the auth token is expiring and not being correctly refreshed by the client.

sharoltd commented 2 years ago

I'm also having the same issue all of a sudden. Added in some logging to the openmqtt method _get_mqtt_config() to see what the error from the API is:

{'code': 1010, 'msg': 'token is expired', 'success': False, 't': 1639929045868}

So it would appear the auth token is expiring and not being correctly refreshed by the client.

I asked IOT.tuya support about this, they ask me "Where did you see the API you called,Could you please provide the address you saw" ;)))

bpfoster commented 2 years ago

This was fixed in tuya-iot-python-sdk v0.6.6 which was included in Home Assistant 2021.12.6.

coolnetworks commented 2 years ago

it is still happening here 17 March 2022 fully patched

helldog136 commented 2 years ago

Looks like the url to refresh mqtt config is wrong : https://developer.tuya.com/en/docs/cloud/c2c2630d7c?id=Kb68mozbi3foh Or am I mistaken here? The code seems to make requests to (openmq.py - l25-26)

TO_C_CUSTOM_MQTT_CONFIG_API = "/v1.0/iot-03/open-hub/access-config"
TO_C_SMART_HOME_MQTT_CONFIG_API = "/v1.0/open-hub/access/config"

These URLS are used at lines 62-81:

    def _get_mqtt_config(self) -> Optional[TuyaMQConfig]:
        response = self.api.post(
            TO_C_CUSTOM_MQTT_CONFIG_API
            if (self.api.auth_type == AuthType.CUSTOM)
            else TO_C_SMART_HOME_MQTT_CONFIG_API,
            {
                "uid": self.api.token_info.uid,
                "link_id": LINK_ID,
                "link_type": "mqtt",
                "topics": "device",
                "msg_encrypted_version": "2.0"
                if (self.api.auth_type == AuthType.CUSTOM)
                else "1.0",
            },
        )

        if response.get("success", False) is False:
            return None

        return TuyaMQConfig(response)

This code doesn't match the actual documentation