Skip to content

Commit

Permalink
Cleanup variable consistancy, improve feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
istnv committed Feb 10, 2024
1 parent 6b532eb commit c9cfea3
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 c9cfea3

Please sign in to comment.