添加 ButtonHA 类及其服务功能,更新相关测试用例以支持新功能

This commit is contained in:
2025-12-23 00:35:39 +08:00
parent 3e46287a89
commit bebb02cfc1
5 changed files with 22 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/ha-api",
"version": "0.0.2",
"version": "0.0.3",
"description": "",
"main": "src/index.ts",
"scripts": {

View File

@@ -47,7 +47,7 @@ export class HACore {
return response.json();
});
}
async getEntities(filter?: (entity: EntityItem) => boolean): Promise<EntityItem[]> {
async getEntities(filter?: (entity: EntityItem) => boolean | undefined): Promise<EntityItem[]> {
const entities = await this.get({ url: '/api/states' });
if (filter) {
return entities.filter(filter);

View File

@@ -53,4 +53,11 @@ export class ScriptHA extends HACore {
constructor(options: HACoreOptions) {
super(options);
}
}
export class ButtonHA extends HACore {
static serviceName = 'button';
constructor(options: HACoreOptions) {
super(options);
}
}

9
test/button.ts Normal file
View File

@@ -0,0 +1,9 @@
import { button, showMore } from "./common.ts";
const buttons = await button.getServiceEntities();
// console.log(showMore(buttons));
const tv = buttons.filter(b => b.attributes?.friendly_name?.includes('电视控制'));
console.log(showMore(tv));

View File

@@ -1,4 +1,4 @@
import { LightHA, AutoHA, EventHA, ScriptHA } from "../src/index.ts";
import { LightHA, AutoHA, EventHA, ScriptHA, ButtonHA } from "../src/index.ts";
import util from 'node:util';
import dotenv from 'dotenv';
@@ -12,9 +12,10 @@ export const hacore = new LightHA({ token: process.env.HAAS_TOKEN || '', homeass
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 });
export const button = new ButtonHA({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
const enti = await hacore.getEntities((item) => item.attributes?.friendly_name?.includes('电视'));
console.log(showMore(enti), enti.length);
// const enti = await hacore.getEntities((item) => item.attributes?.friendly_name?.includes?.('电视'));
// console.log(showMore(enti), enti.length);
// const lightEntities = await hacore.getInfoList();