leeyuentuen / localtuya

local handling for Tuya devices
GNU General Public License v3.0
71 stars 17 forks source link

[3.7.0 beta 10] BLE Gateway devices reporting state correctly, but cannot control devices #70

Closed zaa94 closed 10 months ago

zaa94 commented 1 year ago

The problem

I have two BLE Diivoo water valve timers (Tuya category sfkzq) each with a WiFi gateway (Tuya category wg2) that I've got setup with v3.7.0b10 . I only have the switch and battery level entities configured as that's all I need for my current setup. Both the switch state and battery level state appear to be syncing correctly, but when I attempt to turn on/off the switch to either valve, nothing happens. According to the logs, it looks like once the payload is sent, it hangs, then once it receives an ACK, it stops.

Environment

Configuration configuration.yaml or config_flow

localtuya:  
  - host: 192.168.XX.XX
    device_id: FRONT_YARD_GW_ID
    local_key: <LOCAL_KEY>
    friendly_name: Front Yard Gateway
    protocol_version: "3.4"
    is_gateway: true
  - parent_gateway: FRONT_YARD_GW_ID
    device_id: FRONT_YARD_CONTROLLER_CID
    friendly_name: Front Yard Sprinkler
    protocol_version: "3.4"
    entities:
      - platform: switch
        friendly_name: Front Yard Sprinkler Valve Switch
        id: 1
      - platform: sensor
        friendly_name: Front Yard Sprinkler Valve Battery
        id: 7
        device_class: battery
        unit_of_measurement: "%"
  - host: 192.168.XX.XX
    device_id: BACK_YARD_GW_ID
    local_key: <LOCAL_KEY>
    friendly_name: Back Yard Gateway
    protocol_version: "3.4"
    is_gateway: true
  - parent_gateway: BACK_YARD_GW_ID
    device_id: BACK_YARD_CONTROLLER_CID
    friendly_name: Back Yard Sprinkler
    protocol_version: "3.4"
    entities:
      - platform: switch
        friendly_name: Back Yard Sprinkler Valve Switch
        id: 1
      - platform: sensor
        friendly_name: Back Yard Sprinkler Valve Battery
        id: 7
        device_class: battery
        unit_of_measurement: "%"

DP dump

Gateway DPs

"result": {
    "properties": [
      {
        "code": "up_channel",
        "custom_name": "",
        "dp_id": 1,
        "time": 1686887887646
      },
      {
        "code": "down_channel",
        "custom_name": "",
        "dp_id": 2,
        "time": 1686887887646
      }
    ]
  }

Valve Controller DPs

{
  "result": {
    "properties": [
      {
        "code": "switch",
        "custom_name": "",
        "dp_id": 1,
        "time": 1687475984332,
        "value": false
      },
      {
        "code": "fault",
        "custom_name": "",
        "dp_id": 4,
        "time": 1686888004302,
        "value": 0
      },
      {
        "code": "battery_percentage",
        "custom_name": "",
        "dp_id": 7,
        "time": 1687475757040,
        "value": 100
      },
      {
        "code": "time_use",
        "custom_name": "",
        "dp_id": 9,
        "time": 1687475984332,
        "value": 190355
      },
      {
        "code": "weather_delay",
        "custom_name": "",
        "dp_id": 10,
        "time": 1687475757080,
        "value": "cancel"
      },
      {
        "code": "countdown",
        "custom_name": "",
        "dp_id": 11,
        "time": 1687475980499,
        "value": 599
      },
      {
        "code": "work_state",
        "custom_name": "",
        "dp_id": 12,
        "time": 1687475984331,
        "value": "idle"
      },
      {
        "code": "smart_weather",
        "custom_name": "",
        "dp_id": 13,
        "time": 1686888004302,
        "value": "sunny"
      },
      {
        "code": "switch_weather",
        "custom_name": "",
        "dp_id": 14,
        "time": 1687475757104,
        "value": false
      },
      {
        "code": "use_time_one",
        "custom_name": "",
        "dp_id": 15,
        "time": 1687475983000,
        "value": 4
      },
      {
        "code": "cycle_timing",
        "custom_name": "",
        "dp_id": 16,
        "time": 1687475757120,
        "value": "AA=="
      },
      {
        "code": "timer",
        "custom_name": "",
        "dp_id": 17,
        "time": 1687475757128,
        "value": "AA=="
      }
    ]
  }

Provide Home Assistant taceback/logs

2023-06-22 16:11:42.840 DEBUG (MainThread) [custom_components.localtuya.common] [BACK_YARD_CONTROLLER_CID] Dispatching request request_set_dp to gateway with content {'value': True, 'dp_index': 1}
2023-06-22 16:11:42.841 DEBUG (MainThread) [custom_components.localtuya.common] [BACK_YARD_GW_ID] Received request request_set_dp from BACK_YARD_CONTROLLER_CID with content {'value': True, 'dp_index': 1}
2023-06-22 16:11:42.841 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] Sending command 7 (device type: v3.4)
2023-06-22 16:11:42.842 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] Sending payload: b'{"protocol":5,"t":1687475502,"data":{"dps":{"1":true}},"cid":"BACK_YARD_CONTROLLER_CID"}'
2023-06-22 16:11:42.842 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] final payload for cmd 13: b'3.4\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00{"protocol":5,"t":1687475502,"data":{"dps":{"1":true}},"cid":"BACK_YARD_CONTROLLER_CID"}'
2023-06-22 16:11:42.844 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] Dispatching sequence number 14629
2023-06-22 16:11:42.844 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] payload b'\x00\x00U\xaa\x00\x009%\x00\x00\x00\r\x00\x00\x00\x84v\xff\x1f\xf7hX&\xaa\xad\x88;\x03\x1e\xf7\x1dD\xd5\xe4\x17q\x16\xd0\xcd\xd7\xf5pr\x01H\xd0a\x95\x9d\xfb"\xeb\x8f\xab\x97s\xd6$I!\x81Z\xcf\xa7\x079\x14\xc0b\xd0/\xc0\xbd\x1a*\xddS\xe8}Wr\xe2|\xf1"\xddm\x0ezaK6\x94J\x87\xac\x86\x85\x9e\x98\x18\xb3-\'\x80\x8e<\xc7\xa3\xf9\xac!5\xed1\xfc\x80&\xe6>M\x16n\x99\xf51\xcc;\xfc\xb3Bys\xa8\xdc\xcc\x8c\xbb\x08\xa2J\xb4\x9f\xcf\x00\x00\xaaU' - MessagePayload(cmd=13, payload=b'{"protocol":5,"t":1687475502,"data":{"dps":{"1":true}},"cid":"BACK_YARD_CONTROLLER_CID"}')
2023-06-22 16:11:42.844 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] Command 13 waiting for sequence number 14629
2023-06-22 16:11:42.855 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] Dispatching message CMD 13 TuyaMessage(seqno=14629, cmd=13, retcode=0, payload=b'', crc=b'\x074\x07\xb34\x18i%\xac\xb8\xd0\xc3\x18j\xf5\xffy\xed\x9d\xdd\xf5\xed;\xben"\xe9\t\xe7\xf4\xbd"', crc_good=True)
2023-06-22 16:11:42.855 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] Dispatching sequence number 14629
2023-06-22 16:11:42.856 DEBUG (MainThread) [custom_components.localtuya.pytuya] [BACK_YARD_GW_ID] ACK received for command 13: ignoring it

Additional information

Gateway Information

    "result": {
        "active_time": 1686068403,
        "biz_type": 0,
        "category": "wg2",
        "create_time": 1686068403,
        "icon": "smart/icon/ay1547018215107OLzeG/b6b7db60ecbb436bb77e8a6c00ec4fc5.jpg",
        "id": "XXXXX",
        "ip": "XXXXX",
        "lat": "XXXXX",
        "local_key": "XXXXX",
        "lon": "XXXXX",
        "model": "WG02",
        "name": "XXXXX",
        "online": true,
        "owner_id": "XXXXX",
        "product_id": "v7owd9tzcaninc36",
        "product_name": "Gateway",
        "status": [],
        "sub": true,
        "time_zone": "XXXXX",
        "uid": "XXX",
        "update_time": 1687459834,
        "uuid": "XXX"
    }

Valve Controller Information


    "result": {
        "active_time": 1686068403,
        "biz_type": 0,
        "category": "sfkzq",
        "create_time": 1686068403,
        "icon": "smart/icon/ay1547018215107OLzeG/3d20ab7b5ab3c13f84b7b4ded95bad46.png",
        "id": "XXX",
        "ip": "",
        "lat": "XXX",
        "local_key": "XXX",
        "lon": "XXX",
        "model": "WT-03W",
        "name": "Back Yard Valve Controller",
        "node_id": "XXX",
        "online": true,
        "owner_id": "XXX",
        "product_id": "1fcnd8xk",
        "product_name": "Valve Controller",
        "status": [
            {
                "code": "switch",
                "value": false
            },
            {
                "code": "battery_percentage",
                "value": 100
            },
            {
                "code": "time_use",
                "value": 190355
            },
            {
                "code": "weather_delay",
                "value": "cancel"
            },
            {
                "code": "countdown",
                "value": 599
            },
            {
                "code": "work_state",
                "value": "idle"
            },
            {
                "code": "smart_weather",
                "value": "sunny"
            },
            {
                "code": "use_time_one",
                "value": 4
            }]
}
caco3 commented 1 year ago

@zaa94 I am trying to get the same device working (https://www.amazon.de/dp/B09P3VRVVB/ref=pe_27091401_487027711_TE_SCE_dp_2)

I tried it with UI config as well as with your config. In both cases I only see disabled entities:

grafik

How did you get it working to sync the entities?

zaa94 commented 1 year ago

Hey @caco3 ! The UI config did not work for me at all. Did you make sure that the device ID for the valve controller in the config file is the cid/node_id for the valve controller and not the device ID Tuya?

caco3 commented 1 year ago

Yes, I think so.

This is in configuration.yaml:

localtuya:  
  - host: 192.168.1.170
    device_id: bf641c545b9e1920da14vi
    local_key: V+IuH~QWR`)v6A{r
    friendly_name: Front Yard Gateway
    protocol_version: "3.4"
    is_gateway: true
  - parent_gateway: bf641c545b9e1920da14vi
    device_id: bf166cmaafamcows
    friendly_name: Front Yard Sprinkler
    protocol_version: "3.4"
    entities:
      - platform: switch
        friendly_name: Front Yard Sprinkler Valve Switch
        id: 1
      - platform: sensor
        friendly_name: Front Yard Sprinkler Valve Battery
        id: 7
        device_class: battery
        unit_of_measurement: "%"

And this is the discovery output of tuya-cli wizard:

  {
    name: 'Wi-Fi hub',
    id: 'bf641c545b9e1920da14vi',
    key: 'V+IuH~QWR`)v6A{r'
  },
  {
    name: 'Bewässerungscomputer',
    id: 'bf166cmaafamcows',
    key: 'U=bYOieeUKH6-O5x'
  }
zaa94 commented 1 year ago

@caco3 Yeah it looks like you're using the device ID for the sprinkler controller instead of the cid. What version of the tuya-cli are you using? The output of tuya-cli wizard for the WiFi gateway device should be like this:

  {
    name: 'Front Yard Gateway',
    id: '<GW_DEVICE_ID_REDACTED>',
    key: '<GW_KEY_REDACTED>',
    subDevices: [
      {
        name: 'Front Yard Valve Controller',
        id: 'CONRTOLLER_DEVICE_ID_REDACTED',
        cid: '<CONTROLLER_CID_REDACTED>' //USE THIS FOR THE SPRINKLER DEVICE ID IN YOUR CONFIG
      }
    ]
  }
caco3 commented 1 year ago

@zaa94 Thanks for your support.

I tried now the latest tuya-cli from https://github.com/TuyaAPI/cli but the wizard still just shows me the output of my previous post, so the cid is missing.

The tricky part seems to be to get this cid, as of now I did not yet find a way to get it :(

zaa94 commented 1 year ago

@caco3 Just played around with the newer APIs on the Tuya IoT and I found a way to get it. You'll want to use the Query Device Details API and your cid is equal to the value next to uuid as shown in the screenshot: Screenshot from 2023-06-28 06-59-09

caco3 commented 1 year ago

Thank you very much! I saw the UUID but did not expect it to be the same as the cid.

And I found the issue of the missing cid on the wizard output: I missed to add the Water Irrigation Timer to the Gateway. Odly it still got found by the wizard. but now as I added it (using the smart phone and the Smart Life app) I now also get the cid.

I am able to see the valve status (dps 1):

> tuya-cli get --ip 192.168.1.170 --id bf641c545b9e1920da14vi --key 'V+IuH~QWR`)v6A{r' --protocol-version 3.3 --cid f2a63e4085f363b6 --full
{
  dps: {
    '1': true,
    '4': 0,
    '7': 32,
    '9': 7311,
    '10': 'cancel',
    '11': 599,
    '12': 'manual',
    '14': false,
    '15': 1111
  },
  cid: 'f2a63e4085f363b6'
}

Also I am able to switch it off:

> tuya-cli set --ip 192.168.1.170 --id bf641c545b9e1920da14vi --key 'V+IuH~QWR`)v6A{r' --protocol-version 3.3 --cid f2a63e4085f363b6 --set off --dps 1
Set succeeded.

But I cant switch it on:

> tuya-cli set --ip 192.168.1.170 --id bf641c545b9e1920da14vi --key 'V+IuH~QWR`)v6A{r' --protocol-version 3.3 --cid f2a63e4085f363b6 --set on --dps 1
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('Timeout waiting for status response from device id: bf641c545b9e1920da14vi')
    at TuyaDevice.emit (events.js:303:17)
    at /usr/local/lib/node_modules/@tuyapi/cli/node_modules/tuyapi/index.js:434:12
    at Timeout._onTimeout (/usr/local/lib/node_modules/@tuyapi/cli/node_modules/p-timeout/index.js:25:13)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {
  code: 'ERR_UNHANDLED_ERROR',
  context: 'Timeout waiting for status response from device id: bf641c545b9e1920da14vi'
}

Does this work for you?

caco3 commented 1 year ago

Appendix: I need to use protocol 3.3, with 3.4 even the cli tool gets errors. But using protocoll 3.3, I can (in HA):

But I can not switch it on or off.

Using the cli tool, I can switch it off, but not on.

caco3 commented 1 year ago

So I got it working now with cli, see https://github.com/TuyaAPI/cli/issues/162#issuecomment-1612002727

I guess that this is not yet handled correctly in localtuya

Gyosa3 commented 12 months ago

Hi, I'm confused with the different IDs:

I was thinking. is there any chance that the node_id needed to register the subdevice is not the ID that is needed to place the commands? Which would be the device_id or this UUID mentioned above?

thanks for any clarification!

zaa94 commented 12 months ago

@Gyosa3 Depending on which Tuya API you use, the identifier you want to use in your config.yaml for the sub-device will be labeled any of these three variables in the JSON: 1) uuid, 2) node_id or 3) cid; these are not to be confused with the sub-device's device_id you enter into the Tuya API explorer to obtain these identifiers. To reiterate, the uuid/node_id/cid is what you will enter as the device_id in your config.yaml only for sub-devices.

In the localtuya API, the uuid/node_id/cid of the sub-device is needed to communicate between the sub-device and the gateway. I agree it is confusing and while it is a separate issue, the identifier used for sub-devices in the config should be named to cid rather than device_id.

Gyosa3 commented 12 months ago

ok thanks for the confirmation. So at least I understand the rationale up to the point of reading the DPs of subdevices via this identifier.

then I hope that someone will understand why the commands are then in error. I thought that maybe the wrong identifier was passed on but if they are all the same...

viny182 commented 10 months ago

Hello, Same issue with a Zigbee generic switch device... I Can fetch its status but cannot control it. However, it was working before I've updated my gateway to latest firmware that seem to use 3.4 protocol. In the previous firmware (which I have not done a backup :/) it was working fine with 3.6 release and 3.3 protocol. Unfortunately I have not found a way to downgrade it...

viny182 commented 10 months ago

Hello,

For anybody stumbling on this, there is a patch workaround to fix switch devices and restore its control abilities:

You should add the lines below to the file "/config/custom_components/localtuya/pytuya/init.py", after line 1378, considering you are using the release *"3.7.0 Beta 10"**:

                #START - update from yakish777 
                if cid is not None:
                    json_data[PARAMETER_DATA][PARAMETER_CID] = cid
                #END - update from yakish777 

Here is a screenshot on how it should look like: image

all credits to the guys from https://github.com/leeyuentuen/localtuya/issues/65

leeyuentuen commented 10 months ago

pushed new beta: https://github.com/leeyuentuen/localtuya/releases/tag/3.7.0-beta.12

viny182 commented 10 months ago

I have updated to https://github.com/leeyuentuen/localtuya/tree/3.7.0-beta.11 and confirmed it still working ;) Thanks!