-
Notifications
You must be signed in to change notification settings - Fork 55
Home Assistant support #24
Comments
CC @nijave, @ImNightwing |
Hi, Apologies if this isn't the right place for this. I have also posted on the Home Assistant Forums. I have just bought a 2 gang light switch from Aliexpress (https://bit.ly/2qirAM8 1). It’s model number is SM-SW102U.
I thought that I would easily get this up and running using the existing Tuya work that has been done. https://github.com/sean6541/tuya-homeassistant / https://github.com/clach04/python-tuya 1 Unfortunately, the tuya protocol on this switch seems to be slightly different. I have used WireShark whilst registering the device and the packets are different. See below:
I now know the host, device_id, but cannot seem to identify to the local_key from what I have from WireShark. I have also used adb logcat and my phone, it has provided the above. Still none the closer…
Any help or guidance would be hugely appreciated. |
@fastrax this is not the best issue to discuss registration (either open a new ticket here or https://github.com/codetheweb/tuyapi/ to avoid fragmentation). https://github.com/clach04/python-tuya/wiki is probably your best bet for how to get the key, if you can figure out how to use it with Wireshark that would be great (it would be great if you could record how you do it) as its not a known technique. The wiki (and links to the readme in the upstream javascript project) covers the techniques that are known to work. It was discovered recently that using the latest version of the vendors app no longer revealed the key :( |
In order to be able to create homeassistant support following their best practice advices python-tuya needs to be available on pypi. @clach04 Do you have any plans to create a pypi package. Or the other way around: Do you see any reasons not to do so? |
No reasons not to do this other than time :( I just sent you a collaborators invite. ALso created Issue #34 |
sean6451 packaged the code and uploaded to pypi a while back and we had some progress on getting this working with home assistant but I kept getting hard to reproduce connection issues (especially those that would crop up on HA but not testing on my laptop with Wireshark open). I suspect there's still something subtle missing. When the app streams requests it uses a different method than constantly polling (it keeps a socket open) and I think that might be related Edit looks like you own the pypi project now |
Also I'd be interesting in helping again--I ended up getting Sonoff switches but still have some Voion and those and the other related brands are usually pretty cheap. I also have a pytuya+HA fork on Github somewhere slightly different than sean6541 I'm guessing there's a way to set this up with Travis and hook into pypi for auto publishing but I've never tried to get that setup before |
I saw this package and it looks up to date which is maintained by @sean6541. I'd still like to add the setup.py to the repository. |
@clach04 A couple questions
|
And @NorthernMan54 would you be willing/able to port over your implementation (once finished) from your nodejs version to python or do you not have the necessary knowledge of python? Unfortunately, I don't have Home Assistant setup yet & don't have the devices to be able to test myself. I more so jumped in when I did to make sure they're supported before I selected them for my (planned) setup. |
@caffeinatedMike yep, this version is local (uses a sort of serial protocol over TCP). I tried to integrate with HA but it wasn't reliable enough (there would be random socket/hard disconnect issues). Looking at the Tuya Android App, it uses a different command to stream status from the device instead of repeatedly hammering for single updates (HA polls every 30 seconds for a status update in case it's toggled manually) which I think might be related The device was hard closing the TCP connection (either requests were too fast, something was invalid, etc) |
If you look at https://github.com/clach04/python-tuya/blob/master/pytuya/__init__.py it has BulbDevice and Switch extending the base device so it should work with those two although I think some devices have minor variations |
@nijave That's good to hear (about the local aspect). Any ideas on possible solutions for the disconnects/unreliability at this point? Have you tried letting Packet Sniffer on android run for maybe 5 minutes with the Smart Life app open to see the activity? Probably best to do so with an Android on 5.0 (marshmallow) because from what I heard the app is able to even sniff ssl on that OS, but can't with the more recent OSes. |
Yeah, the device randomly sends TCP RSTs for no apparent reason |
My guess is those are heartbeat requests (to maintain the consistent connection to their api/servers). That's how they're able to react so fast to state changes/requests. @NorthernMan54's update mimics this activity using a sort of what he calls an |
HA is sending "heart beat" requests, but I have no idea why these will randomly start failing (the device will kill the TCP connection forcibly mid-request). The app uses a different command to stream updates instead of constant single polls like this module. The device uses MQTT for connecting to the Tuya cloud service which is how it responses immediately I'm sure there's a workaround to get them working, but I stopped messing around and got some Sonoff switches that work much better with custom firmware |
So, I guess it's safe to say that local HA integration has been abandoned until someone else wants to take it upon themselves? That sucks |
Nick,
In my investigation with wifi dimmers, I found that I needed to send a heartbeat every 15 seconds to hold the socket open. Then any events where sent in real time. I haven’t looked at your code yet, so don’t know if you are doing this. I also added retry logic to handle the odd rst packet etc.
Michael,
I think somewhere the wires got crossed on what I was working with. I have wifi dimmer switches, that I’m looking to pass local events ie button pushes up to HomeKit with. Theoretically this pattern should also work with contact and motion sensors as well, but I don’t have any from Tuya so I can’t say for sure.
… On Sep 28, 2018, at 10:22 AM, Michael Hill ***@***.***> wrote:
So, I guess it's safe to say that local HA integration has been abandoned until someone else wants to take it upon themselves? That sucks
|
Yeah, this doesn't hold the socket open. The app uses a different command
which basically does that. Not sure if you can use the same command while
trying to hold the socket open
By command see hexByte around line 117 in payload dict
https://github.com/clach04/python-tuya/blob/master/pytuya/__init__.py
On Fri, Sep 28, 2018 at 12:56 PM Northern Man <notifications@github.com>
wrote:
… Nick,
In my investigation with wifi dimmers, I found that I needed to send a
heartbeat every 15 seconds to hold the socket open. Then any events where
sent in real time. I haven’t looked at your code yet, so don’t know if you
are doing this. I also added retry logic to handle the odd rst packet etc.
Michael,
I think somewhere the wires got crossed on what I was working with. I have
wifi dimmer switches, that I’m looking to pass local events ie button
pushes up to HomeKit with. Theoretically this pattern should also work with
contact and motion sensors as well, but I don’t have any from Tuya so I
can’t say for sure.
> On Sep 28, 2018, at 10:22 AM, Michael Hill ***@***.***>
wrote:
>
> So, I guess it's safe to say that local HA integration has been
abandoned until someone else wants to take it upon themselves? That sucks
|
@caffeinatedMike are you seeing disconnects/unreliability? For the devices I have, the only unreliability I've seen so far has been with the devices themselves. I've a few devices and whilst they are mostly reliable they occasionally do not respond to the official app (and they've been deliberately sequestered away from this and other 3rd party libraries), sometimes requiring a power cycle (maybe once every 3 months). If you do, take a look at codetheweb/tuyapi#84 (comment) and tixi's experimental fork. |
@clach04 No, I've not had a problem yet. My switches are working well so far. Any chance we could incorporate door/window sensors into the custom HA component? 🙏 |
@caffeinatedMike pytuya would need that support first (it sounds like those need long open sockets support based on some of the posts I've seen). I don't know how feasible that is. Its not on my todo list but patches are always welcome. One other option would be to flash the sensor devices you have with a new (open) firmware that has HA support. I've not yet looked into this in much detail. I have a couple on my research list to check out:
Related to HA. I did add a version string the other day and created a tag (https://github.com/clach04/python-tuya/archive/7.0.1.zip) so it should be possible to document the github dependency (without having a package on pypi). See https://developers.home-assistant.io/docs/en/creating_component_deps_and_reqs.html for more details |
@clach04 @nijave Sincere apologies for necromancing, but I could use your expertise and experience. I've wasted a lot of time with some Tuya smart plugs (haven't we all?) and HA using pytuya (i'm familiar with tuyapi). I just posted about it here: mileperhour/localtuya-homeassistant#8 ... both posts are of interest. I'd be grateful for some input given you obviously have more experience than me with the code (and also Tuya devices) p.s. I might just fork the dosh and buy more TP-Link HS110 ... expensive and very bulky, but work well and the api is a breeze (clear json-rpc over tcp without any auth whatsoever). |
Another apology for necro! :)
No idea where your thoughts are these days, but there are (luckily) quite a few options nowadays:
|
@aleqx just saw the update from @TRSx80 and I would also recommend Sonoff/Tasmota combo. I even flashed my Wuudi SM-S0301-US unit (which I had used with pytuya) with Tasmota, its such a more useful device with that firmware (and it doesn't lock up like the original firmware). There are other firmwares but Tasmota worked out of the box for me so I've not dug much further into those. Re-flashing existing tuya devices varies in complexity. Only issue with the Sonoff is the form factor does require some customization in many use cases (I have a plan to hack a sonoff into window fan and control it over GPIO - which shows this as a positive rather than a negative as its offers more options) |
Looks like there are a few people interested in HA support, and at least one implementation home-assistant/core#11000 in the works. Opening ticket as a place to jot notes down for sharing.
Custom component:
The text was updated successfully, but these errors were encountered: