/drivers/mi-plug/device.js

https://github.com/jghaanstra/com.xiaomi-miio · JavaScript · 104 lines · 84 code · 20 blank · 0 comment · 15 complexity · 494cc274425694444375a81ef1c31e8b MD5 · raw file

  1. const Homey = require("homey");
  2. class MiPlug extends Homey.Device {
  3. async onInit() {
  4. this.initialize = this.initialize.bind(this);
  5. this.onEventFromGateway = this.onEventFromGateway.bind(this);
  6. this.data = this.getData();
  7. this.inUse = false;
  8. this.initialize();
  9. this.log("[Xiaomi Mi Home] Device init - name: " + this.getName() + " - class: " + this.getClass() + " - data: " + JSON.stringify(this.data));
  10. }
  11. async initialize() {
  12. if (Homey.app.gatewaysList.length > 0) {
  13. this.registerStateChangeListener();
  14. this.registerCapabilities();
  15. this.registerConditions();
  16. } else {
  17. this.unregisterStateChangeListener();
  18. }
  19. }
  20. registerCapabilities() {
  21. this.registerToggle("onoff");
  22. }
  23. registerConditions() {
  24. const { conditions } = this.getDriver();
  25. this.registerCondition(conditions.inUse);
  26. }
  27. onEventFromGateway(device) {
  28. const { triggers } = this.getDriver();
  29. const data = JSON.parse(device["data"]);
  30. if (data["status"] == "on") {
  31. this.updateCapabilityValue("onoff", true);
  32. }
  33. if (data["status"] == "off") {
  34. this.updateCapabilityValue("onoff", false);
  35. }
  36. if (data["load_power"]) {
  37. this.updateCapabilityValue("measure_power", parseInt(data["load_power"]));
  38. }
  39. if (data["power_consumed"]) {
  40. this.updateCapabilityValue("meter_power", parseFloat(data["power_consumed"] / 1000));
  41. }
  42. if (data["inuse"]) {
  43. if (this.inUse != !!parseInt(data["inuse"]) && !!parseInt(data["inuse"])) {
  44. triggers.inUse.trigger(this, {}, true);
  45. }
  46. this.inUse = !!parseInt(data["inuse"]);
  47. }
  48. this.setSettings({
  49. gatewaySid: Object.values(Homey.app.mihub.getDevices()).filter(deviceObj => deviceObj.sid == device.sid)[0].gatewaySid
  50. });
  51. }
  52. updateCapabilityValue(name, value) {
  53. if (this.getCapabilityValue(name) != value) {
  54. this.setCapabilityValue(name, value)
  55. .then(() => this.log("[" + this.getName() + "] [" + this.data.sid + "] [" + name + "] [" + value + "] Capability successfully updated"))
  56. .catch(error => this.log("[" + this.getName() + "] [" + this.data.sid + "] [" + name + "] [" + value + "] Capability not updated because there are errors: " + error.message));
  57. }
  58. }
  59. registerToggle(name) {
  60. const sid = this.data.sid;
  61. this.registerCapabilityListener(name, async value => {
  62. const model = Homey.app.mihub.getDeviceModelBySid(sid);
  63. let command = '{"cmd":"write","model":"' + model + '","sid":"' + sid + '","data":{"status":"' + (value ? "on" : "off") + '", "key": "${key}"}}';
  64. return await Homey.app.mihub.sendWriteCommand(sid, command);
  65. });
  66. }
  67. registerCondition(condition) {
  68. condition.registerRunListener((args, state) => Promise.resolve(this.inUse));
  69. }
  70. registerStateChangeListener() {
  71. Homey.app.mihub.on(this.data.sid, this.onEventFromGateway);
  72. }
  73. unregisterStateChangeListener() {
  74. Homey.app.mihub.removeListener(this.data.sid, this.onEventFromGateway);
  75. }
  76. onAdded() {
  77. this.log("[Xiaomi Mi Home] " + this.getName() + " device added");
  78. }
  79. onDeleted() {
  80. this.unregisterStateChangeListener();
  81. this.log("[Xiaomi Mi Home] " + this.getName() + " device deleted!");
  82. }
  83. }
  84. module.exports = MiPlug;