添加灯光管理、自动化和脚本功能,更新依赖项,完善文档

This commit is contained in:
2025-12-22 23:30:13 +08:00
parent 18e4b39ade
commit 47a5c600ea
15 changed files with 392 additions and 118 deletions

5
test/auto.ts Normal file
View File

@@ -0,0 +1,5 @@
import { auto, showMore } from './common.ts'
const autos = await auto.getAutos();
console.log(showMore(autos));

View File

@@ -1,113 +1,127 @@
import { HACore } from "../src/core";
import { LightHA, AutoHA, EventHA, ScriptHA } from "../src/index.ts";
import util from 'node:util';
import dotenv from 'dotenv';
dotenv.config();
export const showMore = (obj: any) => {
return util.inspect(obj, { depth: null, colors: true });
}
export const hacore = new LightHA({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
const hacore = new HACore({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
const enti = await hacore.get({ url: '/api/states' });
// console.log(util.inspect(enti, { depth: null }));
export const auto = new AutoHA({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
export const event = new EventHA({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
export const script = new ScriptHA({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
// const enti = await hacore.getLights();
// console.log(showMore(enti), enti.length);
// const lightEntities = enti.filter((entity: any) => {
// const hasLight = entity.entity_id.startsWith('light.');
// const name = entity.attributes?.friendly_name || '';
// return hasLight && name.includes('次卧');
// // return hasLight && name.includes('次卧');
// return hasLight;
// });
// console.log(util.inspect(lightEntities, { depth: null }));
const lights = [
{
entity_id: 'light.lemesh_wy0c14_f18d_light',
state: 'off',
attributes: {
min_color_temp_kelvin: 2700,
max_color_temp_kelvin: 6500,
min_mireds: 153,
max_mireds: 370,
effect_list: [
'WY', 'Day',
'Night', 'Warmth',
'Tv', 'Reading',
'Computer', 'Hospitality',
'Entertainment', 'Wakeup',
'Dusk', 'Sleep'
],
supported_color_modes: ['color_temp'],
effect: null,
color_mode: null,
brightness: null,
color_temp_kelvin: null,
color_temp: null,
hs_color: null,
rgb_color: null,
xy_color: null,
'light.mode': 0,
'light.on': false,
'light.color_temperature': 6500,
'light.brightness': 1,
friendly_name: '次卧灯 灯光',
supported_features: 4
},
last_changed: '2025-12-02T16:35:00.229216+00:00',
last_reported: '2025-12-02T16:36:00.314622+00:00',
last_updated: '2025-12-02T16:35:00.229216+00:00',
context: {
id: '01KBFYNN05ZYWZHTGC1W7N66Z5',
parent_id: null,
user_id: null
}
},
{
entity_id: 'light.lemesh_cn_1099991426_wy0c14_s_2_light',
state: 'off',
attributes: {
min_color_temp_kelvin: 2700,
max_color_temp_kelvin: 6500,
min_mireds: 153,
max_mireds: 370,
effect_list: [
'色温模式',
'日光',
'月光(夜间)模式',
'温馨',
'电视模式(影院模式)',
'阅读模式',
'电脑模式',
'会客模式',
'娱乐模式',
'清晨唤醒',
'黄昏明亮',
'夜晚助眠'
],
supported_color_modes: ['color_temp'],
effect: null,
color_mode: null,
brightness: null,
color_temp_kelvin: null,
color_temp: null,
hs_color: null,
rgb_color: null,
xy_color: null,
friendly_name: '次卧灯 灯光',
supported_features: 4
},
last_changed: '2025-12-04T13:28:05.284635+00:00',
last_reported: '2025-12-04T13:28:09.331178+00:00',
last_updated: '2025-12-04T13:28:05.284635+00:00',
context: {
id: '01KBMRRTX4ZE10MGHTVVNC1K7Y',
parent_id: null,
user_id: null
}
}
]
// console.log(showMore(lightEntities));
const res = await hacore.post({
url: '/api/services/light/turn_off',
body: {
entity_id: 'light.lemesh_cn_1099991426_wy0c14_s_2_light' // 或第二个次卧灯的ID
}
});
console.log(util.inspect(res, { depth: null }));
// const infoList = await hacore.getInfoList();
// console.log(showMore(infoList));
// const lights = [
// {
// entity_id: 'light.lemesh_wy0c14_f18d_light',
// state: 'off',
// attributes: {
// min_color_temp_kelvin: 2700,
// max_color_temp_kelvin: 6500,
// min_mireds: 153,
// max_mireds: 370,
// effect_list: [
// 'WY', 'Day',
// 'Night', 'Warmth',
// 'Tv', 'Reading',
// 'Computer', 'Hospitality',
// 'Entertainment', 'Wakeup',
// 'Dusk', 'Sleep'
// ],
// supported_color_modes: ['color_temp'],
// effect: null,
// color_mode: null,
// brightness: null,
// color_temp_kelvin: null,
// color_temp: null,
// hs_color: null,
// rgb_color: null,
// xy_color: null,
// 'light.mode': 0,
// 'light.on': false,
// 'light.color_temperature': 6500,
// 'light.brightness': 1,
// friendly_name: '次卧灯 灯光',
// supported_features: 4
// },
// last_changed: '2025-12-02T16:35:00.229216+00:00',
// last_reported: '2025-12-02T16:36:00.314622+00:00',
// last_updated: '2025-12-02T16:35:00.229216+00:00',
// context: {
// id: '01KBFYNN05ZYWZHTGC1W7N66Z5',
// parent_id: null,
// user_id: null
// }
// },
// {
// entity_id: 'light.lemesh_cn_1099991426_wy0c14_s_2_light',
// state: 'off',
// attributes: {
// min_color_temp_kelvin: 2700,
// max_color_temp_kelvin: 6500,
// min_mireds: 153,
// max_mireds: 370,
// effect_list: [
// '色温模式',
// '日光',
// '月光(夜间)模式',
// '温馨',
// '电视模式(影院模式)',
// '阅读模式',
// '电脑模式',
// '会客模式',
// '娱乐模式',
// '清晨唤醒',
// '黄昏明亮',
// '夜晚助眠'
// ],
// supported_color_modes: ['color_temp'],
// effect: null,
// color_mode: null,
// brightness: null,
// color_temp_kelvin: null,
// color_temp: null,
// hs_color: null,
// rgb_color: null,
// xy_color: null,
// friendly_name: '次卧灯 灯光',
// supported_features: 4
// },
// last_changed: '2025-12-04T13:28:05.284635+00:00',
// last_reported: '2025-12-04T13:28:09.331178+00:00',
// last_updated: '2025-12-04T13:28:05.284635+00:00',
// context: {
// id: '01KBMRRTX4ZE10MGHTVVNC1K7Y',
// parent_id: null,
// user_id: null
// }
// }
// ]
// const res = await hacore.toggleLight({
// // name: '次卧灯',
// // service: 'turn_off'
// name: '阳台灯'
// });
// console.log(util.inspect(res, { depth: null }));
// const entityType = await hacore.getEntityTypes();
// console.log(showMore(entityType));

19
test/light.ts Normal file
View File

@@ -0,0 +1,19 @@
import { hacore, showMore } from "./common.ts";
import Fuse from 'fuse.js';
const devices = await hacore.getInfoList();
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(`搜索关键词: "${searchKeyword}"`);
console.log(showMore(result.map(r => r.item)));

16
test/script.ts Normal file
View File

@@ -0,0 +1,16 @@
import { script, showMore } from './common.ts';
const scripts = await script.getScripts();
const values = scripts.map(e => {
return {
entity_id: e.entity_id,
state: e.state,
attributes: e.attributes,
last_changed: e.last_changed,
last_updated: e.last_updated,
};
});
console.log(showMore(values));
console.log(`实体数量: ${scripts.length}`);