generateLovelace.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  15. return new (P || (P = Promise))(function (resolve, reject) {
  16. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  17. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  18. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  19. step((generator = generator.apply(thisArg, _arguments || [])).next());
  20. });
  21. };
  22. var __generator = (this && this.__generator) || function (thisArg, body) {
  23. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  24. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  25. function verb(n) { return function (v) { return step([n, v]); }; }
  26. function step(op) {
  27. if (f) throw new TypeError("Generator is already executing.");
  28. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  29. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  30. if (y = 0, t) op = [op[0] & 2, t.value];
  31. switch (op[0]) {
  32. case 0: case 1: t = op; break;
  33. case 4: _.label++; return { value: op[1], done: false };
  34. case 5: _.label++; y = op[1]; op = [0]; continue;
  35. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  36. default:
  37. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  38. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  39. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  40. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  41. if (t[2]) _.ops.pop();
  42. _.trys.pop(); continue;
  43. }
  44. op = body.call(thisArg, _);
  45. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  46. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  47. }
  48. };
  49. var __values = (this && this.__values) || function(o) {
  50. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  51. if (m) return m.call(o);
  52. if (o && typeof o.length === "number") return {
  53. next: function () {
  54. if (o && i >= o.length) o = void 0;
  55. return { value: o && o[i++], done: !o };
  56. }
  57. };
  58. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  59. };
  60. var __importDefault = (this && this.__importDefault) || function (mod) {
  61. return (mod && mod.__esModule) ? mod : { "default": mod };
  62. };
  63. Object.defineProperty(exports, "__esModule", { value: true });
  64. var lodash_1 = __importDefault(require("lodash"));
  65. var HASocketClass_1 = __importDefault(require("../class/HASocketClass"));
  66. var CloudDoubleColorBulbController_1 = __importDefault(require("../controller/CloudDoubleColorBulbController"));
  67. var CloudDualR3Controller_1 = __importDefault(require("../controller/CloudDualR3Controller"));
  68. var CloudDW2WiFiController_1 = __importDefault(require("../controller/CloudDW2WiFiController"));
  69. var CloudMultiChannelSwitchController_1 = __importDefault(require("../controller/CloudMultiChannelSwitchController"));
  70. var CloudPowerDetectionSwitchController_1 = __importDefault(require("../controller/CloudPowerDetectionSwitchController"));
  71. var CloudRGBBulbController_1 = __importDefault(require("../controller/CloudRGBBulbController"));
  72. var CloudRGBLightStripController_1 = __importDefault(require("../controller/CloudRGBLightStripController"));
  73. var CloudSwitchController_1 = __importDefault(require("../controller/CloudSwitchController"));
  74. var CloudTandHModificationController_1 = __importDefault(require("../controller/CloudTandHModificationController"));
  75. var CloudUIID104Controller_1 = __importDefault(require("../controller/CloudUIID104Controller"));
  76. var CloudUIID130Controller_1 = __importDefault(require("../controller/CloudUIID130Controller"));
  77. var CloudUIID137Controller_1 = __importDefault(require("../controller/CloudUIID137Controller"));
  78. var CloudUIID173Controller_1 = __importDefault(require("../controller/CloudUIID173Controller"));
  79. var CloudUIID181Controller_1 = __importDefault(require("../controller/CloudUIID181Controller"));
  80. var CloudUIID182Controller_1 = __importDefault(require("../controller/CloudUIID182Controller"));
  81. var CloudUIID190Controller_1 = __importDefault(require("../controller/CloudUIID190Controller"));
  82. var CloudZigbeeDoubleColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeDoubleColorBulbController"));
  83. var CloudZigbeeFiveColorBulbController_1 = __importDefault(require("../controller/CloudZigbeeFiveColorBulbController"));
  84. var CloudZigbeeMultiSwitchController_1 = __importDefault(require("../controller/CloudZigbeeMultiSwitchController"));
  85. var CloudZigbeeUIID1000Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID1000Controller"));
  86. var CloudZigbeeUIID1770Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID1770Controller"));
  87. var CloudZigbeeUIID2026Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID2026Controller"));
  88. var CloudZigbeeUIID3026Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID3026Controller"));
  89. var CloudZigbeeUIID4026Controller_1 = __importDefault(require("../controller/CloudZigbeeUIID4026Controller"));
  90. var Controller_1 = __importDefault(require("../controller/Controller"));
  91. var DiyDeviceController_1 = __importDefault(require("../controller/DiyDeviceController"));
  92. var LanDualR3Controller_1 = __importDefault(require("../controller/LanDualR3Controller"));
  93. var LanMultiChannelSwitchController_1 = __importDefault(require("../controller/LanMultiChannelSwitchController"));
  94. var LanPowerDetectionSwitchController_1 = __importDefault(require("../controller/LanPowerDetectionSwitchController"));
  95. var LanSwitchController_1 = __importDefault(require("../controller/LanSwitchController"));
  96. var LanTandHModificationController_1 = __importDefault(require("../controller/LanTandHModificationController"));
  97. var generateLovelace = function () { return __awaiter(void 0, void 0, void 0, function () {
  98. var res, title, views, lovelace_1, isDeviceExist, singalSwitchCard, _loop_1, _a, _b, device;
  99. var e_1, _c;
  100. return __generator(this, function (_d) {
  101. switch (_d.label) {
  102. case 0: return [4, HASocketClass_1.default.getLovelace()];
  103. case 1:
  104. res = _d.sent();
  105. if (!(res && Array.isArray(res.views))) return [3, 3];
  106. title = res.title, views = res.views;
  107. lovelace_1 = { path: '', title: 'eWeLink Smart Home', badges: [], cards: [] };
  108. if (views.length) {
  109. lovelace_1 = views[0];
  110. }
  111. isDeviceExist = function (deviceId) {
  112. try {
  113. var tmp = JSON.stringify(lovelace_1);
  114. return tmp.includes(deviceId);
  115. }
  116. catch (error) {
  117. return false;
  118. }
  119. };
  120. singalSwitchCard = {
  121. type: 'entities',
  122. title: 'Switch',
  123. state_color: true,
  124. show_header_toggle: false,
  125. entities: [],
  126. };
  127. _loop_1 = function (device) {
  128. if (isDeviceExist(device.entityId)) {
  129. return "continue";
  130. }
  131. if (device instanceof DiyDeviceController_1.default || device instanceof CloudSwitchController_1.default || device instanceof CloudPowerDetectionSwitchController_1.default) {
  132. singalSwitchCard.entities.push(device.entityId);
  133. return "continue";
  134. }
  135. if (device instanceof LanSwitchController_1.default || device instanceof LanPowerDetectionSwitchController_1.default) {
  136. if (device.selfApikey && device.devicekey) {
  137. singalSwitchCard.entities.push(device.entityId);
  138. }
  139. return "continue";
  140. }
  141. if (device instanceof CloudMultiChannelSwitchController_1.default ||
  142. device instanceof LanMultiChannelSwitchController_1.default ||
  143. device instanceof CloudDualR3Controller_1.default ||
  144. device instanceof LanDualR3Controller_1.default ||
  145. device instanceof CloudZigbeeMultiSwitchController_1.default ||
  146. device instanceof CloudUIID130Controller_1.default ||
  147. device instanceof CloudUIID182Controller_1.default) {
  148. if (device.maxChannel === 1 && device.deviceName) {
  149. singalSwitchCard.entities.push("".concat(device.entityId, "_1"));
  150. return "continue";
  151. }
  152. if (!device.maxChannel || !device.deviceName) {
  153. return "continue";
  154. }
  155. var entities = Array.from({ length: device.maxChannel }, function (v, k) {
  156. return "".concat(device.entityId, "_").concat(k + 1);
  157. });
  158. var tmpCard = {
  159. type: 'entities',
  160. entities: entities,
  161. title: device.deviceName,
  162. state_color: true,
  163. show_header_toggle: true,
  164. };
  165. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  166. if (~index) {
  167. lovelace_1.cards[index] = tmpCard;
  168. }
  169. else {
  170. lovelace_1.cards.push(tmpCard);
  171. }
  172. }
  173. if (device instanceof CloudTandHModificationController_1.default || device instanceof LanTandHModificationController_1.default) {
  174. if (!device.deviceName) {
  175. return "continue";
  176. }
  177. var tmpCard = {
  178. type: 'entities',
  179. entities: ["switch.".concat(device.deviceId), "sensor.".concat(device.deviceId, "_t"), "sensor.".concat(device.deviceId, "_h")],
  180. title: device.deviceName,
  181. state_color: true,
  182. show_header_toggle: false,
  183. };
  184. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  185. if (~index) {
  186. lovelace_1.cards[index] = tmpCard;
  187. }
  188. else {
  189. lovelace_1.cards.push(tmpCard);
  190. }
  191. }
  192. if (device instanceof CloudZigbeeUIID1000Controller_1.default) {
  193. if (!device.deviceName) {
  194. return "continue";
  195. }
  196. var tmpCard = {
  197. type: 'entities',
  198. entities: [
  199. "sensor.".concat(device.deviceId),
  200. "sensor.".concat(device.deviceId, "_battery")
  201. ],
  202. title: device.deviceName,
  203. state_color: true,
  204. show_header_toggle: false
  205. };
  206. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  207. if (~index) {
  208. lovelace_1.cards[index] = tmpCard;
  209. }
  210. else {
  211. lovelace_1.cards.push(tmpCard);
  212. }
  213. }
  214. if (device instanceof CloudZigbeeUIID1770Controller_1.default) {
  215. if (!device.deviceName) {
  216. return "continue";
  217. }
  218. var tmpCard = {
  219. type: 'entities',
  220. entities: [
  221. "sensor.".concat(device.deviceId, "_humidity"),
  222. "sensor.".concat(device.deviceId, "_temperature"),
  223. "sensor.".concat(device.deviceId, "_battery")
  224. ],
  225. title: device.deviceName,
  226. state_color: true,
  227. show_header_toggle: false
  228. };
  229. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  230. if (~index) {
  231. lovelace_1.cards[index] = tmpCard;
  232. }
  233. else {
  234. lovelace_1.cards.push(tmpCard);
  235. }
  236. }
  237. if (device instanceof CloudZigbeeUIID2026Controller_1.default) {
  238. if (!device.deviceName) {
  239. return "continue";
  240. }
  241. var tmpCard = {
  242. type: 'entities',
  243. entities: [
  244. "binary_sensor.".concat(device.deviceId),
  245. "sensor.".concat(device.deviceId, "_battery")
  246. ],
  247. title: device.deviceName,
  248. state_color: true,
  249. show_header_toggle: false
  250. };
  251. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  252. if (~index) {
  253. lovelace_1.cards[index] = tmpCard;
  254. }
  255. else {
  256. lovelace_1.cards.push(tmpCard);
  257. }
  258. }
  259. if (device instanceof CloudZigbeeUIID3026Controller_1.default) {
  260. if (!device.deviceName) {
  261. return "continue";
  262. }
  263. var tmpCard = {
  264. type: 'entities',
  265. entities: [
  266. "binary_sensor.".concat(device.deviceId),
  267. "sensor.".concat(device.deviceId, "_battery")
  268. ],
  269. title: device.deviceName,
  270. state_color: true,
  271. show_header_toggle: false
  272. };
  273. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  274. if (~index) {
  275. lovelace_1.cards[index] = tmpCard;
  276. }
  277. else {
  278. lovelace_1.cards.push(tmpCard);
  279. }
  280. }
  281. if (device instanceof CloudZigbeeUIID4026Controller_1.default) {
  282. if (!device.deviceName) {
  283. return "continue";
  284. }
  285. var tmpCard = {
  286. type: 'entities',
  287. entities: [
  288. "binary_sensor.".concat(device.deviceId),
  289. "sensor.".concat(device.deviceId, "_battery")
  290. ],
  291. title: device.deviceName,
  292. state_color: true,
  293. show_header_toggle: false
  294. };
  295. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  296. if (~index) {
  297. lovelace_1.cards[index] = tmpCard;
  298. }
  299. else {
  300. lovelace_1.cards.push(tmpCard);
  301. }
  302. }
  303. if (device instanceof CloudRGBBulbController_1.default) {
  304. if (!device.deviceName) {
  305. return "continue";
  306. }
  307. var tmpCard = {
  308. type: 'light',
  309. entity: "light.".concat(device.deviceId),
  310. };
  311. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  312. if (~index) {
  313. lovelace_1.cards[index] = tmpCard;
  314. }
  315. else {
  316. lovelace_1.cards.push(tmpCard);
  317. }
  318. }
  319. if (device instanceof CloudRGBLightStripController_1.default) {
  320. if (!device.deviceName) {
  321. return "continue";
  322. }
  323. var tmpCard = {
  324. type: 'light',
  325. entity: "light.".concat(device.deviceId),
  326. };
  327. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  328. if (~index) {
  329. lovelace_1.cards[index] = tmpCard;
  330. }
  331. else {
  332. lovelace_1.cards.push(tmpCard);
  333. }
  334. }
  335. if (device instanceof CloudDW2WiFiController_1.default) {
  336. if (!device.deviceName) {
  337. return "continue";
  338. }
  339. var tmpCard = {
  340. type: 'entities',
  341. entities: [
  342. "binary_sensor.".concat(device.deviceId, "_lock"),
  343. "binary_sensor.".concat(device.deviceId, "_battery")
  344. ],
  345. title: device.deviceName,
  346. state_color: true,
  347. show_header_toggle: false
  348. };
  349. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  350. if (~index) {
  351. lovelace_1.cards[index] = tmpCard;
  352. }
  353. else {
  354. lovelace_1.cards.push(tmpCard);
  355. }
  356. }
  357. if (device instanceof CloudDoubleColorBulbController_1.default) {
  358. if (!device.deviceName) {
  359. return "continue";
  360. }
  361. var tmpCard = {
  362. type: 'light',
  363. entity: "light.".concat(device.deviceId),
  364. };
  365. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  366. if (~index) {
  367. lovelace_1.cards[index] = tmpCard;
  368. }
  369. else {
  370. lovelace_1.cards.push(tmpCard);
  371. }
  372. }
  373. if (device instanceof CloudUIID104Controller_1.default) {
  374. if (!device.deviceName) {
  375. return "continue";
  376. }
  377. var tmpCard = {
  378. type: 'light',
  379. entity: "light.".concat(device.deviceId),
  380. };
  381. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  382. if (~index) {
  383. lovelace_1.cards[index] = tmpCard;
  384. }
  385. else {
  386. lovelace_1.cards.push(tmpCard);
  387. }
  388. }
  389. if (device instanceof CloudUIID181Controller_1.default) {
  390. if (!device.deviceName) {
  391. return "continue";
  392. }
  393. var tmpCard = {
  394. type: 'entities',
  395. entities: [
  396. "switch.".concat(device.deviceId)
  397. ],
  398. title: device.deviceName,
  399. state_color: true,
  400. show_header_toggle: false
  401. };
  402. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  403. if (~index) {
  404. lovelace_1.cards[index] = tmpCard;
  405. }
  406. else {
  407. lovelace_1.cards.push(tmpCard);
  408. }
  409. }
  410. if (device instanceof CloudUIID190Controller_1.default) {
  411. if (!device.deviceName) {
  412. return "continue";
  413. }
  414. var tmpCard = {
  415. type: 'entities',
  416. entities: [
  417. "switch.".concat(device.deviceId, "_1")
  418. ],
  419. title: device.deviceName,
  420. state_color: true,
  421. show_header_toggle: false
  422. };
  423. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  424. if (~index) {
  425. lovelace_1.cards[index] = tmpCard;
  426. }
  427. else {
  428. lovelace_1.cards.push(tmpCard);
  429. }
  430. }
  431. if (device instanceof CloudZigbeeDoubleColorBulbController_1.default) {
  432. if (!device.deviceName) {
  433. return "continue";
  434. }
  435. var tmpCard = {
  436. type: 'light',
  437. entity: "light.".concat(device.deviceId),
  438. };
  439. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  440. if (~index) {
  441. lovelace_1.cards[index] = tmpCard;
  442. }
  443. else {
  444. lovelace_1.cards.push(tmpCard);
  445. }
  446. }
  447. if (device instanceof CloudZigbeeFiveColorBulbController_1.default) {
  448. if (!device.deviceName) {
  449. return "continue";
  450. }
  451. var tmpCard = {
  452. type: 'light',
  453. entity: "light.".concat(device.deviceId),
  454. };
  455. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  456. if (~index) {
  457. lovelace_1.cards[index] = tmpCard;
  458. }
  459. else {
  460. lovelace_1.cards.push(tmpCard);
  461. }
  462. }
  463. if (device instanceof CloudUIID137Controller_1.default || device instanceof CloudUIID173Controller_1.default) {
  464. if (!device.deviceName) {
  465. return "continue";
  466. }
  467. var tmpCard = {
  468. type: 'light',
  469. entity: "light.".concat(device.deviceId),
  470. };
  471. var index = lodash_1.default.findIndex(lovelace_1.cards, { title: device.deviceName });
  472. if (~index) {
  473. lovelace_1.cards[index] = tmpCard;
  474. }
  475. else {
  476. lovelace_1.cards.push(tmpCard);
  477. }
  478. }
  479. };
  480. try {
  481. for (_a = __values(Controller_1.default.deviceMap.values()), _b = _a.next(); !_b.done; _b = _a.next()) {
  482. device = _b.value;
  483. _loop_1(device);
  484. }
  485. }
  486. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  487. finally {
  488. try {
  489. if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
  490. }
  491. finally { if (e_1) throw e_1.error; }
  492. }
  493. if (singalSwitchCard.entities.length) {
  494. lovelace_1.cards.unshift(singalSwitchCard);
  495. }
  496. if (views) {
  497. views[0] = lovelace_1;
  498. }
  499. return [4, HASocketClass_1.default.query({
  500. type: 'lovelace/config/save',
  501. config: __assign(__assign({}, res), { title: title, views: views }),
  502. })];
  503. case 2: return [2, _d.sent()];
  504. case 3: return [2];
  505. }
  506. });
  507. }); };
  508. exports.default = generateLovelace;