konsumer / tplink-lightbulb

Control TP-Link smart lightbulbs from nodejs
MIT License
191 stars 32 forks source link

It is possible to change the names of the devices #50

Closed migte closed 3 years ago

migte commented 4 years ago

Hello again!

I'm not sure whether or not this is in the scope of the project, but I was able to have a function to change the names of the devices. The if and else is due to lightbulbs and other devices working differently. "system": {"set_dev_alias"{... has worked on all of my switches and power outlets. I have only tested "smartlife.iot.common.system": { "set_dev_alias" {... on my LB130's.

Here is the lib code:

/**
   * Set the name of lightbulb
   * @module name
   * @param {String} name On or off
   * @returns {Promise}          Resolves to output of command
   * @example
   *
// change the lightbulb name
const light = new TPLSmartDevice('10.0.0.200')
light.name("New Name")
  .then(status => {
    console.log(status)
  })
  .catch(err => console.error(err))

   */
  name () {
    return this.info()
      .then(info => {
        if (typeof info.dev_name !== 'undefined') {
          return this.send({
            system: {
              set_dev_alias: {
                alias: name
              }
            }
          })
        } else {
          return this.send({
            'smartlife.iot.common.system': {
              set_dev_alias: {
                alias: name
              }
            }
          })
        }
      })
  }

Is there anything I should cleanup/change? If everything looks alright I can make a PR.

konsumer commented 4 years ago

Looks good to me, except I think you need to have a name param. Also, I like to simplify the logic with a ternary operator when it's a simple switch:

name(alias) {
  return this.info()
  .then(info => {
    return typeof info.dev_name !== 'undefined'
      ? this.send({ system: { set_dev_alias: alias } })
      : this.send({ 'smartlife.iot.common.system': { set_dev_alias: alias } })
  })
}
konsumer commented 3 years ago

Is this resolved in #51 ?

migte commented 3 years ago

Yes it is, I will close it now then thanks.