Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 3 additions & 48 deletions homeassistant/components/airvisual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
KeyExpiredError,
NodeProError,
)
import voluptuous as vol

from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH
from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_API_KEY,
Expand Down Expand Up @@ -49,34 +48,8 @@

DEFAULT_ATTRIBUTION = "Data provided by AirVisual"
DEFAULT_NODE_PRO_UPDATE_INTERVAL = timedelta(minutes=1)
DEFAULT_OPTIONS = {CONF_SHOW_ON_MAP: True}

GEOGRAPHY_COORDINATES_SCHEMA = vol.Schema(
{
vol.Required(CONF_LATITUDE): cv.latitude,
vol.Required(CONF_LONGITUDE): cv.longitude,
}
)

GEOGRAPHY_PLACE_SCHEMA = vol.Schema(
{
vol.Required(CONF_CITY): cv.string,
vol.Required(CONF_STATE): cv.string,
vol.Required(CONF_COUNTRY): cv.string,
}
)

CLOUD_API_SCHEMA = vol.Schema(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Optional(CONF_GEOGRAPHIES, default=[]): vol.All(
cv.ensure_list,
[vol.Any(GEOGRAPHY_COORDINATES_SCHEMA, GEOGRAPHY_PLACE_SCHEMA)],
),
}
)

CONFIG_SCHEMA = vol.Schema({DOMAIN: CLOUD_API_SCHEMA}, extra=vol.ALLOW_EXTRA)
CONFIG_SCHEMA = cv.deprecated(DOMAIN, invalidation_version="0.119")


@callback
Expand Down Expand Up @@ -154,24 +127,6 @@ def async_sync_geo_coordinator_update_intervals(hass, api_key):
async def async_setup(hass, config):
"""Set up the AirVisual component."""
hass.data[DOMAIN] = {DATA_COORDINATOR: {}, DATA_LISTENER: {}}

if DOMAIN not in config:
return True

conf = config[DOMAIN]

for geography in conf.get(
CONF_GEOGRAPHIES,
[{CONF_LATITUDE: hass.config.latitude, CONF_LONGITUDE: hass.config.longitude}],
):
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_API_KEY: conf[CONF_API_KEY], **geography},
)
)

return True


Expand Down Expand Up @@ -347,7 +302,7 @@ async def async_migrate_entry(hass, config_entry):
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
context={"source": "geography"},
data={CONF_API_KEY: config_entry.data[CONF_API_KEY], **geography},
)
)
Expand Down
4 changes: 0 additions & 4 deletions homeassistant/components/airvisual/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ async def async_step_geography_finish(self, user_input, error_step, error_schema
data={**user_input, CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY},
)

async def async_step_import(self, import_config):
"""Import a config entry from configuration.yaml."""
return await self.async_step_geography(import_config)

async def async_step_node_pro(self, user_input=None):
"""Handle the initialization of the integration with a Node/Pro."""
if not user_input:
Expand Down
45 changes: 18 additions & 27 deletions tests/components/airvisual/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
INTEGRATION_TYPE_GEOGRAPHY,
INTEGRATION_TYPE_NODE_PRO,
)
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import (
CONF_API_KEY,
CONF_IP_ADDRESS,
Expand Down Expand Up @@ -37,7 +37,10 @@ async def test_duplicate_error(hass):
).add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=geography_conf
DOMAIN, context={"source": SOURCE_USER}, data={"type": "Geographical Location"}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=geography_conf
)

assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
Expand Down Expand Up @@ -73,8 +76,14 @@ async def test_invalid_identifier(hass):
side_effect=InvalidKeyError,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=geography_conf
DOMAIN,
context={"source": SOURCE_USER},
data={"type": "Geographical Location"},
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=geography_conf
)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}

Expand Down Expand Up @@ -162,6 +171,7 @@ async def test_options_flow(hass):
with patch(
"homeassistant.components.airvisual.async_setup_entry", return_value=True
):
await hass.config_entries.async_setup(config_entry.entry_id)
result = await hass.config_entries.options.async_init(config_entry.entry_id)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
Expand All @@ -187,31 +197,12 @@ async def test_step_geography(hass):
"homeassistant.components.airvisual.async_setup_entry", return_value=True
), patch("pyairvisual.air_quality.AirQuality.nearest_city"):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
DOMAIN,
context={"source": SOURCE_USER},
data={"type": "Geographical Location"},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Cloud API (51.528308, -0.3817765)"
assert result["data"] == {
CONF_API_KEY: "abcde12345",
CONF_LATITUDE: 51.528308,
CONF_LONGITUDE: -0.3817765,
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY,
}


async def test_step_import(hass):
"""Test the import step for both types of configuration."""
geography_conf = {
CONF_API_KEY: "abcde12345",
CONF_LATITUDE: 51.528308,
CONF_LONGITUDE: -0.3817765,
}

with patch(
"homeassistant.components.airvisual.async_setup_entry", return_value=True
), patch("pyairvisual.air_quality.AirQuality.nearest_city"):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=geography_conf
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=conf
)

assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
Expand Down