remote.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. """
  2. Implementation of Tuya remote control devices
  3. Based on broadlink integration for code saving under HA storage
  4. """
  5. import asyncio
  6. import json
  7. import logging
  8. from collections import defaultdict
  9. from collections.abc import Iterable
  10. from datetime import timedelta
  11. from itertools import product
  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_COMMAND_TYPE,
  18. ATTR_DELAY_SECS,
  19. ATTR_DEVICE,
  20. ATTR_NUM_REPEATS,
  21. DEFAULT_DELAY_SECS,
  22. SERVICE_DELETE_COMMAND,
  23. SERVICE_LEARN_COMMAND,
  24. SERVICE_SEND_COMMAND,
  25. RemoteEntity,
  26. RemoteEntityFeature,
  27. )
  28. from homeassistant.components.remote import DOMAIN as RM_DOMAIN
  29. from homeassistant.const import ATTR_COMMAND
  30. from homeassistant.helpers import config_validation as cv
  31. from homeassistant.helpers.storage import Store
  32. from homeassistant.util import dt as dt_util
  33. from .device import TuyaLocalDevice
  34. from .entity import TuyaLocalEntity
  35. from .helpers.config import async_tuya_setup_platform
  36. from .helpers.device_config import TuyaEntityConfig
  37. _LOGGER = logging.getLogger(__name__)
  38. CODE_STORAGE_VERSION = 1
  39. FLAG_STORAGE_VERSION = 1
  40. CODE_SAVE_DELAY = 15
  41. FLAG_SAVE_DELAY = 15
  42. LEARNING_TIMEOUT = timedelta(seconds=30)
  43. # These commands seem to be standard for all devices
  44. CMD_SEND = "send_ir"
  45. CMD_SEND_RF = "rfstudy_send"
  46. CMD_LEARN = "study"
  47. CMD_ENDLEARN = "study_exit"
  48. CMD_STUDYKEY = "study_key"
  49. CMD_STUDYRF = "rf_study"
  50. CMD_ENDSTUDYRF = "rfstudy_exit"
  51. COMMAND_SCHEMA = vol.Schema(
  52. {
  53. vol.Required(ATTR_COMMAND): vol.All(
  54. cv.ensure_list, [vol.All(cv.string, vol.Length(min=1))], vol.Length(min=1)
  55. ),
  56. },
  57. extra=vol.ALLOW_EXTRA,
  58. )
  59. SERVICE_SEND_SCHEMA = COMMAND_SCHEMA.extend(
  60. {
  61. vol.Optional(ATTR_DEVICE): vol.All(cv.string, vol.Length(min=1)),
  62. vol.Optional(ATTR_DELAY_SECS, default=DEFAULT_DELAY_SECS): vol.Coerce(float),
  63. }
  64. )
  65. SERVICE_LEARN_SCHEMA = COMMAND_SCHEMA.extend(
  66. {
  67. vol.Required(ATTR_DEVICE): vol.All(cv.string, vol.Length(min=1)),
  68. vol.Optional(ATTR_ALTERNATIVE, default=False): cv.boolean,
  69. }
  70. )
  71. SERVICE_DELETE_SCHEMA = COMMAND_SCHEMA.extend(
  72. {
  73. vol.Required(ATTR_DEVICE): vol.All(cv.string, vol.Length(min=1)),
  74. }
  75. )
  76. async def async_setup_entry(hass, config_entry, async_add_entities):
  77. config = {**config_entry.data, **config_entry.options}
  78. await async_tuya_setup_platform(
  79. hass,
  80. async_add_entities,
  81. config,
  82. "remote",
  83. TuyaLocalRemote,
  84. )
  85. class TuyaLocalRemote(TuyaLocalEntity, RemoteEntity):
  86. """Representation of a Tuya Remote entity."""
  87. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  88. """
  89. Initialise the remote device.
  90. Args:
  91. device (TuyaLocalDevice): The device API instance.
  92. config (TuyaEntityConfig): The entity config.
  93. """
  94. super().__init__()
  95. dps_map = self._init_begin(device, config)
  96. self._send_dp = dps_map.pop("send", None)
  97. self._receive_dp = dps_map.pop("receive", None)
  98. # Some remotes split out the control (command) into its own dp and just send raw codes in send
  99. self._control_dp = dps_map.pop("control", None)
  100. self._delay_dp = dps_map.pop("delay", None)
  101. self._type_dp = dps_map.pop("code_type", None)
  102. self._init_end(dps_map)
  103. if self._receive_dp:
  104. self._attr_supported_features |= (
  105. RemoteEntityFeature.LEARN_COMMAND | RemoteEntityFeature.DELETE_COMMAND
  106. )
  107. self._code_storage = Store(
  108. device._hass,
  109. CODE_STORAGE_VERSION,
  110. f"tuya_local_remote_{device.unique_id}_codes",
  111. )
  112. self._flag_storage = Store(
  113. device._hass,
  114. FLAG_STORAGE_VERSION,
  115. f"tuya_local_remote_{device.unique_id}_flags",
  116. )
  117. self._storage_loaded = False
  118. self._codes = {}
  119. self._flags = defaultdict(int)
  120. self._lock = asyncio.Lock()
  121. self._attr_is_on = True
  122. async def _async_load_storage(self):
  123. """Load stored codes and flags from disk."""
  124. self._codes.update(await self._code_storage.async_load() or {})
  125. self._flags.update(await self._flag_storage.async_load() or {})
  126. self._storage_loaded = True
  127. def _extract_codes(self, commands, subdevice=None):
  128. """Extract a list of remote codes.
  129. If the command starts with 'b64:', extract the IR code from it.
  130. If the command starts with 'rf:', keep it as-is so that
  131. _encode_send_code can apply the correct RF payload format.
  132. Otherwise use the command and optionally subdevice as keys to extract the
  133. actual command from storage.
  134. The commands are returned in sublists. For toggle commands, the sublist
  135. may contain two codes that must be sent alternately with each call."""
  136. code_list = []
  137. for cmd in commands:
  138. if cmd.startswith("b64:"):
  139. codes = [cmd[4:]]
  140. elif cmd.startswith("rf:"):
  141. codes = [cmd]
  142. else:
  143. if subdevice is None:
  144. raise ValueError("device must be specified")
  145. try:
  146. codes = self._codes[subdevice][cmd]
  147. except KeyError as err:
  148. raise ValueError(
  149. f"Command {repr(cmd)} not found for {subdevice}"
  150. ) from err
  151. if isinstance(codes, list):
  152. codes = codes[:]
  153. else:
  154. codes = [codes]
  155. for idx, code in enumerate(codes):
  156. try:
  157. codes[idx] = code
  158. except ValueError as err:
  159. raise ValueError(f"Invalid code: {repr(code)}") from err
  160. code_list.append(codes)
  161. return code_list
  162. def _encode_send_code(self, code, delay, is_rf=False):
  163. """Encode a remote command into dps values to send.
  164. Set is_rf=True to use the RF sub-GHz payload format.
  165. The default (is_rf=False) uses the IR payload format.
  166. Based on https://github.com/jasonacox/tinytuya/issues/74 and
  167. the docs it references, there are two kinds of IR devices.
  168. 1. separate dps for control, code, study,...
  169. 2. single dp (201) for send_ir, which takes JSON input,
  170. including control, code, delay, etc, and another for
  171. study_ir (202) that receives the codes in study mode.
  172. RF devices also use a single dp (201) but with a different
  173. JSON payload using control 'rfstudy_send'.
  174. """
  175. dps = {}
  176. if self._control_dp:
  177. # control and code are sent in separate dps.
  178. dps = dps | self._control_dp.get_values_to_set(self._device, CMD_SEND, dps)
  179. dps = dps | self._send_dp.get_values_to_set(self._device, code, dps)
  180. if self._delay_dp:
  181. dps = dps | self._delay_dp.get_values_to_set(self._device, delay, dps)
  182. if self._type_dp:
  183. dps = dps | self._type_dp.get_values_to_set(self._device, 0, dps)
  184. elif is_rf:
  185. dps = dps | self._send_dp.get_values_to_set(
  186. self._device,
  187. json.dumps(
  188. {
  189. "control": CMD_SEND_RF,
  190. "rf_type": "sub_2g",
  191. "mode": 0,
  192. "key1": {
  193. "times": 6,
  194. "intervals": 0,
  195. "ver": "2",
  196. "delay": 0,
  197. "code": code,
  198. },
  199. "feq": 0,
  200. "rate": 0,
  201. "ver": "2",
  202. },
  203. ),
  204. dps,
  205. )
  206. else:
  207. dps = dps | self._send_dp.get_values_to_set(
  208. self._device,
  209. json.dumps(
  210. {
  211. "control": CMD_SEND,
  212. "head": "",
  213. # leading zero means use head, any other leading character is discarded.
  214. "key1": "1" + code,
  215. "type": 0,
  216. "delay": int(delay),
  217. },
  218. ),
  219. dps,
  220. )
  221. return dps
  222. async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
  223. """Send remote commands"""
  224. kwargs[ATTR_COMMAND] = command
  225. kwargs = SERVICE_SEND_SCHEMA(kwargs)
  226. subdevice = kwargs.get(ATTR_DEVICE)
  227. repeat = kwargs.get(ATTR_NUM_REPEATS)
  228. delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
  229. service = f"{RM_DOMAIN}.{SERVICE_SEND_COMMAND}"
  230. if not self._storage_loaded:
  231. await self._async_load_storage()
  232. try:
  233. code_list = self._extract_codes(command, subdevice)
  234. except ValueError as err:
  235. _LOGGER.error("Failed to call %s: %s", service, err)
  236. raise
  237. at_least_one_sent = False
  238. for _, codes in product(range(repeat), code_list):
  239. if at_least_one_sent:
  240. await asyncio.sleep(delay)
  241. if len(codes) > 1:
  242. code = codes[self._flags[subdevice]]
  243. else:
  244. code = codes[0]
  245. # Tuya delay is in milliseconds
  246. if code.startswith("rf:"):
  247. dps_to_set = self._encode_send_code(code[3:], delay * 1000, is_rf=True)
  248. else:
  249. dps_to_set = self._encode_send_code(code, delay * 1000)
  250. _LOGGER.info(
  251. "%s sending command %s to %s",
  252. self._config.config_id,
  253. code,
  254. subdevice or "default device",
  255. )
  256. await self._device.async_set_properties(dps_to_set)
  257. if len(codes) > 1:
  258. self._flags[subdevice] ^= 1
  259. at_least_one_sent = True
  260. if at_least_one_sent:
  261. self._flag_storage.async_delay_save(lambda: self._flags, FLAG_SAVE_DELAY)
  262. async def async_learn_command(self, **kwargs: Any) -> None:
  263. """Learn a list of commands from a remote."""
  264. kwargs = SERVICE_LEARN_SCHEMA(kwargs)
  265. commands = kwargs[ATTR_COMMAND]
  266. subdevice = kwargs[ATTR_DEVICE]
  267. toggle = kwargs[ATTR_ALTERNATIVE]
  268. is_rf = kwargs.get(ATTR_COMMAND_TYPE) == "rf"
  269. if not self._storage_loaded:
  270. await self._async_load_storage()
  271. async with self._lock:
  272. should_store = False
  273. for command in commands:
  274. code = await self._async_learn_command(command, is_rf=is_rf)
  275. _LOGGER.info("Learning %s for %s: %s", command, subdevice, code)
  276. if toggle:
  277. code = [code, await self._async_learn_command(command, is_rf=is_rf)]
  278. self._codes.setdefault(subdevice, {}).update({command: code})
  279. should_store = True
  280. if should_store:
  281. await self._code_storage.async_save(self._codes)
  282. async def _async_learn_command(self, command, is_rf=False):
  283. """Learn a single command"""
  284. service = f"{RM_DOMAIN}.{SERVICE_LEARN_COMMAND}"
  285. if is_rf:
  286. cmd_start = json.dumps(
  287. {
  288. "control": CMD_STUDYRF,
  289. "rf_type": "sub_2g",
  290. "study_feq": "0",
  291. "ver": "2",
  292. }
  293. )
  294. cmd_end = json.dumps(
  295. {
  296. "control": CMD_ENDSTUDYRF,
  297. "rf_type": "sub_2g",
  298. "study_feq": "0",
  299. "ver": "2",
  300. }
  301. )
  302. if self._control_dp:
  303. _LOGGER.debug(
  304. "%s starting learning %s using multi dps method",
  305. self._config.config_id,
  306. command,
  307. )
  308. await self._control_dp.async_set_value(self._device, CMD_LEARN)
  309. elif is_rf:
  310. _LOGGER.debug(
  311. "%s starting learning %s using RF",
  312. self._config.config_id,
  313. command,
  314. )
  315. await self._send_dp.async_set_value(self._device, cmd_start)
  316. else:
  317. _LOGGER.debug(
  318. "%s starting learning %s using IR",
  319. self._config.config_id,
  320. command,
  321. )
  322. await self._send_dp.async_set_value(
  323. self._device,
  324. json.dumps({"control": CMD_LEARN}),
  325. )
  326. persistent_notification.async_create(
  327. self._device._hass,
  328. f"Press the '{command}' button.",
  329. title="Learn command",
  330. notification_id="learn_command",
  331. )
  332. try:
  333. start_time = dt_util.utcnow()
  334. while (dt_util.utcnow() - start_time) < LEARNING_TIMEOUT:
  335. await asyncio.sleep(1)
  336. code = self._receive_dp.get_value(self._device)
  337. if code is not None:
  338. _LOGGER.info(
  339. "%s received code for %s: %s",
  340. self._config.config_id,
  341. command,
  342. code,
  343. )
  344. self._device.anticipate_property_value(self._receive_dp.id, None)
  345. return "rf:" + code if is_rf else code
  346. _LOGGER.warning("Timed out without receiving code in %s", service)
  347. raise TimeoutError(
  348. f"No remote code received within {LEARNING_TIMEOUT.total_seconds()} seconds",
  349. )
  350. finally:
  351. persistent_notification.async_dismiss(
  352. self._device._hass, notification_id="learn_command"
  353. )
  354. _LOGGER.debug("%s ending learning mode", self._config.config_id)
  355. if self._control_dp:
  356. await self._control_dp.async_set_value(
  357. self._device,
  358. CMD_ENDLEARN,
  359. )
  360. elif is_rf:
  361. await self._send_dp.async_set_value(self._device, cmd_end)
  362. else:
  363. await self._send_dp.async_set_value(
  364. self._device,
  365. json.dumps({"control": CMD_ENDLEARN}),
  366. )
  367. async def async_delete_command(self, **kwargs: Any) -> None:
  368. """Delete a list of commands from a remote."""
  369. kwargs = SERVICE_DELETE_SCHEMA(kwargs)
  370. commands = kwargs[ATTR_COMMAND]
  371. subdevice = kwargs[ATTR_DEVICE]
  372. service = f"{RM_DOMAIN}.{SERVICE_DELETE_COMMAND}"
  373. if not self._storage_loaded:
  374. await self._async_load_storage()
  375. try:
  376. codes = self._codes[subdevice]
  377. except KeyError as err:
  378. err_msg = f"Device not found {repr(subdevice)}"
  379. _LOGGER.error("Failed to call %s. %s", service, err_msg)
  380. raise ValueError(err_msg) from err
  381. cmds_not_found = []
  382. for command in commands:
  383. try:
  384. del codes[command]
  385. _LOGGER.info(
  386. "%s deleted command %s for %s",
  387. self._config.config_id,
  388. command,
  389. subdevice or "default device",
  390. )
  391. except KeyError:
  392. cmds_not_found.append(command)
  393. if cmds_not_found:
  394. if len(cmds_not_found) == 1:
  395. err_msg = f"Command not found: {repr(cmds_not_found[0])}"
  396. else:
  397. err_msg = f"Commands not found: {repr(cmds_not_found)}"
  398. if len(cmds_not_found) == len(commands):
  399. _LOGGER.error("Failed to call %s. %s", service, err_msg)
  400. raise ValueError(err_msg)
  401. _LOGGER.error("Error during %s. %s", service, err_msg)
  402. # Clean up
  403. if not codes:
  404. _LOGGER.info(
  405. "%s removing unused device %s", self._config.config_id, subdevice
  406. )
  407. del self._codes[subdevice]
  408. if self._flags.pop(subdevice, None) is not None:
  409. self._flag_storage.async_delay_save(
  410. lambda: self._flags, FLAG_SAVE_DELAY
  411. )
  412. self._code_storage.async_delay_save(lambda: self._codes, CODE_SAVE_DELAY)