__init__.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. """
  2. Platform for Tuya WiFi-connected devices.
  3. Based on nikrolls/homeassistant-goldair-climate for Goldair branded devices.
  4. Based on sean6541/tuya-homeassistant for service call logic, and TarxBoy's
  5. investigation into Goldair's tuyapi statuses
  6. https://github.com/codetheweb/tuyapi/issues/31.
  7. """
  8. from time import time
  9. from threading import Timer, Lock
  10. import logging
  11. import json
  12. import voluptuous as vol
  13. import homeassistant.helpers.config_validation as cv
  14. from homeassistant.const import (CONF_NAME, CONF_HOST, TEMP_CELSIUS)
  15. from homeassistant.helpers.discovery import load_platform
  16. VERSION = '0.0.8'
  17. _LOGGER = logging.getLogger(__name__)
  18. DOMAIN = 'tuya_local'
  19. DATA_TUYA_LOCAL = 'data_tuya_local'
  20. API_PROTOCOL_VERSIONS = [3.3, 3.1]
  21. CONF_DEVICE_ID = 'device_id'
  22. CONF_LOCAL_KEY = 'local_key'
  23. CONF_TYPE = 'type'
  24. CONF_TYPE_HEATER = 'heater'
  25. CONF_TYPE_DEHUMIDIFIER = 'dehumidifier'
  26. CONF_TYPE_FAN = 'fan'
  27. CONF_TYPE_KOGAN_HEATER = 'kogan_heater'
  28. CONF_CLIMATE = 'climate'
  29. CONF_DISPLAY_LIGHT = 'display_light'
  30. CONF_CHILD_LOCK = 'child_lock'
  31. CONF_TANK_FULL = 'tank_full'
  32. PLATFORM_SCHEMA = vol.Schema({
  33. vol.Required(CONF_NAME): cv.string,
  34. vol.Required(CONF_HOST): cv.string,
  35. vol.Required(CONF_DEVICE_ID): cv.string,
  36. vol.Required(CONF_LOCAL_KEY): cv.string,
  37. vol.Required(CONF_TYPE): vol.In([CONF_TYPE_HEATER, CONF_TYPE_DEHUMIDIFIER, CONF_TYPE_FAN, CONF_TYPE_KOGAN_HEATER]),
  38. vol.Optional(CONF_CLIMATE, default=True): cv.boolean,
  39. vol.Optional(CONF_DISPLAY_LIGHT, default=False): cv.boolean,
  40. vol.Optional(CONF_CHILD_LOCK, default=False): cv.boolean,
  41. vol.Optional(CONF_TANK_FULL, default=False): cv.boolean,
  42. })
  43. CONFIG_SCHEMA = vol.Schema({
  44. DOMAIN: vol.All(cv.ensure_list, [PLATFORM_SCHEMA])
  45. }, extra=vol.ALLOW_EXTRA)
  46. def setup(hass, config):
  47. hass.data[DOMAIN] = {}
  48. for device_config in config.get(DOMAIN, []):
  49. host = device_config.get(CONF_HOST)
  50. device = TuyaLocalDevice(
  51. device_config.get(CONF_NAME),
  52. device_config.get(CONF_DEVICE_ID),
  53. device_config.get(CONF_HOST),
  54. device_config.get(CONF_LOCAL_KEY)
  55. )
  56. hass.data[DOMAIN][host] = device
  57. discovery_info = {CONF_HOST: host, CONF_TYPE: device_config.get(CONF_TYPE)}
  58. if device_config.get(CONF_CLIMATE) == True:
  59. load_platform(hass, 'climate', DOMAIN, discovery_info, config)
  60. if device_config.get(CONF_DISPLAY_LIGHT) == True:
  61. load_platform(hass, 'light', DOMAIN, discovery_info, config)
  62. if device_config.get(CONF_CHILD_LOCK) == True:
  63. load_platform(hass, 'lock', DOMAIN, discovery_info, config)
  64. if device_config.get(CONF_TANK_FULL) == True:
  65. load_platform(hass, 'binary_sensor', DOMAIN, discovery_info, config)
  66. return True
  67. class TuyaLocalDevice(object):
  68. def __init__(self, name, dev_id, address, local_key):
  69. """
  70. Represents a Tuya-based device.
  71. Args:
  72. dev_id (str): The device id.
  73. address (str): The network address.
  74. local_key (str): The encryption key.
  75. """
  76. import pytuya
  77. self._name = name
  78. self._api_protocol_version_index = None
  79. self._api = pytuya.Device(dev_id, address, local_key, 'device')
  80. self._rotate_api_protocol_version()
  81. self._fixed_properties = {}
  82. self._reset_cached_state()
  83. self._TEMPERATURE_UNIT = TEMP_CELSIUS
  84. # API calls to update Tuya devices are asynchronous and non-blocking. This means
  85. # you can send a change and immediately request an updated state (like HA does),
  86. # but because it has not yet finished processing you will be returned the old state.
  87. # The solution is to keep a temporary list of changed properties that we can overlay
  88. # onto the state while we wait for the board to update its switches.
  89. self._FAKE_IT_TIL_YOU_MAKE_IT_TIMEOUT = 10
  90. self._CACHE_TIMEOUT = 20
  91. self._CONNECTION_ATTEMPTS = 4
  92. self._lock = Lock()
  93. @property
  94. def name(self):
  95. return self._name
  96. @property
  97. def temperature_unit(self):
  98. return self._TEMPERATURE_UNIT
  99. def set_fixed_properties(self, fixed_properties):
  100. self._fixed_properties = fixed_properties
  101. set_fixed_properties = Timer(10, lambda: self._set_properties(self._fixed_properties))
  102. set_fixed_properties.start()
  103. def refresh(self):
  104. now = time()
  105. cached_state = self._get_cached_state()
  106. if now - cached_state['updated_at'] >= self._CACHE_TIMEOUT:
  107. self._cached_state['updated_at'] = time()
  108. self._retry_on_failed_connection(lambda: self._refresh_cached_state(), f'Failed to refresh device state for {self.name}.')
  109. def get_property(self, dps_id):
  110. cached_state = self._get_cached_state()
  111. if dps_id in cached_state:
  112. return cached_state[dps_id]
  113. else:
  114. return None
  115. def set_property(self, dps_id, value):
  116. self._set_properties({dps_id: value})
  117. def anticipate_property_value(self, dps_id, value):
  118. """
  119. Update a value in the cached state only. This is good for when you know the device will reflect a new state in
  120. the next update, but don't want to wait for that update for the device to represent this state.
  121. The anticipated value will be cleared with the next update.
  122. """
  123. self._cached_state[dps_id] = value
  124. def _reset_cached_state(self):
  125. self._cached_state = {
  126. 'updated_at': 0
  127. }
  128. self._pending_updates = {}
  129. def _refresh_cached_state(self):
  130. new_state = self._api.status()
  131. self._cached_state = new_state['dps']
  132. self._cached_state['updated_at'] = time()
  133. _LOGGER.info(f'refreshed device state: {json.dumps(new_state)}')
  134. _LOGGER.debug(f'new cache state (including pending properties): {json.dumps(self._get_cached_state())}')
  135. def _set_properties(self, properties):
  136. if len(properties) == 0:
  137. return
  138. self._add_properties_to_pending_updates(properties)
  139. self._debounce_sending_updates()
  140. def _add_properties_to_pending_updates(self, properties):
  141. now = time()
  142. properties = {**properties, **self._fixed_properties}
  143. pending_updates = self._get_pending_updates()
  144. for key, value in properties.items():
  145. pending_updates[key] = {
  146. 'value': value,
  147. 'updated_at': now
  148. }
  149. _LOGGER.debug(f'new pending updates: {json.dumps(self._pending_updates)}')
  150. def _debounce_sending_updates(self):
  151. try:
  152. self._debounce.cancel()
  153. except AttributeError:
  154. pass
  155. self._debounce = Timer(1, self._send_pending_updates)
  156. self._debounce.start()
  157. def _send_pending_updates(self):
  158. pending_properties = self._get_pending_properties()
  159. payload = self._api.generate_payload('set', pending_properties)
  160. _LOGGER.info(f'sending dps update: {json.dumps(pending_properties)}')
  161. self._retry_on_failed_connection(lambda: self._send_payload(payload), 'Failed to update device state.')
  162. def _send_payload(self, payload):
  163. try:
  164. self._lock.acquire()
  165. self._api._send_receive(payload)
  166. self._cached_state['updated_at'] = 0
  167. now = time()
  168. pending_updates = self._get_pending_updates()
  169. for key, value in pending_updates.items():
  170. pending_updates[key]['updated_at'] = now
  171. finally:
  172. self._lock.release()
  173. def _retry_on_failed_connection(self, func, error_message):
  174. for i in range(self._CONNECTION_ATTEMPTS):
  175. try:
  176. func()
  177. except:
  178. if i + 1 == self._CONNECTION_ATTEMPTS:
  179. self._reset_cached_state()
  180. _LOGGER.error(error_message)
  181. else:
  182. self._rotate_api_protocol_version()
  183. def _get_cached_state(self):
  184. cached_state = self._cached_state.copy()
  185. _LOGGER.debug(f'pending updates: {json.dumps(self._get_pending_updates())}')
  186. return {**cached_state, **self._get_pending_properties()}
  187. def _get_pending_properties(self):
  188. return {key: info['value'] for key, info in self._get_pending_updates().items()}
  189. def _get_pending_updates(self):
  190. now = time()
  191. self._pending_updates = {key: value for key, value in self._pending_updates.items()
  192. if now - value['updated_at'] < self._FAKE_IT_TIL_YOU_MAKE_IT_TIMEOUT}
  193. return self._pending_updates
  194. def _rotate_api_protocol_version(self):
  195. if self._api_protocol_version_index is None:
  196. self._api_protocol_version_index = 0
  197. else:
  198. self._api_protocol_version_index += 1
  199. if self._api_protocol_version_index >= len(API_PROTOCOL_VERSIONS):
  200. self._api_protocol_version_index = 0
  201. new_version = API_PROTOCOL_VERSIONS[self._api_protocol_version_index]
  202. _LOGGER.info(f'Setting protocol version for {self.name} to {new_version}.')
  203. self._api.set_version(new_version)
  204. @staticmethod
  205. def get_key_for_value(obj, value):
  206. keys = list(obj.keys())
  207. values = list(obj.values())
  208. return keys[values.index(value)]