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();