添加 ButtonHA 类及其服务功能,更新相关测试用例以支持新功能
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/ha-api",
|
"name": "@kevisual/ha-api",
|
||||||
"version": "0.0.2",
|
"version": "0.0.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ export class HACore {
|
|||||||
return response.json();
|
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' });
|
const entities = await this.get({ url: '/api/states' });
|
||||||
if (filter) {
|
if (filter) {
|
||||||
return entities.filter(filter);
|
return entities.filter(filter);
|
||||||
|
|||||||
@@ -54,3 +54,10 @@ export class ScriptHA extends HACore {
|
|||||||
super(options);
|
super(options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ButtonHA extends HACore {
|
||||||
|
static serviceName = 'button';
|
||||||
|
constructor(options: HACoreOptions) {
|
||||||
|
super(options);
|
||||||
|
}
|
||||||
|
}
|
||||||
9
test/button.ts
Normal file
9
test/button.ts
Normal 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));
|
||||||
@@ -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 util from 'node:util';
|
||||||
import dotenv from 'dotenv';
|
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 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 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 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('电视'));
|
// const enti = await hacore.getEntities((item) => item.attributes?.friendly_name?.includes?.('电视'));
|
||||||
console.log(showMore(enti), enti.length);
|
// console.log(showMore(enti), enti.length);
|
||||||
|
|
||||||
// const lightEntities = await hacore.getInfoList();
|
// const lightEntities = await hacore.getInfoList();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user