remote.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. """
  2. Implementation of Tuya remote control devices
  3. Based on broadlink integration for code saving under HA storage
  4. """
  5. import asyncio
  6. from collections import defaultdict
  7. from collections.abc import Iterable
  8. from datetime import timedelta
  9. from itertools import product
  10. import json
  11. import logging
  12. from typing import Any
  13. import voluptuous as vol
  14. from homeassistant.components import persistent_notification
  15. from homeassistant.components.remote import (
  16. ATTR_ALTERNATIVE,
  17. ATTR_DELAY_SECS,
  18. ATTR_DEVICE,
  19. ATTR_NUM_REPEATS,
  20. DEFAULT_DELAY_SECS,
  21. DOMAIN as RM_DOMAIN,
  22. RemoteEntity,
  23. RemoteEntityFeature,
  24. SERVICE_DELETE_COMMAND,
  25. SERVICE_LEARN_COMMAND,
  26. SERVICE_SEND_COMMAND,
  27. )
  28. from homeassistant.const import ATTR_COMMAND
  29. from homeassistant.helpers import config_validation as cv
  30. from homeassistant.helpers.storage import Store
  31. from homeassistant.util import dt as dt_util
  32. from .device import TuyaLocalDevice
  33. from .helpers.config import async_tuya_setup_platform
  34. from .helpers.device_config import TuyaEntityConfig
  35. from .helpers.mixin import TuyaLocalEntity
  36. _LOGGER = logging.getLogger(__name__)
  37. CODE_STORAGE_VERSION = 1
  38. FLAG_STORAGE_VERSION = 1
  39. CODE_SAVE_DELAY = 15
  40. FLAG_SAVE_DELAY = 15
  41. LEARNING_TIMEOUT = timedelta(seconds=30)
  42. # These commands seem to be standard for all devices
  43. CMD_SEND = "send_ir"
  44. CMD_LEARN = "study"
  45. CMD_ENDLEARN = "study_exit"
  46. CMD_STUDYKEY = "study_key"
  47. COMMAND_SCHEMA = vol.Schema(
  48. {
  49. vol.Required(ATTR_COMMAND): vol.All(
  50. cv.ensure_list, [vol.All(cv.string, vol.Length(min=1))], vol.Length(min=1)
  51. ),
  52. },
  53. extra=vol.ALLOW_EXTRA,
  54. )
  55. SERVICE_SEND_SCHEMA = COMMAND_SCHEMA.extend(
  56. {
  57. vol.Optional(ATTR_DEVICE): vol.All(cv.string, vol.Length(min=1)),
  58. vol.Optional(ATTR_DELAY_SECS, default=DEFAULT_DELAY_SECS): vol.Coerce(float),
  59. }
  60. )
  61. SERVICE_LEARN_SCHEMA = COMMAND_SCHEMA.extend(
  62. {
  63. vol.Required(ATTR_DEVICE): vol.All(cv.string, vol.Length(min=1)),
  64. vol.Optional(ATTR_ALTERNATIVE, default=False): cv.boolean,
  65. }
  66. )
  67. SERVICE_DELETE_SCHEMA = COMMAND_SCHEMA.extend(
  68. {
  69. vol.Required(ATTR_DEVICE): vol.All(cv.string, vol.Length(min=1)),
  70. }
  71. )
  72. async def async_setup_entry(hass, config_entry, async_add_entities):
  73. config = {**config_entry.data, **config_entry.options}
  74. await async_tuya_setup_platform(
  75. hass,
  76. async_add_entities,
  77. config,
  78. "remote",
  79. TuyaLocalRemote,
  80. )
  81. class TuyaLocalRemote(TuyaLocalEntity, RemoteEntity):
  82. """Representation of a Tuya Remote entity."""
  83. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  84. """
  85. Initialise the remote device.
  86. Args:
  87. device (TuyaLocalDevice): The device API instance.
  88. config (TuyaEntityConfig): The entity config.
  89. """
  90. super().__init__()
  91. dps_map = self._init_begin(device, config)
  92. self._send_dp = dps_map.pop("send", None)
  93. self._receive_dp = dps_map.pop("receive", None)
  94. # Some remotes split out the control (command) into its own dp and just send raw codes in send
  95. self._control_dp = dps_map.pop("control", None)
  96. self._delay_dp = dps_map.pop("delay", None)
  97. self._type_dp = dps_map.pop("code_type", None)
  98. self._init_end(dps_map)
  99. self._attr_supported_features = 0
  100. if self._receive_dp:
  101. self._attr_supported_features |= (
  102. RemoteEntityFeature.LEARN_COMMAND | RemoteEntityFeature.DELETE_COMMAND
  103. )
  104. self._code_storage = Store(
  105. device._hass,
  106. CODE_STORAGE_VERSION,
  107. f"tuya_local_remote_{device.unique_id}_codes",
  108. )
  109. self._flag_storage = Store(
  110. device._hass,
  111. FLAG_STORAGE_VERSION,
  112. f"tuya_local_remote_{device.unique_id}_flags",
  113. )
  114. self._storage_loaded = False
  115. self._codes = {}
  116. self._flags = defaultdict(int)
  117. self._lock = asyncio.Lock()
  118. self._attr_is_on = True
  119. async def _async_load_storage(self):
  120. """Load stored codes and flags from disk."""
  121. self._codes.update(await self._code_storage.async_load() or {})
  122. self._flags.update(await self._flag_storage.async_load() or {})
  123. self._storage_loaded = True
  124. def _extract_codes(self, commands, subdevice=None):
  125. """Extract a list of remote codes.
  126. If the command starts with 'b64:', extract the code from it.
  127. Otherwise use the command and optionally subdevice as keys to extract the
  128. actual command from storage.
  129. The commands are returned in sublists. For toggle commands, the sublist
  130. may contain two codes that must be sent alternately with each call."""
  131. code_list = []
  132. for cmd in commands:
  133. if cmd.startswith("b64:"):
  134. codes = [cmd[4:]]
  135. else:
  136. if subdevice is None:
  137. raise ValueError("device must be specified")
  138. try:
  139. codes = self._codes[subdevice][cmd]
  140. except KeyError as err:
  141. raise ValueError(
  142. f"Command {repr(cmd)} not found for {subdevice}"
  143. ) from err
  144. if isinstance(codes, list):
  145. codes = code[:]
  146. else:
  147. codes = [codes]
  148. for idx, code in enumerate(codes):
  149. try:
  150. codes[idx] = code
  151. except ValueError as err:
  152. raise ValueError(f"Invalid code: {repr(code)}") from err
  153. code_list.append(codes)
  154. return code_list
  155. def _encode_send_code(self, code, delay):
  156. """Encode a remote command into dps values to send."""
  157. # Based on https://github.com/jasonacox/tinytuya/issues/74 and
  158. # the docs it references, there are two kinds of IR devices.
  159. # 1. separate dps for control, code, study,...
  160. # 2. single dp (201) for send_ir, which takes JSON input,
  161. # including control, code, delay, etc, and another for
  162. # study_ir (202) that receives the codes in study mode.
  163. dps = {}
  164. if self._control_dp:
  165. # control and code are sent in seperate dps.
  166. dps = dps | self._control_dp.get_values_to_set(self._device, CMD_SEND)
  167. dps = dps | self._send_dp.get_values_to_set(self._device, code)
  168. if self._delay_dp:
  169. dps = dps | self._delay_dp.get_values_to_set(self._device, delay)
  170. if self._type_dp:
  171. dps = dps | self._type_dp.get_values_to_seet(self._device, 0)
  172. else:
  173. dps = dps | self._send_dp.get_values_to_set(
  174. self._device,
  175. json.dumps(
  176. {
  177. "control": CMD_SEND,
  178. "head": "",
  179. # leading zero means use head, any other leeading character is discarded.
  180. "key1": "1" + code,
  181. "type": 0,
  182. "delay": int(delay),
  183. }
  184. ),
  185. )
  186. return dps
  187. async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
  188. """Send remote commands"""
  189. kwargs[ATTR_COMMAND] = command
  190. kwargs = SERVICE_SEND_SCHEMA(kwargs)
  191. commands = kwargs[ATTR_COMMAND]
  192. subdevice = kwargs.get(ATTR_DEVICE)
  193. repeat = kwargs.get(ATTR_NUM_REPEATS)
  194. delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS) * 1000
  195. service = f"{RM_DOMAIN}.{SERVICE_SEND_COMMAND}"
  196. if not self._storage_loaded:
  197. await self._async_load_storage()
  198. try:
  199. code_list = self._extract_codes(command, subdevice)
  200. except ValueError as err:
  201. _LOGGER.error("Failed to call %s: %s", service, err)
  202. raise
  203. at_least_one_sent = False
  204. for _, codes in product(range(repeat), code_list):
  205. if at_least_one_sent:
  206. await asyncio.sleep(delay)
  207. if len(codes) > 1:
  208. code = codes[self._flags[subdevice]]
  209. else:
  210. code = codes[0]
  211. dps_to_set = self._encode_send_code(code, delay)
  212. await self._device.async_set_properties(dps_to_set)
  213. if len(codes) > 1:
  214. self._flags[subdevice] ^= 1
  215. at_least_one_sent = True
  216. if at_least_one_sent:
  217. self._flag_storage.async_delay_save(self._flags, FLAG_SAVE_DELAY)
  218. async def async_learn_command(self, **kwargs: Any) -> None:
  219. """Learn a list of commands from a remote."""
  220. kwargs = SERVICE_LEARN_SCHEMA(kwargs)
  221. commands = kwargs[ATTR_COMMAND]
  222. subdevice = kwargs[ATTR_DEVICE]
  223. toggle = kwargs[ATTR_ALTERNATIVE]
  224. service = f"{RM_DOMAIN}.{SERVICE_LEARN_COMMAND}"
  225. if not self._storage_loaded:
  226. await self._async_load_storage()
  227. async with self._lock:
  228. should_store = False
  229. for command in commands:
  230. code = await self._async_learn_command(command)
  231. if toggle:
  232. code = [code, await self._async_learn_command(command)]
  233. self._codes.setdefault(subdevice, {}).update({command: code})
  234. should_store = True
  235. if should_store:
  236. await self._code_storage.async_save(self._codes)
  237. async def _async_learn_command(self, command):
  238. """Learn a single command"""
  239. if self._control_dp:
  240. await self._control_dp.async_set_value(self._device, CMD_LEARN)
  241. else:
  242. await self._send_dp.async_set_value(
  243. self._device,
  244. json.dumps({"control": CMD_LEARN}),
  245. )
  246. persistent_notification.async_create(
  247. self._device._hass,
  248. f"Press the '{command}' button.",
  249. title="Learn command",
  250. notification_id="learn_command",
  251. )
  252. try:
  253. start_time = dt_util.utcnow()
  254. while (dt_util.utcnow() - start_time) < LEARNING_TIMEOUT:
  255. await asyncio.sleep(1)
  256. code = self._receive_dp.get_value(self._device)
  257. if code is not None:
  258. return code
  259. raise TimeoutError(
  260. f"No remote code received within {LEARNING_TIMEOUT.total_seconds()} seconds",
  261. )
  262. finally:
  263. persistent_notification.async_dismiss(
  264. self._device._hass, notification_id="learn_command"
  265. )
  266. if self._control_dp:
  267. await self._control_dp.async_set_value(
  268. self._device,
  269. CMD_ENDLEARN,
  270. )
  271. else:
  272. await self._send_dp.async_set_value(
  273. self._device,
  274. json.dumps({"control": CMD_ENDLEARN}),
  275. )
  276. async def async_delete_command(self, **kwargs: Any) -> None:
  277. """Delete a list of commands from a remote."""
  278. kwargs = SERVICE_DELETE_SCHEMA(kwargs)
  279. commands = kwargs[ATTR_COMMAND]
  280. subdevice = kwargs[ATTR_DEVICE]
  281. service = f"{RM_DOMAIN}.{SERVICE_DELETE_COMMAND}"
  282. if not self._storage_loaded:
  283. await self._async_load_storage()
  284. try:
  285. codes = self._codes[subdevice]
  286. except KeyError as err:
  287. err_msg = f"Device not found {repr(subdevice)}"
  288. _LOGGER.error("Failed to call %s. %s", service, err_msg)
  289. raise ValueError(err_msg) from err
  290. cmds_not_found = []
  291. for command in commands:
  292. try:
  293. del codes[command]
  294. except KeyError:
  295. cmds_not_found.append(command)
  296. if cmds_not_found:
  297. if len(cmds_not_found) == 1:
  298. err_msg = f"Command not found: {repr(cmds_not_found[0])}"
  299. else:
  300. err_msg = f"Commands not found: {repr(cmds_not_found)}"
  301. if len(cmds_not_found) == len(commands):
  302. _LOGGER.error("Failed to call %s. %s", service, err_msg)
  303. raise ValueError(err_msg)
  304. _LOGGER.error("Error during %s. %s", service, err_msg)
  305. # Clean up
  306. if not codes:
  307. del self._codes[subdevice]
  308. if self._flags.pop(subdevice, None) is not None:
  309. self._flag_storage.async_delay_save(self._flags, FLAG_SAVE_DELAY)
  310. self._code_storage.async_delay_save(self._codes, CODE_SAVE_DELAY)