rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.84k stars 545 forks source link

Device not showing up in HA after adding a device via Local Tuya #1297

Open MadOtis opened 1 year ago

MadOtis commented 1 year ago

The problem

A device added via Local Tuya is not showing up in the HA device list and Local Tuya reports an error that the device already exists when trying to manually re-add it.

Environment

Steps to reproduce

  1. Added an identical power strip successfully.
  2. Tried to add a 2nd identical strip but recieved "Connection to device succeeded but no datapoints found, please try again."
  3. Googling identified I should add manual DPS values for the 6 switches (DPS: 1,2,3,4,5,6 identified in iot.tuya.com as the 6 available DPS pins in the device that correspond to 1-4 individually addressable switches in the strip, 5 USB ports, 6 Entire strip on-off switch)
  4. Recursively set up all 6 DPS IDs to corresponding switches in the SmartLife app.
  5. Clicked Submit
  6. Device doesn't appear in the HA devices list & I get an error message: "Device has already been configured." if I try to re-add it via the elipsis option.
  7. Cannot find the device to delete it in either the HA devices list, but I can select it in the LocalTuya config menu to edit it.

DP dump

INFO:localtuya:localtuya version 1.0.0
INFO:localtuya:Python 3.9.6 (default, Oct 18 2022, 12:41:40)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
INFO:localtuya:Using pytuya version '10.0.0'
INFO:localtuya:Detecting list of available DPS of device 06737041840d8e9365ab [10.0.1.82], protocol 3.3.
DEBUG:localtuya.pytuya:[067...5ab] Sending command 10 (device type: type_0a)
DEBUG:localtuya.pytuya:[067...5ab] Sending payload: b'{"gwId":"06737041840d8e9365ab","devId":"06737041840d8e9365ab","uid":"06737041840d8e9365ab","t":"1677265731"}'
DEBUG:localtuya.pytuya:[067...5ab] Command 10 waiting for sequence number 1
DEBUG:localtuya.pytuya:[067...5ab] Connection lost: [Errno 32] Broken pipe
ERROR:localtuya.pytuya:[067...5ab] Failed to get status:
Traceback (most recent call last):
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/locks.py", line 413, in acquire
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 492, in wait_for
    fut.result()
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 849, in detect_available_dps
    data = await self.status()
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 781, in status
    status = await self.exchange(DP_QUERY)
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 755, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 446, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 494, in wait_for
    raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
INFO:localtuya:Detecting list of available DPS of device 06737041840d8e9365ab [10.0.1.82], protocol 3.3.
DEBUG:localtuya.pytuya:[067...5ab] Sending command 10 (device type: type_0a)
DEBUG:localtuya.pytuya:[067...5ab] Sending payload: b'{"gwId":"06737041840d8e9365ab","devId":"06737041840d8e9365ab","uid":"06737041840d8e9365ab","t":"1677265738"}'
DEBUG:localtuya.pytuya:[067...5ab] Command 10 waiting for sequence number 1
DEBUG:localtuya.pytuya:[067...5ab] Connection lost: [Errno 54] Connection reset by peer
ERROR:localtuya.pytuya:[067...5ab] Failed to get status:
Traceback (most recent call last):
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/locks.py", line 413, in acquire
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 492, in wait_for
    fut.result()
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 849, in detect_available_dps
    data = await self.status()
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 781, in status
    status = await self.exchange(DP_QUERY)
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 755, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
  File "/Users/randalc/Downloads/tuyadebug/localtuya/pytuya/__init__.py", line 446, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/tasks.py", line 494, in wait_for
    raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
INFO:localtuya:TIMEOUT: No response from device 06737041840d8e9365ab [10.0.1.82] after 2 attempts.

Provide Home Assistant traceback/logs

2023-02-24 09:50:50.669 INFO (MainThread) [custom_components.localtuya] Cloud API connection succeeded.
2023-02-24 09:50:50.845 INFO (MainThread) [homeassistant.components.switch] Setting up switch.localtuya
2023-02-24 09:50:50.848 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.localtuya entity: switch.dyson_fan
2023-02-24 09:50:50.849 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.localtuya entity: switch.speaker
2023-02-24 09:50:50.849 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.localtuya entity: switch.mod_charger
2023-02-24 09:50:50.850 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.localtuya entity: switch.unused
2023-02-24 09:50:50.851 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.localtuya entity: switch.usb
2023-02-24 09:50:50.852 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new switch.localtuya entity: switch.bedside_strip_all_outlets
2023-02-24 09:50:51.627 ERROR (MainThread) [homeassistant.components.google_assistant.http] Request for https://homegraph.googleapis.com/v1/devices:reportStateAndNotification failed: 404
2023-02-24 09:50:51.764 ERROR (MainThread) [homeassistant.components.google_assistant.http] Request for https://homegraph.googleapis.com/v1/devices:reportStateAndNotification failed: 404
2023-02-24 09:51:15.615 ERROR (MainThread) [custom_components.localtuya.pytuya] [067...5ab] Failed to get status:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/asyncio/locks.py", line 390, in acquire
await fut
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/asyncio/tasks.py", line 456, in wait_for
return fut.result()
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/config/custom_components/localtuya/pytuya/__init__.py", line 857, in detect_available_dps
data = await self.status()
File "/config/custom_components/localtuya/pytuya/__init__.py", line 789, in status
status = await self.exchange(DP_QUERY)
File "/config/custom_components/localtuya/pytuya/__init__.py", line 763, in exchange
msg = await self.dispatcher.wait_for(seqno, payload.cmd)
File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
File "/usr/local/lib/python3.10/asyncio/tasks.py", line 458, in wait_for
raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
2023-02-24 09:52:00.389 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'power_on_behavior' when rendering '{{ value_json.power_on_behavior }}'
2023-02-24 09:52:00.390 ERROR (MainThread) [homeassistant.components.mqtt.select] Invalid option for select.lr_overhead_se_power_on_behavior: '' (valid options: ['off', 'on', 'toggle', 'previous'])
2023-02-24 09:52:08.844 ERROR (MainThread) [custom_components.localtuya.pytuya] [067...5ab] Failed to get status:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/asyncio/locks.py", line 390, in acquire
await fut
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/asyncio/tasks.py", line 456, in wait_for
return fut.result()
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/config/custom_components/localtuya/pytuya/__init__.py", line 857, in detect_available_dps
data = await self.status()
File "/config/custom_components/localtuya/pytuya/__init__.py", line 789, in status
status = await self.exchange(DP_QUERY)
File "/config/custom_components/localtuya/pytuya/__init__.py", line 763, in exchange
msg = await self.dispatcher.wait_for(seqno, payload.cmd)
File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
File "/usr/local/lib/python3.10/asyncio/tasks.py", line 458, in wait_for
raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError

^^^ Last error repeated several times with identical output

Additional information

This same device DOES work via the cloud-based Tuya integration well, and as mentioned is identical to the 1st power strip added in the first few log entries.

Ideally, I'd just like to find the errant device and remove it entirely so I can try to re-add it after an HA/LocalTuya restart.

MadOtis commented 1 year ago

After removing localtuya and re-installing (after an HA restart), I was able to add the 2nd smart power strip, but it is always shown as "unavailable", but I was able to find it in the HA device list and was able to download a copy of the "diagnostic data" from the device:

  "integration_manifest": {
    "domain": "localtuya",
    "name": "LocalTuya integration",
    "version": "5.0.0",
    "documentation": "https://github.com/rospogrigio/localtuya/",
    "dependencies": [],
    "codeowners": [
      "@rospogrigio",
      "@postlund"
    ],
    "issue_tracker": "https://github.com/rospogrigio/localtuya/issues",
    "requirements": [],
    "config_flow": true,
    "iot_class": "local_push",
    "is_built_in": false
  },
  "data": {
    "device_config": {
      "friendly_name": "Bedside Strip",
      "host": "10.0.1.25",
      "device_id": "06<sanitized>a1",
      "local_key": "e<sanitized>c",
      "protocol_version": "3.1",
      "enable_debug": true,
      "manual_dps_strings": "1,2,3,4,5,6",
      "model": "Smart Power Strip",
      "dps_strings": [
        "1 (value: -1)",
        "2 (value: -1)",
        "3 (value: -1)",
        "4 (value: -1)",
        "5 (value: -1)",
        "6 (value: -1)"
      ],
      "entities": [
        {
          "id": 6,
          "friendly_name": "Bedroom Strip (All)",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 1,
          "friendly_name": "Dyson Fan",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 2,
          "friendly_name": "Speaker",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 3,
          "friendly_name": "Cell Charger",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 4,
          "friendly_name": "Unused",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 5,
          "friendly_name": "Bedside Lamp 2",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        }
      ]
    },
    "device_cloud_info": {
      "active_time": 1622159170,
      "biz_type": 18,
      "category": "cz",
      "create_time": 1554597255,
      "icon": "smart/icon/1524736914ad18sz4byzvckzb3a38m5nrk9_0.jpg",
      "id": "06<sanitized>a1",
      "ip": "<sanitized>",
      "lat": "<sanitized>",
      "local_key": "e<sanitized>c",
      "lon": "<sanitized>",
      "model": "SM-SO301/306",
      "name": "Bedside Strip",
      "online": true,
      "owner_id": "<sanitized>",
      "product_id": "of0VDuFcWsBTfzWt",
      "product_name": "Smart Power Strip",
      "status": [
        {
          "code": "switch_1",
          "value": true
        },
        {
          "code": "switch_2",
          "value": true
        },
        {
          "code": "switch_3",
          "value": true
        },
        {
          "code": "switch_4",
          "value": false
        },
        {
          "code": "switch_usb1",
          "value": true
        }
      ],
      "sub": false,
      "time_zone": "-08:00",
      "uid": "<sanitized>",
      "update_time": 1658734354,
      "uuid": "<sanitized>"
    }
  }
}

Conversely, for the 1st strip added that does FULLY work as expected:

 "integration_manifest": {
    "domain": "localtuya",
    "name": "LocalTuya integration",
    "version": "5.0.0",
    "documentation": "https://github.com/rospogrigio/localtuya/",
    "dependencies": [],
    "codeowners": [
      "@rospogrigio",
      "@postlund"
    ],
    "issue_tracker": "https://github.com/rospogrigio/localtuya/issues",
    "requirements": [],
    "config_flow": true,
    "iot_class": "local_push",
    "is_built_in": false
  },
  "data": {
    "device_config": {
      "friendly_name": "Printer Strip",
      "host": "<sanitized>",
      "device_id": "06<sanitized>ab",
      "local_key": "b<sanitized>c",
      "protocol_version": "3.1",
      "enable_debug": false,
      "model": "Smart Power Strip",
      "dps_strings": [
        "1 (value: False)",
        "2 (value: False)",
        "3 (value: True)",
        "4 (value: True)",
        "5 (value: True)",
        "6 (value: 0)"
      ],
      "entities": [
        {
          "id": 6,
          "friendly_name": "Printer Switch (All)",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 1,
          "friendly_name": "Mars 2 Pro",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 2,
          "friendly_name": "Mercury Plus",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 3,
          "friendly_name": "Minime",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 4,
          "friendly_name": "Doctor Evil",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        },
        {
          "id": 5,
          "friendly_name": "Octorprints",
          "restore_on_reconnect": true,
          "is_passive_entity": false,
          "platform": "switch"
        }
      ]
    },
    "device_cloud_info": {
      "active_time": 1634706101,
      "biz_type": 18,
      "category": "cz",
      "create_time": 1592802552,
      "icon": "smart/icon/1524736914ad18sz4byzvckzb3a38m5nrk9_0.jpg",
      "id": "06<sanitized>ab",
      "ip": "<sanitized>",
      "lat": "<sanitized>",
      "local_key": "b<sanitized>c",
      "lon": "<sanitized>",
      "model": "SM-SO301/306",
      "name": "Printer Strip",
      "online": true,
      "owner_id": "<sanitized>",
      "product_id": "of0VDuFcWsBTfzWt",
      "product_name": "Smart Power Strip",
      "status": [
        {
          "code": "switch_1",
          "value": false
        },
        {
          "code": "switch_2",
          "value": false
        },
        {
          "code": "switch_3",
          "value": true
        },
        {
          "code": "switch_4",
          "value": true
        },
        {
          "code": "switch_usb1",
          "value": true
        }
      ],
      "sub": false,
      "time_zone": "-08:00",
      "uid": "<sanitized>",
      "update_time": 1658734356,
      "uuid": "<sanitized>"
    }
  }
}

Again, the 1st one added works while adding the second one fails with the "Connection to device succeeded but no datapoints found, please try again." error unless I manually add DPIDs

MadOtis commented 1 year ago

This is still happening after updating from 5.1.0-beta0 to 5.1.0 release tag.

Forge36 commented 10 months ago

I was seeing similar errors. A YAML reload didn't fix it however a full restart of the HA did 🤷‍♂️