config_flow.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. for dev_type in await self.device.async_possible_types():
  405. q = dev_type.match_quality(
  406. self.device._get_cached_state(),
  407. self.device._product_ids,
  408. )
  409. for manufacturer, model in dev_type.product_display_entries(
  410. self.device._product_ids
  411. ):
  412. key = f"{dev_type.config_type}||{manufacturer or ''}||{model or ''}"
  413. parts = [p for p in [manufacturer, model] if p]
  414. if parts:
  415. label = f"{' '.join(parts)} ({dev_type.config_type})"
  416. else:
  417. label = f"{dev_type.name} ({dev_type.config_type})"
  418. all_matches.append((SelectOptionDict(value=key, label=label), q))
  419. if q > best_match:
  420. best_match = q
  421. best_matching_type = dev_type.config_type
  422. best_matching_key = key
  423. all_matches.sort(key=lambda x: x[1], reverse=True)
  424. type_options = [opt for opt, _ in all_matches]
  425. best_match = int(best_match)
  426. dps = self.device._get_cached_state()
  427. if self.__cloud_device:
  428. _LOGGER.warning(
  429. "Adding %s device with product id %s",
  430. self.__cloud_device.get("product_name", "UNKNOWN"),
  431. self.__cloud_device.get("product_id", "UNKNOWN"),
  432. )
  433. if self.__cloud_device.get("local_product_id") and self.__cloud_device.get(
  434. "local_product_id"
  435. ) != self.__cloud_device.get("product_id"):
  436. _LOGGER.warning(
  437. "Local product id differs from cloud: %s",
  438. self.__cloud_device.get("local_product_id"),
  439. )
  440. try:
  441. self.init_cloud()
  442. model = await self.cloud.async_get_datamodel(
  443. self.__cloud_device.get("id"),
  444. )
  445. if model:
  446. _LOGGER.warning(
  447. "Partial cloud device spec:\n%s",
  448. log_json(model),
  449. )
  450. except Exception as e:
  451. _LOGGER.warning(
  452. "Unable to fetch data model from cloud: %s %s",
  453. type(e).__name__,
  454. e,
  455. )
  456. _LOGGER.warning(
  457. "Device matches %s with quality of %d%%. LOCAL DPS: %s",
  458. best_matching_type,
  459. best_match,
  460. log_json(dps),
  461. )
  462. _LOGGER.warning(
  463. "Include the previous log messages with any new device request to https://github.com/make-all/tuya-local/issues/",
  464. )
  465. if type_options:
  466. detected = getattr(self, "_auto_detected_protocol", None)
  467. schema = vol.Schema(
  468. {
  469. vol.Required(
  470. CONF_TYPE,
  471. default=best_matching_key,
  472. ): SelectSelector(SelectSelectorConfig(options=type_options)),
  473. }
  474. )
  475. if detected:
  476. return self.async_show_form(
  477. step_id="select_type_auto_detected",
  478. data_schema=schema,
  479. description_placeholders={
  480. "detected_protocol": str(detected),
  481. "device_name": self._device_name_placeholder,
  482. },
  483. )
  484. return self.async_show_form(
  485. step_id="select_type",
  486. data_schema=schema,
  487. description_placeholders={
  488. "device_name": self._device_name_placeholder,
  489. },
  490. )
  491. else:
  492. return self.async_abort(reason="not_supported")
  493. async def async_step_select_type_auto_detected(self, user_input=None):
  494. return await self.async_step_select_type(user_input)
  495. async def async_step_choose_entities(self, user_input=None):
  496. config = await self.hass.async_add_executor_job(
  497. get_config,
  498. self.data[CONF_TYPE],
  499. )
  500. if user_input is not None:
  501. title = user_input[CONF_NAME]
  502. del user_input[CONF_NAME]
  503. return self.async_create_entry(
  504. title=title, data={**self.data, **user_input}
  505. )
  506. default_name = config.name
  507. if self.__cloud_device and self.__cloud_device.get("name"):
  508. default_name = self.__cloud_device["name"]
  509. schema = {vol.Required(CONF_NAME, default=default_name): str}
  510. return self.async_show_form(
  511. step_id="choose_entities",
  512. data_schema=vol.Schema(schema),
  513. description_placeholders={
  514. "device_name": self._device_name_placeholder,
  515. },
  516. )
  517. @staticmethod
  518. @callback
  519. def async_get_options_flow(config_entry: ConfigEntry):
  520. return OptionsFlowHandler()
  521. class OptionsFlowHandler(OptionsFlow):
  522. def __init__(self):
  523. """Initialize options flow."""
  524. pass
  525. async def async_step_init(self, user_input=None):
  526. return await self.async_step_user(user_input)
  527. async def async_step_user(self, user_input=None):
  528. """Manage the options."""
  529. errors = {}
  530. config = {**self.config_entry.data, **self.config_entry.options}
  531. if user_input is not None:
  532. proto = user_input.get(CONF_PROTOCOL_VERSION)
  533. if proto != "auto":
  534. user_input[CONF_PROTOCOL_VERSION] = float(proto)
  535. config = {**config, **user_input}
  536. device = await async_test_connection(config, self.hass)
  537. if device:
  538. return self.async_create_entry(title="", data=user_input)
  539. else:
  540. errors["base"] = "connection"
  541. schema = {
  542. vol.Required(
  543. CONF_LOCAL_KEY,
  544. default=config.get(CONF_LOCAL_KEY, ""),
  545. ): str,
  546. vol.Required(CONF_HOST, default=config.get(CONF_HOST, "")): str,
  547. vol.Required(
  548. CONF_PROTOCOL_VERSION,
  549. default=str(config.get(CONF_PROTOCOL_VERSION, "auto")),
  550. ): vol.In(["auto"] + [str(v) for v in API_PROTOCOL_VERSIONS]),
  551. vol.Required(
  552. CONF_POLL_ONLY, default=config.get(CONF_POLL_ONLY, False)
  553. ): bool,
  554. }
  555. cfg = await self.hass.async_add_executor_job(
  556. get_config,
  557. config[CONF_TYPE],
  558. )
  559. if cfg is None:
  560. return self.async_abort(reason="not_supported")
  561. return self.async_show_form(
  562. step_id="user",
  563. data_schema=vol.Schema(schema),
  564. description_placeholders={"device_details_url": DEVICE_DETAILS_URL},
  565. errors=errors,
  566. )
  567. def create_test_device(hass: HomeAssistant, config: dict):
  568. """Set up a tuya device based on passed in config."""
  569. subdevice_id = config.get(CONF_DEVICE_CID)
  570. device = TuyaLocalDevice(
  571. "Test",
  572. config[CONF_DEVICE_ID],
  573. config[CONF_HOST],
  574. config[CONF_LOCAL_KEY],
  575. config[CONF_PROTOCOL_VERSION],
  576. subdevice_id,
  577. hass,
  578. True,
  579. )
  580. return device
  581. async def async_test_connection(config: dict, hass: HomeAssistant):
  582. domain_data = hass.data.get(DOMAIN)
  583. existing = domain_data.get(get_device_id(config)) if domain_data else None
  584. if existing and existing.get("device"):
  585. _LOGGER.info("Pausing existing device to test new connection parameters")
  586. existing["device"].pause()
  587. await asyncio.sleep(5)
  588. retval = None
  589. if config.get(CONF_PROTOCOL_VERSION) == "auto":
  590. # Test each protocol with a fresh device object. Reusing one device
  591. # object across protocol rotations causes 3.4/3.5 handshakes to fail:
  592. # the shared tinytuya object carries stale internal state from the
  593. # prior connection attempts.
  594. for proto in API_PROTOCOL_VERSIONS:
  595. proto_config = {**config, CONF_PROTOCOL_VERSION: proto}
  596. device = None
  597. try:
  598. device = await hass.async_add_executor_job(
  599. create_test_device, hass, proto_config
  600. )
  601. await device.async_refresh()
  602. if device.has_returned_state:
  603. retval = device
  604. break
  605. except Exception as e:
  606. _LOGGER.debug("Protocol %s test failed with %s %s", proto, type(e), e)
  607. if device is not None:
  608. device._api.set_socketPersistent(False)
  609. if device._api.parent:
  610. device._api.parent.set_socketPersistent(False)
  611. else:
  612. try:
  613. device = await hass.async_add_executor_job(
  614. create_test_device,
  615. hass,
  616. config,
  617. )
  618. await device.async_refresh()
  619. retval = device if device.has_returned_state else None
  620. except Exception as e:
  621. _LOGGER.warning("Connection test failed with %s %s", type(e), e)
  622. if existing and existing.get("device"):
  623. _LOGGER.info("Restarting device after test")
  624. existing["device"].resume()
  625. return retval
  626. def scan_for_device(id):
  627. return tinytuya.find_device(dev_id=id)