Files
ha-api/test/light.ts
2025-12-23 23:08:25 +08:00

33 lines
1.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { hacore, showMore } from "./common.ts";
import Fuse from 'fuse.js';
const beginTime = Date.now();
const devices = await hacore.getInfoList();
console.log('getInfo', Date.now() - beginTime, 'ms');
// console.log(showMore(devices));
const fuse = new Fuse(devices, {
keys: ['name'], // 搜索字段
threshold: 0.4, // 匹配宽松度0~1越小越严格
includeScore: true,
minMatchCharLength: 1, // 允许单字匹配
});
// const searchKeyword = '次卧灯';
// const searchKeyword = '阳台 灯';
// const searchKeyword = '晾衣机的 灯';
const searchKeyword = ' 阳台 灯';
const result = fuse.search(searchKeyword);
console.log('fuse search', Date.now() - beginTime, 'ms');
// 输出搜索结果
console.log(`搜索关键词: "${searchKeyword}"`);
console.log(showMore(result.map(r => r.item)));
const toggleDevice = await hacore.runService({
entity_id: result[0].item.entity_id,
service: 'toggle',
});
console.log('toggleDevice', Date.now() - beginTime, 'ms');
// devices 2
const devices2 = await hacore.getInfoList();
console.log('getInfo 2', Date.now() - beginTime, 'ms');