test_config_flow.py 19 KB

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