clach04 / python-tuya

Python interface to ESP8266MOD WiFi smart devices from Shenzhen Xenon. NOTE I'm not using any devices with this library so I can't test :-(
MIT License
239 stars 55 forks source link

AttributeError when decoding #26

Closed tadly closed 6 years ago

tadly commented 6 years ago

I've been playing with this and (at least with python 3) the following line causes an AttributeError: 'str' object has no attribute 'decode'

https://github.com/clach04/python-tuya/blob/01c123f899e0ded10554fb0a6fc5e64d20176d30/pytuya/__init__.py#L274

As I'm not familiar with the code yet I quickly fixed it locally with:

if not isinstance(result, str):                                    
    result = result.decode()                                       
result = json.loads(result)

Not sure how python2 behaves with all that.

I'm rather busy lately so I don't have the time to investigate further and come up with a proper fix.

clach04 commented 6 years ago

@tadly when are you seeing this? When running the testsuite? What (sub-)version of Python?

tadly commented 6 years ago

@clach04 nope, not testsuite. Was a live test.

As the power strip wasn't working stable enough when disabling internet access on the router, I sent it back already. So I can't do more testing.

IIRC it was with both, python 3.5.3 and 3.6.5

magneticflux- commented 6 years ago

I'm seeing this as well, when using sean6541/tuya-homeassistant. My error log:

Update for switch.tall_lamp fails

Traceback (most recent call last):
  File "/usr/src/app/homeassistant/helpers/entity.py", line 204, in async_update_ha_state
    yield from self.async_device_update()
  File "/usr/src/app/homeassistant/helpers/entity.py", line 327, in async_device_update
    yield from self.hass.async_add_job(self.update)
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/switch/tuya.py", line 140, in update
    status = self._device.status()
  File "/config/custom_components/switch/tuya.py", line 104, in status
    self._cached_status = self.__get_status()
  File "/config/custom_components/switch/tuya.py", line 86, in __get_status
    status = self._device.status()
  File "/config/deps/lib/python3.6/site-packages/pytuya/__init__.py", line 274, in status
    result = json.loads(result.decode())
AttributeError: 'str' object has no attribute 'decode'