make-all / tuya-local

Local support for Tuya devices in Home Assistant
MIT License
1.09k stars 446 forks source link

New Device Request: Avatto Curtain switch #292

Closed engusz closed 1 year ago

engusz commented 1 year ago

Hi,

The integration didn't recognize the device, the log shows:

Device matches None with quality of 0%. DPS: {'1': 'stop', '101': True, 'updated_at': 1669631896.6501718}

Link: https://www.aliexpress.com/item/4000206502719.html

Response

{
  "result": {
    "category": "clkg",
    "functions": [
      {
        "code": "control",
        "lang_config": {
          "close": "Close",
          "open": "Open",
          "stop": "Paused"
        },
        "name": "Curtain",
        "type": "Enum",
        "values": "{\"range\":[\"open\",\"stop\",\"close\"]}"
      },
      {
        "code": "switch_backlight",
        "lang_config": {
          "false": "",
          "true": ""
        },
        "name": "背光开关",
        "type": "Boolean",
        "values": "{}"
      }
    ],
    "status": [
      {
        "code": "control",
        "lang_config": {
          "close": "Close",
          "open": "Open",
          "stop": "Paused"
        },
        "name": "Curtain",
        "type": "Enum",
        "values": "{\"range\":[\"open\",\"stop\",\"close\"]}"
      },
      {
        "code": "switch_backlight",
        "lang_config": {
          "false": "",
          "true": ""
        },
        "name": "背光开关",
        "type": "Boolean",
        "values": "{}"
      }
    ]
  },
  "success": true,
  "t": 1669633118166,
  "tid": "9c8c8f646f0b11ed8ac2164a0a4aa718"
}

Thanks,

engusz commented 1 year ago

These additional data:

Device matches None with quality of 0%. DPS: {'1': 'stop', '101': True, 'updated_at': 1669922066.2737293}

{
  "result": {
    "category": "clkg",
    "functions": [
      {
        "code": "control",
        "dp_id": 1,
        "type": "Enum",
        "values": "{\"range\":[\"open\",\"stop\",\"close\"]}"
      },
      {
        "code": "switch_backlight",
        "dp_id": 101,
        "type": "Boolean",
        "values": "{}"
      }
    ],
    "status": [
      {
        "code": "control",
        "dp_id": 1,
        "type": "Enum",
        "values": "{\"range\":[\"open\",\"stop\",\"close\"]}"
      },
      {
        "code": "switch_backlight",
        "dp_id": 101,
        "type": "Boolean",
        "values": "{}"
      }
    ]
  },
  "success": true,
  "t": 1669922128510,
  "tid": "8433b83a71ac11edb328265ef7a90a71"
engusz commented 1 year ago

Hi @make-all,

image

The opening and closing buttons do not control the wall switch. Only the STOP button works properly. When the backlight is turned on and off, the STOP button on the wall switch lights up. (It didn't do that before.)

make-all commented 1 year ago

This device doesn't seem to report enough info to reliably track its state. I see from the screenshot that the open button is disabled. But if the curtain is not fully open, then that is incorrect. Also, the info on the Tuya portal may not be correct - if you can capture the device diagnostics while the curtain is in different states, it might help figure out how exactly it is behaving and what it expects

I'm not sure about the meaning of your statement about the backlight. Do you mean the backlight is now working, while it didn't previously?

engusz commented 1 year ago

How to capture the device diagnostic?

Normal operation: if I turn off the backlight, only the LEDs turn off. Nothing else happens. With your solution: if I turn off the backlight, the LEDs turn off and (!) the stop button flashes once.

make-all commented 1 year ago

The stop button flashing is probably indicating an error, or a sign that the device is crashing due to its poor error handling. Does it mention anything in the manual about flashing to indicate error conditions?

Device diagnostics are available under Settings / Devices and Services / Tuya local - your device / 1 device / inside the Device Info panel.

engusz commented 1 year ago

It's here too!

{
  "home_assistant": {
    "installation_type": "Home Assistant OS",
    "version": "2022.12.1",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.10.7",
    "docker": true,
    "arch": "x86_64",
    "timezone": "Europe/Budapest",
    "os_name": "Linux",
    "os_version": "5.15.74",
    "supervisor": "2022.11.2",
    "host_os": "Home Assistant OS 9.3",
    "docker_version": "20.10.18",
    "chassis": "embedded",
    "run_as_root": true
  },
  "custom_components": {
    "thermal_comfort": {
      "version": "1.5.3",
      "requirements": []
    },
    "xiaomi_miot": {
      "version": "0.7.2",
      "requirements": [
        "construct==2.10.56",
        "python-miio>=0.5.6",
        "micloud>=0.3"
      ]
    },
    "tuya_local": {
      "version": "0.21.0",
      "requirements": [
        "pycryptodome~=3.16.0",
        "tinytuya==1.9.1"
      ]
    },
    "sonoff": {
      "version": "3.3.1",
      "requirements": [
        "pycryptodome>=3.6.6"
      ]
    },
    "saj_esolar": {
      "version": "1.2.9",
      "requirements": []
    },
    "watchman": {
      "version": "0.5.1",
      "requirements": [
        "prettytable==3.0.0"
      ]
    },
    "hacs": {
      "version": "1.28.4",
      "requirements": [
        "aiogithubapi>=22.2.4"
      ]
    }
  },
  "integration_manifest": {
    "domain": "tuya_local",
    "iot_class": "local_polling",
    "integration_type": "device",
    "name": "Tuya Local",
    "version": "0.21.0",
    "documentation": "https://github.com/make-all/tuya-local",
    "issue_tracker": "https://github.com/make-all/tuya-local/issues",
    "dependencies": [],
    "codeowners": [
      "@make-all"
    ],
    "requirements": [
      "pycryptodome~=3.16.0",
      "tinytuya==1.9.1"
    ],
    "config_flow": true,
    "is_built_in": false
  },
  "data": {
    "name": "Avatto curtain switch",
    "type": "avatto_curtain_switch",
    "device_id": "**REDACTED**",
    "local_key": "**REDACTED**",
    "host": "**REDACTED**",
    "api_version": 3.3,
    "status": {},
    "cached_state": {
      "updated_at": 1670758551.0678492,
      "1": "stop",
      "101": true
    },
    "pending_state": {
      "1": {
        "value": "stop",
        "updated_at": 1670758547.87472
      },
      "101": {
        "value": true,
        "updated_at": 1670758547.87472
      }
    },
    "home_assistant": {
      "name": "Avatto curtain switch",
      "name_by_user": null,
      "disabled": false,
      "disabled_by": null,
      "entities": [
        {
          "disabled": false,
          "disabled_by": null,
          "entity_category": "config",
          "device_class": null,
          "original_device_class": null,
          "icon": null,
          "unit_of_measurement": null,
          "state": {
            "entity_id": "light.avatto_curtain_switch_backlight",
            "state": "on",
            "attributes": {
              "supported_color_modes": [
                "onoff"
              ],
              "color_mode": "onoff",
              "friendly_name": "Avatto curtain switch Backlight",
              "supported_features": 0
            },
            "last_changed": "2022-12-11T11:35:47.681233+00:00",
            "last_updated": "2022-12-11T11:35:47.681233+00:00"
          }
        },
        {
          "disabled": false,
          "disabled_by": null,
          "entity_category": null,
          "device_class": null,
          "original_device_class": "curtain",
          "icon": null,
          "unit_of_measurement": null,
          "state": {
            "entity_id": "cover.avatto_curtain_switch",
            "state": "open",
            "attributes": {
              "device_class": "curtain",
              "friendly_name": "Avatto curtain switch",
              "supported_features": 11
            },
            "last_changed": "2022-12-11T11:35:36.034465+00:00",
            "last_updated": "2022-12-11T11:35:36.034465+00:00"
          }
        }
      ]
    }
  }
}
make-all commented 1 year ago

I think I understand what is going on. The HA cover API includes is_closed, is_opening and is_closing. If all three of these return False, then it assumes it is open. But in case of this device, the state cannot be determined, so we need to return None instead of False from these three functions to let Home Assistant know that it can't use that process of elimination to work out the state.

engusz commented 1 year ago

The switch doesn't know what position the shutter is in or what the last action was. The up and down button is active for 30-45 seconds from the moment you press it. The shutter motor knows the end positions, so it can't overrun.

make-all commented 1 year ago

I made some changes to the cover implementation so that Home Assistant should not disable the open button now. It is not in the release, but you can try it out by using "Redownload" in HACS and selecting "main" as the version to install.

engusz commented 1 year ago

I downloaded it again, added the switch again, but it still doesn't work. How else can I help?

make-all commented 1 year ago

Reviewing the comments in devices/README.md about cover devices, I think it is not possible to support a curtain switch without any feedback as a cover device in Home Assistant. Instead, I think it will need to be changed to a select entity with the three command values. That is probably not the best representation in the UI though, so I suggest putting buttons with an action to call the select.select_option service with appropriate parameters.

engusz commented 1 year ago

Another Local Tuya app managed this device properly. Its type was a shutter instead of a curtain.

engusz commented 1 year ago

Hi @make-all,

It doesn't work with the new method. After a few tries, the switch became inaccessible. (The backlight switch doesn't work either.)

image

How can I help your work?

make-all commented 1 year ago

I see from the log messages that you are bombarding the device with commands every 3 seconds. Tuya devices are very low powered and cannot handle much network load at all. It looks like the device is getting overloaded and shutting down the network for a while (either deliberately to cope or some internal crash/watchdog).

make-all commented 1 year ago

Closing, as the device appears to be working as well as Tuya devices ever do.

make-all commented 1 year ago

Reopening as I think this device may be a good test candidate for the button entity type requested in #244. This would provide a better UI than the current select entity.

engusz commented 1 year ago

OK thank you.

I didn't describe the error exactly in the previous post. The shutter movement did not work, no matter how many times I clicked the buttons. Then they became unavailable.

make-all commented 1 year ago

I suspect there may be an issue with using select in that it will not send any command if you select the same option as previously selected, maybe even if done through a service call rather than the UI. I'm also not sure whether it reads the current option from the device, or maybe it is intended as a purely write only UI control. Using separate button entities for each command should avoid this, as the buttons are stateless, so should send the corresponding command each time they are pressed.

Beware though that Tuya devices are low powered and easily overrun if you send multiple commands in quick succession. I am not sure that the protections against this will work across multiple entities, but they are anyway not 100% effective, as there is a trade-off between protecting against the worst case and having a reasonably responsive average case.