Allow more than one AirVisual config entry with the same API k… (#33072)
* Allow more than one AirVisual config entry with the same API key * Add tests * Correctly pop geography * Code review * Code review
This commit is contained in:
1 parent
109f083c5d
commit
0e2fa7700d
7 files changed
+169
-86
No files matched your search
@@ -11,15 +11,22 @@ from homeassistant.const import (
|
||||
CONF_LONGITUDE,
|
||||
CONF_SHOW_ON_MAP,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_duplicate_error(hass):
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
conf = {CONF_API_KEY: "abcde12345"}
|
||||
conf = {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_LATITUDE: 51.528308,
|
||||
CONF_LONGITUDE: -0.3817765,
|
||||
}
|
||||
|
||||
MockConfigEntry(domain=DOMAIN, unique_id="abcde12345", data=conf).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN, unique_id="51.528308, -0.3817765", data=conf
|
||||
).add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||
@@ -31,7 +38,11 @@ async def test_duplicate_error(hass):
|
||||
|
||||
async def test_invalid_api_key(hass):
|
||||
"""Test that invalid credentials throws an error."""
|
||||
conf = {CONF_API_KEY: "abcde12345"}
|
||||
conf = {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_LATITUDE: 51.528308,
|
||||
CONF_LONGITUDE: -0.3817765,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"pyairvisual.api.API.nearest_city", side_effect=InvalidKeyError,
|
||||
@@ -42,6 +53,47 @@ async def test_invalid_api_key(hass):
|
||||
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
||||
|
||||
|
||||
async def test_migration_1_2(hass):
|
||||
"""Test migrating from version 1 to version 2."""
|
||||
conf = {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_GEOGRAPHIES: [
|
||||
{CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765},
|
||||
{CONF_LATITUDE: 35.48847, CONF_LONGITUDE: 137.5263065},
|
||||
],
|
||||
}
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, version=1, unique_id="abcde12345", data=conf
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
|
||||
with patch("pyairvisual.api.API.nearest_city"):
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: conf})
|
||||
|
||||
config_entries = hass.config_entries.async_entries(DOMAIN)
|
||||
|
||||
assert len(config_entries) == 2
|
||||
|
||||
assert config_entries[0].unique_id == "51.528308, -0.3817765"
|
||||
assert config_entries[0].title == "Cloud API (51.528308, -0.3817765)"
|
||||
assert config_entries[0].data == {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_LATITUDE: 51.528308,
|
||||
CONF_LONGITUDE: -0.3817765,
|
||||
}
|
||||
|
||||
assert config_entries[1].unique_id == "35.48847, 137.5263065"
|
||||
assert config_entries[1].title == "Cloud API (35.48847, 137.5263065)"
|
||||
assert config_entries[1].data == {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_LATITUDE: 35.48847,
|
||||
CONF_LONGITUDE: 137.5263065,
|
||||
}
|
||||
|
||||
|
||||
async def test_options_flow(hass):
|
||||
"""Test config flow options."""
|
||||
conf = {CONF_API_KEY: "abcde12345"}
|
||||
@@ -84,7 +136,8 @@ async def test_step_import(hass):
|
||||
"""Test that the import step works."""
|
||||
conf = {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_GEOGRAPHIES: [{CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765}],
|
||||
CONF_LATITUDE: 51.528308,
|
||||
CONF_LONGITUDE: -0.3817765,
|
||||
}
|
||||
|
||||
with patch(
|
||||
@@ -95,10 +148,11 @@ async def test_step_import(hass):
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "Cloud API (API key: abcd...)"
|
||||
assert result["title"] == "Cloud API (51.528308, -0.3817765)"
|
||||
assert result["data"] == {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_GEOGRAPHIES: [{CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765}],
|
||||
CONF_LATITUDE: 51.528308,
|
||||
CONF_LONGITUDE: -0.3817765,
|
||||
}
|
||||
|
||||
|
||||
@@ -117,8 +171,9 @@ async def test_step_user(hass):
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "Cloud API (API key: abcd...)"
|
||||
assert result["title"] == "Cloud API (32.87336, -117.22743)"
|
||||
assert result["data"] == {
|
||||
CONF_API_KEY: "abcde12345",
|
||||
CONF_GEOGRAPHIES: [{CONF_LATITUDE: 32.87336, CONF_LONGITUDE: -117.22743}],
|
||||
CONF_LATITUDE: 32.87336,
|
||||
CONF_LONGITUDE: -117.22743,
|
||||
}
|
||||
Reference in new issue
Block a user