Skip to content

Commit

Permalink
refactor: turn on @typescript-eslint/strict-type-checked
Browse files Browse the repository at this point in the history
Enable rule and associated fixes. I think a lot of small bugs may have
been squashed
  • Loading branch information
plasticrake committed Nov 9, 2023
1 parent a8dd13d commit 213fcde
Show file tree
Hide file tree
Showing 35 changed files with 769 additions and 587 deletions.
7 changes: 4 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2019,
ecmaVersion: 2022,
project: ['./tsconfig.json'],
},
env: {
Expand All @@ -21,8 +21,7 @@ module.exports = {
extends: [
'airbnb-base',
'airbnb-typescript/base',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/strict',
'plugin:@typescript-eslint/strict-type-checked',
'plugin:prettier/recommended',
],
rules: {
Expand All @@ -35,12 +34,14 @@ module.exports = {
'ts-check': 'allow-with-description',
},
],
'@typescript-no-unsafe-assignment': 'off',
'no-restricted-syntax': [
'off',
{
selector: 'ForOfStatement',
},
],
'no-void': 'off',
'tsdoc/syntax': 'off', // 'warn',
},
},
Expand Down
14 changes: 10 additions & 4 deletions examples/device.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Client, Sysinfo } from '..'; // 'tplink-smarthome-api'
import { Client } from '..'; // 'tplink-smarthome-api'

const client = new Client();

client.getDevice({ host: '10.0.0.60' }).then((device) => {
device.getSysInfo().then((sysInfo: Sysinfo) => console.log(sysInfo));
});
client
.getDevice({ host: '10.0.0.60' })
.then(async (device) => {
const sysInfo = await device.getSysInfo();
console.log(sysInfo);
})
.catch((reason) => {
console.error(reason);
});
26 changes: 18 additions & 8 deletions examples/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import util from 'util';
import { Client, Device, LightState, RealtimeNormalized } from '..'; // 'tplink-smarthome-api'
import {
Client,
type Device,
type LightState,
type RealtimeNormalized,
} from '..'; // 'tplink-smarthome-api'

const client = new Client();

Expand All @@ -18,15 +23,20 @@ const logEvent = function logEvent(

// Client events `device-*` also have `bulb-*` and `plug-*` counterparts.
// Use those if you want only events for those types and not all devices.
client.on('device-new', (device) => {
client.on('device-new', (device: Device) => {
logEvent('device-new', device);

// Poll device every 5 seconds
setTimeout(function pollDevice() {
device.getInfo().then((data: unknown) => {
console.log(data);
setTimeout(pollDevice, 5000);
});
device
.getInfo()
.then((data: unknown) => {
console.log(data);
setTimeout(pollDevice, 5000);
})
.catch((reason: unknown) => {
console.error(reason);
});
}, 5000);

// Device (Common) Events
Expand Down Expand Up @@ -68,10 +78,10 @@ client.on('device-new', (device) => {
logEvent('lightstate-update', device, lightstate);
});
});
client.on('device-online', (device) => {
client.on('device-online', (device: Device) => {
logEvent('device-online', device);
});
client.on('device-offline', (device) => {
client.on('device-offline', (device: Device) => {
logEvent('device-offline', device);
});

Expand Down
54 changes: 33 additions & 21 deletions examples/multi-plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,45 @@ const monitorEvents = function monitorEvents(device: Device) {

// Poll device every 5 seconds
setTimeout(function pollDevice() {
device.getInfo().then((data) => {
console.log(data);
setTimeout(pollDevice, 5000);
});
device
.getInfo()
.then((data) => {
console.log(data);
setTimeout(pollDevice, 5000);
})
.catch((reason) => {
console.error(reason);
});
}, 5000);
};

(async () => {
const device = await client.getDevice({ host: '10.0.1.136' });
void (async () => {
try {
const device = await client.getDevice({ host: '10.0.1.136' });

console.log(device.alias);
console.log(device.alias);

if (!('children' in device) || !device.children) {
console.log('device has no children');
return;
}
if (!('children' in device)) {
console.log('device has no children');
return;
}

device.children.forEach((child) => {
console.log(child);
});
device.children.forEach((child) => {
console.log(child);
});

await Promise.all(
Array.from(device.children.keys(), async (childId) => {
const childPlug = await client.getDevice({ host: '10.0.1.136', childId });
monitorEvents(childPlug);
}),
);
await Promise.all(
Array.from(device.children.keys(), async (childId) => {
const childPlug = await client.getDevice({
host: '10.0.1.136',
childId,
});
monitorEvents(childPlug);
}),
);

monitorEvents(device);
monitorEvents(device);
} catch (err) {
console.error(err);
}
})();
21 changes: 13 additions & 8 deletions examples/plug-turn-on-off.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { Client } from '..'; // 'tplink-smarthome-api'

const client = new Client();

client.getDevice({ host: '10.0.0.60' }).then((device) => {
console.log('Found device:', device.deviceType, device.alias);
if (device.deviceType === 'plug') {
console.log('Turning plug on, then off', device.alias);
device.setPowerState(true);
device.setPowerState(false);
}
});
client
.getDevice({ host: '10.0.0.60' })
.then(async (device) => {
console.log('Found device:', device.deviceType, device.alias);
if (device.deviceType === 'plug') {
console.log('Turning plug on, then off', device.alias);
await device.setPowerState(true);
await device.setPowerState(false);
}
})
.catch((reason) => {
console.error(reason);
});
15 changes: 10 additions & 5 deletions examples/turn-all-plugs-on.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Client } from '..'; // 'tplink-smarthome-api'
import { Client, Plug } from '..'; // 'tplink-smarthome-api'

const client = new Client();

// Search for all plugs and turn them on
client.on('plug-new', (plug) => {
client.on('plug-new', (plug: Plug) => {
console.log('Found plug:', plug.alias);
plug.setPowerState(true).then(() => {
console.log('Plug', plug.alias, 'is now on');
});
plug
.setPowerState(true)
.then(() => {
console.log('Plug', plug.alias, 'is now on');
})
.catch((reason) => {
console.error(reason);
});
});

client.startDiscovery();
Loading

0 comments on commit 213fcde

Please sign in to comment.