test_config_flow.py 20 KB

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