Skip to content

Commit

Permalink
Merge pull request #39 from bitfocus/bug/winconf
Browse files Browse the repository at this point in the history
Fix bug preventing configuration on Windows
  • Loading branch information
istnv authored Oct 17, 2024
2 parents d02fc99 + e99e7a5 commit af714b0
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 2,345 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tplink-kasasmartplug",
"version": "2.2.1",
"version": "2.2.2",
"main": "src/index.js",
"type": "module",
"scripts": {
Expand All @@ -13,10 +13,10 @@
"url": "git+https://github.com/bitfocus/companion-module-tplink-kasasmartplug.git"
},
"dependencies": {
"@companion-module/base": "^1.9.0"
"@companion-module/base": "^1.11.0"
},
"devDependencies": {
"@companion-module/tools": "^1.5.1",
"@companion-module/tools": "^2.1.0",
"tplink-smarthome-api": "^5.0.0"
},
"prettier": "@companion-module/tools/.prettierrc.json"
Expand Down
13 changes: 7 additions & 6 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ export function getConfigFields() {
})

let ch = []
let def = 'none'
if (Object.keys(this.FOUND_PLUGS || {}).length == 0) {
ch = [{ id: 'none', label: 'No Kasa Plugs located' }]
this.config.plugId = 'none'
this.saveConfig(this.config)
} else {
ch = [{ id: 'none', label: 'No plug selected' }]
const plugs = this.FOUND_PLUGS
this.config.plugId = 'none'
for (const pId of Object.keys(plugs)) {
ch.push({ id: pId, label: `${plugs[pId].alias} at ${plugs[pId].host} (${plugs[pId].mac})` })
}
if (this.config.plugId == 'none') {
this.config.plugId = ch[0].id
this.config.host = this.FOUND_PLUGS[ch[0].id].host
this.saveConfig(this.config)
}
}
cf.push({
type: 'dropdown',
Expand All @@ -50,7 +50,7 @@ export function getConfigFields() {
return !!opt.scan
},
width: 12,
default: this.config?.plugId ? this.config.plugId : ch[0].id,
default: def,
choices: ch,
})
cf.push(
Expand All @@ -70,5 +70,6 @@ export function getConfigFields() {
'Disabling Polling will prevent automatic reconnect on error.\nFeedback and variables will not always match the plug state.',
},
)

return cf
}
38 changes: 32 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class kasaplugInstance extends InstanceBase {
}

async destroy() {
await this.stopInterval()
this.stopInterval()

if (this.cleanupEvents) this.cleanupEvents()

Expand All @@ -66,19 +66,35 @@ class kasaplugInstance extends InstanceBase {
this.configUpdated(config)
}

configUpdated(config) {
async configUpdated(config) {
const oldConfig = this.config
let resave = false
config.interval = 2000

// stop in case polling has been de-selected by config change
this.stopInterval()
if (this.DEVICE) {
this.DEVICE.closeConnection()
delete this.DEVICE
}

if (this.SCANNING) {
this.scanner.stopDiscovery()
this.SCANNING = false
}

if (config.plugId != 'none') {
const newHost = Object.keys(this.FOUND_PLUGS).length ? this.FOUND_PLUGS[config.plugId].host : null
if (!config.plugId) {
config.plugId = ''
}

if (config.scan && !['', 'none'].includes(config.plugId)) {
const newHost = Object.keys(this.FOUND_PLUGS).length ? this.FOUND_PLUGS[config.plugId].host : this.config?.host
if (newHost && config.host != newHost) {
config.host = newHost
this.saveConfig(config)
resave = true
}
} else if (!config.scan) {
config.plugId = ''
}

this.config = config
Expand All @@ -90,10 +106,20 @@ class kasaplugInstance extends InstanceBase {
} else {
this.FOUND_PLUGS = []
delete this.config.plugId
resave = true
}

this.updateStatus(InstanceStatus.Connecting)
if (config.host != '') {
this.updateStatus(InstanceStatus.Connecting)
} else {
this.updateStatus(InstanceStatus.BadConfig, 'IP address not set')
this.FOUND_PLUGS = []
resave = true
}

if (resave) {
this.saveConfig(this.config)
}
this.ERRORED = false

this.getInformation()
Expand Down
56 changes: 32 additions & 24 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ const client = new Client()
* Gather list of local mixer IP numbers and names
*/
export async function scanForPlugs() {
const scan = new Client()

scan.startDiscovery()
this.SCANNING = true
scan.on('device-new', async (device) => {
const plug = await device.getSysInfo({ deviceTypes: ['plug'] })
let me = ''
plug.host = device.host
this.FOUND_PLUGS[plug.deviceId] = plug
if (plug.host == this.config.host) {
this.config.plugId = ''+plug.deviceId
me = '*'
}
this.log('info', `Found plug ${plug.alias} at ${me}${plug.host}(${plug.mac}) ${plug.deviceId}`)
this.scanner = new Client()

this.scanner.startDiscovery()
this.SCANNING = true
this.scanner.on('device-new', async (device) => {
try {
const plug = await device.getSysInfo({ deviceTypes: ['plug'] })
let me = ''
plug.host = device.host
this.FOUND_PLUGS[plug.deviceId] = plug
if (plug.host == this.config.host) {
this.config.plugId = '' + plug.deviceId
me = '*'
}
this.log('info', `Found plug ${plug.alias} at ${me}${plug.host}(${plug.mac}) ${plug.deviceId}`)
} catch (error) {
// ignore
// this.handleError(error)
}
})
}

Expand Down Expand Up @@ -220,8 +225,8 @@ export function updatePlugState(plugId, powerState) {
this.checkFeedbacks()
this.checkVariables()
}
export function setupInterval() {
if (this.INTERVAL !== null) {
export async function setupInterval() {
if (this.INTERVAL != null) {
clearInterval(this.INTERVAL)
this.INTERVAL = null
}
Expand All @@ -232,7 +237,7 @@ export function setupInterval() {
this.log('info', 'Starting Update Interval.')
this.INTERVAL = setInterval(this.getInformation.bind(this), this.config.interval)
} else {
this.getInformation()
await this.getInformation()
}
}
export function stopInterval() {
Expand All @@ -257,14 +262,17 @@ export function handleError(err) {
if (this.config.host && this.config.mac && this.config.scan) {
// if we're scanning see if the IP changed
errorStr = `No connection. Checking if ${this.config.mac} has a new IP`
let newIP = Object.keys(this.FOUND_PLUGS).find(plug => {
return plug.mac == this.config.mac
})
if (newIP) {
this.config.host = newIP
}
let newIP = Object.keys(this.FOUND_PLUGS).find((plug) => {
return plug.mac == this.config.mac
})
if (newIP) {
this.config.host = newIP
}
} else if ('ECONNRESET' == err['code']) {
errorStr = 'Device is aborting connections'
} else {
errorStr = 'Unable to communicate with Device. Is this the right IP address? Is it still online?'
}
errorStr = 'Unable to communicate with Device. Is this the right IP address? Is it still online?'
}
this.updateStatus(InstanceStatus.ConnectionFailure, errorStr)
} else {
Expand Down
Binary file added tplink-kasasmartplug2.zip
Binary file not shown.
Loading

0 comments on commit af714b0

Please sign in to comment.