Skip to content

Commit

Permalink
🔧 Upgrade to Homebridge 1.3.0
Browse files Browse the repository at this point in the history
- Fix plugin slows down warning

Fixes #212
  • Loading branch information
torifat committed Nov 17, 2021
1 parent 586ef41 commit bfcdc19
Show file tree
Hide file tree
Showing 16 changed files with 1,142 additions and 1,222 deletions.
3 changes: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"plugin:@typescript-eslint/eslint-recommended",
// uses the recommended rules from the @typescript-eslint/eslint-plugin
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
"prettier"
],
"parserOptions": {
"ecmaVersion": 2018,
Expand Down
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.rulers": [140],
"eslint.enable": true
"editor.rulers": [140]
}
2,077 changes: 1,040 additions & 1,037 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"engines": {
"node": ">=10.17.0",
"homebridge": ">=1.0.0"
"homebridge": ">=1.3.0"
},
"main": "dist/index.js",
"scripts": {
Expand All @@ -35,20 +35,20 @@
"@rifat/miio": "^1.5.0"
},
"devDependencies": {
"@types/node": "^14.14.6",
"@typescript-eslint/eslint-plugin": "^4.11.1",
"@typescript-eslint/parser": "^4.11.1",
"eslint": "^7.16.0",
"eslint-config-prettier": "^8.0.0",
"homebridge": "^1.2.5",
"husky": "^5.0.6",
"lint-staged": "^10.5.3",
"nodemon": "^2.0.6",
"pinst": "^2.1.1",
"prettier": "^2.2.1",
"@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"eslint": "^8.2.0",
"eslint-config-prettier": "^8.3.0",
"homebridge": "^1.3.6",
"husky": "^7.0.4",
"lint-staged": "^12.0.2",
"nodemon": "^2.0.15",
"pinst": "^2.1.6",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"ts-node": "^9.1.1",
"typescript": "^4.0.5"
"ts-node": "^10.4.0",
"typescript": "^4.4.4"
},
"husky": {
"hooks": {
Expand Down
29 changes: 12 additions & 17 deletions src/characteristics/air-purifier/active.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../../with-device';
import { Service, Characteristic } from 'homebridge';

// https://developers.homebridge.io/#/characteristic/Active
export function add(
Expand All @@ -8,7 +7,6 @@ export function add(
characteristic: typeof Characteristic.Active,
) {
const { ACTIVE, INACTIVE } = characteristic;
const useDevice = withDevice<typeof ACTIVE | typeof INACTIVE>(maybeDevice);

maybeDevice.then((device) => {
device.on('powerChanged', (isOn: boolean) => {
Expand All @@ -18,18 +16,15 @@ export function add(

return service
.getCharacteristic(characteristic)
.on(
CharacteristicEventTypes.GET,
useDevice(async (device) => ((await device.power()) ? ACTIVE : INACTIVE)),
)
.on(
CharacteristicEventTypes.SET,
useDevice(async (device, newStatus) => {
const currentStatus = await device.power();
if (currentStatus !== newStatus) {
const [{ code }] = await device.changePower(newStatus);
return code === 0 ? newStatus : undefined;
}
}),
);
.onGet(async () => {
const device = await maybeDevice;
return (await device.power()) ? ACTIVE : INACTIVE;
})
.onSet(async function (this: Characteristic, newStatus) {
const device = await maybeDevice;
const currentStatus = await device.power();
if (currentStatus !== newStatus) {
await device.changePower(newStatus);
}
});
}
16 changes: 5 additions & 11 deletions src/characteristics/air-purifier/current-air-purifier-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../../with-device';

// https://developers.homebridge.io/#/characteristic/CurrentAirPurifierState
export function add(
Expand All @@ -13,10 +12,6 @@ export function add(
PURIFYING_AIR,
} = characteristic;

const useDevice = withDevice<typeof INACTIVE | typeof PURIFYING_AIR>(
maybeDevice,
);

maybeDevice.then((device) => {
device.on('powerChanged', (isOn: boolean) => {
service.updateCharacteristic(
Expand All @@ -26,10 +21,9 @@ export function add(
});
});

return service.getCharacteristic(characteristic).on(
CharacteristicEventTypes.GET,
useDevice(async (device) =>
(await device.power()) ? PURIFYING_AIR : INACTIVE,
),
);
return service.getCharacteristic(characteristic).onGet(async () => {
const device = await maybeDevice;
const isOn = await device.power();
return isOn ? PURIFYING_AIR : INACTIVE;
});
}
16 changes: 6 additions & 10 deletions src/characteristics/air-purifier/filter-change-indication.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../../with-device';

export const DEFAULT_FILTER_CHANGE_THRESHOLD = 5;

Expand All @@ -15,7 +14,6 @@ export function add(
options: FilterChangeIndicationOptions,
) {
const { FILTER_OK, CHANGE_FILTER } = characteristic;
const useDevice = withDevice<number>(maybeDevice);

maybeDevice.then((device) => {
device.on('filterLifeChanged', (value: number) => {
Expand All @@ -25,12 +23,10 @@ export function add(
});
});

return service.getCharacteristic(characteristic).on(
CharacteristicEventTypes.GET,
useDevice(async (device) =>
device.property('filter_life').value <= options.filterChangeThreshold
? CHANGE_FILTER
: FILTER_OK,
),
);
return service.getCharacteristic(characteristic).onGet(async () => {
const device = await maybeDevice;
return device.property('filter_life').value <= options.filterChangeThreshold
? CHANGE_FILTER
: FILTER_OK;
});
}
11 changes: 4 additions & 7 deletions src/characteristics/air-purifier/filter-life-level.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../../with-device';

// https://developers.homebridge.io/#/characteristic/FilterLifeLevel
export function add(
maybeDevice: Promise<any>,
service: Service,
characteristic: typeof Characteristic.FilterLifeLevel,
) {
const useDevice = withDevice<number>(maybeDevice);

maybeDevice.then((device) => {
device.on('filterLifeChanged', (value: number) => {
service.updateCharacteristic(characteristic, value);
});
});

return service.getCharacteristic(characteristic).on(
CharacteristicEventTypes.GET,
useDevice(async (device) => device.property('filter_life').value),
);
return service.getCharacteristic(characteristic).onGet(async () => {
const device = await maybeDevice;
return device.property('filter_life').value;
});
}
36 changes: 13 additions & 23 deletions src/characteristics/air-purifier/lock-physical-controls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../../with-device';

// https://developers.homebridge.io/#/characteristic/LockPhysicalControls
export function add(
Expand All @@ -8,9 +7,6 @@ export function add(
characteristic: typeof Characteristic.LockPhysicalControls,
) {
const { CONTROL_LOCK_ENABLED, CONTROL_LOCK_DISABLED } = characteristic;
const useDevice = withDevice<
typeof CONTROL_LOCK_ENABLED | typeof CONTROL_LOCK_DISABLED
>(maybeDevice);

maybeDevice.then((device) => {
device.on('childLockChanged', (isLocked: boolean) => {
Expand All @@ -23,23 +19,17 @@ export function add(

return service
.getCharacteristic(characteristic)
.on(
CharacteristicEventTypes.GET,
useDevice(async (device) =>
device.property('child_lock').value
? CONTROL_LOCK_ENABLED
: CONTROL_LOCK_DISABLED,
),
)
.on(
CharacteristicEventTypes.SET,
useDevice(async (device, newStatus) => {
const currentStatus = +device.property('child_lock').value;
if (newStatus !== currentStatus) {
await device.changeChildLock(newStatus);
return newStatus;
}
return undefined;
}),
);
.onGet(async () => {
const device = await maybeDevice;
return device.property('child_lock').value
? CONTROL_LOCK_ENABLED
: CONTROL_LOCK_DISABLED;
})
.onSet(async (newStatus) => {
const device = await maybeDevice;
const currentStatus = +device.property('child_lock').value;
if (newStatus !== currentStatus) {
await device.changeChildLock(newStatus);
}
});
}
31 changes: 13 additions & 18 deletions src/characteristics/air-purifier/rotation-speed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../../with-device';
import { MODE } from '../../miio-consts';

// http://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-ma4:1
Expand All @@ -14,8 +13,6 @@ export function add(
service: Service,
characteristic: typeof Characteristic.RotationSpeed,
) {
const useDevice = withDevice<number>(maybeDevice);

maybeDevice.then((device) => {
device.on('fanSpeedChanged', (speed) => {
service.updateCharacteristic(characteristic, toPercentage(speed));
Expand All @@ -24,19 +21,17 @@ export function add(

return service
.getCharacteristic(characteristic)
.on(
CharacteristicEventTypes.GET,
useDevice(async (device) => toPercentage(await device.fanSpeed())),
)
.on(
CharacteristicEventTypes.SET,
useDevice(async (device, speed) => {
// If the device isn't in manual mode change it first
if ((await device.mode()) !== MODE.NONE) {
await device.changeMode(MODE.NONE);
}
const [{ code }] = await device.changeFanSpeed(speed * RATIO);
return code === 0 ? speed : undefined;
}),
);
.onGet(async () => {
const device = await maybeDevice;
return toPercentage(await device.fanSpeed());
})
.onSet(async (speed) => {
console.log({ speed });
const device = await maybeDevice;
// If the device isn't in manual mode change it first
if ((await device.mode()) !== MODE.NONE) {
await device.changeMode(MODE.NONE);
}
await device.changeFanSpeed(+speed * RATIO);
});
}
30 changes: 12 additions & 18 deletions src/characteristics/air-purifier/target-air-purifier-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../../with-device';
import { MODE } from '../../miio-consts';

// https://developers.homebridge.io/#/characteristic/TargetAirPurifierState
Expand All @@ -9,7 +8,6 @@ export function add(
characteristic: typeof Characteristic.TargetAirPurifierState,
) {
const { AUTO, MANUAL } = characteristic;
const useDevice = withDevice<typeof AUTO | typeof MANUAL>(maybeDevice);

maybeDevice.then((device) => {
device.on('modeChanged', (mode) => {
Expand All @@ -19,20 +17,16 @@ export function add(

return service
.getCharacteristic(characteristic)
.on(
CharacteristicEventTypes.GET,
useDevice(async (device) => ((await device.mode()) ? MANUAL : AUTO)),
)
.on(
CharacteristicEventTypes.SET,
useDevice(async (device, mode) => {
const newMode = mode === AUTO ? MODE.AUTO : MODE.NONE;
const currentMode = await device.mode();
if (newMode !== currentMode) {
const [{ code }] = await device.changeMode(newMode);
return code === 0 ? mode : undefined;
}
return undefined;
}),
);
.onGet(async () => {
const device = await maybeDevice;
return (await device.mode()) ? MANUAL : AUTO;
})
.onSet(async (mode) => {
const device = await maybeDevice;
const newMode = mode === AUTO ? MODE.AUTO : MODE.NONE;
const currentMode = await device.mode();
if (newMode !== currentMode) {
await device.changeMode(newMode);
}
});
}
13 changes: 5 additions & 8 deletions src/characteristics/air-quality.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Characteristic, CharacteristicEventTypes } from 'homebridge';
import { withDevice } from '../with-device';
import { Service, Characteristic } from 'homebridge';

function pm2_5ToAqi(aqi: number) {
if (!aqi) {
Expand All @@ -25,16 +24,14 @@ export function add(
service: Service,
characteristic: typeof Characteristic.AirQuality,
) {
const useDevice = withDevice<number>(maybeDevice);

maybeDevice.then((device) => {
device.on('pm2.5Changed', (value: number) => {
service.updateCharacteristic(characteristic, pm2_5ToAqi(value));
});
});

return service.getCharacteristic(characteristic).on(
CharacteristicEventTypes.GET,
useDevice(async (device) => pm2_5ToAqi(await device.pm2_5())),
);
return service.getCharacteristic(characteristic).onGet(async () => {
const device = await maybeDevice;
return pm2_5ToAqi(await device.pm2_5());
});
}
Loading

0 comments on commit bfcdc19

Please sign in to comment.