make-all / tuya-local

Local support for Tuya devices in Home Assistant
MIT License
1.35k stars 522 forks source link

1/2/3/4 Buttons Switch #253

Closed Fannangir closed 1 year ago

Fannangir commented 2 years ago

Hi, Switches with 1 gang are detected as simple switch and work fine. Switches with 2/3/4 gangs are detected as simple switch as well, and do not work. Is there an option to add 2/3/4 gang switches support? Thanks.

make-all commented 2 years ago

Sure, some powerstrips are supported already. But there is no consistency over how the dps are laid out, so yours may not be compatible with one of the existing ones. If you configure as a simple switch, then download the diagnostics for the device the full dps layout will be visible (from Settings/Devices, selecting your device from inside the Tuya Local section and there should be a menu option to download diagnostics). That doesn't tell the meaning of the dps, but for switches it is usually quite obvious, as there are booleans for the switches, and potentially integers for timers and/or voltage/current/power/energy readings.

Fannangir commented 2 years ago

Thank you very much. I am attaching the info from the API explorer. I guess your assumption is right, and believe that there is no need to implement the timers. The switches are Somgoms 1/2/3/4 gangs switches. Link: https://www.aliexpress.com/item/4000062503762.html

The following is for a 1 Gang switch: { "result": { "category": "kg", "functions": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "countdown_1", "dp_id": 7, "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "switch_backlight", "dp_id": 16, "type": "Boolean", "values": "{}" }, { "code": "relay_status", "dp_id": 14, "type": "Enum", "values": "{\"range\":[\"power_on\",\"power_off\",\"last\"]}" } ], "status": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "countdown_1", "dp_id": 7, "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "switch_backlight", "dp_id": 16, "type": "Boolean", "values": "{}" }, { "code": "relay_status", "dp_id": 14, "type": "Enum", "values": "{\"range\":[\"power_on\",\"power_off\",\"last\"]}" } ] },

The following is for a 2 Gang switch: { "result": { "category": "kg", "functions": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "switch_2", "dp_id": 2, "type": "Boolean", "values": "{}" }, { "code": "countdown_1", "dp_id": 7, "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "countdown_2", "dp_id": 8, "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "relay_status", "dp_id": 14, "type": "Enum", "values": "{\"range\":[\"power_on\",\"power_off\",\"last\"]}" }, { "code": "switch_backlight", "dp_id": 16, "type": "Boolean", "values": "{}" } ], "status": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "switch_2", "dp_id": 2, "type": "Boolean", "values": "{}" }, { "code": "countdown_1", "dp_id": 7, "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "countdown_2", "dp_id": 8, "type": "Integer", "values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}" }, { "code": "relay_status", "dp_id": 14, "type": "Enum", "values": "{\"range\":[\"power_on\",\"power_off\",\"last\"]}" }, { "code": "switch_backlight", "dp_id": 16, "type": "Boolean", "values": "{}" } ] }, "success": true, "t": 1666889859047, "tid": "76c3afe0561811ed89f3fe1b67a66e75" }

The following is for a 3 Gang switch: { "result": { "category": "kg", "functions": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "switch_2", "dp_id": 2, "type": "Boolean", "values": "{}" }, { "code": "switch_3", "dp_id": 3, "type": "Boolean", "values": "{}" }, { "code": "switch_4", "type": "Boolean", "values": "{}" } ], "status": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "switch_2", "dp_id": 2, "type": "Boolean", "values": "{}" }, { "code": "switch_3", "dp_id": 3, "type": "Boolean", "values": "{}" } ] },

The following is for a 4 Gang switch: { "result": { "category": "kg", "functions": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "switch_2", "dp_id": 2, "type": "Boolean", "values": "{}" }, { "code": "switch_3", "dp_id": 3, "type": "Boolean", "values": "{}" }, { "code": "switch_4", "dp_id": 4, "type": "Boolean", "values": "{}" } ], "status": [ { "code": "switch_1", "dp_id": 1, "type": "Boolean", "values": "{}" }, { "code": "switch_2", "dp_id": 2, "type": "Boolean", "values": "{}" }, { "code": "switch_3", "dp_id": 3, "type": "Boolean", "values": "{}" }, { "code": "switch_4", "dp_id": 4, "type": "Boolean", "values": "{}" } ] },

Fannangir commented 2 years ago

Just tried to setup a 1-Gang and 2-Gang switches. Both are detected as a simple switch. For a 1-Gang it does work (as in previous version). For the 2-gang, only 1 gang is detected. Attached device diagnostic info of the trial to setup 2-gang (setup as a simple switch): { "home_assistant": { "installation_type": "Home Assistant OS", "version": "2022.10.5", "dev": false, "hassio": true, "virtualenv": false, "python_version": "3.10.5", "docker": true, "arch": "x86_64", "timezone": "Asia/Jerusalem", "os_name": "Linux", "os_version": "5.15.74", "supervisor": "2022.10.0", "host_os": "Home Assistant OS 9.3", "docker_version": "20.10.18", "chassis": "vm", "run_as_root": true }, "custom_components": { "tuya_local": { "version": "0.20.2", "requirements": [ "pycryptodome~=3.15.0", "tinytuya==1.7.1" ] }, }, "integration_manifest": { "domain": "tuya_local", "iot_class": "local_polling", "name": "Tuya Local", "version": "0.20.2", "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.15.0", "tinytuya==1.7.1" ], "config_flow": true, "is_built_in": false }, "data": { "name": "Hall / Dining Light", "type": "simple_switch", "device_id": "REDACTED", "local_key": "REDACTED", "host": "REDACTED", "api_version": 3.3, "status": {}, "cached_state": { "1": false, "2": false, "7": 0, "8": 0, "16": true, "updated_at": 1667243290.6000712 }, "pending_state": {}, "home_assistant": { "name": "Hall / Dining Light", "name_by_user": null, "disabled": false, "disabled_by": null, "entities": [ { "disabled": false, "disabled_by": null, "entity_category": null, "device_class": null, "original_device_class": "switch", "icon": null, "unit_of_measurement": null, "state": { "entity_id": "switch.hall_dining_light", "state": "off", "attributes": { "device_class": "switch", "friendly_name": "Hall / Dining Light" }, "last_changed": "2022-10-31T19:06:40.593461+00:00", "last_updated": "2022-10-31T19:06:40.593461+00:00" } } ] } } }

make-all commented 2 years ago

It looks like the dpId 14 that is listed in the tuya portal is missing from the local data. I assume this is the same case for the single switch as well.

Fannangir commented 2 years ago

I have just updated the files and added the 2-gang switch with no issues, detected both gangs, background light and timer. For the one gang, it is still detected as simple_switch. My guess is that the switches could be different, similar to the 3 and 4 gangs, which do not have the backlight and the timer. The log from the 1 gang switch: { "home_assistant": { "installation_type": "Home Assistant OS", "version": "2022.10.5", "dev": false, "hassio": true, "virtualenv": false, "python_version": "3.10.5", "docker": true, "arch": "x86_64", "timezone": "Asia/Jerusalem", "os_name": "Linux", "os_version": "5.15.74", "supervisor": "2022.10.0", "host_os": "Home Assistant OS 9.3", "docker_version": "20.10.18", "chassis": "vm", "run_as_root": true }, "custom_components": { "tuya_local": { "version": "0.20.2", "requirements": [ "pycryptodome~=3.15.0", "tinytuya==1.7.1" ] }, }, "integration_manifest": { "domain": "tuya_local", "iot_class": "local_polling", "name": "Tuya Local", "version": "0.20.2", "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.15.0", "tinytuya==1.7.1" ], "config_flow": true, "is_built_in": false }, "data": { "name": "Shelter Room Light", "type": "simple_switch", "device_id": "REDACTED", "local_key": "REDACTED", "host": "REDACTED", "api_version": 3.3, "status": {}, "cached_state": { "1": false, "updated_at": 1667296757.625818 }, "pending_state": {}, "home_assistant": { "name": "Shelter Room Light", "name_by_user": null, "disabled": false, "disabled_by": null, "entities": [ { "disabled": false, "disabled_by": null, "entity_category": null, "device_class": null, "original_device_class": "switch", "icon": null, "unit_of_measurement": null, "state": { "entity_id": "switch.shelter_light_master", "state": "off", "attributes": { "device_class": "switch", "friendly_name": "Shelter Room Light" }, "last_changed": "2022-11-01T09:54:10.444116+00:00", "last_updated": "2022-11-01T09:54:10.444116+00:00" } } ] } } }

make-all commented 2 years ago

"cached_state": {
"1": false,
"updated_at": 1667296757.625818
},

That looks like it is just a simple switch, there are no extra features to
expose.  I'll leave the config there in case there is a variant of this
that matches the iot.tuya.com documentation.
Fannangir commented 2 years ago

That is what I thought. The switches are probably different. I have just tested the 2-gang switch. There is a massive delay between the activation of the switch to its response. If I physically press the switch, the status in home assistant some times updated in 2 seconds, sometimes in 10 seconds and sometimes in 30 seconds. The same happens when I activate the switch via home assistant, it is actually turned on (or off) after 2 seconds, sometimes 10 seconds, sometimes 30 seconds. As a comparison, I have tested the local tuya integration, it reacts immediately in both cases.

A small edit: just tested a simple switch (1-gang) there is the same issue as well, it updates very late. As a comparison, I have tested the local tuya integration here as well, it reacts immediately in both cases.

Do you have a clue what could be the issue?

Fannangir commented 2 years ago

Just tested a bit more. The activation delay from home assistant is actually severe, about 1 sec or less. The problem is with the activation from the switch itself. The update on home assistant could be 30 secs, which is way too long. Is it something that is common with the simple switch?

make-all commented 2 years ago

Delays in getting data from devices are a general problem, with local tuya connections, since the state needs to be polled from the device. Many devices refuse connections or reboot and misbehave if polled too quickly, so for stability this integration polls only every 30 seconds. Previously we did try polling at 10s intervals, but some devices had problems at that rate.

Fannangir commented 2 years ago

Got the point. But then the question is how local tuya does that with no issues? I am using local tuya for about a year now. The switches work perfectly with no issues. I have decided to try and switch to Tuya local to integrate all of my devices into a single integration. Unfortunately waiting for a switch press for up to 30 seconds is not an option. Maybe the solution could be an optional field to configure the delay? Let’s say I can try to set it to 1 sec and see what would be the result? I believe it could be implemented per device, as not all of the devices are affected.

make-all commented 2 years ago

The update speed already has its own issue open at #176. I just had a look at localtuya's internal modified pytuya implementation, and I think I understand how they are using persistent connections to get timely updates.

dragosmd commented 1 year ago

Same problem: Living Product ID = key9nfnyp89j4esc [Valid payload]: Address = 192.168.2.114, Version = 3.3, Status: {'1': False, '2': False, '7': 0, '8': 0} Dining Product ID = key9nfnyp89j4esc [Valid payload]: Address = 192.168.2.107, Version = 3.3, Status: {'1': False, '2': False, '7': 0, '8': 0}

Are detected as 1gang switch. '2': should be also a Switch

RomchikL commented 1 year ago

Same problem with Aubess 2 gang Switch (v. 3.4). I have only one gang via the integration.

RomchikL commented 1 year ago

Hmmm, for me the problem is relevant. With Aubess 2 gang Switch and v. 0.20.3. In HA I have only one switch and one timer.

{
  "home_assistant": {
    "installation_type": "Home Assistant Container",
    "version": "2022.11.3",
    "dev": false,
    "hassio": false,
    "virtualenv": false,
    "python_version": "3.10.7",
    "docker": true,
    "arch": "x86_64",
    "os_name": "Linux",
    "os_version": "5.15.0-52-generic",
    "run_as_root": true
  },
  .....
    "tuya_local": {
      "version": "0.20.3",
      "requirements": [
        "pycryptodome~=3.15.0",
        "tinytuya==1.7.1"
      ]
    },
    "localtuya": {
      "version": "4.1.1",
      "requirements": []
    }
  },
  "integration_manifest": {
    "domain": "tuya_local",
    "iot_class": "local_polling",
    "name": "Tuya Local",
    "version": "0.20.3",
    "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.15.0",
      "tinytuya==1.7.1"
    ],
    "config_flow": true,
    "is_built_in": false
  },
  "data": {
    "name": "Simple switch with timer",
    "type": "simple_switch_timerv2",
    "device_id": "**REDACTED**",
    "local_key": "**REDACTED**",
    "host": "**REDACTED**",
    "api_version": 3.4,
    "status": {},
    "cached_state": {
      "1": true,
      "2": true,
      "9": 0,
      "10": 0,
      "38": "memory",
      "42": "",
      "43": "",
      "44": "",
      "47": "flip",
      "updated_at": 1668972061.063756
    },
    "pending_state": {},
    "home_assistant": {
      "name": "Simple switch with timer",
      "name_by_user": null,
      "disabled": false,
      "disabled_by": null,
      "entities": [
        {
          "disabled": false,
          "disabled_by": null,
          "entity_category": null,
          "device_class": null,
          "original_device_class": "outlet",
          "icon": null,
          "unit_of_measurement": null,
          "state": {
            "entity_id": "switch.simple_switch_with_timer",
            "state": "on",
            "attributes": {
              "device_class": "outlet",
              "friendly_name": "Simple switch with timer"
            },
            "last_changed": "2022-11-20T19:21:01.065357+00:00",
            "last_updated": "2022-11-20T19:21:01.065357+00:00"
          }
        },
        {
          "disabled": false,
          "disabled_by": null,
          "entity_category": "config",
          "device_class": null,
          "original_device_class": null,
          "icon": null,
          "unit_of_measurement": "min",
          "state": {
            "entity_id": "number.simple_switch_with_timer_timer",
            "state": "0.0",
            "attributes": {
              "min": 0.0,
              "max": 1440.0,
              "step": 1.0,
              "mode": "auto",
              "unit_of_measurement": "min",
              "icon": "mdi:timer",
              "friendly_name": "Simple switch with timer Timer"
            },
            "last_changed": "2022-11-20T19:21:01.066368+00:00",
            "last_updated": "2022-11-20T19:21:01.066368+00:00"
          }
        }
      ]
    }
  }
}
RomchikL commented 1 year ago

Now it works, thanks a lot!