Closed pinhao1994 closed 1 year ago
I'm having the same error.
+1. Was working fine then stopped working for no reason. Tried everything, removing and adding the plugin but now refuses to discover any device via API.
Also tried @switchbot/homebridge-switchbot@2.8.0-beta.13
Debug logging shows:
[SwitchBot] [DEBUG] SwitchBot Device Config Not Set: undefined [15/08/2023, 11:59:54 pm] [SwitchBot] [DEBUG] Failed to Discover Devices, Error Message: "Cannot read properties of undefined (reading 'map')", Submit Bugs Here: https://tinyurl.com/SwitchBotBug [15/08/2023, 11:59:54 pm] [SwitchBot] [DEBUG] Failed to Discover Devices, Error: TypeError: Cannot read properties of undefined (reading 'map')
@mrlt8 have you experienced this at all?
The batteries in my bots are all dead so I haven't encountered this, but the API is pretty flakey and the culprit seems to be:
[02/08/2023, 12:17:11] [SwitchBot] Bot: Bot Light Unknown statusCode: 500, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug
You'll probably want to assert the status code is 200 or throw an error around here to avoid the undefined message: https://github.com/OpenWonderLabs/homebridge-switchbot/blob/09ff2520b0e2c7409a371e5c5c264aee17823a7e/src/device/bot.ts#L563-L566
or
at the end of the statusCode()
?
something like:
if (statusCode !== 200) {
throw new Error(`Unexpected status code: ${statusCode}`);
}
As for discoverDevices
, that one seems to return a 200, so maybe check the length of deviceLists
instead of devicesAPI.body
?
Probably a good idea to also throw an error if the status code is not 200.
@mrlt8, I have status code here: https://github.com/OpenWonderLabs/homebridge-switchbot/blob/472aef8bf69434f757c7cc573dc29d2b81d7bdf6/src/platform.ts#L292 So should be good with that.
Pushed a beta for this:
As for
discoverDevices
, that one seems to return a 200, so maybe check the length ofdeviceLists
instead ofdevicesAPI.body
?
Can you try the latest beta:
You'll probably want to assert the status code is 200 or throw an error around here to avoid the undefined message:
or at the end of the
statusCode()
?something like:
if (statusCode !== 200) { throw new Error(`Unexpected status code: ${statusCode}`); }
As for
discoverDevices
, that one seems to return a 200, so maybe check the length ofdeviceLists
instead ofdevicesAPI.body
?Probably a good idea to also throw an error if the status code is not 200.
Modified both of these with beta.18
https://github.com/OpenWonderLabs/homebridge-switchbot/blob/8aa1b4dfdc003ede00b7d8909a434192e272f081/src/platform.ts#L303 https://github.com/OpenWonderLabs/homebridge-switchbot/blob/8aa1b4dfdc003ede00b7d8909a434192e272f081/src/device/bot.ts#L566
@mrlt8, anything you would change?
I would probably move the this.statusCode(statusCode);
out of the if check and errorLog the statusCode:
this.statusCode(statusCode);
if (statusCode === 200) {
...
} else {
this.errorLog(`Failed <openAPIRefreshStatus/openAPIRefreshStatus>, statusCode: ${statusCode}`);
}
May also want to update all the other requests to catch any non-200 requests.
Since you already have the request wrapped in a try-catch, could potentially be easier to just update the statusCode
method to throw an error instead, and the try-catch block should catch that:
async statusCode(statusCode: number): Promise<void> {
switch (statusCode) {
...
default:
this.infoLog(
`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
`${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
);
throw new Error(`Unknown Status Code: ${statusCode}`);
}
}
Hey @donavanbecker thanks for the latest beta! It is now working again!
Hi @donavanbecker thanks for the support.
I have updated to the latest version, v2.8.0-beta.19
, but I still encounter the issue.
From my 3 days' observation, I found that my hb starts raising exceptions after 18:00 (UTC + 8) and it will recover after I wake up tomorrow after 8:00 (UTC + 8).
What could be the cause of making the plugin down every night?
Most likely an issue on the switchbot API. Might want to report it there: https://github.com/OpenWonderLabs/SwitchBotAPI, but support seems non-existent.
Or a network issue
I would probably move the
this.statusCode(statusCode);
out of the if check and errorLog the statusCode:this.statusCode(statusCode); if (statusCode === 200) { ... } else { this.errorLog(`Failed <openAPIRefreshStatus/openAPIRefreshStatus>, statusCode: ${statusCode}`); }
May also want to update all the other requests to catch any non-200 requests.
Since you already have the request wrapped in a try-catch, could potentially be easier to just update the
statusCode
method to throw an error instead, and the try-catch block should catch that:async statusCode(statusCode: number): Promise<void> { switch (statusCode) { ... default: this.infoLog( `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`, ); throw new Error(`Unknown Status Code: ${statusCode}`); } }
@mrlt8 if you have time, wanna throw this in a PR?
@pinhao1994 is getting Unknown statusCode: 500
which is an "Internal Server Error" on the API: https://github.com/OpenWonderLabs/SwitchBotAPI#standard-http-error-codes
@pinhao1994 is getting
Unknown statusCode: 500
which is an "Internal Server Error" on the API: https://github.com/OpenWonderLabs/SwitchBotAPI#standard-http-error-codes
@mrlt8 Could you give me more details on this?
I believe I am a stable network connection since my other smart devices (that are not Switchbot) still work perfectly. At the time when I am writing this message, I faced the same issue again. It is quite interesting that the SwitchBotAPI always die during the night.
I wonder if this could be caused by the request limit? Since I have a short pull time period, I could reach 1000 times API calls per day which caused the error.
Sounds like your theory is plausible! Mine stop working again and I have set it to 5 second API refresh rate, which would incur at least 17,280 calls per day.
I didn't think so at first as the API should return a 429
error.
However, I was doing some debugging and noticed that I kept getting a 200
response in Homebridge when taking my devices offline even though I would get a 171
when making a request directly to the API:
{"statusCode":171,"body":{},"message":"No hub record,is offline"}
It turns out the plug-in was only checking HTTP response and NOT the statusCode
in the json response:
So we could potentially be ignoring some unexpected statusCode
in the json response as long as the HTTP response was 200
.
v2.8.0 is released
@nathennod @donavanbecker
I confirm the bug is caused by the 10k request limitation per day.
First, I increased the poll rate from 6 sec to 15 sec for 2 bots and the advanced settings. I did not face the issue after 10 pm (UTC+8). However, I meet the issue again at 7:30 am tomorrow morning, but the service recovers at 8:00 am (aka. 00:00 UTC). Therefore, I think although I have 3 poll rates to be exactly the same as 15 sec, I believe they could be polled separately and could still cause the issue. Lastly, I increase the rate to 60 sec and wish for the best.
@pinhao1994 Are you setting a refreshRate for each device? You can set different rates for each device.
I have 1 bot and I’ve set it to poll 30 seconds and so far so good!
@donavanbecker
I understand that we could set individual refreshRate
for each device, but I suspect the devices are refreshed separately. That is to say, if I have one device, it could be refreshed as frequently as 9 sec which is equal to 9600 (< 10k) API calls per day.
My situation is that I have 2 switchbot bots, 1 IR for Air Conditioner, and 1 external IR for TV. Based on the settings, I could set individual refreshRate
for each bot and one refreshRate
in the "advanced settings" (I assume that the advanced settings refreshRate
is for the IR devices".)
Now let's assume all devices will be refreshed "separately" in the background based on their refresh rates. Then, although I have max 10K API quota per day, each device could actually only be updated 10k / 4 = 2500 per day. That is to say, in the worst case, each device could only have refreshRate
86400 / 2500 ~= 35 sec.
Therefore, to optimize this part, we should first figure out the logic of refresh in the background.
max(device1_rate, device2_rate, ..., deviceN_rate, 9)
where 9 is the minimum rate to avoid reaching 10k limitation.(86400 sec / (10k / num_of_devices)) = (86400 sec * num_of_devices) / 10k
sec to avoid the worst case.@pinhao1994 IR devices don't refresh data.
Describe the Bug
The devices are not able to be found after adding/updating the children's devices.
I updated the existing device "switchbot bot" with different "device refresh rate".
I added a new device "switchbot bot" for the another new-buy bot.
After restarting the hb, the hb cannot find any devices nor hk showing any switchbot devices.
I have check the issue list, it seems that the below issue are similar
Please help solve the bug, thank you =)
To Reproduce
Not sure
Expected Behavior
The homebridge-switchbot plugin should be able to get all the devices.
Relevant Log Output
Config for homebridge-switchbot
Screenshots
Device and Model
SwitchBot Bot
Node.js Version
v20.5.0
NPM Version
v9.8.0
Homebridge Version
v1.6.1
Homebridge Switchbot Plugin Version
v2.8.0-beta.12
Homebridge Config UI X Plugin Version
v4.50.4
Operating System
Raspbian