codetheweb / tuyapi

🌧 An easy-to-use API for devices that use Tuya's cloud services. Documentation: https://codetheweb.github.io/tuyapi.
MIT License
2.1k stars 343 forks source link

Commands for PIR sensors #409

Closed mnifakram closed 3 years ago

mnifakram commented 3 years ago

Hi There,

First thanks to all contributors for all the huge work that has been done on this library. Second thing, this not an issue with this library but it's more with Tuya and their documentation and I'm not sure where to even ask this question so my apologies if I'm bothering anyone. I have 10 PIR sensors that I want to be able to control via API instead of the app. I have managed to query devices for their information and status but I wasn't able to change their status via commands. Based on the documentation and the status output of the device, I can see that there's a pir function that accepts either pir or none but trying to send this command body

commands: [
        {
          code: 'pir',
          value: 'pir' // I also tried 'none'
        }
      ]

always result to

  resp: {
    code: 2008,
    msg: 'command or value not support',
    success: false,
    t: 1611313327657
  }

I'm wondering what could be the right command to be able to enable and disable PIR alarm push notification like I can do from the app? The only thing that I'm able to think of, is that I have to try to intercept the HTTPS requests being sent by the app to Tuya server which may not be an easy task if they use pinning certificate.

If someone has any idea or hints that will be much appreciated.

Switch that I want to control: Screenshot_20210122-121434

/devices/{deviceId} output:

 {
  active_time: 1610729410,
  biz_type: 0,
  category: 'pir',
  create_time: 1610729410,
  icon: 'smart/icon/ay1562583874631k02Aw/cc23c6cfacdb918618d5db7d2a23e76a.jpg',
  id: 'ebdxxxxxxxxxxxxx',
  ip: '68.XX.XX.XX',
  local_key: '93xxxxxxxxxxxxx',
  name: 'Front 2',
  online: true,
  owner_id: '3xxxxxxxx',
  product_id: 'nmvopzrclciuskmd',
  product_name: 'Motion Sensor',
  status: [
    { code: 'pir', value: 'none' },
    { code: 'battery_percentage', value: 96 },
    { code: 'temper_alarm', value: false }
  ],
  sub: false,
  time_zone: '-05:00',
  uid: 'azxxxxxxxxxxxxxxxxx',
  update_time: 1611151176,
  uuid: 'cfxxxxxxxxxxxxx'
}

/devices/{deviceId}/status output:

[
  { code: 'pir', value: 'none' },
  { code: 'battery_percentage', value: 96 },
  { code: 'temper_alarm', value: false }
]

/devices/{deviceId}/specifications output:

{
  category: 'pir',
  functions: [],
  status: [
    { code: 'pir', type: 'Enum', values: '{"range":["pir","none"]}' },
    {
      code: 'battery_percentage',
      type: 'Integer',
      values: '{"unit":"%","min":0,"max":100,"scale":0,"step":1}'
    },
    { code: 'temper_alarm', type: 'Boolean', values: '{}' }
  ]
}

/devices/{deviceId}/functions output:

  resp: {
    code: 2009,
    msg: 'not support this device',
    success: false,
    t: 1611313933657
  }
}
codetheweb commented 3 years ago

Are you using the OpenAPI package?

What you're sending looks right, so I think at this point you'll have to sniff traffic to debug further.

mnifakram commented 3 years ago

I'm using this library so I'm not sure what's the difference but I guess they all use the same Official Tuya Open API. Unless there's something else that I'm missing.

milo526 commented 3 years ago

This library does not use the OpenAPI package.

The OpenAPI package uses the TuyaCloud to connect to devices while this library uses your local network to communicate.

The output that you posted however is output from the OpenAPI (specifically their Device Control endpoints)

mnifakram commented 3 years ago

Oh I see, you're right I'm using the OpenAPI as all my rest query are hitting https://openapi.tuyaus.com/v1.0/. So do you think that I can't go further without sniffing?

milo526 commented 3 years ago

Please note that I have no experience with this specific device (or the use of Tuya sensors in general) so all of the following is based on anecdotes and gut feeling. But I'd expect that you cannot change this variable for this device.

The OpenAPI dictates that all functions of the device are placed on the /devices/{deviceId}/functions endpoint. That endpoints returns an error for you - this strongly seems to indicate that you cannot update anything for this device. The only thing you can do is get its current status - i.e. get if it's triggered, get its battery percentage and get the state of the temper alarm.

This idea is also backed by the fact that the value that you intend to change is device specific. It indicates if your phone should receive a notification when the status of the device changes - how would the API handle this if you have multiple devices?

mnifakram commented 3 years ago

Yeah totally understood and I'm here for a second opinion based on experience/expertise or some hints/guidance as this is my first time with Tuya in general .

I'm not sure how the API will handle multiple devices but currently I have 10 devices all of them are connected to the same account and I could set that toggle for each device separately. The only thing that is missing (and that's why I'm trying to do it using the API) is that you cannot control multiple devices with automation. I need to find a way to set them all on/off with a single click. Thank you though for the help, I do really appreciate it.