jasonacox/tuyapower

python error: local variable 'sw' referenced before assignment

ricorico94 opened this issue · 1 comments

Hi,
When I tried to launch plugjson.py on a smart plug being switched off, I received a python error message on line 175.
I'm unfortunately not an expert in python, but I think tehre might be indeed a line missing between rows 174 and 175 to initiate variable sw (entry missing like: sw = False ?).
Instead of
if watchdog > RETRY: log.info( "NO POWER DATA: Response from plug %s [%s] missing power data." % (deviceid, ip) ) return (sw, w, mA, V, "Missing Power Data")
maybe we should have:
if watchdog > RETRY: log.info( "NO POWER DATA: Response from plug %s [%s] missing power data." % (deviceid, ip) ) sw = False return (sw, w, mA, V, "Missing Power Data")
At least, I don't have the error anymore that way.
Let me know if that's the way it should be or if I solved just by chance..
br,
Ricorico94

Great catch, Ricorico94! Your fix would work, but I updated the code to initialize sw with the rest of the variables. Pull latest and see if that fixes the error.

# default polling response for error condition
_DEFAULTS = (False, -99, -99, -99)
sw, w, mA, V = _DEFAULTS