|
| 1 | +import { v4 as uuid } from "uuid"; |
| 2 | +import app from "../../govee.app.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "govee-control-device", |
| 6 | + name: "Control Device", |
| 7 | + description: "Send a command to control a Govee device, such as turning it on/off, changing its brightness, or adjusting its color. [See the documentation](https://developer.govee.com/reference/control-you-devices).", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + app, |
| 12 | + deviceId: { |
| 13 | + propDefinition: [ |
| 14 | + app, |
| 15 | + "deviceId", |
| 16 | + ], |
| 17 | + }, |
| 18 | + commandType: { |
| 19 | + reloadProps: true, |
| 20 | + propDefinition: [ |
| 21 | + app, |
| 22 | + "commandType", |
| 23 | + ({ deviceId }) => ({ |
| 24 | + deviceId, |
| 25 | + }), |
| 26 | + ], |
| 27 | + }, |
| 28 | + }, |
| 29 | + async additionalProps() { |
| 30 | + const { |
| 31 | + deviceId, |
| 32 | + commandType, |
| 33 | + } = this; |
| 34 | + |
| 35 | + const { data: devices } = await this.app.listDevices(); |
| 36 | + const device = devices.find(({ device }) => device === deviceId); |
| 37 | + const capability = device?.capabilities?.find(({ type }) => type === commandType); |
| 38 | + const { parameters } = capability ?? {}; |
| 39 | + |
| 40 | + if (parameters.dataType === "ENUM") { |
| 41 | + return { |
| 42 | + value: { |
| 43 | + type: "string", |
| 44 | + label: "Value", |
| 45 | + description: "The value of the command.", |
| 46 | + options: parameters?.options.map(({ |
| 47 | + name: label, |
| 48 | + value, |
| 49 | + }) => ({ |
| 50 | + label, |
| 51 | + value: String(value), |
| 52 | + })), |
| 53 | + }, |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + if (parameters.dataType === "INTEGER") { |
| 58 | + return { |
| 59 | + value: { |
| 60 | + type: "integer", |
| 61 | + label: "Value", |
| 62 | + description: `The value of the command. Min value: \`${parameters.range.min}\`, Max value: \`${parameters.range.max}\`.`, |
| 63 | + min: parameters.range.min, |
| 64 | + max: parameters.range.max, |
| 65 | + }, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + if (parameters.dataType === "STRUCT") { |
| 70 | + return parameters.fields.reduce((acc, { |
| 71 | + fieldName, |
| 72 | + dataType, |
| 73 | + range, |
| 74 | + required, |
| 75 | + options, |
| 76 | + }) => { |
| 77 | + if (dataType === "INTEGER") { |
| 78 | + acc[fieldName] = { |
| 79 | + type: "integer", |
| 80 | + label: fieldName, |
| 81 | + description: `The value of the ${fieldName} field. Min value: \`${range.min}\`, Max value: \`${range.max}\`.`, |
| 82 | + min: range.min, |
| 83 | + max: range.max, |
| 84 | + optional: !required, |
| 85 | + }; |
| 86 | + } else if (dataType === "ENUM") { |
| 87 | + acc[fieldName] = { |
| 88 | + type: "string", |
| 89 | + label: fieldName, |
| 90 | + description: `The value of the ${fieldName} field.`, |
| 91 | + optional: !required, |
| 92 | + options: options.reduce((acc, { |
| 93 | + name: label, |
| 94 | + value, |
| 95 | + defaultValue, |
| 96 | + options: nestedOptions, |
| 97 | + }) => { |
| 98 | + if (nestedOptions) { |
| 99 | + return acc.concat(nestedOptions.map(({ |
| 100 | + name: label, |
| 101 | + value, |
| 102 | + }) => ({ |
| 103 | + label, |
| 104 | + value: String(value), |
| 105 | + }))); |
| 106 | + } |
| 107 | + return acc.concat({ |
| 108 | + label, |
| 109 | + value: value !== undefined && String(value) || String(defaultValue), |
| 110 | + }); |
| 111 | + }, []), |
| 112 | + }; |
| 113 | + } else if (dataType === "Array") { |
| 114 | + acc[fieldName] = { |
| 115 | + type: "string[]", |
| 116 | + label: fieldName, |
| 117 | + description: `The value of the ${fieldName} field.`, |
| 118 | + optional: !required, |
| 119 | + options: options.map(({ value }) => String(value)), |
| 120 | + }; |
| 121 | + } else { |
| 122 | + acc[fieldName] = { |
| 123 | + type: "string", |
| 124 | + label: fieldName, |
| 125 | + description: `The value of the ${fieldName} field.`, |
| 126 | + optional: !required, |
| 127 | + }; |
| 128 | + } |
| 129 | + return acc; |
| 130 | + }, {}); |
| 131 | + } |
| 132 | + }, |
| 133 | + methods: { |
| 134 | + controlDevice(args = {}) { |
| 135 | + return this.app.post({ |
| 136 | + path: "/device/control", |
| 137 | + ...args, |
| 138 | + }); |
| 139 | + }, |
| 140 | + }, |
| 141 | + async run({ $ }) { |
| 142 | + const { |
| 143 | + app, |
| 144 | + controlDevice, |
| 145 | + deviceId, |
| 146 | + commandType, |
| 147 | + value, |
| 148 | + ...fields |
| 149 | + } = this; |
| 150 | + |
| 151 | + const { data: devices } = await app.listDevices(); |
| 152 | + const device = devices.find(({ device }) => device === deviceId); |
| 153 | + |
| 154 | + const response = await controlDevice({ |
| 155 | + $, |
| 156 | + data: { |
| 157 | + requestId: uuid(), |
| 158 | + payload: { |
| 159 | + sku: device.sku, |
| 160 | + device: deviceId, |
| 161 | + capability: { |
| 162 | + type: commandType, |
| 163 | + parameters: { |
| 164 | + value: value ?? fields, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }); |
| 170 | + |
| 171 | + $.export("$summary", "Successfully sent command to device."); |
| 172 | + return response; |
| 173 | + }, |
| 174 | +}; |
0 commit comments