Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
split and restructure for local execution
Browse files Browse the repository at this point in the history
  • Loading branch information
gytisgreitai committed Dec 4, 2019
1 parent 1064b37 commit ef996e7
Show file tree
Hide file tree
Showing 75 changed files with 4,778 additions and 285 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
braindump
dist
gactions
yarn-error.log
7 changes: 7 additions & 0 deletions core/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OpenhabItem } from "../model/openhab";

export interface Api {
getAll: () => Promise<OpenhabItem[]>
get: (item: string) => Promise<OpenhabItem>
updateState: (item: string, value: string) => Promise<any>
}
File renamed without changes.
12 changes: 6 additions & 6 deletions src/execute/execute.ts → core/execute/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import {
SmartHomeV1ExecuteStatus } from 'actions-on-google';
import { BaseCustomData } from '../model/google';
import { OpenhabItemType, OpenhabItem } from '../model/openhab';
import { api } from '../api';
import { getExecutor, getTargetItems } from '../traits';
import { verifyTFA } from '../tfa/tfa';
import deepmerge = require('deepmerge');
import { Api } from '../api/api';

export async function execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution) {
export async function execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution) {

await verifyTFA(authToken, device, req)
await verifyTFA(api, device, req)

// if we have exact item type, just push it through
// if we don't have it, we must lookup all items and pass them as candidates to the function
let targetItems: OpenhabItem[] = await getTargetItems(authToken, device, req.command);
let targetItems: OpenhabItem[] = await getTargetItems(api, device, req.command);

const customData = (device.customData as BaseCustomData);

Expand All @@ -24,7 +24,7 @@ export async function execute(authToken: string, device: SmartHomeV1QueryRequest
type = targetItems[0].type;
}
const executor = getExecutor(req.command);
const executions = executor(authToken, device, req, type, targetItems);
const executions = executor(api, device, req, type, targetItems);
let hasErrors = false;
let states = {}
for await( const { value, deviceId, states : execState } of executions) {
Expand All @@ -36,7 +36,7 @@ export async function execute(authToken: string, device: SmartHomeV1QueryRequest
targetDeviceId = targetItems[0].name
}
try {
await api.updateState(authToken, targetDeviceId, value)
await api.updateState(targetDeviceId, value)
states = deepmerge(states, execState)
} catch(e){
console.log('Failed executing', targetDeviceId, e)
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/execute/model.ts → core/execute/model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution } from "actions-on-google";

import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { Api } from "../api/api";

export type ExecuteHandler = (
authToken: string,
api: Api,
device: SmartHomeV1QueryRequestDevices,
req: SmartHomeV1ExecuteRequestExecution,
devicetype: OpenhabItemType,
Expand Down
7 changes: 7 additions & 0 deletions core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './traits';
export * from './api';
export * from './model';
export * from './sync';
export * from './execute';
export * from './query';
export * from './tfa';
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions core/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './google';
export * from './openhab';
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@openhab-google-home/core",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"actions-on-google": "^2.12.0",
"deepmerge": "^4.0.0",
"typescript": "^3.6.3"
},
"devDependencies": {
}
}
1 change: 1 addition & 0 deletions core/query/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './query';
File renamed without changes.
6 changes: 3 additions & 3 deletions src/query/query.ts → core/query/query.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { api } from "../api";
import { OpenhabItem, OpenhabItemType } from "../model/openhab";
import { groupItemsOfSameType } from "../model/selectors";
import { lookupTraits, getStateQuery } from "../traits";
import { SmartHomeV1QueryRequestDevices } from "actions-on-google";
import * as deepmerge from 'deepmerge';
import { Api } from "../api/api";

export async function query(authToken: string, device: SmartHomeV1QueryRequestDevices) {
export async function query(api: Api, device: SmartHomeV1QueryRequestDevices) {
let targetItems: OpenhabItem[] = []
const item = await api.getItem(authToken, device.id);
const item = await api.get(device.id);
if (item.type !== OpenhabItemType.Group || groupItemsOfSameType(item)) {
targetItems = [item]
} else {
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions src/sync/sync.ts → core/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export function toGoogleDevice(item: OpenhabItem, allItems: OpenhabItem[]): Smar
name: item.label,
nicknames: [item.label, ...getSynonyms(item.metadata)]
},
// otherDeviceIds: [
// {deviceId: item.name}
// ],
roomHint: config && config.roomHint,
willReportState: false,
customData: {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions core/tfa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './tfa';
6 changes: 3 additions & 3 deletions src/tfa/tfa.ts → core/tfa/tfa.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution } from "actions-on-google";
import { BaseCustomData, TFAType } from "../model/google";
import { api } from "../api";
import { Api } from "../api/api";


export class TFAError extends Error {
Expand All @@ -12,7 +12,7 @@ export class TFAError extends Error {
}
}

export async function verifyTFA(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution) {
export async function verifyTFA(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution) {
const customData = (device.customData as BaseCustomData);

if (!customData.tfa) {
Expand All @@ -27,7 +27,7 @@ export async function verifyTFA(authToken: string, device: SmartHomeV1QueryReque
throw new TFAError('ackNeeded');
}

const item = await api.getItem(authToken, device.id);
const item = await api.get(device.id);
const { config } = item.metadata.google
if (customData.tfa === TFAType.pin && req.challenge.pin !== config.tfaPin) {
throw new TFAError('challengeFailedPinNeeded');
Expand Down
3 changes: 2 additions & 1 deletion src/traits/armDisarm.ts → core/traits/armDisarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface ArmDisarmCustomData extends BaseCustomData {
disarm?: string;
Expand All @@ -24,7 +25,7 @@ export interface ArmDisarmParams {
armLevel?: string;
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const { arm, cancel, armLevel } = req.params as ArmDisarmParams;
const customData = device.customData as ArmDisarmCustomData;
let value
Expand Down
3 changes: 2 additions & 1 deletion src/traits/brightness.ts → core/traits/brightness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { Trait } from "./model";
import { BaseCustomData } from "../model/google";
import { Api } from "../api/api";

export interface BrightnessAbsoluteParams {
brightness: number;
Expand All @@ -13,7 +14,7 @@ export interface BrightnessConfig {
export interface BrightnessCustomData extends BaseCustomData {
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const { brightness } = req.params as BrightnessAbsoluteParams;
let value
switch(type) {
Expand Down
3 changes: 2 additions & 1 deletion src/traits/fanSpeed.ts → core/traits/fanSpeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem, BaseGoogleConfig } from "../model/openhab";
import { Trait } from "./model";
import { BaseCustomData } from "../model/google";
import { Api } from "../api/api";

export interface SetFanSpeedParams {
fanSpeed: string;
Expand All @@ -17,7 +18,7 @@ export interface FanSpeedConfig extends BaseGoogleConfig {
ordered?: string;
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as FanSpeedCustomData;
const { fanSpeed } = req.params as SetFanSpeedParams;
let value;
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/traits/lockUnlock.ts → core/traits/lockUnlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface LockUnlockCustomData extends BaseCustomData {
lockCommand?: string;
Expand All @@ -17,7 +18,7 @@ export interface LockUnlockConfig {
unlockCommand?: string;
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as LockUnlockCustomData;
const { lock } = req.params as LockUnlockParams;
let value: string;
Expand Down
3 changes: 2 additions & 1 deletion src/traits/mediaState.ts → core/traits/mediaState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, SmartHomeV1SyncDevices } from "actions-on-google";
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { Trait } from "./model";
import { Api } from "../api/api";

function sync(type: OpenhabItemType, item: OpenhabItem, device: Partial<SmartHomeV1SyncDevices>) {
return device
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
let value
switch(req.command) {
case 'action.devices.commands.mediaPause':
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/traits/modes.ts → core/traits/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface ModesParams {
updateModeSettings: {
Expand All @@ -21,7 +22,7 @@ export interface ModesTraitConfig {
modeCommandMap?: string;
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as ModesCustomData;
const { updateModeSettings } = req.params as ModesParams;
const modes = Object.keys(updateModeSettings)
Expand Down
3 changes: 2 additions & 1 deletion src/traits/onOff.ts → core/traits/onOff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SmartHomeV1ExecuteRequestExecution, SmartHomeV1QueryRequestDevices, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface OnOffTraitConfig {
onCommand?: string;
Expand All @@ -17,7 +18,7 @@ export interface OnOffCustomData extends BaseCustomData {
offCommand?: string;
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as OnOffCustomData;
const { on } = req.params as OnOffParams;
let value;
Expand Down
3 changes: 2 additions & 1 deletion src/traits/openClose.ts → core/traits/openClose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface OpenCloseParams {
openPercent: number;
Expand All @@ -26,7 +27,7 @@ const getStateOnOffFromType = (type: OpenhabItemType) => {
];
}

export async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
export async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as OpenCloseCustomData;
const params = req.params as OpenCloseParams;
let value;
Expand Down
3 changes: 2 additions & 1 deletion src/traits/scene.ts → core/traits/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SmartHomeV1ExecuteRequestExecution, SmartHomeV1QueryRequestDevices, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface SceneTraitConfig {
activateCommand?: string;
Expand All @@ -18,7 +19,7 @@ export interface SceneCustomData extends BaseCustomData {
deactivateCommand?: string;
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as SceneCustomData;
const { deactivate } = req.params as SceneParams;
let value;
Expand Down
5 changes: 3 additions & 2 deletions src/traits/startStop.ts → core/traits/startStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface StartStopCustomData extends BaseCustomData {
}
Expand All @@ -23,7 +24,7 @@ export interface PauseUnpauseParams {
}


async function * executeStartStop(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * executeStartStop(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as StartStopCustomData;
const { zone, start } = req.params as StartStopParams;
let value
Expand All @@ -43,7 +44,7 @@ async function * executeStartStop(authToken: string, device: SmartHomeV1QueryReq
yield { value, states: { isRunning: start, isPaused: false } };
}

async function * executePauseUnpause(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * executePauseUnpause(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
const customData = device.customData as StartStopCustomData;
const { pause } = req.params as PauseUnpauseParams;
let value
Expand Down
3 changes: 2 additions & 1 deletion src/traits/toggles.ts → core/traits/toggles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SmartHomeV1QueryRequestDevices, SmartHomeV1ExecuteRequestExecution, Sma
import { OpenhabItemType, OpenhabItem } from "../model/openhab";
import { BaseCustomData } from "../model/google";
import { Trait } from "./model";
import { Api } from "../api/api";

export interface TogglesParams {
updateToggleSettings: {
Expand All @@ -22,7 +23,7 @@ export interface TogglesTraitConfig {
toggleOffCommand?: string;
}

async function * execute(authToken: string, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {
async function * execute(api: Api, device: SmartHomeV1QueryRequestDevices, req: SmartHomeV1ExecuteRequestExecution, type: OpenhabItemType, targetItems?: OpenhabItem[]) {

const { updateToggleSettings } = req.params as TogglesParams;
const toggles = Object.keys(updateToggleSettings);
Expand Down
6 changes: 3 additions & 3 deletions src/traits/traits.ts → core/traits/traits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { modes } from "./modes";
import { GoogleMeta, OpenhabItem, OpenhabItemType } from "../model/openhab";
import { SmartHomeV1QueryRequestDevices } from "actions-on-google";
import { BaseCustomData } from "../model/google";
import { api } from "../api";
import { groupItemsOfSameType } from "../model/selectors";
import { volume } from "./volume";
import { mediaState } from "./mediaState";
import { toggles } from "./toggles";
import { armDisarm } from "./armDisarm";
import { scene } from "./scene";
import { Api } from "../api/api";

export const traits = [
brightness,
Expand Down Expand Up @@ -79,11 +79,11 @@ export function getStateQuery(traitName: string) {
return traits.find(({name}) => name === traitName).query;
}

export async function getTargetItems(authToken: string, device: SmartHomeV1QueryRequestDevices, command: string) {
export async function getTargetItems(api: Api, device: SmartHomeV1QueryRequestDevices, command: string) {
let targetItems: OpenhabItem[] = []
const customData = (device.customData as BaseCustomData);
if (!customData || !customData.itemType || customData.lookup) {
const item = await api.getItem(authToken, device.id)
const item = await api.get(device.id)
if (item.type === OpenhabItemType.Group && !groupItemsOfSameType(item)) {
const wantedTrait = getTraitByCommand(command);
targetItems = item.members.filter(i => {
Expand Down
Loading

0 comments on commit ef996e7

Please sign in to comment.