Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1/2/3/4 Buttons Switch #253

Closed
Fannangir opened this issue Oct 26, 2022 · 15 comments
Closed

1/2/3/4 Buttons Switch #253

Fannangir opened this issue Oct 26, 2022 · 15 comments

Comments

@Fannangir
Copy link

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
Copy link
Owner

make-all commented Oct 26, 2022

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
Copy link
Author

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": "{}"
}
]
},

@make-all make-all added the new device Unsupported device label Oct 27, 2022
make-all added a commit that referenced this issue Oct 30, 2022
- 1/2 gang added as specific devices, as they contain backlight control, timers and initial state configuration.
- 3/4 gang added as generic devices, as they simply contain the switches with no other features, and are likely to match other similar devices (even as partial matches if the other devices have more features).

Issue #253
@make-all make-all added awaiting confirmation Wating for confirmation the issue is solved unreleased Will be in next release and removed new device Unsupported device unreleased Will be in next release labels Oct 30, 2022
@Fannangir
Copy link
Author

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 added a commit that referenced this issue Oct 31, 2022
Despite being listed in the tuya portal, this does not appear in the
local data.  It may be a write-only dp, but these are not supported.

Issue #253
@make-all make-all added the unreleased Will be in next release label Oct 31, 2022
@make-all
Copy link
Owner

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
Copy link
Author

Fannangir commented Nov 1, 2022

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
Copy link
Owner

make-all commented Nov 1, 2022 via email

@make-all make-all removed the awaiting confirmation Wating for confirmation the issue is solved label Nov 1, 2022
@Fannangir
Copy link
Author

Fannangir commented Nov 1, 2022

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
Copy link
Author

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
Copy link
Owner

make-all commented Nov 3, 2022

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
Copy link
Author

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
Copy link
Owner

make-all commented Nov 3, 2022

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
Copy link

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
Copy link

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

make-all added a commit that referenced this issue Nov 13, 2022
Should be a fairly common config, so implemented as generic.
Issue #253
@make-all make-all removed the unreleased Will be in next release label Nov 20, 2022
@RomchikL
Copy link

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"
          }
        }
      ]
    }
  }
}

make-all added a commit that referenced this issue Nov 26, 2022
@RomchikL
Copy link

Now it works, thanks a lot!

make-all added a commit that referenced this issue Jan 11, 2023
  - create a loop for reading from the persistent connection.
  - read from that loop in a separate async task
  - register links back to entities from the device so that we can asynchronously inform HA of updates
  - remove the old poll mechanism
  - make some old synchronous methods asynchronous, remove duplicates where there were both
  - use asyncio.sleep instead of a separate Timer thread to send with delay
  - send things without waiting for replies (as the reply will come through the read loop)
  - keep track of what was sent and avoid sending it again while we are keeping it to reflect in state.
  - call the api asynchronously in the retry method to avoid blocking the event thread, and return the return value.
  - use HomeAssistant lifecycle events to avoid delaying startup and shutdown

    Issue #92, #146, #176, #231, #249, #253, #277, #286, #311, #330
make-all added a commit that referenced this issue Jun 10, 2023
This was mentioned in the original report #253, and also seems to be the only
difference in the config for single Novadigital switch submitted in #767.

In both switches, it does not show in the logs, so needs to be optional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants