Skip to content

Commit

Permalink
Add target to service call (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Feb 10, 2021
1 parent 1836b3a commit 49b1141
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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])`

Expand Down Expand Up @@ -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<HassEntity[]>`
- `callService(connection, domain, service, serviceData?) -> Promise`
- `getUser(connection) -> Promise<HassUser>`
- `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.

Expand Down
13 changes: 10 additions & 3 deletions lib/commands.ts
Original file line number Diff line number Diff line change
@@ -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<HassEntity[]>(messages.states());
Expand All @@ -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)
);
2 changes: 1 addition & 1 deletion lib/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 16 additions & 13 deletions lib/messages.ts
Original file line number Diff line number Diff line change
@@ -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",
};
}

Expand All @@ -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) {
Expand All @@ -63,7 +66,7 @@ type SubscribeEventMessage = {

export function subscribeEvents(eventType?: string) {
const message: SubscribeEventMessage = {
type: "subscribe_events"
type: "subscribe_events",
};

if (eventType) {
Expand All @@ -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",
};
}

Expand All @@ -92,7 +95,7 @@ export function error(code: Error, message: string) {
success: false,
error: {
code,
message
}
message,
},
};
}
9 changes: 9 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: {};
};
};
};
Expand All @@ -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[];
};

0 comments on commit 49b1141

Please sign in to comment.