Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework device matching | Store members in customData | Add AC Unit & Extended Fan #405

Merged
merged 16 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 79 additions & 13 deletions docs/USAGE.md

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions functions/apihandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ApiHandler {
*/
constructor(config = { host: '', path: '/rest/items/', port: 80 }) {
if (!config.path.startsWith('/')) {
config.path = '/' + config.path;
config.path = `/${config.path}`;
}
if (!config.path.endsWith('/')) {
config.path += '/';
Expand All @@ -51,13 +51,13 @@ class ApiHandler {
getOptions(method = 'GET', itemName = '', length = 0) {
const queryString =
method === 'GET'
? '?metadata=ga,synonyms' + (itemName ? '' : '&fields=groupNames,groupType,name,label,metadata,type')
? `?metadata=ga,synonyms${itemName ? '' : '&fields=groupNames,groupType,name,label,metadata,type,state'}`
: '';
const options = {
hostname: this._config.host,
port: this._config.port,
path: this._config.path + (itemName ? itemName : '') + queryString,
method: method,
path: this._config.path + (itemName || '') + queryString,
headers: {
Accept: 'application/json'
}
Expand All @@ -66,7 +66,7 @@ class ApiHandler {
if (this._config.userpass) {
options.auth = this._config.userpass;
} else if (this._authToken) {
options.headers['Authorization'] = 'Bearer ' + this._authToken;
options.headers.Authorization = `Bearer ${this._authToken}`;
}

if (method === 'POST') {
Expand All @@ -85,8 +85,8 @@ class ApiHandler {
return new Promise((resolve, reject) => {
const protocol = options.port === 443 ? https : http;
const req = protocol.request(options, (response) => {
if (200 !== response.statusCode) {
reject({ statusCode: response.statusCode, message: 'getItem - failed for path: ' + options.path });
if (response.statusCode !== 200) {
reject({ statusCode: response.statusCode, message: `getItem - failed for path: ${options.path}` });
return;
}

Expand All @@ -103,7 +103,7 @@ class ApiHandler {
} catch (e) {
reject({
statusCode: 415,
message: 'getItem - JSON parse failed for path: ' + options.path + ' - ' + e.toString()
message: `getItem - JSON parse failed for path: ${options.path} - ${e.toString()}`
});
}
});
Expand All @@ -130,10 +130,10 @@ class ApiHandler {
const protocol = options.port === 443 ? https : http;
const req = protocol.request(options, (response) => {
if (!response.statusCode || ![200, 201].includes(response.statusCode)) {
reject({ statusCode: response.statusCode, message: 'sendCommand - failed for path: ' + options.path });
reject({ statusCode: response.statusCode, message: `sendCommand - failed for path: ${options.path}` });
return;
}
resolve(null);
resolve(true);
});
req.on('error', (error) => {
console.error(`openhabGoogleAssistant - sendCommand: ERROR ${JSON.stringify(error)}`);
Expand Down
6 changes: 3 additions & 3 deletions functions/commands/appselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class AppSelect extends DefaultCommand {
return true;
}

static getItemName(item) {
const members = TV.getMembers(item);
static getItemName(device) {
const members = this.getMembers(device);
if ('tvApplication' in members) {
return members.tvApplication.name;
return members.tvApplication;
}
throw { statusCode: 400 };
}
Expand Down
12 changes: 6 additions & 6 deletions functions/commands/armdisarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ class ArmDisarm extends DefaultCommand {
return arm ? 'ON' : 'OFF';
}

static getItemName(item, device, params) {
static getItemName(device, params) {
if (this.getDeviceType(device) === 'SecuritySystem') {
const members = SecuritySystem.getMembers(item);
const members = this.getMembers(device);
if (params.armLevel) {
if (SecuritySystem.armLevelMemberName in members) {
return members[SecuritySystem.armLevelMemberName].name;
return members[SecuritySystem.armLevelMemberName];
}
throw { statusCode: 400 };
}
if (SecuritySystem.armedMemberName in members) {
return members[SecuritySystem.armedMemberName].name;
return members[SecuritySystem.armedMemberName];
}
throw { statusCode: 400 };
}
return item.name;
return device.id;
}

static requiresItem() {
Expand Down Expand Up @@ -84,7 +84,7 @@ class ArmDisarm extends DefaultCommand {
return {
ids: [device.id],
status: 'EXCEPTIONS',
states: Object.assign({ online: true, currentStatusReport: report }, SecuritySystem.getState(item))
states: { online: true, currentStatusReport: report, ...SecuritySystem.getState(item) }
};
}
throw { errorCode: 'armFailure' };
Expand Down
13 changes: 4 additions & 9 deletions functions/commands/brightnessabsolute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const DefaultCommand = require('./default.js');
const SpecialColorLight = require('../devices/specialcolorlight.js');

class BrightnessAbsolute extends DefaultCommand {
static get type() {
Expand All @@ -10,19 +9,15 @@ class BrightnessAbsolute extends DefaultCommand {
return 'brightness' in params && typeof params.brightness === 'number';
}

static requiresItem(device) {
return this.getDeviceType(device) === 'SpecialColorLight';
}

static getItemName(item, device) {
static getItemName(device) {
if (this.getDeviceType(device) === 'SpecialColorLight') {
const members = SpecialColorLight.getMembers(item);
const members = this.getMembers(device);
if ('lightBrightness' in members) {
return members.lightBrightness.name;
return members.lightBrightness;
}
throw { statusCode: 400 };
}
return item.name;
return device.id;
}

static convertParamsToValue(params) {
Expand Down
10 changes: 3 additions & 7 deletions functions/commands/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ class Charge extends DefaultCommand {
return 'charge' in params && typeof params.charge === 'boolean';
}

static requiresItem() {
return true;
}

static getItemName(item) {
const members = Charger.getMembers(item);
static getItemName(device) {
const members = this.getMembers(device);
if ('chargerCharging' in members) {
return members.chargerCharging.name;
return members.chargerCharging;
}
throw { statusCode: 400 };
}
Expand Down
13 changes: 4 additions & 9 deletions functions/commands/colorabsolute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const DefaultCommand = require('./default.js');
const SpecialColorLight = require('../devices/specialcolorlight.js');

class ColorAbsolute extends DefaultCommand {
static get type() {
Expand All @@ -15,19 +14,15 @@ class ColorAbsolute extends DefaultCommand {
);
}

static requiresItem(device) {
return this.getDeviceType(device) === 'SpecialColorLight';
}

static getItemName(item, device) {
static getItemName(device) {
if (this.getDeviceType(device) === 'SpecialColorLight') {
const members = SpecialColorLight.getMembers(item);
const members = this.getMembers(device);
if ('lightColor' in members) {
return members.lightColor.name;
return members.lightColor;
}
throw { statusCode: 400 };
}
return item.name;
return device.id;
}

static convertParamsToValue(params, _, device) {
Expand Down
20 changes: 10 additions & 10 deletions functions/commands/colorabsolutetemperature.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const DefaultCommand = require('./default.js');
const SpecialColorLight = require('../devices/specialcolorlight.js');
const { convertMired, convertRgbToHsv, convertKelvinToRgb } = require('../utilities.js');

class ColorAbsoluteTemperature extends DefaultCommand {
Expand All @@ -16,34 +15,35 @@ class ColorAbsoluteTemperature extends DefaultCommand {
);
}

static requiresItem() {
return true;
static requiresItem(device) {
return this.getDeviceType(device) !== 'SpecialColorLight';
}

static getItemName(item, device) {
static getItemName(device) {
if (this.getDeviceType(device) === 'SpecialColorLight') {
const members = SpecialColorLight.getMembers(item);
const members = this.getMembers(device);
if ('lightColorTemperature' in members) {
return members.lightColorTemperature.name;
return members.lightColorTemperature;
}
throw { statusCode: 400 };
}
return item.name;
return device.id;
}

static convertParamsToValue(params, item, device) {
if (this.getDeviceType(device) === 'SpecialColorLight') {
try {
const colorUnit = SpecialColorLight.getColorUnit(item);
const customData = device.customData || {};
const colorUnit = customData.colorUnit;
if (colorUnit === 'kelvin') {
return params.color.temperature.toString();
}
if (colorUnit === 'mired') {
return convertMired(params.color.temperature).toString();
}
const { temperatureMinK, temperatureMaxK } = SpecialColorLight.getAttributes(item).colorTemperatureRange;
const { temperatureMinK, temperatureMaxK } = customData.colorTemperatureRange;
let percent = ((params.color.temperature - temperatureMinK) / (temperatureMaxK - temperatureMinK)) * 100;
if (SpecialColorLight.getColorTemperatureInverted(item)) {
if (customData.colorTemperatureInverted) {
percent = 100 - percent;
}
return percent.toString();
Expand Down
17 changes: 11 additions & 6 deletions functions/commands/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ class DefaultCommand {
}

/**
* @param {object} item
* @param {object} device
* @param {object} params
*/
static getItemName(item, device, params) {
return item.name;
static getItemName(device, params) {
return device.id;
}

/**
Expand All @@ -101,6 +100,13 @@ class DefaultCommand {
return (device.customData && device.customData.itemType) || '';
}

/**
* @param {object} device
*/
static getMembers(device) {
return (device.customData && device.customData.members) || {};
}

/**
* @param {object} device
*/
Expand Down Expand Up @@ -179,7 +185,7 @@ class DefaultCommand {
console.log(`openhabGoogleAssistant - ${this.type}: Waiting ${secondsToWait} second(s) for state to update`);
setTimeout(() => {
console.log(`openhabGoogleAssistant - ${this.type}: Finished Waiting`);
resolve(null);
resolve(true);
}, secondsToWait * 1000);
});
}
Expand Down Expand Up @@ -236,12 +242,11 @@ class DefaultCommand {

return getItemPromise
.then((item) => {
const targetItem = this.getItemName(item, device, params);
const targetItem = this.getItemName(device, params);
const targetValue = this.convertParamsToValue(params, item, device);
if (shouldCheckState) {
let currentState = this.getNormalizedState(item);
if (targetItem !== device.id && item.members && item.members.length) {
// @ts-ignore
const member = item.members.find((m) => m.name === targetItem);
currentState = member ? this.getNormalizedState(member) : currentState;
}
Expand Down
11 changes: 3 additions & 8 deletions functions/commands/medianext.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const DefaultCommand = require('./default.js');
const TV = require('../devices/tv.js');

class MediaNext extends DefaultCommand {
static get type() {
return 'action.devices.commands.mediaNext';
}

static requiresItem() {
return true;
}

static getItemName(item) {
const members = TV.getMembers(item);
static getItemName(device) {
const members = this.getMembers(device);
if ('tvTransport' in members) {
return members.tvTransport.name;
return members.tvTransport;
}
throw { statusCode: 400 };
}
Expand Down
11 changes: 3 additions & 8 deletions functions/commands/mediapause.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const DefaultCommand = require('./default.js');
const TV = require('../devices/tv.js');

class MediaPause extends DefaultCommand {
static get type() {
return 'action.devices.commands.mediaPause';
}

static requiresItem() {
return true;
}

static getItemName(item) {
const members = TV.getMembers(item);
static getItemName(device) {
const members = this.getMembers(device);
if ('tvTransport' in members) {
return members.tvTransport.name;
return members.tvTransport;
}
throw { statusCode: 400 };
}
Expand Down
11 changes: 3 additions & 8 deletions functions/commands/mediaprevious.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const DefaultCommand = require('./default.js');
const TV = require('../devices/tv.js');

class MediaPrevious extends DefaultCommand {
static get type() {
return 'action.devices.commands.mediaPrevious';
}

static requiresItem() {
return true;
}

static getItemName(item) {
const members = TV.getMembers(item);
static getItemName(device) {
const members = this.getMembers(device);
if ('tvTransport' in members) {
return members.tvTransport.name;
return members.tvTransport;
}
throw { statusCode: 400 };
}
Expand Down
11 changes: 3 additions & 8 deletions functions/commands/mediaresume.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const DefaultCommand = require('./default.js');
const TV = require('../devices/tv.js');

class MediaResume extends DefaultCommand {
static get type() {
return 'action.devices.commands.mediaResume';
}

static requiresItem() {
return true;
}

static getItemName(item) {
const members = TV.getMembers(item);
static getItemName(device) {
const members = this.getMembers(device);
if ('tvTransport' in members) {
return members.tvTransport.name;
return members.tvTransport;
}
throw { statusCode: 400 };
}
Expand Down
Loading