diff --git a/README.md b/README.md index 8ea040a8..34201bf5 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,6 @@ connection.suspendReconnectUntil( resolve(); }) ); -connection.suspend(); ``` When the suspend promise resolves until the connection is re-established, all messages being send will be delayed until the connection is established. If the first reconnect fails, the queued messages will be rejected. @@ -344,6 +343,7 @@ String containing the current version of Home Assistant. Examples: - `0.107.0` - `0.107.0b1` - `0.107.0.dev0` +- `2021.3.0` ##### `conn.subscribeEvents(eventCallback[, eventType])` @@ -407,8 +407,8 @@ Makes a request to the server to revoke the refresh and all related access token The library also contains a few helper method that you can use to ineract with the API. -- `getUser(connection) -> Promise` -- `callService(connection, domain, service, serviceData?) -> Promise` +- `getUser(connection) -> Promise` +- `callService(connection, domain, service, serviceData?, target?) -> Promise` (Support for `target` was added in Home Assistant Core 2021.3) The following are also available, but it's recommended that you use the subscribe methods documented above. diff --git a/lib/commands.ts b/lib/commands.ts index 417fd7e8..43baeec7 100644 --- a/lib/commands.ts +++ b/lib/commands.ts @@ -1,6 +1,12 @@ import { Connection } from "./connection.js"; import * as messages from "./messages.js"; -import { HassEntity, HassServices, HassConfig, HassUser } from "./types.js"; +import { + HassEntity, + HassServices, + HassConfig, + HassUser, + HassServiceTarget, +} from "./types.js"; export const getStates = (connection: Connection) => connection.sendMessagePromise(messages.states()); @@ -18,8 +24,9 @@ export const callService = ( connection: Connection, domain: string, service: string, - serviceData?: object + serviceData?: object, + target?: HassServiceTarget ) => connection.sendMessagePromise( - messages.callService(domain, service, serviceData) + messages.callService(domain, service, serviceData, target) ); diff --git a/lib/connection.ts b/lib/connection.ts index 074b5c72..1c596054 100644 --- a/lib/connection.ts +++ b/lib/connection.ts @@ -87,7 +87,7 @@ export class Connection { // We use this to queue messages in flight for the first reconnect // after the connection has been suspended. _queuedMessages?: Array<{ - resolve: () => unknown; + resolve: (value?: unknown) => unknown; reject?: (err: typeof ERR_CONNECTION_LOST) => unknown; }>; // @ts-ignore: incorrectly claiming it's not set in constructor. diff --git a/lib/messages.ts b/lib/messages.ts index b7091c89..57258c13 100644 --- a/lib/messages.ts +++ b/lib/messages.ts @@ -1,33 +1,33 @@ -import { Error } from "./types.js"; +import { Error, HassServiceTarget } from "./types.js"; export function auth(accessToken: string) { return { type: "auth", - access_token: accessToken + access_token: accessToken, }; } export function states() { return { - type: "get_states" + type: "get_states", }; } export function config() { return { - type: "get_config" + type: "get_config", }; } export function services() { return { - type: "get_services" + type: "get_services", }; } export function user() { return { - type: "auth/current_user" + type: "auth/current_user", }; } @@ -36,17 +36,20 @@ type ServiceCallMessage = { domain: string; service: string; service_data?: object; + target?: HassServiceTarget; }; export function callService( domain: string, service: string, - serviceData?: object + serviceData?: object, + target?: HassServiceTarget ) { const message: ServiceCallMessage = { type: "call_service", domain, - service + service, + target, }; if (serviceData) { @@ -63,7 +66,7 @@ type SubscribeEventMessage = { export function subscribeEvents(eventType?: string) { const message: SubscribeEventMessage = { - type: "subscribe_events" + type: "subscribe_events", }; if (eventType) { @@ -76,13 +79,13 @@ export function subscribeEvents(eventType?: string) { export function unsubscribeEvents(subscription: number) { return { type: "unsubscribe_events", - subscription + subscription, }; } export function ping() { return { - type: "ping" + type: "ping", }; } @@ -92,7 +95,7 @@ export function error(code: Error, message: string) { success: false, error: { code, - message - } + message, + }, }; } diff --git a/lib/types.ts b/lib/types.ts index 2b3d5bb1..ece00d07 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -86,10 +86,13 @@ export type HassEntities = { [entity_id: string]: HassEntity }; export type HassService = { description: string; + target?: { selector?: {} }; fields: { [field_name: string]: { + name?: string; description: string; example: string | boolean | number; + selector?: {}; }; }; }; @@ -107,3 +110,9 @@ export type HassUser = { is_owner: boolean; name: string; }; + +export type HassServiceTarget = { + entity_id?: string | string[]; + device_id?: string | string[]; + area_id?: string | string[]; +};