luisiam / homebridge-cmdswitch2

CMD Plugin for HomeBridge (API 2.0): https://github.com/nfarina/homebridge
Apache License 2.0
176 stars 29 forks source link

State/Polling Problems #36

Closed seydx closed 3 years ago

seydx commented 6 years ago

Hey everyone,

i have an issue with the state/polling of my tv. I created a bash file to get the status of my tv (on/standby) with curl. If i turn off the tv (standby), it disconnects from the internet after certain time and HomeKit tells me that the tv is offline. So far so good.

But if i turn it on with my script (i am using broadlink rm to turn it on if there is no internet connection, but if the tv is still connected with internet i am using curl to turn it on ) the tv will turn on. BUT the state of my tv goes offline and after 1-2 sec it goes online again... This happens several times before it stays online..

It only happens by reconnecting the tv with internet..

Can anyone help me?

theend92 commented 6 years ago

show us the bash file

eliottrobson commented 5 years ago

I am not sure if it is related but there is a bug in the code that I have found. This bug only occurs when using Polling mode AND the command throws an error.

For example when using ps4-waker: PS4 State = REST, ps4-waker = FOUND, status = OFF (correct) PS4 State = ON, ps4-waker = FOUND, status = ON (correct) PS4 State = OFF, ps4-waker = ERROR, status = whatever it was previously (incorrect)

According to the docs, this plugin is supposed to set the device as off in the case of an error, however, what it actually does is just use the previous value from the cache.

This is due to differences in how the state is determined when using polling or not. The correct way can be found when polling is off:

https://github.com/luisiam/homebridge-cmdswitch2/blob/c866c20c8ae54522f3b72a9f4770078f68ae2a07/index.js#L203-L209

However, the error can be seen here (line 182):

https://github.com/luisiam/homebridge-cmdswitch2/blob/c866c20c8ae54522f3b72a9f4770078f68ae2a07/index.js#L180-L188

The important part is that during polling, the state is only updated if there was no error. This can be easily fixed by changing it to if (state !== thisSwitch.state) {. The bug causes the device to constantly display whatever state the device was in before the last error, regardless to response.