rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.84k stars 546 forks source link

Climate entity setup - separate DP for ON/Off and Mode #1589

Open 7leman7 opened 9 months ago

7leman7 commented 9 months ago

This is potentially not a bug, but just a lack of understanding on my side how the climate entity is to be properly set up - there seems to be no documentation on this in the integration description.

I'm trying to set up a heated floor thermostat (Teploluxe MCS350 Tuya), all good and I was able to identify the DPs I need incl.:

I understand the on/off DP is generally supposed to be used as climate entity ID, hence I use DP 111 as entity ID. I set "HAVC Mode DP (Optional)" as DP 2 and I added respective values (only "manual" = HVAC_MODE_HEAT and "program" = HVAC_MODE_AUTO, I do not plan on using antifreeze mode) to climate.py in root/homeassistant/custom_components/localtuya/.

Now I have in Home Assistant climate entity mode selector working OK (i.e. I may switch between Heat and Auto modes correctly), but when I switch the climate entity mode selector to "Off" it turns off the thermostat and does not turn it on when switching back to "Heat" or "Auto". My guess it's due to DP 111 needed to turn it on and mode selector only changing DP 2. Is there a way to set up this correctly so that it turns off when mode is set to "Off" in Home Assistant but changing mode to "Auto" or "Heat" not only changes the mode but also turns thermostat on?

7leman7 commented 9 months ago

I was able to identify (I think, I know nothing about Python) the part which covers the change in HVAC modes and I changed True / False for the main DP (which is also entity ID) to "ON" and "OFF" as these are the values for my themostat.

I also changed "not self._state in the second if to hvac_mode != HVAC_MODE_OFF but maybe this was not necessary. The result is as follows:

async def async_set_hvac_mode(self, hvac_mode):
    """Set new target operation mode."""
    if hvac_mode == HVAC_MODE_OFF:
        await self._device.set_dp("OFF", self._dp_id)
        return
    if hvac_mode != HVAC_MODE_OFF and self._conf_hvac_mode_dp != self._dp_id:
        await self._device.set_dp("ON", self._dp_id)
        # Some thermostats need a small wait before sending another update
        await asyncio.sleep(MODE_WAIT)
    await self._device.set_dp(
        self._conf_hvac_mode_set[hvac_mode], self._conf_hvac_mode_dp
    )

async def async_turn_on(self) -> None:
    """Turn the entity on."""
    await self._device.set_dp("ON", self._dp_id)

async def async_turn_off(self) -> None:
    """Turn the entity off."""
    await self._device.set_dp("OFF", self._dp_id)

My thermostat entity now turns off the thermostat when selecting "Off" mode in entity card and turns on the thermostat when selecting onthe mode ("Auto" or "Heat"). I can see it in the Tuya app. But the entity state in Home Assiatnt does not change to "off". The side effect is that entity state stays in Heat or Auto and to turn it on you need to select other mode.

Now the question is, apparently "async def async_turn_off(self)" part does not work in my case. Why is that? Is it supposed to work on climate entity at all (in HA docs I'm not seeing this as allowed method for climate entity)?

7leman7 commented 8 months ago

I'll just leave it here in case it will help someone. So the issue is that current climate.py code assumes that self._dp_id (which is the DP which is used as a climate entity ID and is supposed to indicate whether thermostat is on or off) has the values "true" or "false". Setup flow does not allow to change this in a way it allows to change the other DPs value sets. In my specific case DP which indicates the on/off state of thermostat (DP 111) has values "ON" or "OFF".

To make it work in my case I had to change another part of the code as follows:

def status_updated(self):
    """Device status was updated."""
    if self.dps(self._dp_id) == "ON":
        self._state = True
    else:
        self._state = False

I'll also leave the datapoints for Teploluxe MCS350 Tuya I was able to found as (a bit surprisingly) this seems to make it one of the most completely integrateable heated floor thermostats for Home Assistant.

DP# - Meaning 111 - overall thermostat status ("ON"/"OFF"), should be used as entity ID 2 - HVAC mode ("antifreeze"/"manual"/"program"), I only used "manual"/"program" 16 - target floor temp (in degrees C) 22 - consumed power (in kW) - seems to be an accumulated consumption value based on relay on time and power of load value 24 - actual floor temp (in degrees C) 27 - ? "temp_correction" or "fault" (did not yet figure that out) 43 - currently used temp senso ("air"/"flor"/"booth") 45 - ? "temp_correction" or "fault" (did not yet figure that out) 102 - actual air temp (in degrees C) 103 - set max air temp (in degrees C) 105 - power of load (in watts) 106 - relay on/off status ("True"/"False") 107 - self training ("ON"/"OFF") 108 - type of external temp sensor (mine is "6_8k") 110 - window check (ON/OFF) 112 - target temp in atifreeze mode (in degrees C)