From bebb02cfc16fda1c3d14bafac74c8fa008259d3e Mon Sep 17 00:00:00 2001 From: abearxiong Date: Tue, 23 Dec 2025 00:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20ButtonHA=20=E7=B1=BB?= =?UTF-8?q?=E5=8F=8A=E5=85=B6=E6=9C=8D=E5=8A=A1=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9B=B8=E5=85=B3=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E4=BB=A5=E6=94=AF=E6=8C=81=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/core.ts | 2 +- src/light.ts | 7 +++++++ test/button.ts | 9 +++++++++ test/common.ts | 7 ++++--- 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 test/button.ts diff --git a/package.json b/package.json index 3ae4eb2..3f2cc1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/ha-api", - "version": "0.0.2", + "version": "0.0.3", "description": "", "main": "src/index.ts", "scripts": { diff --git a/src/core.ts b/src/core.ts index 46a0e34..012cee5 100644 --- a/src/core.ts +++ b/src/core.ts @@ -47,7 +47,7 @@ export class HACore { return response.json(); }); } - async getEntities(filter?: (entity: EntityItem) => boolean): Promise { + async getEntities(filter?: (entity: EntityItem) => boolean | undefined): Promise { const entities = await this.get({ url: '/api/states' }); if (filter) { return entities.filter(filter); diff --git a/src/light.ts b/src/light.ts index f31d840..04ba225 100644 --- a/src/light.ts +++ b/src/light.ts @@ -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); + } } \ No newline at end of file diff --git a/test/button.ts b/test/button.ts new file mode 100644 index 0000000..671dd0b --- /dev/null +++ b/test/button.ts @@ -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)); \ No newline at end of file diff --git a/test/common.ts b/test/common.ts index 72bc60e..596f435 100644 --- a/test/common.ts +++ b/test/common.ts @@ -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();