CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Add support for some Tuya devices. #11000
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
Conversation
Add support for some devices that use the Tuya Cloud.
config.get(CONF_ID) | ||
)]) | ||
|
||
class tuya(SwitchDevice): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
vol.Optional(CONF_ID, default=DEFAULT_ID): cv.string, | ||
}) | ||
|
||
def setup_platform(hass, config, add_devices, discovery_info=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
#print(payload_dict[self.dev_type][command]['prefix']) | ||
#print(repr(buffer)) | ||
#print(bin2hex(buffer, pretty=True)) | ||
#print(bin2hex(buffer, pretty=False)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
#print('prefix') | ||
#print(payload_dict[self.dev_type][command]['prefix']) | ||
#print(repr(buffer)) | ||
#print(bin2hex(buffer, pretty=True)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
#print('command', command) | ||
#print('prefix') | ||
#print(payload_dict[self.dev_type][command]['prefix']) | ||
#print(repr(buffer)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
#print('json_payload %r' % repr(json_payload)) | ||
#print('json_payload len %r' % len(json_payload)) | ||
#print(bin2hex(json_payload)) | ||
self.cipher = None # expect to connect and then disconnect to set new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (82 > 79 characters)
#print(json_payload) | ||
#print('json_payload %r' % repr(json_payload)) | ||
#print('json_payload len %r' % len(json_payload)) | ||
#print(bin2hex(json_payload)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
#print('data_to_send') | ||
#print(json_payload) | ||
#print('json_payload %r' % repr(json_payload)) | ||
#print('json_payload len %r' % len(json_payload)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
json_payload = str(self.version).encode('latin1') + hexdigest[8:][:16].encode('latin1') + json_payload | ||
#print('data_to_send') | ||
#print(json_payload) | ||
#print('json_payload %r' % repr(json_payload)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
#print(hexdigest[8:][:16]) | ||
json_payload = str(self.version).encode('latin1') + hexdigest[8:][:16].encode('latin1') + json_payload | ||
#print('data_to_send') | ||
#print(json_payload) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
block comment should start with '# '
config.get(CONF_ID) | ||
)]) | ||
|
||
class tuya(SwitchDevice): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
vol.Optional(CONF_ID, default=DEFAULT_ID): cv.string, | ||
}) | ||
|
||
def setup_platform(hass, config, add_devices, discovery_info=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
@@ -0,0 +1,64 @@ | |||
import voluptuous as vol | |||
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA | |||
from homeassistant.const import ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'homeassistant.const.CONF_USERNAME' imported but unused
'homeassistant.const.CONF_PASSWORD' imported but unused
|
||
It uses a slightly modified version of the pytuya library (https://github.com/clach04/python-tuya) to directly control the device. | ||
|
||
Most devices that use the Tuya cloud should work. If port 6668 is open on the device then it will work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (103 > 79 characters)
""" | ||
This is a simple platform to control **SOME** devices that use the Tuya cloud for control. | ||
|
||
It uses a slightly modified version of the pytuya library (https://github.com/clach04/python-tuya) to directly control the device. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (130 > 79 characters)
@@ -0,0 +1,77 @@ | |||
""" | |||
This is a simple platform to control **SOME** devices that use the Tuya cloud for control. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (90 > 79 characters)
|
||
def turn_off(self, **kwargs): | ||
"""Turn Tuya switch off.""" | ||
device = self._pytuy.OutletDevice(self._devid, self._host, self._localkey) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (82 > 79 characters)
|
||
def turn_on(self, **kwargs): | ||
"""Turn Tuya switch on.""" | ||
device = self._pytuy.OutletDevice(self._devid, self._host, self._localkey) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (82 > 79 characters)
|
||
Most switch devices that use the Tuya cloud should work. | ||
If port 6668 is open on the device then it will work. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move details to the documentation please. A simple file header like the other platforms is enough.
REQUIREMENTS = ['pytuya==1.0'] | ||
|
||
CONF_DEVID = 'device_id' | ||
CONF_LOCKEY = 'local_key' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use CONF_DEVICE_ID
and CONF_LOCAL_KEY
.
|
||
|
||
def setup_platform(hass, config, add_devices, discovery_info=None): | ||
"""Setup the Tuya switch.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be """Set up of the...
. Docstrings should be valid according PEP257.
config.get(CONF_LOCKEY), | ||
config.get(CONF_ID) | ||
)]) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should check if the given configuration is working. Otherwise the users end-up with a non-functional platform in their setup.
You also need to move the init of the device to the platform setup.
device = pytuya.OutletDevice()
and work with it for the switch device.
How about also adding energy information like the tplink switches have? These will report current/volts/watts. Edit: Looks like exactly the same as the node version. In that case "status" works as follows: |
I don't have any switches that support energy monitoring so I couldn't test it. I am not that experienced with Python so it would most likely need lots of testing. |
Do you have a sample output for the status on yours? Mine looks like
Mine are Voion outlets (from https://www.amazon.com/Alexa-Smart-Wifi-Plug-Mini/dp/B075RWTHQ8) |
Nijave, I am thinking about getting the same switch you pointed to. Are you able to get these switches to work with both Home Assistant and Google Home at the same time? (As I am researching this, it looks like Google Home requires Smart Life app rather than Tuya Smart app.) |
I used the Smart Life app to setup them up (per instructions). I only use HA so not sure how GH works. There is a limitation so they can only support a single connection at a time so if you have the app open then something else will fail to connect to get/change status. I believe they connect back to the tuya website over the internet so they can be controlled when not connected to the same network so that might potentially alleviate the issue with GH. I believe that's how ifttt integration works. |
What's the status of this? |
Sean just to say thank you for your work and to all those helping refine/test/incorporate. I have partial control over a couple of Tuya S2 switches "wifi sockets" but homeassistant errors concerning state. If I can help at all with logs/testing/protocol capture please let me know. sorry if this is not appropriate to offer here. |
Haven't had time to get this finished. Need to check dependency clach04/pytuya and make sure it's stable/reliable. I think resolving the connection blocking issue I opened is the last step in getting good support. |
Are you ever going to look at tuya rgb light bulb support you think? |
This is based off pytuya which is based off tuyapi (node module) so that
should give some indication of its direction
…On Feb 5, 2018 5:38 PM, "Jolanrensen" ***@***.***> wrote:
Are you ever going to look at tuya rgb light bulb support you think?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#11000 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAXd2siFbmtTbEGFCD-H9So3PMwQ8j2Tks5tR4LzgaJpZM4Q4ekg>
.
|
"""Get state of Tuya switch.""" | ||
success = False | ||
for i in range(3): | ||
if success is False: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use break
to break out of loops.
success = True | ||
except ConnectionError: | ||
if i+1 == 3: | ||
success = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed if you're going to raise.
@sean6541 Please update package on pypi and see change in https://github.com/nijave/home-assistant/blob/component-tuya/homeassistant/components/switch/tuya.py This is done as far as I'm concerned |
Please include a PR with documentation |
Can be reopen after all comments are addressed |
@Jolanrensen rgb bulb support is in the supporting module now. |
@sean6541 Any progress to report on this? |
So the underlying pytuya library had some reliability issues and I switched
to Sonoff outlets with Espurna instead of Tuya-based ones. I was having
issues where HA wasn't able to get the status from the outlet so toggle
wasn't working--only "on" and "off" commands and I think the issue was with
pytuya not implementing something quite right. I tried wrapping pytuya with
some error handling code but didn't have much luck and the issues were hard
to reproduce/debug (pretty much worked fine in testing but failed when
trying to automate with HA)
…On Mon, Jun 25, 2018 at 11:39 PM Harshit Sanghvi ***@***.***> wrote:
@sean6541 <https://github.com/sean6541> I would like to help with
development/ testing of this if you need. @nijave
<https://github.com/nijave> Do you mind opening the PR if @sean6541
<https://github.com/sean6541> can't move this forward?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#11000 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAXd2jvJP2uOP0apnW1eiC3_Aspx-Qrnks5uAazfgaJpZM4Q4ekg>
.
|
Aah, thank for the update. I have some tuya smart plugs from a long time and I hate having a separate app ;) |
@nijave When getting this to work with my setup, I found that the Tuya devices were very inconsistent until I uninstalled all phone apps to control them and made sure that my server was the only thing trying to communicate with the devices. Whenever I used a phone app to toggle the switch, it would lock out everything else from toggling it and they would receive |
@magneticflux- I tried your suggestion (saw this on pytuya's thread) and even removed Smartlife from Google Home connection... This still doesn't work for me. |
@lolento Are you sure you have the right |
@Magneticflux - yes, I used a very old version of this where status polling is not implemented and I can toggle my switches. After status polling is implemented, I cannot toggle. |
Yeah, tried that and it didn't help. Managed to eventually get some in packet captures and there didn't appear to be anything different. The device was only communicating with HA and it'd work fine for 50+ polls then all of a sudden the TCP RESET would happen. There was also an issue with the device randomly returning encrypted results when it normally returned then plain-text. In addition, the phone app uses a different command that streams status from the device instead of constantly polling. With those in mind, I suspect the approach was had errors when compared with how it was supposed to work. Most of that should be documented between the python-tuya Github project and the Node version
…On Tue, Jun 26, 2018, 11:07 PM Mitchell Skaggs ***@***.***> wrote:
@nijave <https://github.com/nijave> When getting this to work with my
setup, I found that the Tuya devices were very inconsistent *until* I
uninstalled all phone apps to control them and made sure that my server was
the only thing trying to communicate with the devices. Whenever I used a
phone app to toggle the switch, it would lock out everything else from
toggling it and they would receive HTTP code 104, Connection reset by peer
.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#11000 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAXd2pkQmOU5_Mn8AJjkNdCMiTSowkISks5uAvbUgaJpZM4Q4ekg>
.
|
tuya-homeassistant
THIS WILL ONLY WORK FOR SWITCHES. WILL NOT WORK WITH DIMMERS.
This is a simple platform to control SOME switch devices that use the Tuya cloud for control.
It uses a slightly modified version of the pytuya library (https://github.com/clach04/python-tuya) to directly control the switch device.
Most switch devices that use the Tuya cloud should work. If port 6668 is open on the switch device then it will work.
switch id is if the switch device has multiple switches, the switch number.
See here for how to find localKey and devId: https://seandev.org/tuyainst
To use, add config below to configuration.yaml
Config Fields:
Example: