rospogrigio / localtuya

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

Changing fan speed cause device to be unavailable #1495

Open tekdru opened 9 months ago

tekdru commented 9 months ago

The problem

Device: Ceiling Fan Dimmer Switch DS03 Firmware: 3.3

The on/off function works fine for both the light and fan but whenever I try to change the fan speed, both fan and light becomes "unavailable".

Environment

I haven't tried Tuya Cloud Component, TinyTuya or any previous version of local Tuya.

Steps to reproduce

DP dump

Provide Home Assistant traceback/logs

2023-09-14 14:18:14.152 ERROR (MainThread) [custom_components.localtuya.common] [720...ae9] Failed to set DP 3 to 1
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 490, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 387, in acquire
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 321, in set_dp
    await self._interface.set_dp(state, dp_index)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 842, in set_dp
    return await self.exchange(CONTROL, {str(dp_index): value})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 769, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 492, in wait_for
    raise exceptions.TimeoutError() from exc
TimeoutError
2023-09-14 14:18:16.772 WARNING (MainThread) [custom_components.localtuya.common] [720...ae9] Disconnected - waiting for discovery broadcast
2023-09-14 14:19:04.903 ERROR (MainThread) [custom_components.localtuya.common] [720...ae9] Failed to set DP 3 to 1
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 490, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 387, in acquire
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 321, in set_dp
    await self._interface.set_dp(state, dp_index)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 842, in set_dp
    return await self.exchange(CONTROL, {str(dp_index): value})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 769, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 492, in wait_for
    raise exceptions.TimeoutError() from exc
TimeoutError
2023-09-14 14:19:09.635 WARNING (MainThread) [custom_components.localtuya.common] [720...ae9] Disconnected - waiting for discovery broadcast
2023-09-14 14:20:04.080 ERROR (MainThread) [custom_components.localtuya.common] [720...ae9] Failed to set DP 3 to 1
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 490, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 387, in acquire
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 321, in set_dp
    await self._interface.set_dp(state, dp_index)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 842, in set_dp
    return await self.exchange(CONTROL, {str(dp_index): value})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 769, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 492, in wait_for
    raise exceptions.TimeoutError() from exc
TimeoutError
2023-09-14 14:20:06.979 WARNING (MainThread) [custom_components.localtuya.common] [720...ae9] Disconnected - waiting for discovery broadcast
2023-09-14 14:21:03.287 ERROR (MainThread) [custom_components.localtuya.common] [720...ae9] Failed to set DP 3 to 1
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 490, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 387, in acquire
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 321, in set_dp
    await self._interface.set_dp(state, dp_index)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 842, in set_dp
    return await self.exchange(CONTROL, {str(dp_index): value})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 769, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 492, in wait_for
    raise exceptions.TimeoutError() from exc
TimeoutError
2023-09-14 14:21:06.372 WARNING (MainThread) [custom_components.localtuya.common] [720...ae9] Disconnected - waiting for discovery broadcast
2023-09-14 14:29:05.367 ERROR (MainThread) [custom_components.localtuya.common] [720...ae9] Failed to set DP 3 to 4
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 490, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 387, in acquire
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 321, in set_dp
    await self._interface.set_dp(state, dp_index)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 842, in set_dp
    return await self.exchange(CONTROL, {str(dp_index): value})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 769, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 492, in wait_for
    raise exceptions.TimeoutError() from exc
TimeoutError
2023-09-14 14:29:07.312 ERROR (MainThread) [custom_components.localtuya.common] [720...ae9] Failed to set DP 1 to True
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 490, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 387, in acquire
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 321, in set_dp
    await self._interface.set_dp(state, dp_index)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 842, in set_dp
    return await self.exchange(CONTROL, {str(dp_index): value})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 769, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 492, in wait_for
    raise exceptions.TimeoutError() from exc
TimeoutError
2023-09-14 14:29:08.142 WARNING (MainThread) [custom_components.localtuya.common] [720...ae9] Disconnected - waiting for discovery broadcast
2023-09-14 14:29:10.874 ERROR (MainThread) [custom_components.localtuya.common] [720...ae9] Failed to set DP 1 to True
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 490, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 387, in acquire
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/localtuya/common.py", line 321, in set_dp
    await self._interface.set_dp(state, dp_index)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 842, in set_dp
    return await self.exchange(CONTROL, {str(dp_index): value})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 769, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 450, in wait_for
    await asyncio.wait_for(self.listeners[seqno].acquire(), timeout=timeout)
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 492, in wait_for
    raise exceptions.TimeoutError() from exc
TimeoutError

Additional information

Screenshot 2023-09-14 at 2 56 42 PM Screenshot 2023-09-14 at 3 00 21 PM Screenshot 2023-09-14 at 3 00 28 PM Screenshot 2023-09-14 at 3 00 37 PM Screenshot 2023-09-14 at 3 00 42 PM Screenshot 2023-09-14 at 3 54 38 PM Screenshot 2023-09-14 at 4 09 00 PM Screenshot 2023-09-14 at 4 09 22 PM

grzesw72 commented 5 months ago

my fix based on https://community.home-assistant.io/t/local-tuya-fan-controller/306985/45.

fan.py line 133+: function definition async_set_percentage (original)

    async def async_set_percentage(self, percentage):
        """Set the speed of the fan."""
        _LOGGER.debug("Fan async_set_percentage: %s", percentage)

        if percentage is not None:
            if percentage == 0:
                return await self.async_turn_off()
            if not self.is_on:
                await self.async_turn_on()
            if self._use_ordered_list:
                await self._device.set_dp(
                    self._dps_type(
                        percentage_to_ordered_list_item(self._ordered_list, percentage)
                    ),
                    self._config.get(CONF_FAN_SPEED_CONTROL),
                )
                _LOGGER.debug(
                    "Fan async_set_percentage: %s > %s",
                    percentage,
                    percentage_to_ordered_list_item(self._ordered_list, percentage),
                )

            else:
                await self._device.set_dp(
                    self._dps_type(
                        math.ceil(
                            percentage_to_ranged_value(self._speed_range, percentage)
                        )
                    ),
                    self._config.get(CONF_FAN_SPEED_CONTROL),
                )
                _LOGGER.debug(
                    "Fan async_set_percentage: %s > %s",
                    percentage,
                    percentage_to_ranged_value(self._speed_range, percentage),
                )
            self.schedule_update_ha_state()

change to

    async def async_set_percentage(self, percentage):
        """Set the speed of the fan."""
        _LOGGER.debug("Fan async_set_percentage: %s", percentage)

        if percentage is not None:
            if percentage == 0:
                return await self.async_turn_off()
            if not self.is_on:
                await self.async_turn_on()
            if self._use_ordered_list:
                await self._device.set_dp(
#fix https://community.home-assistant.io/t/local-tuya-fan-controller/306985/45
                    percentage_to_ordered_list_item(self._ordered_list, percentage)
                    ,
                    self._config.get(CONF_FAN_SPEED_CONTROL),
                )
                _LOGGER.debug(
                    "Fan async_set_percentage: %s > %s",
                    percentage,
                    percentage_to_ordered_list_item(self._ordered_list, percentage),
                )

            else:
                await self._device.set_dp(
#fix https://community.home-assistant.io/t/local-tuya-fan-controller/306985/45
                    math.ceil(
                        percentage_to_ranged_value(self._speed_range, percentage)
                    ),

                    self._config.get(CONF_FAN_SPEED_CONTROL),
                )
                _LOGGER.debug(
                    "Fan async_set_percentage: %s > %s",
                    percentage,
                    percentage_to_ranged_value(self._speed_range, percentage),
                )
            self.schedule_update_ha_state()
tekdru commented 4 months ago

Thanks @grzesw72,

I just try using your code but I'm still getting the same "Unavailable" message after trying to change fan speed. Am I missing something else?

grzesw72 commented 4 months ago

Hello. I'm moved to fork -> https://github.com/xZetsubou/hass-localtuya

śr., 21 lut 2024 o 18:26 tekdru @.***> napisał(a):

Thanks @grzesw72 https://github.com/grzesw72,

I just try using your code but I'm still getting the same "Unavailable" message after trying to change fan speed. Am I missing something else?

— Reply to this email directly, view it on GitHub https://github.com/rospogrigio/localtuya/issues/1495#issuecomment-1957394546, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJGYC7VDUWNNVD6Y6APDMXTYUYU2TAVCNFSM6AAAAAA4ZCW32WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJXGM4TINJUGY . You are receiving this because you were mentioned.Message ID: @.***>