-
Notifications
You must be signed in to change notification settings - Fork 18
/
tuya.py
83 lines (67 loc) · 2.35 KB
/
tuya.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Simple platform to control **SOME** Tuya switch devices.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.tuya/
"""
import voluptuous as vol
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME, CONF_HOST, CONF_ID)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pytuya==2.0']
CONF_DEVICE_ID = 'device_id'
CONF_LOCAL_KEY = 'local_key'
DEFAULT_ID = 1
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_DEVICE_ID): cv.string,
vol.Required(CONF_LOCAL_KEY): cv.string,
vol.Optional(CONF_ID, default=DEFAULT_ID): cv.string,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up of the Tuya switch."""
import pytuya
add_devices([TuyaDevice(
pytuya.OutletDevice(
config.get(CONF_DEVICE_ID),
config.get(CONF_HOST),
config.get(CONF_LOCAL_KEY),
),
config.get(CONF_NAME),
config.get(CONF_ID)
)])
class TuyaDevice(SwitchDevice):
"""Representation of a Tuya switch."""
def __init__(self, device, name, switchid):
"""Initialize the Tuya switch."""
self._device = device
self._name = name
self._state = False
self._switchid = switchid
@property
def name(self):
"""Get name of Tuya switch."""
return self._name
@property
def is_on(self):
"""Check if Tuya switch is on."""
return self._state
def turn_on(self, **kwargs):
"""Turn Tuya switch on."""
self._device.set_status(True, self._switchid)
def turn_off(self, **kwargs):
"""Turn Tuya switch off."""
self._device.set_status(False, self._switchid)
def update(self):
"""Get state of Tuya switch."""
success = False
for i in range(3):
if success is False:
try:
status = self._device.status()
self._state = status['dps'][self._switchid]
success = True
except ConnectionError:
if i+1 == 3:
success = False
raise ConnectionError("Failed to update status.")