test_config_flow.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. """Tests for the config flow."""
  2. import pytest
  3. import voluptuous as vol
  4. from homeassistant.const import CONF_HOST, CONF_NAME
  5. from homeassistant.data_entry_flow import FlowResultType
  6. from pytest_homeassistant_custom_component.common import MockConfigEntry
  7. from custom_components.tuya_local import (
  8. async_migrate_entry,
  9. config_flow,
  10. get_device_unique_id,
  11. )
  12. from custom_components.tuya_local.const import (
  13. CONF_DEVICE_CID,
  14. CONF_DEVICE_ID,
  15. CONF_LOCAL_KEY,
  16. CONF_POLL_ONLY,
  17. CONF_PROTOCOL_VERSION,
  18. CONF_TYPE,
  19. DOMAIN,
  20. )
  21. # Designed to contain "special" characters that users constantly suspect.
  22. TESTKEY = ")<jO<@)'P1|kR$Kd"
  23. @pytest.fixture(autouse=True)
  24. def auto_enable_custom_integrations(enable_custom_integrations):
  25. yield
  26. @pytest.fixture(autouse=True)
  27. def prevent_task_creation(mocker):
  28. mocker.patch("custom_components.tuya_local.device.TuyaLocalDevice.register_entity")
  29. yield
  30. @pytest.fixture
  31. def bypass_setup(mocker):
  32. """Prevent actual setup of the integration after config flow."""
  33. mocker.patch("custom_components.tuya_local.async_setup_entry", return_value=True)
  34. yield
  35. @pytest.fixture
  36. def bypass_data_fetch(mocker):
  37. """Prevent actual data fetching from the device."""
  38. mocker.patch("tinytuya.Device.status", return_value={"1": True})
  39. yield
  40. @pytest.mark.asyncio
  41. async def test_init_entry(hass, bypass_data_fetch):
  42. """Test initialisation of the config flow."""
  43. entry = MockConfigEntry(
  44. domain=DOMAIN,
  45. version=11,
  46. title="test",
  47. data={
  48. CONF_DEVICE_ID: "deviceid",
  49. CONF_HOST: "hostname",
  50. CONF_LOCAL_KEY: TESTKEY,
  51. CONF_POLL_ONLY: False,
  52. CONF_PROTOCOL_VERSION: "auto",
  53. CONF_TYPE: "kogan_kahtp_heater",
  54. CONF_DEVICE_CID: None,
  55. },
  56. options={},
  57. )
  58. entry.add_to_hass(hass)
  59. await hass.config_entries.async_setup(entry.entry_id)
  60. await hass.async_block_till_done()
  61. assert hass.states.get("climate.test")
  62. assert hass.states.get("lock.test_child_lock")
  63. @pytest.mark.asyncio
  64. async def test_migrate_entry(hass, mocker):
  65. """Test migration from old entry format."""
  66. mock_device = mocker.MagicMock()
  67. mock_device.async_inferred_type = mocker.AsyncMock(
  68. return_value="goldair_gpph_heater"
  69. )
  70. mocker.patch("custom_components.tuya_local.setup_device", return_value=mock_device)
  71. entry = MockConfigEntry(
  72. domain=DOMAIN,
  73. version=1,
  74. title="test",
  75. data={
  76. CONF_DEVICE_ID: "deviceid",
  77. CONF_HOST: "hostname",
  78. CONF_LOCAL_KEY: TESTKEY,
  79. CONF_TYPE: "auto",
  80. "climate": True,
  81. "child_lock": True,
  82. "display_light": True,
  83. },
  84. )
  85. entry.add_to_hass(hass)
  86. assert await async_migrate_entry(hass, entry)
  87. mock_device.async_inferred_type = mocker.AsyncMock(return_value=None)
  88. mock_device.reset_mock()
  89. entry = MockConfigEntry(
  90. domain=DOMAIN,
  91. version=1,
  92. title="test2",
  93. data={
  94. CONF_DEVICE_ID: "deviceid",
  95. CONF_HOST: "hostname",
  96. CONF_LOCAL_KEY: TESTKEY,
  97. CONF_TYPE: "unknown",
  98. "climate": False,
  99. },
  100. )
  101. entry.add_to_hass(hass)
  102. assert not await async_migrate_entry(hass, entry)
  103. mock_device.reset_mock()
  104. entry = MockConfigEntry(
  105. domain=DOMAIN,
  106. version=2,
  107. title="test3",
  108. data={
  109. CONF_DEVICE_ID: "deviceid",
  110. CONF_HOST: "hostname",
  111. CONF_LOCAL_KEY: TESTKEY,
  112. CONF_TYPE: "auto",
  113. },
  114. options={
  115. "climate": False,
  116. },
  117. )
  118. entry.add_to_hass(hass)
  119. assert not await async_migrate_entry(hass, entry)
  120. mock_device.async_inferred_type = mocker.AsyncMock(return_value="smartplugv1")
  121. mock_device.reset_mock()
  122. entry = MockConfigEntry(
  123. domain=DOMAIN,
  124. version=3,
  125. title="test4",
  126. data={
  127. CONF_DEVICE_ID: "deviceid",
  128. CONF_HOST: "hostname",
  129. CONF_LOCAL_KEY: TESTKEY,
  130. CONF_TYPE: "smartplugv1",
  131. },
  132. options={
  133. "switch": True,
  134. },
  135. )
  136. entry.add_to_hass(hass)
  137. assert await async_migrate_entry(hass, entry)
  138. mock_device.async_inferred_type = mocker.AsyncMock(return_value="smartplugv2")
  139. mock_device.reset_mock()
  140. entry = MockConfigEntry(
  141. domain=DOMAIN,
  142. version=3,
  143. title="test5",
  144. data={
  145. CONF_DEVICE_ID: "deviceid",
  146. CONF_HOST: "hostname",
  147. CONF_LOCAL_KEY: TESTKEY,
  148. CONF_TYPE: "smartplugv1",
  149. },
  150. options={
  151. "switch": True,
  152. },
  153. )
  154. entry.add_to_hass(hass)
  155. assert await async_migrate_entry(hass, entry)
  156. mock_device.async_inferred_type = mocker.AsyncMock(
  157. return_value="goldair_dehumidifier"
  158. )
  159. mock_device.reset_mock()
  160. entry = MockConfigEntry(
  161. domain=DOMAIN,
  162. version=4,
  163. title="test6",
  164. data={
  165. CONF_DEVICE_ID: "deviceid",
  166. CONF_HOST: "hostname",
  167. CONF_LOCAL_KEY: TESTKEY,
  168. CONF_TYPE: "goldair_dehumidifier",
  169. },
  170. options={
  171. "humidifier": True,
  172. "fan": True,
  173. "light": True,
  174. "lock": False,
  175. "switch": True,
  176. },
  177. )
  178. entry.add_to_hass(hass)
  179. assert await async_migrate_entry(hass, entry)
  180. mock_device.async_inferred_type = mocker.AsyncMock(
  181. return_value="grid_connect_usb_double_power_point"
  182. )
  183. mock_device.reset_mock()
  184. entry = MockConfigEntry(
  185. domain=DOMAIN,
  186. version=6,
  187. title="test7",
  188. data={
  189. CONF_DEVICE_ID: "deviceid",
  190. CONF_HOST: "hostname",
  191. CONF_LOCAL_KEY: TESTKEY,
  192. CONF_TYPE: "grid_connect_usb_double_power_point",
  193. },
  194. options={
  195. "switch_main_switch": True,
  196. "switch_left_outlet": True,
  197. "switch_right_outlet": True,
  198. },
  199. )
  200. entry.add_to_hass(hass)
  201. assert await async_migrate_entry(hass, entry)
  202. @pytest.mark.asyncio
  203. async def test_flow_user_init(hass, mocker):
  204. """Test the initialisation of the form in the first page of the manual config flow path."""
  205. result = await hass.config_entries.flow.async_init(
  206. DOMAIN, context={"source": "local"}
  207. )
  208. expected = {
  209. "data_schema": mocker.ANY,
  210. "description_placeholders": mocker.ANY,
  211. "errors": {},
  212. "flow_id": mocker.ANY,
  213. "handler": DOMAIN,
  214. "step_id": "local",
  215. "type": "form",
  216. "last_step": mocker.ANY,
  217. "preview": mocker.ANY,
  218. }
  219. assert expected == result
  220. # Check the schema. Simple comparison does not work since they are not
  221. # the same object
  222. try:
  223. result["data_schema"](
  224. {CONF_DEVICE_ID: "test", CONF_LOCAL_KEY: TESTKEY, CONF_HOST: "test"}
  225. )
  226. except vol.MultipleInvalid:
  227. assert False
  228. try:
  229. result["data_schema"]({CONF_DEVICE_ID: "missing_some"})
  230. assert False
  231. except vol.MultipleInvalid:
  232. pass
  233. @pytest.mark.asyncio
  234. async def test_async_test_connection_valid(hass, mocker):
  235. """Test that device is returned when connection is valid."""
  236. mock_device = mocker.patch(
  237. "custom_components.tuya_local.config_flow.TuyaLocalDevice"
  238. )
  239. mock_instance = mocker.AsyncMock()
  240. mock_instance.has_returned_state = True
  241. mock_instance.pause = mocker.MagicMock()
  242. mock_instance.resume = mocker.MagicMock()
  243. mock_device.return_value = mock_instance
  244. hass.data[DOMAIN] = {"deviceid": {"device": mock_instance}}
  245. device = await config_flow.async_test_connection(
  246. {
  247. CONF_DEVICE_ID: "deviceid",
  248. CONF_LOCAL_KEY: TESTKEY,
  249. CONF_HOST: "hostname",
  250. CONF_PROTOCOL_VERSION: "auto",
  251. },
  252. hass,
  253. )
  254. assert device == mock_instance
  255. mock_instance.pause.assert_called_once()
  256. mock_instance.resume.assert_called_once()
  257. @pytest.mark.asyncio
  258. async def test_async_test_connection_for_subdevice_valid(hass, mocker):
  259. """Test that subdevice is returned when connection is valid."""
  260. mock_device = mocker.patch(
  261. "custom_components.tuya_local.config_flow.TuyaLocalDevice"
  262. )
  263. mock_instance = mocker.AsyncMock()
  264. mock_instance.has_returned_state = True
  265. mock_instance.pause = mocker.MagicMock()
  266. mock_instance.resume = mocker.MagicMock()
  267. mock_device.return_value = mock_instance
  268. hass.data[DOMAIN] = {"subdeviceid": {"device": mock_instance}}
  269. device = await config_flow.async_test_connection(
  270. {
  271. CONF_DEVICE_ID: "deviceid",
  272. CONF_LOCAL_KEY: TESTKEY,
  273. CONF_HOST: "hostname",
  274. CONF_PROTOCOL_VERSION: "auto",
  275. CONF_DEVICE_CID: "subdeviceid",
  276. },
  277. hass,
  278. )
  279. assert device == mock_instance
  280. mock_instance.pause.assert_called_once()
  281. mock_instance.resume.assert_called_once()
  282. @pytest.mark.asyncio
  283. async def test_async_test_connection_invalid(hass, mocker):
  284. """Test that None is returned when connection is invalid."""
  285. mock_device = mocker.patch(
  286. "custom_components.tuya_local.config_flow.TuyaLocalDevice"
  287. )
  288. mock_instance = mocker.AsyncMock()
  289. mock_instance.has_returned_state = False
  290. mock_instance._api = mocker.MagicMock()
  291. mock_device.return_value = mock_instance
  292. device = await config_flow.async_test_connection(
  293. {
  294. CONF_DEVICE_ID: "deviceid",
  295. CONF_LOCAL_KEY: TESTKEY,
  296. CONF_HOST: "hostname",
  297. CONF_PROTOCOL_VERSION: "auto",
  298. },
  299. hass,
  300. )
  301. assert device is None
  302. @pytest.mark.asyncio
  303. async def test_flow_user_init_invalid_config(hass, mocker):
  304. """Test errors populated when config is invalid."""
  305. mocker.patch(
  306. "custom_components.tuya_local.config_flow.async_test_connection",
  307. return_value=None,
  308. )
  309. flow = await hass.config_entries.flow.async_init(
  310. DOMAIN, context={"source": "local"}
  311. )
  312. result = await hass.config_entries.flow.async_configure(
  313. flow["flow_id"],
  314. user_input={
  315. CONF_DEVICE_ID: "deviceid",
  316. CONF_HOST: "hostname",
  317. CONF_LOCAL_KEY: "badkey",
  318. CONF_PROTOCOL_VERSION: "auto",
  319. CONF_POLL_ONLY: False,
  320. },
  321. )
  322. assert {"base": "connection"} == result["errors"]
  323. def setup_device_mock(mock, mocker, failure=False, type="test"):
  324. mock_type = mocker.MagicMock()
  325. mock_type.legacy_type = type
  326. mock_type.config_type = type
  327. mock_type.match_quality.return_value = 100
  328. mock.async_possible_types = mocker.AsyncMock(
  329. return_value=[mock_type] if not failure else []
  330. )
  331. @pytest.mark.asyncio
  332. async def test_flow_user_init_data_valid(hass, mocker):
  333. """Test we advance to the next step when connection config is valid."""
  334. mock_device = mocker.MagicMock()
  335. mock_device._protocol_configured = "auto"
  336. setup_device_mock(mock_device, mocker)
  337. mocker.patch(
  338. "custom_components.tuya_local.config_flow.async_test_connection",
  339. return_value=mock_device,
  340. )
  341. flow = await hass.config_entries.flow.async_init(
  342. DOMAIN, context={"source": "local"}
  343. )
  344. result = await hass.config_entries.flow.async_configure(
  345. flow["flow_id"],
  346. user_input={
  347. CONF_DEVICE_ID: "deviceid",
  348. CONF_HOST: "hostname",
  349. CONF_LOCAL_KEY: TESTKEY,
  350. },
  351. )
  352. assert "form" == result["type"]
  353. assert "select_type" == result["step_id"]
  354. @pytest.mark.asyncio
  355. async def test_flow_select_type_init(hass, mocker):
  356. """Test the initialisation of the form in the 2nd step of the config flow."""
  357. mock_device = mocker.patch.object(config_flow.ConfigFlowHandler, "device")
  358. setup_device_mock(mock_device, mocker)
  359. result = await hass.config_entries.flow.async_init(
  360. DOMAIN, context={"source": "select_type"}
  361. )
  362. expected = {
  363. "data_schema": mocker.ANY,
  364. "description_placeholders": None,
  365. "errors": None,
  366. "flow_id": mocker.ANY,
  367. "handler": DOMAIN,
  368. "step_id": "select_type",
  369. "type": "form",
  370. "last_step": mocker.ANY,
  371. "preview": mocker.ANY,
  372. }
  373. assert expected == result
  374. # Check the schema. Simple comparison does not work since they are not
  375. # the same object
  376. try:
  377. result["data_schema"]({CONF_TYPE: "test"})
  378. except vol.MultipleInvalid:
  379. assert False
  380. try:
  381. result["data_schema"]({CONF_TYPE: "not_test"})
  382. assert False
  383. except vol.MultipleInvalid:
  384. pass
  385. @pytest.mark.asyncio
  386. async def test_flow_select_type_aborts_when_no_match(hass, mocker):
  387. """Test the flow aborts when an unsupported device is used."""
  388. mock_device = mocker.patch.object(config_flow.ConfigFlowHandler, "device")
  389. setup_device_mock(mock_device, mocker, failure=True)
  390. result = await hass.config_entries.flow.async_init(
  391. DOMAIN, context={"source": "select_type"}
  392. )
  393. assert result["type"] == "abort"
  394. assert result["reason"] == "not_supported"
  395. @pytest.mark.asyncio
  396. async def test_flow_select_type_data_valid(hass, mocker):
  397. """Test the flow continues when valid data is supplied."""
  398. mock_device = mocker.patch.object(config_flow.ConfigFlowHandler, "device")
  399. setup_device_mock(mock_device, mocker, type="smartplugv1")
  400. flow = await hass.config_entries.flow.async_init(
  401. DOMAIN, context={"source": "select_type"}
  402. )
  403. result = await hass.config_entries.flow.async_configure(
  404. flow["flow_id"],
  405. user_input={CONF_TYPE: "smartplugv1"},
  406. )
  407. assert "form" == result["type"]
  408. assert "choose_entities" == result["step_id"]
  409. @pytest.mark.asyncio
  410. async def test_flow_choose_entities_init(hass, mocker):
  411. """Test the initialisation of the form in the 3rd step of the config flow."""
  412. mocker.patch.dict(config_flow.ConfigFlowHandler.data, {CONF_TYPE: "smartplugv1"})
  413. result = await hass.config_entries.flow.async_init(
  414. DOMAIN, context={"source": "choose_entities"}
  415. )
  416. expected = {
  417. "data_schema": mocker.ANY,
  418. "description_placeholders": None,
  419. "errors": None,
  420. "flow_id": mocker.ANY,
  421. "handler": DOMAIN,
  422. "step_id": "choose_entities",
  423. "type": "form",
  424. "last_step": mocker.ANY,
  425. "preview": mocker.ANY,
  426. }
  427. assert expected == result
  428. # Check the schema. Simple comparison does not work since they are not
  429. # the same object
  430. try:
  431. result["data_schema"]({CONF_NAME: "test"})
  432. except vol.MultipleInvalid:
  433. assert False
  434. try:
  435. result["data_schema"]({"climate": True})
  436. assert False
  437. except vol.MultipleInvalid:
  438. pass
  439. @pytest.mark.asyncio
  440. async def test_flow_choose_entities_creates_config_entry(hass, bypass_setup, mocker):
  441. """Test the flow ends when data is valid."""
  442. mocker.patch.dict(
  443. config_flow.ConfigFlowHandler.data,
  444. {
  445. CONF_DEVICE_ID: "deviceid",
  446. CONF_LOCAL_KEY: TESTKEY,
  447. CONF_HOST: "hostname",
  448. CONF_POLL_ONLY: False,
  449. CONF_PROTOCOL_VERSION: "auto",
  450. CONF_TYPE: "kogan_kahtp_heater",
  451. CONF_DEVICE_CID: None,
  452. },
  453. )
  454. flow = await hass.config_entries.flow.async_init(
  455. DOMAIN, context={"source": "choose_entities"}
  456. )
  457. result = await hass.config_entries.flow.async_configure(
  458. flow["flow_id"],
  459. user_input={
  460. CONF_NAME: "test",
  461. },
  462. )
  463. expected = {
  464. "version": 13,
  465. "minor_version": mocker.ANY,
  466. "context": {"source": "choose_entities"},
  467. "type": FlowResultType.CREATE_ENTRY,
  468. "flow_id": mocker.ANY,
  469. "handler": DOMAIN,
  470. "title": "test",
  471. "description": None,
  472. "description_placeholders": None,
  473. "result": mocker.ANY,
  474. "subentries": (),
  475. "options": {},
  476. "data": {
  477. CONF_DEVICE_ID: "deviceid",
  478. CONF_HOST: "hostname",
  479. CONF_LOCAL_KEY: TESTKEY,
  480. CONF_POLL_ONLY: False,
  481. CONF_PROTOCOL_VERSION: "auto",
  482. CONF_TYPE: "kogan_kahtp_heater",
  483. CONF_DEVICE_CID: None,
  484. },
  485. }
  486. assert expected == result
  487. @pytest.mark.asyncio
  488. async def test_options_flow_init(hass, bypass_data_fetch):
  489. """Test config flow options."""
  490. config_entry = MockConfigEntry(
  491. domain=DOMAIN,
  492. version=13,
  493. unique_id="uniqueid",
  494. data={
  495. CONF_DEVICE_ID: "deviceid",
  496. CONF_HOST: "hostname",
  497. CONF_LOCAL_KEY: TESTKEY,
  498. CONF_NAME: "test",
  499. CONF_POLL_ONLY: False,
  500. CONF_PROTOCOL_VERSION: "auto",
  501. CONF_TYPE: "smartplugv1",
  502. CONF_DEVICE_CID: "",
  503. },
  504. )
  505. config_entry.add_to_hass(hass)
  506. assert await hass.config_entries.async_setup(config_entry.entry_id)
  507. await hass.async_block_till_done()
  508. # show initial form
  509. result = await hass.config_entries.options.async_init(config_entry.entry_id)
  510. assert "form" == result["type"]
  511. assert "user" == result["step_id"]
  512. assert {} == result["errors"]
  513. assert result["data_schema"](
  514. {
  515. CONF_HOST: "hostname",
  516. CONF_LOCAL_KEY: TESTKEY,
  517. }
  518. )
  519. @pytest.mark.asyncio
  520. async def test_options_flow_modifies_config(hass, bypass_setup, mocker):
  521. mock_device = mocker.MagicMock()
  522. mocker.patch(
  523. "custom_components.tuya_local.config_flow.async_test_connection",
  524. return_value=mock_device,
  525. )
  526. config_entry = MockConfigEntry(
  527. domain=DOMAIN,
  528. version=13,
  529. unique_id="uniqueid",
  530. data={
  531. CONF_DEVICE_ID: "deviceid",
  532. CONF_HOST: "hostname",
  533. CONF_LOCAL_KEY: TESTKEY,
  534. CONF_NAME: "test",
  535. CONF_POLL_ONLY: False,
  536. CONF_PROTOCOL_VERSION: "auto",
  537. CONF_TYPE: "ble_pt216_temp_humidity",
  538. CONF_DEVICE_CID: "subdeviceid",
  539. },
  540. )
  541. config_entry.add_to_hass(hass)
  542. assert await hass.config_entries.async_setup(config_entry.entry_id)
  543. await hass.async_block_till_done()
  544. # show initial form
  545. form = await hass.config_entries.options.async_init(config_entry.entry_id)
  546. # submit updated config
  547. result = await hass.config_entries.options.async_configure(
  548. form["flow_id"],
  549. user_input={
  550. CONF_HOST: "new_hostname",
  551. CONF_LOCAL_KEY: "new_key",
  552. CONF_POLL_ONLY: False,
  553. CONF_PROTOCOL_VERSION: 3.3,
  554. },
  555. )
  556. expected = {
  557. CONF_HOST: "new_hostname",
  558. CONF_LOCAL_KEY: "new_key",
  559. CONF_POLL_ONLY: False,
  560. CONF_PROTOCOL_VERSION: 3.3,
  561. }
  562. assert "create_entry" == result["type"]
  563. assert "" == result["title"]
  564. assert expected == result["data"]
  565. @pytest.mark.asyncio
  566. async def test_options_flow_fails_when_connection_fails(
  567. hass, bypass_data_fetch, mocker
  568. ):
  569. mocker.patch(
  570. "custom_components.tuya_local.config_flow.async_test_connection",
  571. return_value=None,
  572. )
  573. config_entry = MockConfigEntry(
  574. domain=DOMAIN,
  575. version=13,
  576. unique_id="uniqueid",
  577. data={
  578. CONF_DEVICE_ID: "deviceid",
  579. CONF_HOST: "hostname",
  580. CONF_LOCAL_KEY: TESTKEY,
  581. CONF_NAME: "test",
  582. CONF_POLL_ONLY: False,
  583. CONF_PROTOCOL_VERSION: "auto",
  584. CONF_TYPE: "smartplugv1",
  585. CONF_DEVICE_CID: "",
  586. },
  587. )
  588. config_entry.add_to_hass(hass)
  589. assert await hass.config_entries.async_setup(config_entry.entry_id)
  590. await hass.async_block_till_done()
  591. # show initial form
  592. form = await hass.config_entries.options.async_init(config_entry.entry_id)
  593. # submit updated config
  594. result = await hass.config_entries.options.async_configure(
  595. form["flow_id"],
  596. user_input={
  597. CONF_HOST: "new_hostname",
  598. CONF_LOCAL_KEY: "new_key",
  599. },
  600. )
  601. assert "form" == result["type"]
  602. assert "user" == result["step_id"]
  603. assert {"base": "connection"} == result["errors"]
  604. @pytest.mark.asyncio
  605. async def test_options_flow_fails_when_config_is_missing(hass, mocker):
  606. mock_device = mocker.MagicMock()
  607. mocker.patch(
  608. "custom_components.tuya_local.config_flow.async_test_connection",
  609. return_value=mock_device,
  610. )
  611. config_entry = MockConfigEntry(
  612. domain=DOMAIN,
  613. version=13,
  614. unique_id="uniqueid",
  615. data={
  616. CONF_DEVICE_ID: "deviceid",
  617. CONF_HOST: "hostname",
  618. CONF_LOCAL_KEY: TESTKEY,
  619. CONF_NAME: "test",
  620. CONF_POLL_ONLY: False,
  621. CONF_PROTOCOL_VERSION: "auto",
  622. CONF_TYPE: "non_existing",
  623. },
  624. )
  625. config_entry.add_to_hass(hass)
  626. await hass.config_entries.async_setup(config_entry.entry_id)
  627. await hass.async_block_till_done()
  628. # show initial form
  629. result = await hass.config_entries.options.async_init(config_entry.entry_id)
  630. assert result["type"] == "abort"
  631. assert result["reason"] == "not_supported"
  632. def test_migration_gets_correct_device_id():
  633. """Test that migration gets the correct device id."""
  634. # Normal device
  635. entry = MockConfigEntry(
  636. domain=DOMAIN,
  637. version=1,
  638. title="test",
  639. data={
  640. CONF_DEVICE_ID: "deviceid",
  641. CONF_HOST: "hostname",
  642. CONF_LOCAL_KEY: TESTKEY,
  643. CONF_TYPE: "auto",
  644. },
  645. )
  646. assert get_device_unique_id(entry) == "deviceid"