test_config_flow.py 21 KB

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