jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
937 stars 167 forks source link

Issues with setting integer values #271

Closed elockman closed 1 year ago

elockman commented 1 year ago

I am communicating with a thermostat locally, but get an error when trying to set integer values.

This is a link to the thermostat on Amazon:

devices.json reveals:

        "product_name": "Thermostat ",
        "model": "PCT513-WB",

setMiddleSetpoint() works, but setSetpoint() gives the following error:

Traceback (most recent call last):
  File "/home/user/projects/xeal/tuya/tinytuya/examples/tstat.py", line 18, in <module>
    tstatdev.setSetpoint( 72, 'f' )
  File "/home/user/.local/lib/python3.9/site-packages/tinytuya/Contrib/ThermostatDevice.py", line 306, in setSetpoint
    return self.setMiddleSetpoint( self, setpoint, cf )
TypeError: setMiddleSetpoint() takes from 2 to 3 positional arguments but 4 were given

Below is my full code(dev_id and key redacted). It is written and called from the examples folder. All of the commented lines provide similar errors.

from tinytuya import Contrib
import time

tstatdev = Contrib.ThermostatDevice( '{dev_id}', '192.168.1.100', '{key}' )

## we do not need to set persistant or v3.3 as ThermostatDevice() does that for us

#tstatdev.setFanRuntime( 25 )
#tstatdev.setSetpoint( 72, 'f' )
#tstatdev.setCoolSetpoint( 72, 'f' )
#tstatdev.setHeatSetpoint( 72, 'f' )
#tstatdev.setSchedule( None )
tstatdev.setMiddleSetpoint( 72, 'f' )
tstatdev.setMode( 'auto' )
tstatdev.setFan( 'auto' )
tstatdev.setUnits( 'f' )
data = tstatdev.status()
print('Device status: %r' % data)

Is there a bug in the code, or am I doing something wrong here?

uzlonewolf commented 1 year ago

That would be a bug in the code. As a temporary fix, you can patch it with:

tstatdev = Contrib.ThermostatDevice( '{dev_id}', '192.168.1.100', '{key}' )

def setsetpoint_fixed( self, setpoint, cf=None ):
    if self.mode == 'cool':
        return self.setCoolSetpoint( setpoint, cf )
    elif self.mode == 'heat' or self.mode == 'emergencyheat':
        return self.setHeatSetpoint( setpoint, cf )
    else:
        # no idea, let the thermostat figure it out
        return self.setMiddleSetpoint( setpoint, cf )

tstatdev.setSetpoint = setsetpoint_fixed

tstatdev.setSetpoint(72, 'f') should then work. I'll try to get an update that fixes this pushed soon.

elockman commented 1 year ago

@uzlonewolf thank you for the quick response (and patch). I guess it makes sense that the setpoints are mode related.