homebridge-virtual-accessories
Version:
Virtual HomeKit accessories for Homebridge.
202 lines • 9.77 kB
JavaScript
/* eslint-disable brace-style */
/* eslint-disable max-len */
import { Accessory } from './accessory.js';
/**
* AirPurifier - Accessory implementation
*/
export class AirPurifier extends Accessory {
static ACCESSORY_TYPE_NAME = 'AirPurifier';
static CURRENTLY_INACTIVE = 0; // Characteristic.CurrentAirPurifierState.INACTIVE
static CURRENTLY_IDLE = 1; // Characteristic.CurrentAirPurifierState.IDLE
static CURRENTLY_PURIFYING_AIR = 2; // Characteristic.CurrentAirPurifierState.PURIFYING_AIR
static MANUAL = 0; // Characteristic.TargetAirPurifierState.MANUAL
static AUTO = 1; // Characteristic.TargetAirPurifierState.AUTO
static INACTIVE = 0; // Characteristic.Active.INACTIVE
static ACTIVE = 1; // Characteristic.Active.ACTIVE
stateStorageKey = 'AirPurifierActive';
targetStateStorageKey = 'AirPurifierTargetState';
rotatioSpeedStorageKey = 'AirPurifierRotationSpeed';
states = {
AirPurifierActive: AirPurifier.INACTIVE,
AirPurifierCurrentState: AirPurifier.CURRENTLY_INACTIVE,
AirPurifierTargetState: AirPurifier.MANUAL,
AirPurifierRotationSpeed: 100,
};
constructor(platform, accessory, accessoryConfiguration) {
super(platform, accessory, accessoryConfiguration);
// First configure the device based on the accessory details
const rotationSpeed = this.accessoryConfiguration.airPurifier.rotationSpeed;
this.states.AirPurifierActive = AirPurifier.INACTIVE;
this.states.AirPurifierCurrentState = AirPurifier.CURRENTLY_INACTIVE;
this.states.AirPurifierTargetState = AirPurifier.MANUAL;
this.states.AirPurifierRotationSpeed = rotationSpeed;
// If the accessory is stateful retrieve stored state
if (this.accessoryConfiguration.accessoryIsStateful) {
const accessoryState = this.loadAccessoryState(this.storagePath);
const cachedState = accessoryState[this.stateStorageKey];
const cachedTargetState = accessoryState[this.targetStateStorageKey];
const cachedRotationSpeed = accessoryState[this.rotatioSpeedStorageKey];
if (cachedState !== undefined) {
this.states.AirPurifierActive = cachedState;
}
if (cachedTargetState !== undefined) {
this.states.AirPurifierTargetState = cachedTargetState;
}
if (cachedRotationSpeed !== undefined) {
this.states.AirPurifierRotationSpeed = cachedRotationSpeed;
}
}
this.setDeviceOperationalCondition();
this.service = this.accessory.getService(this.platform.Service.AirPurifier) || this.accessory.addService(this.platform.Service.AirPurifier);
this.service.setCharacteristic(this.platform.Characteristic.Name, this.accessoryConfiguration.accessoryName);
// Update the initial state of the accessory
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Setting Air Purifier Current State: ${AirPurifier.getCurrentStateName(this.states.AirPurifierActive)}`);
this.service.updateCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, (this.states.AirPurifierCurrentState));
this.service.updateCharacteristic(this.platform.Characteristic.TargetAirPurifierState, (this.states.AirPurifierTargetState));
this.service.updateCharacteristic(this.platform.Characteristic.RotationSpeed, (this.states.AirPurifierRotationSpeed));
// register handlers
this.service.getCharacteristic(this.platform.Characteristic.Active)
.onSet(this.setActive.bind(this))
.onGet(this.getActive.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CurrentAirPurifierState)
.onGet(this.getCurrentAirPurifierState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.TargetAirPurifierState)
.onSet(this.setTargetAirPurifierState.bind(this))
.onGet(this.getTargetAirPurifierState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.RotationSpeed)
.onSet(this.setRotationSpeed.bind(this))
.onGet(this.getRotationSpeed.bind(this));
}
// Handlers
async setActive(value) {
this.states.AirPurifierActive = value;
this.setDeviceOperationalCondition();
this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting State: ${AirPurifier.getActiveName(this.states.AirPurifierActive)}`);
}
async getActive() {
const airPurifierActive = this.states.AirPurifierActive;
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting State: ${AirPurifier.getActiveName(airPurifierActive)}`);
return airPurifierActive;
}
async getCurrentAirPurifierState() {
const airPurifierCurrentState = this.states.AirPurifierCurrentState;
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Current Air Purifier State: ${AirPurifier.getCurrentStateName(airPurifierCurrentState)}`);
return airPurifierCurrentState;
}
async setTargetAirPurifierState(value) {
this.states.AirPurifierTargetState = value;
this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Target Air Purifier State: ${AirPurifier.getTargetStateName(this.states.AirPurifierTargetState)}`);
this.setDeviceOperationalCondition();
this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Current Air Purifier State: ${AirPurifier.getCurrentStateName(this.states.AirPurifierCurrentState)}`);
}
async getTargetAirPurifierState() {
const airPurifierTargetState = this.states.AirPurifierTargetState;
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Target Air Purifier State: ${AirPurifier.getTargetStateName(airPurifierTargetState)}`);
return airPurifierTargetState;
}
async setRotationSpeed(value) {
this.states.AirPurifierRotationSpeed = value;
this.storeState();
this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Rotation Speed: ${this.states.AirPurifierRotationSpeed}%`);
}
async getRotationSpeed() {
const airPurifierRotationSpeed = this.states.AirPurifierRotationSpeed;
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Rotation Speed: ${airPurifierRotationSpeed}%`);
return airPurifierRotationSpeed;
}
getJsonState() {
const json = JSON.stringify({
[this.stateStorageKey]: this.states.AirPurifierActive,
[this.targetStateStorageKey]: this.states.AirPurifierTargetState,
[this.rotatioSpeedStorageKey]: this.states.AirPurifierRotationSpeed,
});
return json;
}
getAccessoryTypeName() {
return AirPurifier.ACCESSORY_TYPE_NAME;
}
setDeviceOperationalCondition() {
if (this.states.AirPurifierActive === AirPurifier.ACTIVE) {
this.states.AirPurifierCurrentState = AirPurifier.CURRENTLY_PURIFYING_AIR;
}
else { // (this.states.AirPurifierActive === AirPurifier.INACTIVE)
if (this.states.AirPurifierTargetState === AirPurifier.AUTO) {
this.states.AirPurifierCurrentState = AirPurifier.CURRENTLY_IDLE;
}
else if (this.states.AirPurifierTargetState === AirPurifier.MANUAL) {
this.states.AirPurifierCurrentState = AirPurifier.CURRENTLY_INACTIVE;
}
}
this.service?.setCharacteristic(this.platform.Characteristic.CurrentAirPurifierState, (this.states.AirPurifierCurrentState));
this.storeState();
this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Air Purifier current state: ${AirPurifier.getCurrentStateName(this.states.AirPurifierCurrentState)}`);
}
static getActiveName(status) {
let activeName;
switch (status) {
case undefined: {
activeName = 'undefined';
break;
}
case AirPurifier.INACTIVE: {
activeName = 'INACTIVE';
break;
}
case AirPurifier.ACTIVE: {
activeName = 'ACTIVE';
break;
}
default: {
activeName = status.toString();
}
}
return activeName;
}
static getCurrentStateName(state) {
let stateName;
switch (state) {
case undefined: {
stateName = 'undefined';
break;
}
case AirPurifier.CURRENTLY_INACTIVE: {
stateName = 'INACTIVE';
break;
}
case AirPurifier.CURRENTLY_IDLE: {
stateName = 'IDLE';
break;
}
case AirPurifier.CURRENTLY_PURIFYING_AIR: {
stateName = 'HEATING';
break;
}
default: {
stateName = state.toString();
}
}
return stateName;
}
static getTargetStateName(state) {
let stateName;
switch (state) {
case undefined: {
stateName = 'undefined';
break;
}
case AirPurifier.MANUAL: {
stateName = 'MANUAL';
break;
}
case AirPurifier.AUTO: {
stateName = 'AUTO';
break;
}
default: {
stateName = state.toString();
}
}
return stateName;
}
}
//# sourceMappingURL=virtualAccessoryAirPurifier.js.map