Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing support for Zigbee devices #1493

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Conversation

rospogrigio
Copy link
Owner

@rospogrigio rospogrigio commented Sep 14, 2023

Introducing support for Zigbee devices.
Devices are automatically detected if the Cloud APIs are used.
These devices must be configured setting the dedicated "sub_device" protocol.

PLEASE NOTE: the gateway can be added/configured only if it provides any entity, so it might not be possible to add it, it depends on the specific model.

@witokondoria
Copy link

Been testing (with and without cloud) this PR and I always get a "Connection to device succeeded but no datapoints found". After manually introducing them, they get a -1 value.

On a side note, DPS gets listed with its correct value on tinytuya
Finally, this PR will also provide support for #1492 (which is my device)

@rospogrigio
Copy link
Owner Author

Been testing (with and without cloud) this PR and I always get a "Connection to device succeeded but no datapoints found". After manually introducing them, they get a -1 value.

You should provide some logging please. Also, are you trying to add the device, right? You cannot add the gateway, you should find the attached devices, with the same IP address as the gateway. Let me know

@witokondoria
Copy link

Only trying to add the device. It gets added properly, only failing to fetch the datapoints. Find attached the diagnostics.

On top of that, the tuyadebug.tgz might not contain zigbee-related code

config_entry-localtuya-8bb82a9fcfdd8088dde1bc8f31fba15d.json.txt

@rospogrigio
Copy link
Owner Author

You should check the "Enable debug" button, and post the relevant parts you find in home-assistant.log .
And yes, tuyadebug.tgz is not updated yet, thanks for reminding...

@witokondoria
Copy link

Got this:

[bfe...qqm] Failed to get status:
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/pytuya/__init__.py", line 886, in detect_available_dps
    data = await self.status()
           ^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 818, in status
    status = await self.exchange(DP_QUERY)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 792, in exchange
    msg = await self.dispatcher.wait_for(seqno, payload.cmd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/localtuya/pytuya/__init__.py", line 469, 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

@rospogrigio
Copy link
Owner Author

[bfe...qqm] Failed to get status:

...

TimeoutError

Looks like the device is not online/reachable... is it working with the Tuya integration, or with the app?

@witokondoria
Copy link

witokondoria commented Sep 15, 2023

Yes, works when only one consumer access to it. Tinytuya dumps:

{'dps': {'1': False, '2': False, '101': '19:29:21', '102': '19:29:28', '103': 0, '104': 600, '105': 0, '106': 0, '107': 'sunny', '108': 100, '110': 322, '111': 0, '112': '1', '114': '00:00:07,0'}, 'cid': 'XXX', 'device': OutletDevice( 'ZZZqqm', address=None, local_key='', dev_type='default', connection_timeout=5, version=3.4, persist=False, cid='YYY', parent='...jwst', children={} )}

@rospogrigio
Copy link
Owner Author

Could be a device that doesn't allow multiple connections, can you try closing/disabling the app or put other devices in Flight mode maybe?

@witokondoria
Copy link

Same timeout on flight mode.

On tinytuya, I'm specifying the version on the gateway (if it helps):

gw = tinytuya.Device( 'ZZZ', address="10.0.0.141", local_key="A_KEY", persist=True, version=3.4 )
zigbee1 = tinytuya.OutletDevice( 'ZZqqm', cid='XXX', parent=gw )

@rospogrigio
Copy link
Owner Author

rospogrigio commented Sep 15, 2023

OK, it looks like the gateway is using 3.4 protocol, mine is 3.3 and it is the only one I can test.
Can you please provide the full debug output of tinytuya?
Also the output of tuya-cli would be very helpful:

DEBUG=TuyAPI tuya-cli get --ip 10.0.0.141 --id <device_id> --cid <device_cid> --key <local_key> --protocol-version 3.4

@witokondoria
Copy link

This is the debug output from tinytuya. The one from tuya-cli will take some more time
DEBUG_TinyTuya.log

@vanek-865
Copy link

@rospogrigio
re-pairing the device did not help, but I managed to find out a few details, before that a Zigbee hub was added, if you remove it, the Aubess Smart Switch z1-gang relay starts working, but then the hub does not work.
can this be fixed? There are no problems with other devices on this hub

@vanek-865
Copy link

Additionally, after adding the hub and relay, I started adding motion sensors, one was added without problems, the second was added, but became unavailable, and when the second sensor detected motion, I was shown that the relay was turned on
It seems that there is a failure when receiving information from devices

@rospogrigio
Copy link
Owner Author

rospogrigio commented Sep 21, 2023

OK I admit that I have tested with my environment with only one gateway and one device, maybe if you add more devices there are problems, I will try in the next days. It's strange, however. Please keep trying and tell me any progress should you make.
Edit: OK I think I know what's happening: we are opening multiple connections to the gateway (the host), so only one can succeed and the others would fail. I think I need to re-think everything in this PR...

@vanek-865
Copy link

If I can help with anything, tell me, I'll try

@rospogrigio
Copy link
Owner Author

Of course, give me some days to refactor the code a bit, I have a couple of ideas. Thank you in the meantime!

@rospogrigio
Copy link
Owner Author

@witokondoria and @vanek-865 I have been working on supporting multiple devices and I am at a good stage, but there is something weird going on with my gateway so, since you are the only ones who provided feedback so far, I need to ask you if you experience the same behavior too.
Basically, what happens is that I cannot recognize when a subdevice goes offline, since the gateway seems to cache its status and report it even when it gets powered off. The only way to un-cache the status is to power-off the gateway itself.
So I need you to make this test:

  1. launch the tuyadebug test, and check that you can retrieve the subdevice status
  2. power-off the subdevice
  3. launch the tuyadebug test: here is where I would like to know whether you still get the subdevice status, or not
  4. in case you still get the subdevice status, power off and on the gateway, and re-launch the test: please confirm that now you are not able to get the subdevice status

Can you please test the above and let me know the outcome? Thank you!

@vanek-865
Copy link

vanek-865 commented Sep 28, 2023

@rospogrigio
Checked on the motion sensor:

  1. I run the test and got:
    {"protocol":4,"t":1695883237,"data":{"dps":{"1":"pir"},"cid":"******","type":" query"}}
  2. Turned off the motion sensor, run the test, got the same thing:
    {"protocol":4,"t":1695883258,"data":{"dps":{"1":"pir"},"cid":"******","type":" query"}
  3. Turned off the gateway, run the test, received a timeout error
  4. Turned on the gateway and started the test, also getting a timeout
  5. Turned on the motion sensor, started the test and successfully received data

@rospogrigio
Copy link
Owner Author

Oh, crap, it looks like it's a standard Zigbee behavior then. I'll wait also @witokondoria 's feedback but this makes the communication very unreliable since there seems to be no way to tell whether a subdevice has gone offline. I am trying to do some research but cannot find a way to ask for a real status of the offline device, the gateway always responds with the cached status. Unbelievable.
The Tuya integration does recognize that the device is offline so one way could be to use some cloud API call, but this would go totally in the opposite direction with respect to localtuya's intention, which is to keep the communication local.
I'll think about it, thank you in the meantime...

@vanek-865
Copy link

I don’t know much about this, but here (#628) the command is indicated (When requesting a status update (0x0A) with a cid, the gateway will query that particular sub-device and return its status) updates the device status and it seems that the gateway should poll the devices, maybe this will help somehow

@rospogrigio
Copy link
Owner Author

That is what I already do, and that keeps reporting the cached status. Just to make it clearer, can you repeat the above steps using tuya-cli instead of my tuyadebug? Like this:

DEBUG=* tuya-cli get --ip <device_ip> --id <device_id> --cid <device_cid> --key <local_key> --protocol-version 3.4

You will probably get the same behavior...

@vanek-865
Copy link

Additionally, #739 may help:
"Sub-devices do not have known disconnection / connection messages yet, I'm implementing status checks for connection every 60 seconds (configurable in const.py) for now"

@vanek-865
Copy link

@rospogrigio
ran tuya-cli, the result is exactly the same

@witokondoria
Copy link

Sorry for the late answer: my device (the gateway) still dumps DPs while being powered off. Moreover, tuya.com displayed the device as online, the whole time it was powered off. Additionally, the smart life app displayed a nice (and fake) signal detection from the device (a ggq one if that matters).

@rospogrigio
Copy link
Owner Author

For how long? I saw that usually it takes one minute or so for the cloud to report that a device went offline.

@witokondoria
Copy link

More than one minute for sure. Will leave the device powered off and measure the time properly.

@ddpsft
Copy link

ddpsft commented Oct 15, 2023

@rospogrigio , by the current stage to fix It you think it will be possible? I'm waiting the delivery of my Gateway and a couple devices and just now I realize that maybe I should sell the Gateway asap and get some local tasmota/zb2mqtt solution. I've watching you working together to find a solution and would prefer to keep my devices but only If there is some light at the end of the tunnel.

More than one minute for sure. Will leave the device powered off and measure the time properly.

@witokondoria , any news on that?

@rospogrigio
Copy link
Owner Author

rospogrigio commented Oct 15, 2023

@ddpsft , I'm nearly there I guess. The devices basically work as expected, I have set up the config flow and it seems to be working fine, the only missing thing is the detection of whether the subdevices go offline, and this can be done, VERY UNFORTUNATELY, only using the cloud API calls. Or at least, I couldn't find a way to detect when the subdevices go offline, not even using other tools different from localtuya (tuya-cli for example)... any suggestion on this would be very welcome but I highly doubt that there'll be a way.
I aim to publish something in the next 2-3 weeks, but it's a very tough period at work so I'm doing many extra hours and that gives me little spare time to focus on this. I'll keep you posted, though. I also own 3 Zigbee devices so I'm very interested to publish this release.
Maybe when you receive your devices you can perform the tests I have suggested and report feedback?
Thank you

@ddpsft
Copy link

ddpsft commented Oct 16, 2023

@ddpsft , I'm nearly there I guess. The devices basically work as expected, I have set up the config flow and it seems to be working fine, the only missing thing is the detection of whether the subdevices go offline, and this can be done, VERY UNFORTUNATELY, only using the cloud API calls. Or at least, I couldn't find a way to detect when the subdevices go offline, not even using other tools different from localtuya (tuya-cli for example)... any suggestion on this would be very welcome but I highly doubt that there'll be a way. I aim to publish something in the next 2-3 weeks, but it's a very tough period at work so I'm doing many extra hours and that gives me little spare time to focus on this. I'll keep you posted, though. I also own 3 Zigbee devices so I'm very interested to publish this release. Maybe when you receive your devices you can perform the tests I have suggested and report feedback? Thank you

As soon as I get my stuff I'll come back, test and report.

@hocuspocus69
Copy link

I've 4 BRT100 thermostatic valve to test ...where can I download localtuya with zigbee support to test?

@rospogrigio
Copy link
Owner Author

I've 4 BRT100 thermostatic valve to test ...where can I download localtuya with zigbee support to test?

I have something almost ready to test but I need to finalize it and I'm having personal issues these days... Give me some more days and I'll publish something.

@yabdali
Copy link

yabdali commented Nov 30, 2023

I've 4 BRT100 thermostatic valve to test ...where can I download localtuya with zigbee support to test?

I have something almost ready to test but I need to finalize it and I'm having personal issues these days... Give me some more days and I'll publish something.

Hi, thanks for the great work done here. Just wanted to check if there has been any progress on this? I ordered few devices including a Tuya multi-mode gateway and trying to have them setup so I can use them through Home Assistant. I had posted in Reddit to get feedback about having my Zigbee devices which are connected to Tuya Gateway added to HA but most of the feedback I get is to abandon Tuya gateway and buy a stick/dongle from Sonoff which I am really not in favor for as I believe there are different options and some hope that I can use the gateway from Tuya.

Again, thanks for the great work and keep it up...

@presidentio
Copy link

Hi, is there any progress on this PR?

@hocuspocus69
Copy link

I've 4 BRT100 thermostatic valve to test ...where can I download localtuya with zigbee support to test?

I have something almost ready to test but I need to finalize it and I'm having personal issues these days... Give me some more days and I'll publish something.

Hi, thanks for the great work done here. Just wanted to check if there has been any progress on this? I ordered few devices including a Tuya multi-mode gateway and trying to have them setup so I can use them through Home Assistant. I had posted in Reddit to get feedback about having my Zigbee devices which are connected to Tuya Gateway added to HA but most of the feedback I get is to abandon Tuya gateway and buy a stick/dongle from Sonoff which I am really not in favor for as I believe there are different options and some hope that I can use the gateway from Tuya.

Again, thanks for the great work and keep it up...

You can download https://github.com/rospogrigio/localtuya/tree/zigbee_support it's branch version but only 2 zigbee device can be added... Now I'm using a fork of this project and it's ok https://github.com/xZetsubou/hass-localtuya

@shift-del1
Copy link

shift-del1 commented Dec 13, 2023

You can download https://github.com/rospogrigio/localtuya/tree/zigbee_support it's branch version but only 2 zigbee device can be added... Now I'm using a fork of this project and it's ok https://github.com/xZetsubou/hass-localtuya

I tried to set up my TRVs, the integration was able to add them to devices, but all of my entities were unavailable. Did you succeed to set up a climate entity?

@hocuspocus69
Copy link

You can download https://github.com/rospogrigio/localtuya/tree/zigbee_support it's branch version but only 2 zigbee device can be added... Now I'm using a fork of this project and it's ok https://github.com/xZetsubou/hass-localtuya

I tried to set up my TRVs, the integration was able to add them to devices, but all of my entities were unavailable. Did you succeed to set up a climate entity?

I've 5 Moes BRT-100 through Moes Hub zigbee connected as climate entity with xZetsubou fork and i had only 2 connected with rospogrigio version. never have problem.

@shift-del1
Copy link

I've 5 Moes BRT-100 through Moes Hub zigbee connected as climate entity with xZetsubou fork and i had only 2 connected with rospogrigio version. never have problem.

I have opened an issue and all of my problems are now solved by the developer. My 5 Zigbee TRVs are working fine, thanks for the recomnendation. 👍🏻

@md4c
Copy link

md4c commented Feb 17, 2024

unfortunatelly, not working for me...I can see the zigbee gateway, but that's all. I'm not able to do anything about control it or any device behing the gateway. What do you suggest? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants