rospogrigio / localtuya

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

Bulb becomes unavailable immediately after setup #308

Open riddledaxis opened 3 years ago

riddledaxis commented 3 years ago

I have a Calex Holland Tuya wifi bulb that I have configured in the TuyaSmart app and then grabbed the Key and ID via the Tuya API.

I have then configured the bulb through the HA UI (so I know that there is communication), but it then becomes unavailable in HA straight away. I have also tried configuring the bulb through configuration.yaml and rebooting and the same thing happens.

The logs are set to debug and it shows the bulb being set up and then nothing else, no errors.

2021-01-11 19:05:58 DEBUG (MainThread) [custom_components.localtuya.light] [bf3...u6r] Adding light.jacks_main_light with configuration: {'platform': 'light', 'friendly_name': 'Jacks main light', 'id': 20, 'color_mode': 21, 'brightness': 22, 'color_temp': 23, 'color': 24, 'brightness_lower': 29, 'brightness_upper': 1000, 'color_temp_min_kelvin': 2700, 'color_temp_max_kelvin': 6500, 'scene': 25, 'music_mode': False}

2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Started heartbeat loop
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Sending command heartbeat (device type: type_0a)
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Send payload: b'{}'
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Waiting for sequence number -100
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Sending command status (device type: type_0a)
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Send payload: b'{"gwId":"bf3a33eb890e30f84alu6r","devId":"bf3a33eb890e30f84alu6r"}'
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Waiting for sequence number 1
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Got heartbeat response
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Decrypted payload: {}
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=0, payload=b'\x8aW\xf2\xda\xf0\r\x85\xa4\x87\xd0\xd8`\xca\xf2\x94AH4\xa7\x91\x1a\x13\x819P\x8c\xad\x10S\xe7\xd6\x8b\xf0\xed^\xa0\x06\xb8\xdeas\x11.\xa6\xab{$D\xe9\xe0\x1a\xbcJA\xef5:\xa3\xcck\x0c$a\x06\xd0\xe5\xd62\xef\xc0(\x13E\xb9\xa9\xf9\xabT_\xae\xbeS\xc9fB\xb2\x17\xfc\xea\tIB"\xf2T\x0fo,he\xd2\x9f\xb9\xb2O\x19${]<\x01\xd3\xb0\x95._\xf4\x10\xae_\xa9+\xbc\xc2\x98(\x9a\xda*\xa17\xe0\x90\xc5\x12\xe7vY\xbez\xa6\xe1\xb5\xd4', crc=2642979998)
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Dispatching sequence number 1
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Decrypted payload: {"dps":{"20":false,"21":"colour","22":1000,"23":1000,"24":"000003e803e8","25":"000e0d0000000000000000c80000","26":0,"101":true}}
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Closing connection
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Stopped heartbeat loop
2021-01-11 19:07:41 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Connection lost: None

2021-01-11 19:08:50 DEBUG (MainThread) [custom_components.localtuya.light] [bf3...u6r] Adding light.jacks_main_light with configuration: {'brightness_lower': 29, 'brightness_upper': 1000, 'color_temp_min_kelvin': 2700, 'color_temp_max_kelvin': 6500, 'music_mode': False, 'friendly_name': 'jack main light', 'id': 20, 'color_mode': 21, 'brightness': 22, 'color_temp': 23, 'color': 24, 'scene': 25, 'platform': 'light'}

tuya1 tuya 2

The bulb is on a different Unifi vlan and I have configured the firewall to allow all ports to communicate with the HA server on my LAN.

My HA server is running HassOS 5.10, HA 2021.1.1, HA Supervisor 2020.12.7 and Local Tuya version 3.2.0 with Firmware v3.3

The bulb is working fine in the TuyaSmart app, just not through local tuya in HA

Any help is gratefully received

misterk99 commented 3 years ago

I think I have the exact same issue, lamp is working in tuya app but not in home assistant with local tuya. also added through YAML

localtuya:
  - host: 192.168.86.51
    device_id: 01242673c44f33ca7484
    local_key: 58d75862371cb3ce
    friendly_name: Spot2-Local
    protocol_version: "3.3"
    entities:
      - platform: light
        friendly_name: Spot2-Local
        id: 4 # Usually 1 or 20

also not sure if ID is supposed to be incremental so next device is 5 or if ID is linked to type of device like 4 is a lamp

ultratoto14 commented 3 years ago

ID is the switch on/off of the light so depending on your DPs but usually for bulb it's 1 or 20. @riddledaxis Do you use at the same time the tuya app, if it's the case, try to close it.

ultratoto14 commented 3 years ago

@misterk99 can you do a DPS Dump

misterk99 commented 3 years ago

@misterk99 can you do a DPS Dump

Not sure how to run that command? Is there a command line in home assistant?

ultratoto14 commented 3 years ago

Perhaps you can just try to add your device through config flow and see what localtuya is proposing.

misterk99 commented 3 years ago

Perhaps you can just try to add your device through config flow and see what localtuya is proposing.

that's what I did at first, this also didn't work. Does it matter that I have the lights still in the tuya app? or do I need to link them with the tuya api? since that removes them from the app.

ultratoto14 commented 3 years ago

@misterk99 do you block in any way the connection to the cloud ?do you use different vlans ?

ultratoto14 commented 3 years ago

The 3.2 version is using the discovery packet to initiate the connection. If localtuya is not receiving it for any reason, it will not try to connect to your bulb. You can then check with previous version of the plugin or with the content of the PR #288

misterk99 commented 3 years ago

The 3.2 version is using the discovery packet to initiate the connection. If localtuya is not receiving it for any reason, it will not try to connect to your bulb. You can then check with previous version of the plugin or with the content of the PR #288

Not that I am aware of or did myself? I'm actually using 3.3 according to the YAML, should I try 3.1? I got the command line running as well, any specific place I need to extract the tuya debug folder on the Pi?

riddledaxis commented 3 years ago

@ultratoto14 I closed the app on my iPhone and also enabled flight mode to ensure there was no communication with Tuya or the bulb.

I rebooted the HA server and the bulb was still showing as unavailable. I then removed the light bulb from my HA config rebooted to ensure it was gone and then re-added the bulb through integration>localtuya. The bulb was created in HA successfully and it immediately became unavailable.

I forgot to add yesterday that I had reinstalled localtuya through HACS and selected the Master package as this seemed to have been recommended for other peoples issues.

These are today's log files from HA

2021-01-12 18:39:56 DEBUG (MainThread) [custom_components.localtuya.discovery] Listening to broadcasts on UDP port 6666 and 6667

2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Started heartbeat loop
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Sending command heartbeat (device type: type_0a)
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Send payload: b'{}'
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Waiting for sequence number -100
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Sending command status (device type: type_0a)
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Send payload: b'{"gwId":"bf3a33eb890e30f84alu6r","devId":"bf3a33eb890e30f84alu6r"}'
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Waiting for sequence number 1
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Dispatching message TuyaMessage(seqno=0, cmd=9, retcode=0, payload=b'', crc=2958142211)
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Got heartbeat response
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Decrypted payload: {}
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Dispatching message TuyaMessage(seqno=1, cmd=10, retcode=0, payload=b'\x8aW\xf2\xda\xf0\r\x85\xa4\x87\xd0\xd8`\xca\xf2\x94AS\xe1\xbcg\xc2!\xff\x900\xea\x90\x01w\xaf\x89\xe8\xd2\xfe\xdb\xe93Mz\x15\xf1\xc8Lh&\x95\xd0\xad\xfe}\xad\xdb\xe0W\xa8;\xc4\x16\xd1\'\x82L@4\x1b\xb1\x8b\xd4\xd0OC\xe5\xb2y~J\xdb\xfa,M!\xfd\xf1+T\xa0#YizY\xb42\x92\xef\xfb\x06\x92\xb5\'\xd7)\x81\xfb\x10"\x18^P\xe3|\xd1!\xfa4\xbb\xfef\xa4\xdc\x17\xd8\x0e\xc2\xca\xdb\x7f\xa8', crc=1737167095)
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Dispatching sequence number 1
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Decrypted payload: {"dps":{"20":false,"21":"scene","22":1000,"23":1000,"24":"012003e803e8","25":"000e0d0000000000000000c80000","26":0,"101":true}}
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Closing connection
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Stopped heartbeat loop
2021-01-12 18:40:52 DEBUG (MainThread) [custom_components.localtuya.pytuya] [bf3...u6r] Connection lost: None

2021-01-12 18:43:25 INFO (MainThread) [homeassistant.setup] Setting up localtuya
2021-01-12 18:43:25 DEBUG (MainThread) [custom_components.localtuya.discovery] Listening to broadcasts on UDP port 6666 and 6667
2021-01-12 18:43:25 INFO (MainThread) [homeassistant.setup] Setup of domain localtuya took 0.0 seconds
2021-01-12 18:43:25 INFO (MainThread) [homeassistant.components.light] Setting up light.localtuya
2021-01-12 18:43:25 INFO (MainThread) [homeassistant.helpers.entity_registry] Registered new light.localtuya entity: light.jacks_main_light
2021-01-12 18:43:25 DEBUG (MainThread) [custom_components.localtuya.light] [bf3...u6r] Adding light.jacks_main_light with configuration: {'brightness_lower': 29, 'brightness_upper': 1000, 'color_temp_min_kelvin': 2700, 'color_temp_max_kelvin': 6500, 'music_mode': False, 'friendly_name': 'jacks main light', 'id': 20, 'scene': 25, 'color': 24, 'brightness': 22, 'color_temp': 23, 'color_mode': 21, 'platform': 'light'}
ultratoto14 commented 3 years ago

Not that I am aware of or did myself? I'm actually using 3.3 according to the YAML, should I try 3.1? I got the command line running as well, any specific place I need to extract the tuya debug folder on the Pi?

I'm talking about 3.2.0 version of localtuya 😄

ultratoto14 commented 3 years ago

@riddledaxis it seems that you do not receive any UDP discovery packet, can you try the PR #288 ?

riddledaxis commented 3 years ago

@ultratoto14 I tried updating commony.py and init.py as per PR #288, but after a reboot, I still had the same issue when I added the bulb.

So I bit the bullet and moved the bulb to my LAN and Local Tuya picked it up through discovery straight away, it is now connected and working.

I have created a custom firewall rule for it to block it from DNS and the Internet and everything seems to be working well 🤞

misterk99 commented 3 years ago

@ultratoto14 I tried updating commony.py and init.py as per PR #288, but after a reboot, I still had the same issue when I added the bulb.

So I bit the bullet and moved the bulb to my LAN and Local Tuya picked it up through discovery straight away, it is now connected and working.

I have created a custom firewall rule for it to block it from DNS and the Internet and everything seems to be working well 🤞

How did you add them to the LAN? Removed them from the app and linked them with the Tuya CLI api? Do you still see them in the app?

riddledaxis commented 3 years ago

@misterk99 I removed the bulb from the app and factory reset it (from within the app) and then made sure that my phone was on my 2.4GHz wifi for my LAN (I use Unifi infrastructure including AP's) and then reconnected through the app.

Once I had done this I used the tuya-CLI wizard to get the local id and key.

As the bulb was now on my LAN, when I went to add it through Integrations>Local Tuya it was picked up straight away by discovery so I didn't have to add it manually (clicking on the ...). Only adding the local key and then configuring the light.

The bulb now works through the app and HA. Although I only want to use it through HA

misterk99 commented 3 years ago

@misterk99 I removed the bulb from the app and factory reset it (from within the app) and then made sure that my phone was on my 2.4GHz wifi for my LAN (I use Unifi infrastructure including AP's) and then reconnected through the app.

Once I had done this I used the tuya-CLI wizard to get the local id and key.

As the bulb was now on my LAN, when I went to add it through Integrations>Local Tuya it was picked up straight away by discovery so I didn't have to add it manually (clicking on the ...). Only adding the local key and then configuring the light.

The bulb now works through the app and HA. Although I only want to use it through HA

Sorry what I don't understand is how did you add it to your LAN? Wasn't it before? Do you mean your bulb is no longer part of the Tuya cloud? My bulbs are also connected to my wifi but still no luck. I got to the part with the CLI wizard and all it just kept being unavailable in HA so I'm curious what you exactly did that fixed your problem.

riddledaxis commented 3 years ago

@misterk99 I have a Ubiquiti home network, which includes the USG router (includes enterprise firewall) an 8 port L3 switch and 2 x Unifi AC Pro AP's.

I have 4 wifi networks, which includes 2 SSID's for my LAN 10.0.2. (1 is 2.4GHz only) and 2 Vlans 1 for IoT 10.0.20. (no LAN access apart for HA but can access the internet) and 1 for kit that I don't want to access anything 10.0.30.* (internet or LAN)

So in my original post the bulb was on the IoT network 10.0.20.* and was able to talk to my HA server on the LAN but local tuya discovery on UDP 6666 & 6667 wasn't reaching the server from the bulb even though I had a firewall rule that allowed it.

The only way to get the local discovery working was to move the bulb from the IoT network to the LAN and then put a firewall rule in place to block access to the internet and DNS.

Hope that helps

jfparis commented 3 years ago

Hi @riddledaxis @ultratoto14

I have a similar setup to yours.

All my IOT devices on a separate (untrusted vlan where devices have limited access to the net and non to my trusted) and the rest of my machines (included the Rpi that run HASS) on another (trusted) vlan. This creates an issue (the one you faced) as the broadcast packets issued by the tuya devices don't reach the HASS machine.

I believe that some big iron firewalls allow to write rules to forward the broadcast packets. Mine (tomato based) does not.

So my plan was as follow

I have already written the script and was planning to test it before sharing it with the group. However, because Covid we won't be flying back to UK for a couple of months at least. HASS is running my home for the moment so won't be messing around /testing

As it could be useful to you (and maybe other), I have put the script here. Pretty simple (but not tested)

Hope this helps