test_config_flow.py 20 KB

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