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

Fix filter command logic and filter maintenance #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
.homebridge
node_modules
dist
package-lock.json
package-lock.json
/.vs/VSWorkspaceState.json
/.vs/tasks.vs.json
/.vs/slnx.sqlite
/.vs/ProjectSettings.json
/.vs/homebridge-airmega/v16/.suo
/Readme.txt
4 changes: 2 additions & 2 deletions lib/AirmegaPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AirmegaPlatform {
log: Log;

constructor(log: Log, config: PluginConfig, platform: Platform) {
Logger.setLogger(log, config.debug);
Logger.setLogger(log, config.debug, config.diagnostic);

this.platform = platform;
this.accessories = new Map<string, Accessory>();
Expand Down Expand Up @@ -55,7 +55,7 @@ export class AirmegaPlatform {
accessory = new HAP.Accessory(purifier.name, uuid);
this.accessories.set(accessory.UUID, accessory);

this.platform.registerPlatformAccessories('homebridge-airmega', 'Airmega', [accessory]);
this.platform.registerPlatformAccessories('homebridge-airmega', 'Airmega', [accessory]);
}

this.registerServices(purifier, accessory, config);
Expand Down
6 changes: 5 additions & 1 deletion lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export class Client {
let filterStatuses = response.body.filterList.map(filter => {
let filterStatus: FilterStatus = {
name: filter.filterName,
lifeLevel: filter.filterPer
lifeLevel: filter.filterPer,
code: filter.filterCode
}

return filterStatus;
Expand All @@ -53,20 +54,23 @@ export class Client {
async setPower(id: string, on: boolean): Promise<void> {
let value = on ? '1' : '0';
let payload = await this.buildControlPayload(id, Config.Codes.POWER, value);
Logger.diagnostic(`Client.setPower: ${value}`);

await this.sendControlRequest(id, payload);
}

async setMode(id: string, auto: boolean): Promise<void> {
let value = auto ? '1' : '2';
let payload = await this.buildControlPayload(id, Config.Codes.MODE, value);
Logger.diagnostic(`Client.setMode: ${value}`);

await this.sendControlRequest(id, payload);
}

async setFanSpeed(id: string, speed: number): Promise<void> {
let value = speed.toString();
let payload = await this.buildControlPayload(id, Config.Codes.FAN, value);
Logger.diagnostic(`Client.setFanSpeed: ${value}`);

await this.sendControlRequest(id, payload);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export namespace Config {
}

export namespace Filters {
export const PRE_FILTER = 'Pre Filter';
export const MAIN_FILTER = 'Max2 Filter';
export const PRE_FILTER = '3121332';
export const MAIN_FILTER = '3111735';
}
}
15 changes: 14 additions & 1 deletion lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Log } from "./interfaces/HAP";
class AirmegaLogger {
public log: Log;
private debugMode: boolean;
private diagnosticMode: boolean;

setLogger(log: Log, debugMode: boolean): void {
setLogger(log: Log, debugMode: boolean, diagnosticMode: boolean): void {
this.log = log;
this.debugMode = debugMode;
this.diagnosticMode = diagnosticMode;
}

debug(message: string, data?: any): void {
Expand All @@ -20,6 +22,17 @@ class AirmegaLogger {
this.log(result);
}

diagnostic(message: string, data?: any): void {
if (!this.diagnosticMode) return;

let result = message;
if (data) {
result += `: ${JSON.stringify(data)}`
}

this.log(result);
}

error(message: string, error: Error) {
this.log(`[ERROR] ${message}. ${error.message}`)
}
Expand Down
1 change: 1 addition & 0 deletions lib/interfaces/PluginConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface PluginConfig {
password: string;
exclude?: string[];
debug?: boolean;
diagnostic?: boolean;
}
1 change: 1 addition & 0 deletions lib/interfaces/PurifierStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface Status {
export interface FilterStatus {
name: string
lifeLevel: number
code: string
}

export interface Metadata {
Expand Down
2 changes: 1 addition & 1 deletion lib/services/AirQualityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AirQualityService extends AbstractService {
let airQualityService = this.accessory.getService(HAP.Service.AirQualitySensor);

if (!airQualityService) {
airQualityService = this.accessory.addService(HAP.Service.AirQualitySensor, this.purifier.name);
airQualityService = this.accessory.addService(HAP.Service.AirQualitySensor, this.purifier.name + ' Air Quality');
}

return airQualityService;
Expand Down
16 changes: 8 additions & 8 deletions lib/services/FilterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FilterService extends AbstractService {
let filterService = this.accessory.getServiceByUUIDAndSubType(HAP.Service.FilterMaintenance, 'main');

if (!filterService) {
filterService = this.accessory.addService(HAP.Service.FilterMaintenance, Config.Filters.MAIN_FILTER, 'main');
filterService = this.accessory.addService(HAP.Service.FilterMaintenance, this.purifier.name + ' Max2-filter', Config.Filters.MAIN_FILTER);
}

return filterService;
Expand All @@ -36,7 +36,7 @@ export class FilterService extends AbstractService {
let filterService = this.accessory.getServiceByUUIDAndSubType(HAP.Service.FilterMaintenance, 'pre');

if (!filterService) {
filterService = this.accessory.addService(HAP.Service.FilterMaintenance, Config.Filters.PRE_FILTER, 'pre');
filterService = this.accessory.addService(HAP.Service.FilterMaintenance, this.purifier.name +' Pre-filter', Config.Filters.PRE_FILTER);
}

return filterService;
Expand Down Expand Up @@ -78,10 +78,10 @@ export class FilterService extends AbstractService {
}
}

async getChangeIndicationForFilter(name: string): Promise<any> {
async getChangeIndicationForFilter(code: string): Promise<any> {
try {
let status = await this.purifier.waitForFilterStatusUpdate();
let statusForFilter = status.find(filter => filter.name == name);
let statusForFilter = status.find(filter => filter.code == code);
let indication;

if (statusForFilter.lifeLevel <= 20) {
Expand All @@ -92,19 +92,19 @@ export class FilterService extends AbstractService {

return indication;
} catch(e) {
Logger.error(`Unable to get filter change indication for ${name}`, e);
Logger.error(`Unable to get filter change indication for ${code}`, e);
throw e;
}
}

async getLifeLevelForFilter(name: string): Promise<number> {
async getLifeLevelForFilter(code: string): Promise<number> {
try {
let status = await this.purifier.waitForFilterStatusUpdate();
let statusForFilter = status.find(filter => filter.name == name);
let statusForFilter = status.find(filter => filter.code == code);

return statusForFilter.lifeLevel;
} catch(e) {
Logger.error(`Unable to get filter life level for ${name}`, e);
Logger.error(`Unable to get filter life level for ${code}`, e);
throw e;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/services/LightbulbService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class LightbulbService extends AbstractService {
let lightbulbService = this.accessory.getService(HAP.Service.Lightbulb);

if (!lightbulbService) {
lightbulbService = this.accessory.addService(HAP.Service.Lightbulb, this.purifier.name);
lightbulbService = this.accessory.addService(HAP.Service.Lightbulb, this.purifier.name + ' Light');
}

return lightbulbService;
Expand Down
24 changes: 21 additions & 3 deletions lib/services/PurifierService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class PurifierService extends AbstractService {
let purifierService = this.accessory.getService(HAP.Service.AirPurifier);

if (!purifierService) {
purifierService = this.accessory.addService(HAP.Service.AirPurifier, this.purifier.name);
purifierService = this.accessory.addService(HAP.Service.AirPurifier, this.purifier.name + ' Purifier');
}

return purifierService;
Expand All @@ -56,16 +56,19 @@ export class PurifierService extends AbstractService {
// the fan speed (setRotationSpeed ensures device is on).
if (Number(this.purifier.power) == targetState) {
callback();
Logger.diagnostic('PurifierService.setActiveState targetState = this.purifier.power');
return;
}

try {
await this.client.setPower(this.purifier.id, targetState);

if (targetState) {
Logger.diagnostic('PurifierService.setActiveState targetState = Power.On');
this.purifierService.setCharacteristic(HAP.Characteristic.CurrentAirPurifierState, HAP.Characteristic.CurrentAirPurifierState.PURIFYING_AIR);
this.purifier.power = Power.On;
} else {
Logger.diagnostic('PurifierService.setActiveState targetState = Power.Off');
this.purifierService.setCharacteristic(HAP.Characteristic.CurrentAirPurifierState, HAP.Characteristic.CurrentAirPurifierState.INACTIVE);
this.purifier.power = Power.Off;
}
Expand Down Expand Up @@ -119,14 +122,22 @@ export class PurifierService extends AbstractService {
}

async setTargetPurifierState(targetState, callback): Promise<void> {
try {
if (Number(this.purifier.mode) == targetState) {
callback();
Logger.diagnostic('PurifierService.setTargetPurifierState targetState = this.purifier.mode');
return;
}

try {
await this.client.setMode(this.purifier.id, targetState);

if (targetState) {
Logger.diagnostic('PurifierService.setTargetPurifierState targetState = Mode.Auto');
this.purifier.mode = Mode.Auto;
this.purifierService.getCharacteristic(HAP.Characteristic.CurrentAirPurifierState)
.updateValue(HAP.Characteristic.CurrentAirPurifierState.PURIFYING_AIR);
} else {
Logger.diagnostic('PurifierService.setTargetPurifierState targetState = Mode.Manual');
this.purifier.mode = Mode.Manual;
this.purifierService.getCharacteristic(HAP.Characteristic.CurrentAirPurifierState)
.updateValue(HAP.Characteristic.CurrentAirPurifierState.IDLE);
Expand Down Expand Up @@ -172,18 +183,25 @@ export class PurifierService extends AbstractService {
}
}

if (this.purifier.fan == targetSpeed) {
if (this.purifier.fan == targetSpeed && this.purifier.mode == Mode.Manual) {
Logger.diagnostic('PurifierService.setRotationSpeed targetState = this.purifier.fan && mode = Manual');

return callback();
}
else
Logger.diagnostic(`PurifierService.setRotationSpeed Current speed = ${this.purifier.fan} & Target speed = ${targetSpeed} & Mode = ${this.purifier.mode}`);

try {
await this.client.setFanSpeed(this.purifier.id, targetSpeed);

this.purifier.fan = targetSpeed;

Logger.diagnostic('PurifierService.setRotationSpeed CurrentAirPurifierState = PURIFYING_AIR');
this.purifierService.getCharacteristic(HAP.Characteristic.CurrentAirPurifierState)
.updateValue(HAP.Characteristic.CurrentAirPurifierState.PURIFYING_AIR);

Logger.diagnostic('PurifierService.setRotationSpeed TargetAirPurifierState = MANUAL');
this.purifier.mode = Mode.Manual;
this.purifierService.getCharacteristic(HAP.Characteristic.TargetAirPurifierState)
.updateValue(HAP.Characteristic.TargetAirPurifierState.MANUAL);

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-airmega",
"version": "3.1.9",
"version": "3.1.10",
"description": "Homebridge plugin for the Airmega air purifier",
"main": "./dist/index.js",
"author": "Andrew Schaper",
Expand All @@ -24,7 +24,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/aschzero/homebridge-airmega"
"url": "https://github.com/GTPatriots/homebridge-airmega"
},
"keywords": [
"coway",
Expand Down