Skip to content

Commit b2e7d59

Browse files
committed
add tests and fix bugs
1 parent c8a4cf0 commit b2e7d59

File tree

2 files changed

+116
-5
lines changed

2 files changed

+116
-5
lines changed

custom_components/pyscript/config_flow.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Config flow for Vizio."""
2-
import copy
2+
import voluptuous as vol
33

44
from homeassistant import config_entries
55

6-
from . import PYSCRIPT_SCHEMA
76
from .const import CONF_ALLOW_ALL_IMPORTS, DOMAIN
87

8+
PYSCRIPT_SCHEMA = vol.Schema(
9+
{vol.Optional(CONF_ALLOW_ALL_IMPORTS, default=False): bool}, extra=vol.ALLOW_EXTRA,
10+
)
11+
912

1013
class PyscriptConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
1114
"""Handle a pyscript config flow."""
@@ -16,9 +19,10 @@ class PyscriptConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
1619
async def async_step_user(self, user_input):
1720
"""Handle a flow initialized by the user."""
1821
if user_input is not None:
19-
if await self.async_set_unique_id(unique_id=DOMAIN, raise_on_progress=True):
20-
self.async_abort(reason="single_instance_allowed")
22+
if len(self.hass.config_entries.async_entries(DOMAIN)) > 0:
23+
return self.async_abort(reason="single_instance_allowed")
2124

25+
await self.async_set_unique_id(DOMAIN)
2226
return self.async_create_entry(title=DOMAIN, data=user_input)
2327

2428
return self.async_show_form(step_id="user", data_schema=PYSCRIPT_SCHEMA)
@@ -33,7 +37,7 @@ async def async_step_import(self, import_config):
3337
if entry.data.get(CONF_ALLOW_ALL_IMPORTS, False) != import_config.get(
3438
CONF_ALLOW_ALL_IMPORTS, False
3539
):
36-
updated_data = copy.copy(entry.data)
40+
updated_data = entry.data.copy()
3741
updated_data[CONF_ALLOW_ALL_IMPORTS] = import_config.get(CONF_ALLOW_ALL_IMPORTS, False)
3842
self.hass.config_entries.async_update_entry(entry=entry, data=updated_data)
3943
await self.hass.config_entries.async_reload(entry.entry_id)

tests/test_config_flow.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""Tests for pyscript config flow."""
2+
import logging
3+
4+
from custom_components.pyscript import PYSCRIPT_SCHEMA
5+
from custom_components.pyscript.const import CONF_ALLOW_ALL_IMPORTS, DOMAIN
6+
import pytest
7+
from pytest_homeassistant.async_mock import patch
8+
9+
from homeassistant import data_entry_flow
10+
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
11+
12+
_LOGGER = logging.getLogger(__name__)
13+
14+
15+
@pytest.fixture(name="pyscript_bypass_setup")
16+
def pyscript_bypass_setup_fixture():
17+
"""Mock component setup."""
18+
with patch("custom_components.pyscript.async_setup_entry", return_value=True):
19+
yield
20+
21+
22+
async def test_user_flow_minimum_fields(hass, pyscript_bypass_setup):
23+
"""Test user config flow with minimum fields."""
24+
# test form shows
25+
result = await hass.config_entries.flow.async_init(DOMAIN, context={"source": SOURCE_USER})
26+
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
27+
assert result["step_id"] == "user"
28+
29+
result = await hass.config_entries.flow.async_configure(result["flow_id"], user_input={})
30+
31+
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
32+
assert CONF_ALLOW_ALL_IMPORTS in result["data"]
33+
assert not result["data"][CONF_ALLOW_ALL_IMPORTS]
34+
35+
36+
async def test_user_flow_all_fields(hass, pyscript_bypass_setup,) -> None:
37+
"""Test user config flow with all fields."""
38+
# test form shows
39+
result = await hass.config_entries.flow.async_init(DOMAIN, context={"source": SOURCE_USER})
40+
41+
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
42+
assert result["step_id"] == "user"
43+
44+
result = await hass.config_entries.flow.async_configure(
45+
result["flow_id"], user_input={CONF_ALLOW_ALL_IMPORTS: True}
46+
)
47+
48+
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
49+
assert CONF_ALLOW_ALL_IMPORTS in result["data"]
50+
assert result["data"][CONF_ALLOW_ALL_IMPORTS]
51+
52+
53+
async def test_user_already_configured(hass, pyscript_bypass_setup,) -> None:
54+
"""Test service is already configured during user setup."""
55+
result = await hass.config_entries.flow.async_init(
56+
DOMAIN, context={"source": SOURCE_USER}, data={CONF_ALLOW_ALL_IMPORTS: True}
57+
)
58+
59+
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
60+
61+
result = await hass.config_entries.flow.async_init(
62+
DOMAIN, context={"source": SOURCE_USER}, data={CONF_ALLOW_ALL_IMPORTS: True}
63+
)
64+
65+
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
66+
assert result["reason"] == "single_instance_allowed"
67+
68+
69+
async def test_import_flow(hass, pyscript_bypass_setup):
70+
"""Test import config flow works."""
71+
result = await hass.config_entries.flow.async_init(
72+
DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({})
73+
)
74+
75+
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
76+
77+
78+
async def test_import_flow_update_entry(hass, pyscript_bypass_setup):
79+
"""Test import config flow updates existing entry."""
80+
result = await hass.config_entries.flow.async_init(
81+
DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({})
82+
)
83+
84+
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
85+
86+
result = await hass.config_entries.flow.async_init(
87+
DOMAIN, context={"source": SOURCE_IMPORT}, data={CONF_ALLOW_ALL_IMPORTS: True}
88+
)
89+
90+
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
91+
assert result["reason"] == "updated_entry"
92+
93+
94+
async def test_import_flow_no_update(hass, pyscript_bypass_setup):
95+
"""Test import config flow doesn't update existing entry when data is same."""
96+
result = await hass.config_entries.flow.async_init(
97+
DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({})
98+
)
99+
100+
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
101+
102+
result = await hass.config_entries.flow.async_init(
103+
DOMAIN, context={"source": SOURCE_IMPORT}, data=PYSCRIPT_SCHEMA({})
104+
)
105+
106+
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
107+
assert result["reason"] == "already_configured_service"

0 commit comments

Comments
 (0)