Skip to content

Commit

Permalink
Merge pull request #23 from bitfocus/chore/tuneup
Browse files Browse the repository at this point in the history
Cleanup variable consistancy, improve feedbacks
  • Loading branch information
istnv authored Feb 10, 2024
2 parents 73b1ed4 + c9cfea3 commit 4add5eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tplink-kasasmartplug",
"version": "2.1.2",
"version": "2.1.3",
"main": "src/index.js",
"type": "module",
"scripts": {
Expand Down
14 changes: 9 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ export async function powerToggle(plugId) {
const plugName = this.CHOICES_PLUGS.find((PLUG) => PLUG.id == plugId)?.label || ''

try {
let plug
let plug, state
if (this.SINGLEPLUGMODE) {
plug = await client.getDevice({ host: this.config.host })
state = plug.relayState
} else {
plug = await client.getDevice({ host: this.config.host, childId: plugId })
state = plug.state
}
this.log('info', `Toggling ${plugName} Power`)
await plug.togglePowerState()
this.updatePlugState(plug, !state)
plug.togglePowerState()
} catch (error) {
this.handleError(error)
}
Expand Down Expand Up @@ -162,7 +165,7 @@ export function monitorEvents(plug) {
this.updatePlugState(plugId, 0)
})
plug.on('power-update', (powerOn) => {
this.updatePlugState(plugId, powerOn)
this.updatePlugState(plugId, +powerOn)
})
plug.on('in-use', () => {})
plug.on('not-in-use', () => {})
Expand Down Expand Up @@ -231,8 +234,6 @@ export function handleError(err) {

let errorStr = err.toString()

this.updateStatus(InstanceStatus.UnknownError)

if ('code' in err) {
if (err['code'] === 'ECONNREFUSED') {
errorStr =
Expand All @@ -243,6 +244,9 @@ export function handleError(err) {
}
}

this.updateStatus(InstanceStatus.UnknownError, errorStr)


if (this.INTERVAL && stoppit) {
this.log('error', 'Stopping Update interval due to error.')
this.stopInterval()
Expand Down

0 comments on commit 4add5eb

Please sign in to comment.