test_config_flow.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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=3,
  40. title="test",
  41. data={
  42. CONF_DEVICE_ID: "deviceid",
  43. CONF_HOST: "hostname",
  44. CONF_LOCAL_KEY: "localkey",
  45. CONF_TYPE: "kogan_heater",
  46. CONF_CLIMATE: True,
  47. CONF_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")
  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="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. async def test_flow_user_init(hass):
  77. """Test the initialisation of the form in the first step of the config flow."""
  78. result = await hass.config_entries.flow.async_init(
  79. DOMAIN, context={"source": "user"}
  80. )
  81. expected = {
  82. "data_schema": ANY,
  83. "description_placeholders": None,
  84. "errors": {},
  85. "flow_id": ANY,
  86. "handler": DOMAIN,
  87. "step_id": "user",
  88. "type": "form",
  89. "last_step": ANY,
  90. }
  91. assert expected == result
  92. # Check the schema. Simple comparison does not work since they are not
  93. # the same object
  94. try:
  95. result["data_schema"](
  96. {CONF_DEVICE_ID: "test", CONF_LOCAL_KEY: "test", CONF_HOST: "test"}
  97. )
  98. except vol.MultipleInvalid:
  99. assert False
  100. try:
  101. result["data_schema"]({CONF_DEVICE_ID: "missing_some"})
  102. assert False
  103. except vol.MultipleInvalid:
  104. pass
  105. @patch("custom_components.tuya_local.config_flow.TuyaLocalDevice")
  106. async def test_async_test_connection_valid(mock_device, hass):
  107. """Test that device is returned when connection is valid."""
  108. mock_instance = AsyncMock()
  109. mock_instance.has_returned_state = True
  110. mock_device.return_value = mock_instance
  111. device = await config_flow.async_test_connection(
  112. {
  113. CONF_DEVICE_ID: "deviceid",
  114. CONF_LOCAL_KEY: "localkey",
  115. CONF_HOST: "hostname",
  116. },
  117. hass,
  118. )
  119. assert device == mock_instance
  120. @patch("custom_components.tuya_local.config_flow.TuyaLocalDevice")
  121. async def test_async_test_connection_invalid(mock_device, hass):
  122. """Test that None is returned when connection is invalid."""
  123. mock_instance = AsyncMock()
  124. mock_instance.has_returned_state = False
  125. mock_device.return_value = mock_instance
  126. device = await config_flow.async_test_connection(
  127. {
  128. CONF_DEVICE_ID: "deviceid",
  129. CONF_LOCAL_KEY: "localkey",
  130. CONF_HOST: "hostname",
  131. },
  132. hass,
  133. )
  134. assert device is None
  135. @patch("custom_components.tuya_local.config_flow.async_test_connection")
  136. async def test_flow_user_init_invalid_config(mock_test, hass):
  137. """Test errors populated when config is invalid."""
  138. mock_test.return_value = None
  139. flow = await hass.config_entries.flow.async_init(DOMAIN, context={"source": "user"})
  140. result = await hass.config_entries.flow.async_configure(
  141. flow["flow_id"],
  142. user_input={
  143. CONF_DEVICE_ID: "deviceid",
  144. CONF_HOST: "hostname",
  145. CONF_LOCAL_KEY: "badkey",
  146. },
  147. )
  148. assert {"base": "connection"} == result["errors"]
  149. def setup_device_mock(mock, failure=False, type="test"):
  150. mock_type = MagicMock()
  151. mock_type.legacy_type = type
  152. mock_type.match_quality.return_value = 100
  153. mock_iter = MagicMock()
  154. mock_iter.__aiter__.return_value = [mock_type] if not failure else []
  155. mock.async_possible_types = MagicMock(return_value=mock_iter)
  156. @patch("custom_components.tuya_local.config_flow.async_test_connection")
  157. async def test_flow_user_init_data_valid(mock_test, hass):
  158. """Test we advance to the next step when connection config is valid."""
  159. mock_device = MagicMock()
  160. setup_device_mock(mock_device)
  161. mock_test.return_value = mock_device
  162. flow = await hass.config_entries.flow.async_init(DOMAIN, context={"source": "user"})
  163. result = await hass.config_entries.flow.async_configure(
  164. flow["flow_id"],
  165. user_input={
  166. CONF_DEVICE_ID: "deviceid",
  167. CONF_HOST: "hostname",
  168. CONF_LOCAL_KEY: "localkey",
  169. },
  170. )
  171. assert "form" == result["type"]
  172. assert "select_type" == result["step_id"]
  173. @patch.object(config_flow.ConfigFlowHandler, "device")
  174. async def test_flow_select_type_init(mock_device, hass):
  175. """Test the initialisation of the form in the 2nd step of the config flow."""
  176. setup_device_mock(mock_device)
  177. result = await hass.config_entries.flow.async_init(
  178. DOMAIN, context={"source": "select_type"}
  179. )
  180. expected = {
  181. "data_schema": ANY,
  182. "description_placeholders": None,
  183. "errors": None,
  184. "flow_id": ANY,
  185. "handler": DOMAIN,
  186. "step_id": "select_type",
  187. "type": "form",
  188. "last_step": ANY,
  189. }
  190. assert expected == result
  191. # Check the schema. Simple comparison does not work since they are not
  192. # the same object
  193. try:
  194. result["data_schema"]({CONF_TYPE: "test"})
  195. except vol.MultipleInvalid:
  196. assert False
  197. try:
  198. result["data_schema"]({CONF_TYPE: "not_test"})
  199. assert False
  200. except vol.MultipleInvalid:
  201. pass
  202. @patch.object(config_flow.ConfigFlowHandler, "device")
  203. async def test_flow_select_type_aborts_when_no_match(mock_device, hass):
  204. """Test the flow aborts when an unsupported device is used."""
  205. setup_device_mock(mock_device, failure=True)
  206. result = await hass.config_entries.flow.async_init(
  207. DOMAIN, context={"source": "select_type"}
  208. )
  209. assert result["type"] == "abort"
  210. assert result["reason"] == "not_supported"
  211. @patch.object(config_flow.ConfigFlowHandler, "device")
  212. async def test_flow_select_type_data_valid(mock_device, hass):
  213. """Test the flow continues when valid data is supplied."""
  214. setup_device_mock(mock_device, type="kogan_switch")
  215. flow = await hass.config_entries.flow.async_init(
  216. DOMAIN, context={"source": "select_type"}
  217. )
  218. result = await hass.config_entries.flow.async_configure(
  219. flow["flow_id"],
  220. user_input={CONF_TYPE: "kogan_switch"},
  221. )
  222. assert "form" == result["type"]
  223. assert "choose_entities" == result["step_id"]
  224. async def test_flow_choose_entities_init(hass):
  225. """Test the initialisation of the form in the 3rd step of the config flow."""
  226. with patch.dict(config_flow.ConfigFlowHandler.data, {CONF_TYPE: "kogan_switch"}):
  227. result = await hass.config_entries.flow.async_init(
  228. DOMAIN, context={"source": "choose_entities"}
  229. )
  230. expected = {
  231. "data_schema": ANY,
  232. "description_placeholders": None,
  233. "errors": None,
  234. "flow_id": ANY,
  235. "handler": DOMAIN,
  236. "step_id": "choose_entities",
  237. "type": "form",
  238. "last_step": ANY,
  239. }
  240. assert expected == result
  241. # Check the schema. Simple comparison does not work since they are not
  242. # the same object
  243. try:
  244. result["data_schema"]({CONF_NAME: "test", CONF_SWITCH: True})
  245. except vol.MultipleInvalid:
  246. assert False
  247. try:
  248. result["data_schema"]({CONF_CLIMATE: True})
  249. assert False
  250. except vol.MultipleInvalid:
  251. pass
  252. async def test_flow_choose_entities_creates_config_entry(hass, bypass_setup):
  253. """Test the flow ends when data is valid."""
  254. with patch.dict(
  255. config_flow.ConfigFlowHandler.data,
  256. {
  257. CONF_DEVICE_ID: "deviceid",
  258. CONF_LOCAL_KEY: "localkey",
  259. CONF_HOST: "hostname",
  260. CONF_TYPE: "kogan_heater",
  261. },
  262. ):
  263. flow = await hass.config_entries.flow.async_init(
  264. DOMAIN, context={"source": "choose_entities"}
  265. )
  266. result = await hass.config_entries.flow.async_configure(
  267. flow["flow_id"],
  268. user_input={CONF_NAME: "test", CONF_CLIMATE: True, CONF_LOCK: False},
  269. )
  270. expected = {
  271. "version": 3,
  272. "type": "create_entry",
  273. "flow_id": ANY,
  274. "handler": DOMAIN,
  275. "title": "test",
  276. "description": None,
  277. "description_placeholders": None,
  278. "result": ANY,
  279. "options": {},
  280. "data": {
  281. CONF_CLIMATE: True,
  282. CONF_DEVICE_ID: "deviceid",
  283. CONF_HOST: "hostname",
  284. CONF_LOCAL_KEY: "localkey",
  285. CONF_LOCK: False,
  286. CONF_TYPE: "kogan_heater",
  287. },
  288. }
  289. assert expected == result
  290. async def test_options_flow_init(hass):
  291. """Test config flow options."""
  292. config_entry = MockConfigEntry(
  293. domain=DOMAIN,
  294. version=3,
  295. unique_id="uniqueid",
  296. data={
  297. CONF_DEVICE_ID: "deviceid",
  298. CONF_HOST: "hostname",
  299. CONF_LOCAL_KEY: "localkey",
  300. CONF_NAME: "test",
  301. CONF_SWITCH: True,
  302. CONF_TYPE: "kogan_switch",
  303. },
  304. )
  305. config_entry.add_to_hass(hass)
  306. assert await hass.config_entries.async_setup(config_entry.entry_id)
  307. await hass.async_block_till_done()
  308. # show initial form
  309. result = await hass.config_entries.options.async_init(config_entry.entry_id)
  310. assert "form" == result["type"]
  311. assert "user" == result["step_id"]
  312. assert {} == result["errors"]
  313. assert result["data_schema"](
  314. {
  315. CONF_HOST: "hostname",
  316. CONF_LOCAL_KEY: "localkey",
  317. CONF_SWITCH: True,
  318. }
  319. )
  320. @patch("custom_components.tuya_local.config_flow.async_test_connection")
  321. async def test_options_flow_modifies_config(mock_test, hass):
  322. mock_device = MagicMock()
  323. mock_test.return_value = mock_device
  324. config_entry = MockConfigEntry(
  325. domain=DOMAIN,
  326. version=3,
  327. unique_id="uniqueid",
  328. data={
  329. CONF_CLIMATE: True,
  330. CONF_DEVICE_ID: "deviceid",
  331. CONF_HOST: "hostname",
  332. CONF_LOCAL_KEY: "localkey",
  333. CONF_LOCK: True,
  334. CONF_NAME: "test",
  335. CONF_TYPE: "kogan_heater",
  336. },
  337. )
  338. config_entry.add_to_hass(hass)
  339. assert await hass.config_entries.async_setup(config_entry.entry_id)
  340. await hass.async_block_till_done()
  341. # show initial form
  342. form = await hass.config_entries.options.async_init(config_entry.entry_id)
  343. # submit updated config
  344. result = await hass.config_entries.options.async_configure(
  345. form["flow_id"],
  346. user_input={
  347. CONF_CLIMATE: True,
  348. CONF_HOST: "new_hostname",
  349. CONF_LOCAL_KEY: "new_key",
  350. CONF_LOCK: False,
  351. },
  352. )
  353. expected = {
  354. CONF_CLIMATE: True,
  355. CONF_HOST: "new_hostname",
  356. CONF_LOCAL_KEY: "new_key",
  357. CONF_LOCK: False,
  358. }
  359. assert "create_entry" == result["type"]
  360. assert "" == result["title"]
  361. assert result["result"] is True
  362. assert expected == result["data"]
  363. @patch("custom_components.tuya_local.config_flow.async_test_connection")
  364. async def test_options_flow_fails_when_connection_fails(mock_test, hass):
  365. mock_test.return_value = None
  366. config_entry = MockConfigEntry(
  367. domain=DOMAIN,
  368. version=3,
  369. unique_id="uniqueid",
  370. data={
  371. CONF_DEVICE_ID: "deviceid",
  372. CONF_HOST: "hostname",
  373. CONF_LOCAL_KEY: "localkey",
  374. CONF_NAME: "test",
  375. CONF_SWITCH: True,
  376. CONF_TYPE: "kogan_switch",
  377. },
  378. )
  379. config_entry.add_to_hass(hass)
  380. assert await hass.config_entries.async_setup(config_entry.entry_id)
  381. await hass.async_block_till_done()
  382. # show initial form
  383. form = await hass.config_entries.options.async_init(config_entry.entry_id)
  384. # submit updated config
  385. result = await hass.config_entries.options.async_configure(
  386. form["flow_id"],
  387. user_input={
  388. CONF_HOST: "new_hostname",
  389. CONF_LOCAL_KEY: "new_key",
  390. CONF_SWITCH: False,
  391. },
  392. )
  393. assert "form" == result["type"]
  394. assert "user" == result["step_id"]
  395. assert {"base": "connection"} == result["errors"]
  396. @patch("custom_components.tuya_local.config_flow.async_test_connection")
  397. async def test_options_flow_fails_when_config_is_missing(mock_test, hass):
  398. mock_device = MagicMock()
  399. mock_test.return_value = mock_device
  400. config_entry = MockConfigEntry(
  401. domain=DOMAIN,
  402. version=3,
  403. unique_id="uniqueid",
  404. data={
  405. CONF_DEVICE_ID: "deviceid",
  406. CONF_HOST: "hostname",
  407. CONF_LOCAL_KEY: "localkey",
  408. CONF_NAME: "test",
  409. CONF_SWITCH: True,
  410. CONF_TYPE: "non_existing",
  411. },
  412. )
  413. config_entry.add_to_hass(hass)
  414. assert await hass.config_entries.async_setup(config_entry.entry_id)
  415. await hass.async_block_till_done()
  416. # show initial form
  417. result = await hass.config_entries.options.async_init(config_entry.entry_id)
  418. assert result["type"] == "abort"
  419. assert result["reason"] == "not_supported"
  420. # More tests to exercise code branches that earlier tests missed.
  421. @patch("custom_components.tuya_local.setup_device")
  422. async def test_async_setup_entry_for_dehumidifier(mock_setup, hass):
  423. """Test setting up based on a config entry. Repeats test_init_entry."""
  424. config_entry = MockConfigEntry(
  425. domain=DOMAIN,
  426. version=3,
  427. unique_id="uniqueid",
  428. data={
  429. CONF_CLIMATE: False,
  430. CONF_DEVICE_ID: "deviceid",
  431. CONF_FAN: True,
  432. CONF_HOST: "hostname",
  433. CONF_HUMIDIFIER: True,
  434. CONF_LIGHT: True,
  435. CONF_LOCK: False,
  436. CONF_LOCAL_KEY: "localkey",
  437. CONF_NAME: "test",
  438. CONF_TYPE: "dehumidifier",
  439. },
  440. )
  441. assert await async_setup_entry(hass, config_entry)
  442. @patch("custom_components.tuya_local.setup_device")
  443. async def test_async_setup_entry_for_switch(mock_device, hass):
  444. """Test setting up based on a config entry. Repeats test_init_entry."""
  445. config_entry = MockConfigEntry(
  446. domain=DOMAIN,
  447. version=3,
  448. unique_id="uniqueid",
  449. data={
  450. CONF_DEVICE_ID: "deviceid",
  451. CONF_HOST: "hostname",
  452. CONF_LOCAL_KEY: "localkey",
  453. CONF_NAME: "test",
  454. CONF_SWITCH: True,
  455. CONF_TYPE: "kogan_switch",
  456. },
  457. )
  458. assert await async_setup_entry(hass, config_entry)