@o-lukas/homebridge-smartthings-tv
Version:
This is a plugin for Homebridge. It offers some basic functions to control Samsung TVs using the SmartThings API.
132 lines • 5.68 kB
JavaScript
import { CustomCapabilityStatus } from '@smartthings/core-sdk';
import axios from 'axios';
/**
* Class implements a base class for SmartThings accessories.
*/
export class SmartThingsAccessory {
device;
component;
client;
platform;
accessory;
log;
constructor(device, component, client, platform, accessory, log) {
this.device = device;
this.component = component;
this.client = client;
this.platform = platform;
this.accessory = accessory;
this.log = log;
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, device.ocf?.firmwareVersion ?? 'Unknown')
.setCharacteristic(this.platform.Characteristic.Manufacturer, device.manufacturerName)
.setCharacteristic(this.platform.Characteristic.Model, device.ocf?.modelNumber ?? 'Unknown')
.setCharacteristic(this.platform.Characteristic.SerialNumber, device.deviceId);
}
/**
* Executes the command of the capability passed in using the arguments passed in.
* Handles error values returned by api.
*
* @param capability the capability identifier
* @param command the command identifier
* @param args the command arguments
*/
async executeCommand(capability, command, args = []) {
try {
await this.client.devices.executeCommand(this.device.deviceId, {
capability: capability,
command: command,
arguments: args,
});
this.logDebug('Successfully executed command %s of capability %s', command, capability);
}
catch (error) {
let errorMessage = 'unknown';
if (error instanceof Error) {
errorMessage = error.message;
}
let statusCode = -1;
if (axios.isAxiosError(error)) {
statusCode = error.response?.status ?? -1;
}
this.logError('Error when executing %s of capability %s: [%s] %s', command, capability, statusCode, errorMessage);
throw new this.platform.api.hap.HapStatusError(-70402 /* this.platform.api.hap.HAPStatus.SERVICE_COMMUNICATION_FAILURE */);
}
}
/**
* Returns the status of the capability passed in.
* Handles error values returned by api.
*
* @param capability the capability identifier
* @param log flag to turn logging on/off
* @returns the capability status or undefined for errors returned by API
*/
async getCapabilityStatus(capability, log = true) {
try {
const status = await this.client.devices.getCapabilityStatus(this.device.deviceId, this.component.id, capability);
if (log) {
this.logDebug('Successfully get status of %s: %s', capability, JSON.stringify(status, null, 2));
}
return status;
}
catch (error) {
let errorMessage = 'unknown';
if (error instanceof Error) {
errorMessage = error.message;
}
let statusCode = -1;
if (axios.isAxiosError(error)) {
statusCode = error.response?.status ?? -1;
}
this.logError('Error when getting status of %s: [%s] %s', capability, statusCode, errorMessage);
return null;
}
}
/**
* Starts polling the status of the capability passed in using the parameters passed in.
* Must only be used on capabilities that are not updated cyclical automatically.
*
* @param capability the capability that will be updated
* @param service the service containing the characteristic
* @param characteristic the characteristic that will be updated
* @param getter the function to be used to get the new value
* @param interval the interval in milliseconds (if set to undefined polling will not be started)
*/
startStatusPolling(capability, service, characteristic, getter, interval) {
if (interval === undefined) {
return;
}
this.logInfo('Start status polling for %s with interval of %ims', capability, interval);
setInterval(() => {
getter()
.then((value) => {
service.updateCharacteristic(characteristic, value);
})
.catch((reason) => {
this.logError('Error in cyclic update of capability %s: %s', capability, reason);
});
}, interval);
}
logCapabilityRegistration(capability) {
this.logInfo('Registering capability:', capability.name);
this.logCapabilityState(capability);
}
logCapabilityState(capability) {
if (capability.status !== CustomCapabilityStatus.LIVE) {
this.logWarn('Capability %s might not work as expected because it\'s status is: %s', capability.name, capability.status);
}
}
logInfo(message, ...parameters) {
this.log.info('[' + (this.device.name ?? this.device.deviceId) + '] ' + message, ...parameters);
}
logWarn(message, ...parameters) {
this.log.warn('[' + (this.device.name ?? this.device.deviceId) + '] ' + message, ...parameters);
}
logError(message, ...parameters) {
this.log.error('[' + (this.device.name ?? this.device.deviceId) + '] ' + message, ...parameters);
}
logDebug(message, ...parameters) {
this.log.debug('[' + (this.device.name ?? this.device.deviceId) + '] ' + message, ...parameters);
}
}
//# sourceMappingURL=smartThingsAccessory.js.map