config_flow.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. import asyncio
  2. import logging
  3. from collections import OrderedDict
  4. from typing import Any
  5. import tinytuya
  6. import voluptuous as vol
  7. from homeassistant.config_entries import (
  8. CONN_CLASS_LOCAL_PUSH,
  9. ConfigEntry,
  10. ConfigFlow,
  11. OptionsFlow,
  12. )
  13. from homeassistant.const import CONF_HOST, CONF_NAME
  14. from homeassistant.core import HomeAssistant, callback
  15. from homeassistant.data_entry_flow import FlowResult
  16. from homeassistant.helpers.selector import (
  17. QrCodeSelector,
  18. QrCodeSelectorConfig,
  19. QrErrorCorrectionLevel,
  20. SelectOptionDict,
  21. SelectSelector,
  22. SelectSelectorConfig,
  23. SelectSelectorMode,
  24. )
  25. from . import DOMAIN
  26. from .cloud import Cloud
  27. from .const import (
  28. API_PROTOCOL_VERSIONS,
  29. CONF_DEVICE_CID,
  30. CONF_DEVICE_ID,
  31. CONF_LOCAL_KEY,
  32. CONF_MANUFACTURER,
  33. CONF_MODEL,
  34. CONF_POLL_ONLY,
  35. CONF_PROTOCOL_VERSION,
  36. CONF_TYPE,
  37. CONF_USER_CODE,
  38. DATA_STORE,
  39. )
  40. from .device import TuyaLocalDevice
  41. from .helpers.config import get_device_id
  42. from .helpers.device_config import get_config
  43. from .helpers.log import log_json
  44. _LOGGER = logging.getLogger(__name__)
  45. DEVICE_DETAILS_URL = (
  46. "https://github.com/make-all/tuya-local/blob/main/DEVICE_DETAILS.md"
  47. "#finding-your-device-id-and-local-key"
  48. )
  49. class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
  50. VERSION = 13
  51. MINOR_VERSION = 17
  52. CONNECTION_CLASS = CONN_CLASS_LOCAL_PUSH
  53. device = None
  54. data = {}
  55. __qr_code: str | None = None
  56. __cloud_devices: dict[str, Any] = {}
  57. __cloud_device: dict[str, Any] | None = None
  58. def __init__(self) -> None:
  59. """Initialize the config flow."""
  60. self.cloud = None
  61. def init_cloud(self):
  62. if self.cloud is None:
  63. self.cloud = Cloud(self.hass)
  64. async def async_step_user(self, user_input=None):
  65. errors = {}
  66. if self.hass.data.get(DOMAIN) is None:
  67. self.hass.data[DOMAIN] = {}
  68. if self.hass.data[DOMAIN].get(DATA_STORE) is None:
  69. self.hass.data[DOMAIN][DATA_STORE] = {}
  70. if user_input is not None:
  71. mode = user_input.get("setup_mode")
  72. if mode == "cloud" or mode == "cloud_fresh_login":
  73. self.init_cloud()
  74. try:
  75. if mode == "cloud_fresh_login":
  76. # Force a fresh login
  77. self.cloud.logout()
  78. if self.cloud.is_authenticated:
  79. self.__cloud_devices = await self.cloud.async_get_devices()
  80. return await self.async_step_choose_device()
  81. except Exception as e:
  82. # Re-authentication is needed.
  83. _LOGGER.warning("Connection test failed with %s %s", type(e), e)
  84. _LOGGER.warning("Re-authentication is required.")
  85. return await self.async_step_cloud()
  86. if mode == "manual":
  87. return await self.async_step_local()
  88. # Build form
  89. fields: OrderedDict[vol.Marker, Any] = OrderedDict()
  90. fields[vol.Required("setup_mode")] = SelectSelector(
  91. SelectSelectorConfig(
  92. options=["cloud", "manual", "cloud_fresh_login"],
  93. mode=SelectSelectorMode.LIST,
  94. translation_key="setup_mode",
  95. )
  96. )
  97. return self.async_show_form(
  98. step_id="user",
  99. data_schema=vol.Schema(fields),
  100. errors=errors or {},
  101. last_step=False,
  102. )
  103. async def async_step_cloud(
  104. self, user_input: dict[str, Any] | None = None
  105. ) -> FlowResult:
  106. """Step user."""
  107. errors = {}
  108. placeholders = {}
  109. self.init_cloud()
  110. if user_input is not None:
  111. response = await self.cloud.async_get_qr_code(user_input[CONF_USER_CODE])
  112. if response:
  113. self.__qr_code = response
  114. return await self.async_step_scan()
  115. errors["base"] = "login_error"
  116. placeholders = self.cloud.last_error
  117. else:
  118. user_input = {}
  119. return self.async_show_form(
  120. step_id="cloud",
  121. data_schema=vol.Schema(
  122. {
  123. vol.Required(
  124. CONF_USER_CODE, default=user_input.get(CONF_USER_CODE, "")
  125. ): str,
  126. }
  127. ),
  128. errors=errors,
  129. description_placeholders=placeholders,
  130. )
  131. async def async_step_scan(
  132. self, user_input: dict[str, Any] | None = None
  133. ) -> FlowResult:
  134. """Step scan."""
  135. if user_input is None:
  136. return self.async_show_form(
  137. step_id="scan",
  138. data_schema=vol.Schema(
  139. {
  140. vol.Optional("QR"): QrCodeSelector(
  141. config=QrCodeSelectorConfig(
  142. data=f"tuyaSmart--qrLogin?token={self.__qr_code}",
  143. scale=5,
  144. error_correction_level=QrErrorCorrectionLevel.QUARTILE,
  145. )
  146. )
  147. }
  148. ),
  149. )
  150. self.init_cloud()
  151. if not await self.cloud.async_login():
  152. # Try to get a new QR code on failure
  153. response = await self.cloud.async_get_qr_code()
  154. errors = {"base": "login_error"}
  155. placeholders = self.cloud.last_error
  156. if response:
  157. self.__qr_code = response
  158. return self.async_show_form(
  159. step_id="scan",
  160. errors=errors,
  161. data_schema=vol.Schema(
  162. {
  163. vol.Optional("QR"): QrCodeSelector(
  164. config=QrCodeSelectorConfig(
  165. data=f"tuyaSmart--qrLogin?token={self.__qr_code}",
  166. scale=5,
  167. error_correction_level=QrErrorCorrectionLevel.QUARTILE,
  168. )
  169. )
  170. }
  171. ),
  172. description_placeholders=placeholders,
  173. )
  174. self.__cloud_devices = await self.cloud.async_get_devices()
  175. return await self.async_step_choose_device()
  176. async def async_step_choose_device(self, user_input=None):
  177. errors = {}
  178. if user_input is not None:
  179. device_choice = self.__cloud_devices[user_input["device_id"]]
  180. if device_choice["ip"] != "":
  181. # This is a directly addable device.
  182. if user_input["hub_id"] == "None":
  183. device_choice["ip"] = ""
  184. self.__cloud_device = device_choice
  185. return await self.async_step_search()
  186. else:
  187. # Show error if user selected a hub.
  188. errors["base"] = "does_not_need_hub"
  189. # Fall through to reshow the form.
  190. else:
  191. # This is an indirectly addressable device. Need to know which hub it is connected to.
  192. if user_input["hub_id"] != "None":
  193. hub_choice = self.__cloud_devices[user_input["hub_id"]]
  194. # Populate node_id or uuid and local_key from the child
  195. # device to pass on complete information to the local step.
  196. hub_choice["ip"] = ""
  197. hub_choice[CONF_DEVICE_CID] = (
  198. device_choice["node_id"] or device_choice["uuid"]
  199. )
  200. if device_choice.get(CONF_LOCAL_KEY):
  201. hub_choice[CONF_LOCAL_KEY] = device_choice[CONF_LOCAL_KEY]
  202. # Communicate the sub device product id to help match the
  203. # correect device config in the next step.
  204. hub_choice["product_id"] = device_choice["product_id"]
  205. self.__cloud_device = hub_choice
  206. return await self.async_step_search()
  207. else:
  208. # Show error if user did not select a hub.
  209. errors["base"] = "needs_hub"
  210. # Fall through to reshow the form.
  211. device_list = []
  212. for key in self.__cloud_devices.keys():
  213. device_entry = self.__cloud_devices[key]
  214. if device_entry.get("exists"):
  215. continue
  216. if device_entry[CONF_LOCAL_KEY] != "":
  217. if device_entry["online"]:
  218. device_list.append(
  219. SelectOptionDict(
  220. value=key,
  221. label=f"{device_entry['name']} ({device_entry['product_name']})",
  222. )
  223. )
  224. else:
  225. device_list.append(
  226. SelectOptionDict(
  227. value=key,
  228. label=f"{device_entry['name']} ({device_entry['product_name']}) OFFLINE",
  229. )
  230. )
  231. _LOGGER.debug(f"Device count: {len(device_list)}")
  232. if len(device_list) == 0:
  233. return self.async_abort(reason="no_devices")
  234. device_selector = SelectSelector(
  235. SelectSelectorConfig(options=device_list, mode=SelectSelectorMode.DROPDOWN)
  236. )
  237. hub_list = []
  238. hub_list.append(SelectOptionDict(value="None", label="None"))
  239. for key in self.__cloud_devices.keys():
  240. hub_entry = self.__cloud_devices[key]
  241. if hub_entry["is_hub"]:
  242. hub_list.append(
  243. SelectOptionDict(
  244. value=key,
  245. label=f"{hub_entry['name']} ({hub_entry['product_name']})",
  246. )
  247. )
  248. _LOGGER.debug(f"Hub count: {len(hub_list) - 1}")
  249. hub_selector = SelectSelector(
  250. SelectSelectorConfig(options=hub_list, mode=SelectSelectorMode.DROPDOWN)
  251. )
  252. # Build form
  253. fields: OrderedDict[vol.Marker, Any] = OrderedDict()
  254. fields[vol.Required("device_id")] = device_selector
  255. fields[vol.Required("hub_id")] = hub_selector
  256. return self.async_show_form(
  257. step_id="choose_device",
  258. data_schema=vol.Schema(fields),
  259. errors=errors or {},
  260. last_step=False,
  261. )
  262. @property
  263. def _device_name_placeholder(self) -> str:
  264. """Return device name placeholder for step descriptions."""
  265. if self.__cloud_device and self.__cloud_device.get("product_name"):
  266. parts = []
  267. if self.__cloud_device.get("name"):
  268. parts.append(self.__cloud_device["name"])
  269. parts.append(self.__cloud_device["product_name"])
  270. return "**" + " — ".join(parts) + "**\n\n"
  271. return ""
  272. async def async_step_search(self, user_input=None):
  273. if user_input is not None:
  274. # Current IP is the WAN IP which is of no use. Need to try and discover to the local IP.
  275. # This scan will take 18s with the default settings. If we cannot find the device we
  276. # will just leave the IP address blank and hope the user can discover the IP by other
  277. # means such as router device IP assignments.
  278. _LOGGER.debug(
  279. f"Scanning network to get IP address for {self.__cloud_device.get('id', 'DEVICE_KEY_UNAVAILABLE')}."
  280. )
  281. self.__cloud_device["ip"] = ""
  282. try:
  283. local_device = await self.hass.async_add_executor_job(
  284. scan_for_device, self.__cloud_device.get("id")
  285. )
  286. except OSError:
  287. local_device = {"ip": None, "version": ""}
  288. if local_device.get("ip"):
  289. _LOGGER.debug(f"Found: {local_device}")
  290. self.__cloud_device["ip"] = local_device.get("ip")
  291. self.__cloud_device["version"] = local_device.get("version")
  292. if not self.__cloud_device.get(CONF_DEVICE_CID):
  293. self.__cloud_device["local_product_id"] = local_device.get(
  294. "productKey"
  295. )
  296. else:
  297. _LOGGER.warning(
  298. f"Could not find device: {self.__cloud_device.get('id', 'DEVICE_KEY_UNAVAILABLE')}"
  299. )
  300. return await self.async_step_local()
  301. return self.async_show_form(
  302. step_id="search",
  303. data_schema=vol.Schema({}),
  304. description_placeholders={
  305. "device_name": self._device_name_placeholder,
  306. },
  307. errors={},
  308. last_step=False,
  309. )
  310. async def async_step_local(self, user_input=None):
  311. errors = {}
  312. devid_opts = {}
  313. host_opts = {"default": ""}
  314. key_opts = {}
  315. proto_opts = {"default": "auto"}
  316. polling_opts = {"default": False}
  317. devcid_opts = {}
  318. if self.__cloud_device is not None:
  319. # We already have some or all of the device settings from the cloud flow. Set them into the defaults.
  320. devid_opts = {"default": self.__cloud_device.get("id")}
  321. host_opts = {"default": self.__cloud_device.get("ip")}
  322. key_opts = {"default": self.__cloud_device.get(CONF_LOCAL_KEY)}
  323. if self.__cloud_device.get("version"):
  324. proto_opts = {"default": str(self.__cloud_device.get("version"))}
  325. if self.__cloud_device.get(CONF_DEVICE_CID):
  326. devcid_opts = {"default": self.__cloud_device.get(CONF_DEVICE_CID)}
  327. if user_input is not None:
  328. proto = user_input.get(CONF_PROTOCOL_VERSION)
  329. if proto != "auto":
  330. user_input[CONF_PROTOCOL_VERSION] = float(proto)
  331. self.device = await async_test_connection(user_input, self.hass)
  332. if self.device:
  333. self.data = user_input
  334. # If auto mode found a working protocol, save it so future
  335. # HA restarts connect directly without re-cycling all versions.
  336. self._auto_detected_protocol = None
  337. if (
  338. user_input.get(CONF_PROTOCOL_VERSION) == "auto"
  339. and self.device._protocol_configured != "auto"
  340. ):
  341. self._auto_detected_protocol = self.device._protocol_configured
  342. self.data = {
  343. **self.data,
  344. CONF_PROTOCOL_VERSION: self._auto_detected_protocol,
  345. }
  346. if self.__cloud_device:
  347. if self.__cloud_device.get("product_id"):
  348. self.device.set_detected_product_id(
  349. self.__cloud_device.get("product_id")
  350. )
  351. if self.__cloud_device.get("local_product_id"):
  352. self.device.set_detected_product_id(
  353. self.__cloud_device.get("local_product_id")
  354. )
  355. await self.async_set_unique_id(
  356. user_input.get(CONF_DEVICE_CID, user_input[CONF_DEVICE_ID])
  357. )
  358. self._abort_if_unique_id_configured()
  359. return await self.async_step_select_type()
  360. else:
  361. errors["base"] = "connection"
  362. devid_opts["default"] = user_input[CONF_DEVICE_ID]
  363. host_opts["default"] = user_input[CONF_HOST]
  364. key_opts["default"] = user_input[CONF_LOCAL_KEY]
  365. if CONF_DEVICE_CID in user_input:
  366. devcid_opts["default"] = user_input[CONF_DEVICE_CID]
  367. proto_opts["default"] = str(user_input[CONF_PROTOCOL_VERSION])
  368. polling_opts["default"] = user_input[CONF_POLL_ONLY]
  369. return self.async_show_form(
  370. step_id="local",
  371. data_schema=vol.Schema(
  372. {
  373. vol.Required(CONF_DEVICE_ID, **devid_opts): str,
  374. vol.Required(CONF_HOST, **host_opts): str,
  375. vol.Required(CONF_LOCAL_KEY, **key_opts): str,
  376. vol.Required(
  377. CONF_PROTOCOL_VERSION,
  378. **proto_opts,
  379. ): vol.In(["auto"] + [str(v) for v in API_PROTOCOL_VERSIONS]),
  380. vol.Required(CONF_POLL_ONLY, **polling_opts): bool,
  381. vol.Optional(CONF_DEVICE_CID, **devcid_opts): str,
  382. }
  383. ),
  384. description_placeholders={
  385. "device_details_url": DEVICE_DETAILS_URL,
  386. "device_name": self._device_name_placeholder,
  387. },
  388. errors=errors,
  389. )
  390. async def async_step_select_type(self, user_input=None):
  391. if user_input is not None:
  392. # Value is a compound key: "config_type||manufacturer||model"
  393. parts = user_input[CONF_TYPE].split("||", 2)
  394. self.data[CONF_TYPE] = parts[0]
  395. if len(parts) > 1 and parts[1]:
  396. self.data[CONF_MANUFACTURER] = parts[1]
  397. if len(parts) > 2 and parts[2]:
  398. self.data[CONF_MODEL] = parts[2]
  399. return await self.async_step_choose_entities()
  400. all_matches = []
  401. best_match = 0
  402. best_matching_type = None
  403. best_matching_key = None
  404. has_product_id_match = False
  405. for dev_type in await self.device.async_possible_types():
  406. q = dev_type.match_quality(
  407. self.device._get_cached_state(),
  408. self.device._product_ids,
  409. )
  410. if q > 100:
  411. has_product_id_match = True
  412. for manufacturer, model in dev_type.product_display_entries(
  413. self.device._product_ids
  414. ):
  415. key = f"{dev_type.config_type}||{manufacturer or ''}||{model or ''}"
  416. parts = [p for p in [manufacturer, model] if p]
  417. if parts:
  418. label = f"{' '.join(parts)} ({dev_type.config_type})"
  419. else:
  420. label = f"{dev_type.name} ({dev_type.config_type})"
  421. all_matches.append((SelectOptionDict(value=key, label=label), q))
  422. if q > best_match:
  423. best_match = q
  424. best_matching_type = dev_type.config_type
  425. best_matching_key = key
  426. if has_product_id_match:
  427. type_options = [opt for opt, q in all_matches if q > 100]
  428. else:
  429. type_options = [opt for opt, _ in all_matches]
  430. best_match = int(best_match)
  431. dps = self.device._get_cached_state()
  432. if self.__cloud_device:
  433. _LOGGER.warning(
  434. "Adding %s device with product id %s",
  435. self.__cloud_device.get("product_name", "UNKNOWN"),
  436. self.__cloud_device.get("product_id", "UNKNOWN"),
  437. )
  438. if self.__cloud_device.get("local_product_id") and self.__cloud_device.get(
  439. "local_product_id"
  440. ) != self.__cloud_device.get("product_id"):
  441. _LOGGER.warning(
  442. "Local product id differs from cloud: %s",
  443. self.__cloud_device.get("local_product_id"),
  444. )
  445. try:
  446. self.init_cloud()
  447. model = await self.cloud.async_get_datamodel(
  448. self.__cloud_device.get("id"),
  449. )
  450. if model:
  451. _LOGGER.warning(
  452. "Partial cloud device spec:\n%s",
  453. log_json(model),
  454. )
  455. except Exception as e:
  456. _LOGGER.warning(
  457. "Unable to fetch data model from cloud: %s %s",
  458. type(e).__name__,
  459. e,
  460. )
  461. _LOGGER.warning(
  462. "Device matches %s with quality of %d%%. LOCAL DPS: %s",
  463. best_matching_type,
  464. best_match,
  465. log_json(dps),
  466. )
  467. _LOGGER.warning(
  468. "Include the previous log messages with any new device request to https://github.com/make-all/tuya-local/issues/",
  469. )
  470. if type_options:
  471. detected = getattr(self, "_auto_detected_protocol", None)
  472. schema = vol.Schema(
  473. {
  474. vol.Required(
  475. CONF_TYPE,
  476. default=best_matching_key,
  477. ): SelectSelector(SelectSelectorConfig(options=type_options)),
  478. }
  479. )
  480. if detected:
  481. return self.async_show_form(
  482. step_id="select_type_auto_detected",
  483. data_schema=schema,
  484. description_placeholders={
  485. "detected_protocol": str(detected),
  486. "device_name": self._device_name_placeholder,
  487. },
  488. )
  489. return self.async_show_form(
  490. step_id="select_type",
  491. data_schema=schema,
  492. description_placeholders={
  493. "device_name": self._device_name_placeholder,
  494. },
  495. )
  496. else:
  497. return self.async_abort(reason="not_supported")
  498. async def async_step_select_type_auto_detected(self, user_input=None):
  499. return await self.async_step_select_type(user_input)
  500. async def async_step_choose_entities(self, user_input=None):
  501. config = await self.hass.async_add_executor_job(
  502. get_config,
  503. self.data[CONF_TYPE],
  504. )
  505. if user_input is not None:
  506. title = user_input[CONF_NAME]
  507. del user_input[CONF_NAME]
  508. return self.async_create_entry(
  509. title=title, data={**self.data, **user_input}
  510. )
  511. default_name = config.name
  512. if self.__cloud_device and self.__cloud_device.get("name"):
  513. default_name = self.__cloud_device["name"]
  514. schema = {vol.Required(CONF_NAME, default=default_name): str}
  515. return self.async_show_form(
  516. step_id="choose_entities",
  517. data_schema=vol.Schema(schema),
  518. description_placeholders={
  519. "device_name": self._device_name_placeholder,
  520. },
  521. )
  522. @staticmethod
  523. @callback
  524. def async_get_options_flow(config_entry: ConfigEntry):
  525. return OptionsFlowHandler()
  526. class OptionsFlowHandler(OptionsFlow):
  527. def __init__(self):
  528. """Initialize options flow."""
  529. pass
  530. async def async_step_init(self, user_input=None):
  531. return await self.async_step_user(user_input)
  532. async def async_step_user(self, user_input=None):
  533. """Manage the options."""
  534. errors = {}
  535. config = {**self.config_entry.data, **self.config_entry.options}
  536. if user_input is not None:
  537. proto = user_input.get(CONF_PROTOCOL_VERSION)
  538. if proto != "auto":
  539. user_input[CONF_PROTOCOL_VERSION] = float(proto)
  540. config = {**config, **user_input}
  541. device = await async_test_connection(config, self.hass)
  542. if device:
  543. return self.async_create_entry(title="", data=user_input)
  544. else:
  545. errors["base"] = "connection"
  546. schema = {
  547. vol.Required(
  548. CONF_LOCAL_KEY,
  549. default=config.get(CONF_LOCAL_KEY, ""),
  550. ): str,
  551. vol.Required(CONF_HOST, default=config.get(CONF_HOST, "")): str,
  552. vol.Required(
  553. CONF_PROTOCOL_VERSION,
  554. default=str(config.get(CONF_PROTOCOL_VERSION, "auto")),
  555. ): vol.In(["auto"] + [str(v) for v in API_PROTOCOL_VERSIONS]),
  556. vol.Required(
  557. CONF_POLL_ONLY, default=config.get(CONF_POLL_ONLY, False)
  558. ): bool,
  559. }
  560. cfg = await self.hass.async_add_executor_job(
  561. get_config,
  562. config[CONF_TYPE],
  563. )
  564. if cfg is None:
  565. return self.async_abort(reason="not_supported")
  566. return self.async_show_form(
  567. step_id="user",
  568. data_schema=vol.Schema(schema),
  569. description_placeholders={"device_details_url": DEVICE_DETAILS_URL},
  570. errors=errors,
  571. )
  572. def create_test_device(hass: HomeAssistant, config: dict):
  573. """Set up a tuya device based on passed in config."""
  574. subdevice_id = config.get(CONF_DEVICE_CID)
  575. device = TuyaLocalDevice(
  576. "Test",
  577. config[CONF_DEVICE_ID],
  578. config[CONF_HOST],
  579. config[CONF_LOCAL_KEY],
  580. config[CONF_PROTOCOL_VERSION],
  581. subdevice_id,
  582. hass,
  583. True,
  584. )
  585. return device
  586. async def async_test_connection(config: dict, hass: HomeAssistant):
  587. domain_data = hass.data.get(DOMAIN)
  588. existing = domain_data.get(get_device_id(config)) if domain_data else None
  589. if existing and existing.get("device"):
  590. _LOGGER.info("Pausing existing device to test new connection parameters")
  591. existing["device"].pause()
  592. await asyncio.sleep(5)
  593. retval = None
  594. if config.get(CONF_PROTOCOL_VERSION) == "auto":
  595. # Test each protocol with a fresh device object. Reusing one device
  596. # object across protocol rotations causes 3.4/3.5 handshakes to fail:
  597. # the shared tinytuya object carries stale internal state from the
  598. # prior connection attempts.
  599. for proto in API_PROTOCOL_VERSIONS:
  600. proto_config = {**config, CONF_PROTOCOL_VERSION: proto}
  601. device = None
  602. try:
  603. device = await hass.async_add_executor_job(
  604. create_test_device, hass, proto_config
  605. )
  606. await device.async_refresh()
  607. if device.has_returned_state:
  608. retval = device
  609. break
  610. except Exception as e:
  611. _LOGGER.debug("Protocol %s test failed with %s %s", proto, type(e), e)
  612. if device is not None:
  613. device._api.set_socketPersistent(False)
  614. if device._api.parent:
  615. device._api.parent.set_socketPersistent(False)
  616. else:
  617. try:
  618. device = await hass.async_add_executor_job(
  619. create_test_device,
  620. hass,
  621. config,
  622. )
  623. await device.async_refresh()
  624. retval = device if device.has_returned_state else None
  625. except Exception as e:
  626. _LOGGER.warning("Connection test failed with %s %s", type(e), e)
  627. if existing and existing.get("device"):
  628. _LOGGER.info("Restarting device after test")
  629. existing["device"].resume()
  630. return retval
  631. def scan_for_device(id):
  632. return tinytuya.find_device(dev_id=id)