Tuya switch

You could use an Android emulator with configurable root on your PC, e.g. NOX Player.

hi oliver, I have got mine working too but have you solved the status delay issue, thanks for contributing in advance!

I think it is being solved Tuya switch

Всем привет.

Я только начал изучение Hass.io и у меня есть вопросы по интеграции устройств Tuya в HA. У меня есть настенные и встраиваемые диммеры, настенные и встраиваемые выключатели (1-,2-,3-канальные). Также скоро буду иметь выключатель на ДИН-рейку, измеритель энергии на ДИН-рейку и Smart вилки.

Какой алгоритм добавления этих устройств, кроме записи в файле конфигурации?

tuya:
   username: YOUR_TUYA_USERNAME
   password: YOUR_TUYA_PASSWORD
   country_code: YOUR_ACCOUNT_COUNTRYCODE

Что мне нужно доустановить в ХА (пользовательские компоненты ?), чтобы интегрировать эти устройства? Будут ли работать диммеры?

Готов для тестирования устройств.

Спасибо всем заранее.


Hello.

I just started studying Hass.io and I have questions about integrating Tuya devices in HA.

I have several wall and built-in Dimmers, several wall and built-in Switches (1-, 2-, 3-channel). Also, I will soon have a DIN rail switch, a DIN rail Energy meter and several Smart plugs.

What is the algorithm for adding these devices, besides writing to the configuration file?

tuya:
   username: YOUR_TUYA_USERNAME
   password: YOUR_TUYA_PASSWORD
   country_code: YOUR_ACCOUNT_COUNTRYCODE

What do I need to install in HA (custom components ?) in order to integrate these devices?

Ready for device testing.

Thanks to everyone in advance.

I use this code:

switch:
  - platform: localtuya
    host: 192.168.1.50
    local_key: 3fbb92a00fd12198
    device_id: bfccec00e0015e7005sezw
    name: switch_two
#    protocol_version: 3.3
    id: 2

From this: https://github.com/NameLessJedi/localtuya-homeassistant

When I use the: protocol_version: 3.x

Invalid config for [switch.localtuya]: [protocol_version] is an invalid option for [switch.localtuya]. Check: switch.localtuya->protocol_version. (See ?, line ?).

When I use without protocol_version: 3.x


Error while setting up localtuya platform for switch
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 179, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/localtuya/switch.py", line 86, in setup_platform
    config.get(CONF_VOLTAGE)
  File "/config/custom_components/localtuya/switch.py", line 142, in __init__
    self._status = self._device.status()
  File "/config/custom_components/localtuya/switch.py", line 123, in status
    self._cached_status = self.__get_status()
  File "/config/custom_components/localtuya/switch.py", line 105, in __get_status
    status = self._device.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 281, in status
    data = self._send_receive(payload)
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 177, in _send_receive
    s.connect((self.address, self.port))
socket.timeout: timed out

Can you help me?

try

protocol_version: 3.1

There are two protocol options only: 3.1 or 3.3. You use 3.x which is not 3.1 or 3.3 :slight_smile:

If I add this:

switch:
  - platform: localtuya
    host: 192.168.1.50
    local_key: 3fb353443512198
    device_id: bfcce5435345e7965sezw
    protocol_version: 3.1
    switches:
      switch1:
        friendly_name: TUYA1
        id: 1
      switch2:
        friendly_name: TUYA2
        id: 2

I have:
Invalid config for [switch.localtuya]: [protocol_version] is an invalid option for [switch.localtuya]. Check: switch.localtuya->protocol_version. (See ?, line ?).

When I wrote the 3.x I meant there was no difference between 3.1 or 3.3 :frowning:

According to this :

You have a protocol 3.2 device, which is not supported by localtuya

I have this product:

on Realtek WR4 chipset not ESP and I cant change the software :confused:

I feel your pain. I have 2 ceiling lights that use the realtek chip. Tried localtuya and agentk, can’t get them to work locally.

There are several versions of this component. Your version ( https://github.com/NameLessJedi/localtuya-homeassistant) does not have different protocols functionality. If you open the file “switch.py” in your component - there is no mentioning of “protocol”.
I have another version of the component with the possibility to set protocols. However, it works with 1 gang devices only. So, guess, it is not suitable for you anyway.
If you have any knowledge of Python, you can try to join both components in one.

Perhaps you have the wrong config.

If you use a multi-channel switch, your config should look like this:

# Tuya custom integration
switch:
  - platform: localtuya
    host: 192.168.1.50
    local_key: 3fbb92a00fd12198
    device_id: bfccec00e0015e7005sezw
    protocol_version: 3.1
    name: TUYA1
    id: 1
  - platform: localtuya
    host: 192.168.1.95
    local_key: 3fbb92a00fd12198
    device_id: bfccec00e0015e7005sezw
    protocol_version: 3.1
    name: TUYA2
    id: 2
  - platform: localtuya
    host: 192.168.1.50
    local_key: 3fbb92a00fd12198
    device_id: bfccec00e0015e7005sezw
    protocol_version: 3.1
    name: TUYA3
    id: 3
  - platform: localtuya
    host: 192.168.1.95
    local_key: 3fbb92a00fd12198
    device_id: bfccec00e0015e7005sezw
    protocol_version: 3.1
    name: TUYA4
    id: 4

if you ID is really 1, 2, 3, 4

But I think you need to use protocol 3.3 in config.

protocol_version: 3.3

I have several of these switches from this series: 1-, 2- and 3-channel. The 1-channel switch accurately uses protocol 3.3.

P.S. And use this integration (I use it): https://github.com/stast1/localtuya

Я написал выше, его софт не имеет функциональности протоколов. Ему это бесполезно.

Добавил ссылку, с которой должно работать.

I am using the https://github.com/stast1/localtuya component and my home-assistant log shows the following error:

2020-03-27 22:09:27 ERROR (MainThread) [homeassistant.components.switch] Error while setting up localtuya platform for switch
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 179, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/localtuya/switch.py", line 68, in setup_platform
    config.get(CONF_VOLTAGE)
  File "/config/custom_components/localtuya/switch.py", line 129, in __init__
    self._status = self._device.status()
  File "/config/custom_components/localtuya/switch.py", line 111, in status
    self._cached_status = self.__get_status()
  File "/config/custom_components/localtuya/switch.py", line 87, in __get_status
    status = self._device.status()
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 311, in status
    result = json.loads(result)
  File "/usr/local/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

My configuration.yaml file has the following:

switch:
  - platform: localtuya
    host: 192.168.0.150
    local_key: 4981fd008cbcb29d
    device_id: bf8c8a6fbd1cb5d4dbhmay
    name: tuya_01
    protocol_version: 3.3

That is the plug that I am trying to control: https://www.aliexpress.com/item/4000060646437.html?spm=a2g0s.9042311.0.0.27424c4d2Umcvc

When I inspect the switch’s firmware from the tuya app, it says:
WiFi Module: 1.4.2
MCU: 1.4.2

Tried with protocol_version 3.1 - different error:

2020-03-27 22:17:07 ERROR (MainThread) [homeassistant.components.switch] Error while setting up localtuya platform for switch
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 179, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/localtuya/switch.py", line 68, in setup_platform
    config.get(CONF_VOLTAGE)
  File "/config/custom_components/localtuya/switch.py", line 130, in __init__
    self._state = self._status['dps'][self._switch_id]
TypeError: byte indices must be integers or slices, not str

What could be the reason?

Localtuya seems not to support light colored bulbs… What shall I do?

I already fixed the light.py official tuya plugin…

My decision was to flash them with tasmota over the air. Now they are working fine!

I don’t want to flas the firmware… localtuya seems to be a good solution without HW impact…