mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Add ability to configure AirVisual with city/state/country in UI (#44116)
This commit is contained in:
parent
197c857e1f
commit
3bdf962838
@ -23,6 +23,7 @@ from homeassistant.const import (
|
|||||||
CONF_STATE,
|
CONF_STATE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
@ -37,7 +38,7 @@ from .const import (
|
|||||||
CONF_INTEGRATION_TYPE,
|
CONF_INTEGRATION_TYPE,
|
||||||
DATA_COORDINATOR,
|
DATA_COORDINATOR,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
INTEGRATION_TYPE_GEOGRAPHY,
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
INTEGRATION_TYPE_NODE_PRO,
|
INTEGRATION_TYPE_NODE_PRO,
|
||||||
LOGGER,
|
LOGGER,
|
||||||
)
|
)
|
||||||
@ -145,7 +146,7 @@ def _standardize_geography_config_entry(hass, config_entry):
|
|||||||
# If the config entry data doesn't contain the integration type, add it:
|
# If the config entry data doesn't contain the integration type, add it:
|
||||||
entry_updates["data"] = {
|
entry_updates["data"] = {
|
||||||
**config_entry.data,
|
**config_entry.data,
|
||||||
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY,
|
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
}
|
}
|
||||||
|
|
||||||
if not entry_updates:
|
if not entry_updates:
|
||||||
@ -232,7 +233,6 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
update_method=async_update_data,
|
update_method=async_update_data,
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.data[DOMAIN][DATA_COORDINATOR][config_entry.entry_id] = coordinator
|
|
||||||
async_sync_geo_coordinator_update_intervals(
|
async_sync_geo_coordinator_update_intervals(
|
||||||
hass, config_entry.data[CONF_API_KEY]
|
hass, config_entry.data[CONF_API_KEY]
|
||||||
)
|
)
|
||||||
@ -262,9 +262,11 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
update_method=async_update_data,
|
update_method=async_update_data,
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.data[DOMAIN][DATA_COORDINATOR][config_entry.entry_id] = coordinator
|
|
||||||
|
|
||||||
await coordinator.async_refresh()
|
await coordinator.async_refresh()
|
||||||
|
if not coordinator.last_update_success:
|
||||||
|
raise ConfigEntryNotReady
|
||||||
|
|
||||||
|
hass.data[DOMAIN][DATA_COORDINATOR][config_entry.entry_id] = coordinator
|
||||||
|
|
||||||
for component in PLATFORMS:
|
for component in PLATFORMS:
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
@ -299,10 +301,14 @@ async def async_migrate_entry(hass, config_entry):
|
|||||||
|
|
||||||
# For any geographies that remain, create a new config entry for each one:
|
# For any geographies that remain, create a new config entry for each one:
|
||||||
for geography in geographies:
|
for geography in geographies:
|
||||||
|
if CONF_LATITUDE in geography:
|
||||||
|
source = "geography_by_coords"
|
||||||
|
else:
|
||||||
|
source = "geography_by_name"
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.flow.async_init(
|
hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": "geography"},
|
context={"source": source},
|
||||||
data={CONF_API_KEY: config_entry.data[CONF_API_KEY], **geography},
|
data={CONF_API_KEY: config_entry.data[CONF_API_KEY], **geography},
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -327,7 +333,10 @@ async def async_unload_entry(hass, config_entry):
|
|||||||
remove_listener = hass.data[DOMAIN][DATA_LISTENER].pop(config_entry.entry_id)
|
remove_listener = hass.data[DOMAIN][DATA_LISTENER].pop(config_entry.entry_id)
|
||||||
remove_listener()
|
remove_listener()
|
||||||
|
|
||||||
if config_entry.data[CONF_INTEGRATION_TYPE] == INTEGRATION_TYPE_GEOGRAPHY:
|
if (
|
||||||
|
config_entry.data[CONF_INTEGRATION_TYPE]
|
||||||
|
== INTEGRATION_TYPE_GEOGRAPHY_COORDS
|
||||||
|
):
|
||||||
# Re-calculate the update interval period for any remaining consumers of
|
# Re-calculate the update interval period for any remaining consumers of
|
||||||
# this API key:
|
# this API key:
|
||||||
async_sync_geo_coordinator_update_intervals(
|
async_sync_geo_coordinator_update_intervals(
|
||||||
|
@ -2,7 +2,12 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from pyairvisual import CloudAPI, NodeSamba
|
from pyairvisual import CloudAPI, NodeSamba
|
||||||
from pyairvisual.errors import InvalidKeyError, NodeProError
|
from pyairvisual.errors import (
|
||||||
|
AirVisualError,
|
||||||
|
InvalidKeyError,
|
||||||
|
NodeProError,
|
||||||
|
NotFoundError,
|
||||||
|
)
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
@ -13,20 +18,46 @@ from homeassistant.const import (
|
|||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
CONF_PASSWORD,
|
CONF_PASSWORD,
|
||||||
CONF_SHOW_ON_MAP,
|
CONF_SHOW_ON_MAP,
|
||||||
|
CONF_STATE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
||||||
|
|
||||||
from . import async_get_geography_id
|
from . import async_get_geography_id
|
||||||
from .const import ( # pylint: disable=unused-import
|
from .const import ( # pylint: disable=unused-import
|
||||||
CONF_GEOGRAPHIES,
|
CONF_CITY,
|
||||||
|
CONF_COUNTRY,
|
||||||
CONF_INTEGRATION_TYPE,
|
CONF_INTEGRATION_TYPE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
INTEGRATION_TYPE_GEOGRAPHY,
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
||||||
INTEGRATION_TYPE_NODE_PRO,
|
INTEGRATION_TYPE_NODE_PRO,
|
||||||
LOGGER,
|
LOGGER,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
API_KEY_DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): cv.string})
|
||||||
|
GEOGRAPHY_NAME_SCHEMA = API_KEY_DATA_SCHEMA.extend(
|
||||||
|
{
|
||||||
|
vol.Required(CONF_CITY): cv.string,
|
||||||
|
vol.Required(CONF_STATE): cv.string,
|
||||||
|
vol.Required(CONF_COUNTRY): cv.string,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
NODE_PRO_SCHEMA = vol.Schema(
|
||||||
|
{vol.Required(CONF_IP_ADDRESS): str, vol.Required(CONF_PASSWORD): cv.string}
|
||||||
|
)
|
||||||
|
PICK_INTEGRATION_TYPE_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required("type"): vol.In(
|
||||||
|
[
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
||||||
|
INTEGRATION_TYPE_NODE_PRO,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle an AirVisual config flow."""
|
"""Handle an AirVisual config flow."""
|
||||||
@ -36,16 +67,13 @@ class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize the config flow."""
|
"""Initialize the config flow."""
|
||||||
|
self._entry_data_for_reauth = None
|
||||||
self._geo_id = None
|
self._geo_id = None
|
||||||
self._latitude = None
|
|
||||||
self._longitude = None
|
|
||||||
|
|
||||||
self.api_key_data_schema = vol.Schema({vol.Required(CONF_API_KEY): str})
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def geography_schema(self):
|
def geography_coords_schema(self):
|
||||||
"""Return the data schema for the cloud API."""
|
"""Return the data schema for the cloud API."""
|
||||||
return self.api_key_data_schema.extend(
|
return API_KEY_DATA_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(
|
vol.Required(
|
||||||
CONF_LATITUDE, default=self.hass.config.latitude
|
CONF_LATITUDE, default=self.hass.config.latitude
|
||||||
@ -56,24 +84,6 @@ class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
def pick_integration_type_schema(self):
|
|
||||||
"""Return the data schema for picking the integration type."""
|
|
||||||
return vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required("type"): vol.In(
|
|
||||||
[INTEGRATION_TYPE_GEOGRAPHY, INTEGRATION_TYPE_NODE_PRO]
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def node_pro_schema(self):
|
|
||||||
"""Return the data schema for a Node/Pro."""
|
|
||||||
return vol.Schema(
|
|
||||||
{vol.Required(CONF_IP_ADDRESS): str, vol.Required(CONF_PASSWORD): str}
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _async_set_unique_id(self, unique_id):
|
async def _async_set_unique_id(self, unique_id):
|
||||||
"""Set the unique ID of the config flow and abort if it already exists."""
|
"""Set the unique ID of the config flow and abort if it already exists."""
|
||||||
await self.async_set_unique_id(unique_id)
|
await self.async_set_unique_id(unique_id)
|
||||||
@ -85,33 +95,36 @@ class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"""Define the config flow to handle options."""
|
"""Define the config flow to handle options."""
|
||||||
return AirVisualOptionsFlowHandler(config_entry)
|
return AirVisualOptionsFlowHandler(config_entry)
|
||||||
|
|
||||||
async def async_step_geography(self, user_input=None):
|
async def async_step_geography(self, user_input, integration_type):
|
||||||
"""Handle the initialization of the integration via the cloud API."""
|
"""Handle the initialization of the integration via the cloud API."""
|
||||||
if not user_input:
|
|
||||||
return self.async_show_form(
|
|
||||||
step_id="geography", data_schema=self.geography_schema
|
|
||||||
)
|
|
||||||
|
|
||||||
self._geo_id = async_get_geography_id(user_input)
|
self._geo_id = async_get_geography_id(user_input)
|
||||||
await self._async_set_unique_id(self._geo_id)
|
await self._async_set_unique_id(self._geo_id)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
|
return await self.async_step_geography_finish(user_input, integration_type)
|
||||||
|
|
||||||
# Find older config entries without unique ID:
|
async def async_step_geography_by_coords(self, user_input=None):
|
||||||
for entry in self._async_current_entries():
|
"""Handle the initialization of the cloud API based on latitude/longitude."""
|
||||||
if entry.version != 1:
|
if not user_input:
|
||||||
continue
|
return self.async_show_form(
|
||||||
|
step_id="geography_by_coords", data_schema=self.geography_coords_schema
|
||||||
|
)
|
||||||
|
|
||||||
if any(
|
return await self.async_step_geography(
|
||||||
self._geo_id == async_get_geography_id(geography)
|
user_input, INTEGRATION_TYPE_GEOGRAPHY_COORDS
|
||||||
for geography in entry.data[CONF_GEOGRAPHIES]
|
|
||||||
):
|
|
||||||
return self.async_abort(reason="already_configured")
|
|
||||||
|
|
||||||
return await self.async_step_geography_finish(
|
|
||||||
user_input, "geography", self.geography_schema
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_geography_finish(self, user_input, error_step, error_schema):
|
async def async_step_geography_by_name(self, user_input=None):
|
||||||
|
"""Handle the initialization of the cloud API based on city/state/country."""
|
||||||
|
if not user_input:
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id="geography_by_name", data_schema=GEOGRAPHY_NAME_SCHEMA
|
||||||
|
)
|
||||||
|
|
||||||
|
return await self.async_step_geography(
|
||||||
|
user_input, INTEGRATION_TYPE_GEOGRAPHY_NAME
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_step_geography_finish(self, user_input, integration_type):
|
||||||
"""Validate a Cloud API key."""
|
"""Validate a Cloud API key."""
|
||||||
websession = aiohttp_client.async_get_clientsession(self.hass)
|
websession = aiohttp_client.async_get_clientsession(self.hass)
|
||||||
cloud_api = CloudAPI(user_input[CONF_API_KEY], session=websession)
|
cloud_api = CloudAPI(user_input[CONF_API_KEY], session=websession)
|
||||||
@ -123,16 +136,40 @@ class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"airvisual_checked_api_keys_lock", asyncio.Lock()
|
"airvisual_checked_api_keys_lock", asyncio.Lock()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if integration_type == INTEGRATION_TYPE_GEOGRAPHY_COORDS:
|
||||||
|
coro = cloud_api.air_quality.nearest_city()
|
||||||
|
error_schema = self.geography_coords_schema
|
||||||
|
error_step = "geography_by_coords"
|
||||||
|
else:
|
||||||
|
coro = cloud_api.air_quality.city(
|
||||||
|
user_input[CONF_CITY], user_input[CONF_STATE], user_input[CONF_COUNTRY]
|
||||||
|
)
|
||||||
|
error_schema = GEOGRAPHY_NAME_SCHEMA
|
||||||
|
error_step = "geography_by_name"
|
||||||
|
|
||||||
async with valid_keys_lock:
|
async with valid_keys_lock:
|
||||||
if user_input[CONF_API_KEY] not in valid_keys:
|
if user_input[CONF_API_KEY] not in valid_keys:
|
||||||
try:
|
try:
|
||||||
await cloud_api.air_quality.nearest_city()
|
await coro
|
||||||
except InvalidKeyError:
|
except InvalidKeyError:
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id=error_step,
|
step_id=error_step,
|
||||||
data_schema=error_schema,
|
data_schema=error_schema,
|
||||||
errors={CONF_API_KEY: "invalid_api_key"},
|
errors={CONF_API_KEY: "invalid_api_key"},
|
||||||
)
|
)
|
||||||
|
except NotFoundError:
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id=error_step,
|
||||||
|
data_schema=error_schema,
|
||||||
|
errors={CONF_CITY: "location_not_found"},
|
||||||
|
)
|
||||||
|
except AirVisualError as err:
|
||||||
|
LOGGER.error(err)
|
||||||
|
return self.async_show_form(
|
||||||
|
step_id=error_step,
|
||||||
|
data_schema=error_schema,
|
||||||
|
errors={"base": "unknown"},
|
||||||
|
)
|
||||||
|
|
||||||
valid_keys.add(user_input[CONF_API_KEY])
|
valid_keys.add(user_input[CONF_API_KEY])
|
||||||
|
|
||||||
@ -143,15 +180,13 @@ class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=f"Cloud API ({self._geo_id})",
|
title=f"Cloud API ({self._geo_id})",
|
||||||
data={**user_input, CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY},
|
data={**user_input, CONF_INTEGRATION_TYPE: integration_type},
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_node_pro(self, user_input=None):
|
async def async_step_node_pro(self, user_input=None):
|
||||||
"""Handle the initialization of the integration with a Node/Pro."""
|
"""Handle the initialization of the integration with a Node/Pro."""
|
||||||
if not user_input:
|
if not user_input:
|
||||||
return self.async_show_form(
|
return self.async_show_form(step_id="node_pro", data_schema=NODE_PRO_SCHEMA)
|
||||||
step_id="node_pro", data_schema=self.node_pro_schema
|
|
||||||
)
|
|
||||||
|
|
||||||
await self._async_set_unique_id(user_input[CONF_IP_ADDRESS])
|
await self._async_set_unique_id(user_input[CONF_IP_ADDRESS])
|
||||||
|
|
||||||
@ -163,7 +198,7 @@ class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
LOGGER.error("Error connecting to Node/Pro unit: %s", err)
|
LOGGER.error("Error connecting to Node/Pro unit: %s", err)
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="node_pro",
|
step_id="node_pro",
|
||||||
data_schema=self.node_pro_schema,
|
data_schema=NODE_PRO_SCHEMA,
|
||||||
errors={CONF_IP_ADDRESS: "cannot_connect"},
|
errors={CONF_IP_ADDRESS: "cannot_connect"},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -176,39 +211,34 @@ class AirVisualFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
async def async_step_reauth(self, data):
|
async def async_step_reauth(self, data):
|
||||||
"""Handle configuration by re-auth."""
|
"""Handle configuration by re-auth."""
|
||||||
|
self._entry_data_for_reauth = data
|
||||||
self._geo_id = async_get_geography_id(data)
|
self._geo_id = async_get_geography_id(data)
|
||||||
self._latitude = data[CONF_LATITUDE]
|
|
||||||
self._longitude = data[CONF_LONGITUDE]
|
|
||||||
|
|
||||||
return await self.async_step_reauth_confirm()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
async def async_step_reauth_confirm(self, user_input=None):
|
async def async_step_reauth_confirm(self, user_input=None):
|
||||||
"""Handle re-auth completion."""
|
"""Handle re-auth completion."""
|
||||||
if not user_input:
|
if not user_input:
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth_confirm", data_schema=self.api_key_data_schema
|
step_id="reauth_confirm", data_schema=API_KEY_DATA_SCHEMA
|
||||||
)
|
)
|
||||||
|
|
||||||
conf = {
|
conf = {CONF_API_KEY: user_input[CONF_API_KEY], **self._entry_data_for_reauth}
|
||||||
CONF_API_KEY: user_input[CONF_API_KEY],
|
|
||||||
CONF_LATITUDE: self._latitude,
|
|
||||||
CONF_LONGITUDE: self._longitude,
|
|
||||||
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY,
|
|
||||||
}
|
|
||||||
|
|
||||||
return await self.async_step_geography_finish(
|
return await self.async_step_geography_finish(
|
||||||
conf, "reauth_confirm", self.api_key_data_schema
|
conf, self._entry_data_for_reauth[CONF_INTEGRATION_TYPE]
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(self, user_input=None):
|
||||||
"""Handle the start of the config flow."""
|
"""Handle the start of the config flow."""
|
||||||
if not user_input:
|
if not user_input:
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=self.pick_integration_type_schema
|
step_id="user", data_schema=PICK_INTEGRATION_TYPE_SCHEMA
|
||||||
)
|
)
|
||||||
|
|
||||||
if user_input["type"] == INTEGRATION_TYPE_GEOGRAPHY:
|
if user_input["type"] == INTEGRATION_TYPE_GEOGRAPHY_COORDS:
|
||||||
return await self.async_step_geography()
|
return await self.async_step_geography_by_coords()
|
||||||
|
if user_input["type"] == INTEGRATION_TYPE_GEOGRAPHY_NAME:
|
||||||
|
return await self.async_step_geography_by_name()
|
||||||
return await self.async_step_node_pro()
|
return await self.async_step_node_pro()
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@ import logging
|
|||||||
DOMAIN = "airvisual"
|
DOMAIN = "airvisual"
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
INTEGRATION_TYPE_GEOGRAPHY = "Geographical Location"
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS = "Geographical Location by Latitude/Longitude"
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_NAME = "Geographical Location by Name"
|
||||||
INTEGRATION_TYPE_NODE_PRO = "AirVisual Node/Pro"
|
INTEGRATION_TYPE_NODE_PRO = "AirVisual Node/Pro"
|
||||||
|
|
||||||
CONF_CITY = "city"
|
CONF_CITY = "city"
|
||||||
|
@ -27,7 +27,8 @@ from .const import (
|
|||||||
CONF_INTEGRATION_TYPE,
|
CONF_INTEGRATION_TYPE,
|
||||||
DATA_COORDINATOR,
|
DATA_COORDINATOR,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
INTEGRATION_TYPE_GEOGRAPHY,
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = getLogger(__name__)
|
_LOGGER = getLogger(__name__)
|
||||||
@ -115,7 +116,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
"""Set up AirVisual sensors based on a config entry."""
|
"""Set up AirVisual sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][config_entry.entry_id]
|
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][config_entry.entry_id]
|
||||||
|
|
||||||
if config_entry.data[CONF_INTEGRATION_TYPE] == INTEGRATION_TYPE_GEOGRAPHY:
|
if config_entry.data[CONF_INTEGRATION_TYPE] in [
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
||||||
|
]:
|
||||||
sensors = [
|
sensors = [
|
||||||
AirVisualGeographySensor(
|
AirVisualGeographySensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
@ -208,17 +212,32 @@ class AirVisualGeographySensor(AirVisualEntity):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if CONF_LATITUDE in self._config_entry.data:
|
# Displaying the geography on the map relies upon putting the latitude/longitude
|
||||||
if self._config_entry.options[CONF_SHOW_ON_MAP]:
|
# in the entity attributes with "latitude" and "longitude" as the keys.
|
||||||
self._attrs[ATTR_LATITUDE] = self._config_entry.data[CONF_LATITUDE]
|
# Conversely, we can hide the location on the map by using other keys, like
|
||||||
self._attrs[ATTR_LONGITUDE] = self._config_entry.data[CONF_LONGITUDE]
|
# "lati" and "long".
|
||||||
self._attrs.pop("lati", None)
|
#
|
||||||
self._attrs.pop("long", None)
|
# We use any coordinates in the config entry and, in the case of a geography by
|
||||||
else:
|
# name, we fall back to the latitude longitude provided in the coordinator data:
|
||||||
self._attrs["lati"] = self._config_entry.data[CONF_LATITUDE]
|
latitude = self._config_entry.data.get(
|
||||||
self._attrs["long"] = self._config_entry.data[CONF_LONGITUDE]
|
CONF_LATITUDE,
|
||||||
self._attrs.pop(ATTR_LATITUDE, None)
|
self.coordinator.data["location"]["coordinates"][1],
|
||||||
self._attrs.pop(ATTR_LONGITUDE, None)
|
)
|
||||||
|
longitude = self._config_entry.data.get(
|
||||||
|
CONF_LONGITUDE,
|
||||||
|
self.coordinator.data["location"]["coordinates"][0],
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._config_entry.options[CONF_SHOW_ON_MAP]:
|
||||||
|
self._attrs[ATTR_LATITUDE] = latitude
|
||||||
|
self._attrs[ATTR_LONGITUDE] = longitude
|
||||||
|
self._attrs.pop("lati", None)
|
||||||
|
self._attrs.pop("long", None)
|
||||||
|
else:
|
||||||
|
self._attrs["lati"] = latitude
|
||||||
|
self._attrs["long"] = longitude
|
||||||
|
self._attrs.pop(ATTR_LATITUDE, None)
|
||||||
|
self._attrs.pop(ATTR_LONGITUDE, None)
|
||||||
|
|
||||||
|
|
||||||
class AirVisualNodeProSensor(AirVisualEntity):
|
class AirVisualNodeProSensor(AirVisualEntity):
|
||||||
|
@ -1,15 +1,25 @@
|
|||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
"step": {
|
"step": {
|
||||||
"geography": {
|
"geography_by_coords": {
|
||||||
"title": "Configure a Geography",
|
"title": "Configure a Geography",
|
||||||
"description": "Use the AirVisual cloud API to monitor a geographical location.",
|
"description": "Use the AirVisual cloud API to monitor a latitude/longitude.",
|
||||||
"data": {
|
"data": {
|
||||||
"api_key": "[%key:common::config_flow::data::api_key%]",
|
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||||
"latitude": "[%key:common::config_flow::data::latitude%]",
|
"latitude": "[%key:common::config_flow::data::latitude%]",
|
||||||
"longitude": "[%key:common::config_flow::data::longitude%]"
|
"longitude": "[%key:common::config_flow::data::longitude%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"geography_by_name": {
|
||||||
|
"title": "Configure a Geography",
|
||||||
|
"description": "Use the AirVisual cloud API to monitor a city/state/country.",
|
||||||
|
"data": {
|
||||||
|
"api_key": "[%key:common::config_flow::data::api_key%]",
|
||||||
|
"city": "City",
|
||||||
|
"country": "Country",
|
||||||
|
"state": "state"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_pro": {
|
"node_pro": {
|
||||||
"title": "Configure an AirVisual Node/Pro",
|
"title": "Configure an AirVisual Node/Pro",
|
||||||
"description": "Monitor a personal AirVisual unit. The password can be retrieved from the unit's UI.",
|
"description": "Monitor a personal AirVisual unit. The password can be retrieved from the unit's UI.",
|
||||||
@ -26,17 +36,13 @@
|
|||||||
},
|
},
|
||||||
"user": {
|
"user": {
|
||||||
"title": "Configure AirVisual",
|
"title": "Configure AirVisual",
|
||||||
"description": "Pick what type of AirVisual data you want to monitor.",
|
"description": "Pick what type of AirVisual data you want to monitor."
|
||||||
"data": {
|
|
||||||
"cloud_api": "Geographical Location",
|
|
||||||
"node_pro": "AirVisual Node Pro",
|
|
||||||
"type": "Integration Type"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"general_error": "[%key:common::config_flow::error::unknown%]",
|
"general_error": "[%key:common::config_flow::error::unknown%]",
|
||||||
"invalid_api_key": "[%key:common::config_flow::error::invalid_api_key%]",
|
"invalid_api_key": "[%key:common::config_flow::error::invalid_api_key%]",
|
||||||
|
"location_not_found": "Location not found",
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
|
@ -7,16 +7,27 @@
|
|||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "Failed to connect",
|
"cannot_connect": "Failed to connect",
|
||||||
"general_error": "Unexpected error",
|
"general_error": "Unexpected error",
|
||||||
"invalid_api_key": "Invalid API key"
|
"invalid_api_key": "Invalid API key",
|
||||||
|
"location_not_found": "Location not found"
|
||||||
},
|
},
|
||||||
"step": {
|
"step": {
|
||||||
"geography": {
|
"geography_by_coords": {
|
||||||
"data": {
|
"data": {
|
||||||
"api_key": "API Key",
|
"api_key": "API Key",
|
||||||
"latitude": "Latitude",
|
"latitude": "Latitude",
|
||||||
"longitude": "Longitude"
|
"longitude": "Longitude"
|
||||||
},
|
},
|
||||||
"description": "Use the AirVisual cloud API to monitor a geographical location.",
|
"description": "Use the AirVisual cloud API to monitor a latitude/longitude.",
|
||||||
|
"title": "Configure a Geography"
|
||||||
|
},
|
||||||
|
"geography_by_name": {
|
||||||
|
"data": {
|
||||||
|
"api_key": "API Key",
|
||||||
|
"city": "City",
|
||||||
|
"country": "Country",
|
||||||
|
"state": "state"
|
||||||
|
},
|
||||||
|
"description": "Use the AirVisual cloud API to monitor a city/state/country.",
|
||||||
"title": "Configure a Geography"
|
"title": "Configure a Geography"
|
||||||
},
|
},
|
||||||
"node_pro": {
|
"node_pro": {
|
||||||
@ -34,11 +45,6 @@
|
|||||||
"title": "Re-authenticate AirVisual"
|
"title": "Re-authenticate AirVisual"
|
||||||
},
|
},
|
||||||
"user": {
|
"user": {
|
||||||
"data": {
|
|
||||||
"cloud_api": "Geographical Location",
|
|
||||||
"node_pro": "AirVisual Node Pro",
|
|
||||||
"type": "Integration Type"
|
|
||||||
},
|
|
||||||
"description": "Pick what type of AirVisual data you want to monitor.",
|
"description": "Pick what type of AirVisual data you want to monitor.",
|
||||||
"title": "Configure AirVisual"
|
"title": "Configure AirVisual"
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,22 @@
|
|||||||
"""Define tests for the AirVisual config flow."""
|
"""Define tests for the AirVisual config flow."""
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from pyairvisual.errors import InvalidKeyError, NodeProError
|
from pyairvisual.errors import (
|
||||||
|
AirVisualError,
|
||||||
|
InvalidKeyError,
|
||||||
|
NodeProError,
|
||||||
|
NotFoundError,
|
||||||
|
)
|
||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.airvisual import (
|
from homeassistant.components.airvisual.const import (
|
||||||
|
CONF_CITY,
|
||||||
|
CONF_COUNTRY,
|
||||||
CONF_GEOGRAPHIES,
|
CONF_GEOGRAPHIES,
|
||||||
CONF_INTEGRATION_TYPE,
|
CONF_INTEGRATION_TYPE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
INTEGRATION_TYPE_GEOGRAPHY,
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
|
INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
||||||
INTEGRATION_TYPE_NODE_PRO,
|
INTEGRATION_TYPE_NODE_PRO,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import SOURCE_USER
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
@ -19,6 +27,7 @@ from homeassistant.const import (
|
|||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
CONF_PASSWORD,
|
CONF_PASSWORD,
|
||||||
CONF_SHOW_ON_MAP,
|
CONF_SHOW_ON_MAP,
|
||||||
|
CONF_STATE,
|
||||||
)
|
)
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
@ -38,7 +47,9 @@ async def test_duplicate_error(hass):
|
|||||||
).add_to_hass(hass)
|
).add_to_hass(hass)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data={"type": "Geographical Location"}
|
DOMAIN,
|
||||||
|
context={"source": SOURCE_USER},
|
||||||
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS},
|
||||||
)
|
)
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input=geography_conf
|
result["flow_id"], user_input=geography_conf
|
||||||
@ -64,14 +75,8 @@ async def test_duplicate_error(hass):
|
|||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
async def test_invalid_identifier(hass):
|
async def test_invalid_identifier_geography_api_key(hass):
|
||||||
"""Test that an invalid API key or Node/Pro ID throws an error."""
|
"""Test that an invalid API key throws an error."""
|
||||||
geography_conf = {
|
|
||||||
CONF_API_KEY: "abcde12345",
|
|
||||||
CONF_LATITUDE: 51.528308,
|
|
||||||
CONF_LONGITUDE: -0.3817765,
|
|
||||||
}
|
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"pyairvisual.air_quality.AirQuality.nearest_city",
|
"pyairvisual.air_quality.AirQuality.nearest_city",
|
||||||
side_effect=InvalidKeyError,
|
side_effect=InvalidKeyError,
|
||||||
@ -79,64 +84,73 @@ async def test_invalid_identifier(hass):
|
|||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
data={"type": "Geographical Location"},
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS},
|
||||||
)
|
)
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input=geography_conf
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_API_KEY: "abcde12345",
|
||||||
|
CONF_LATITUDE: 51.528308,
|
||||||
|
CONF_LONGITUDE: -0.3817765,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
||||||
|
|
||||||
|
|
||||||
async def test_migration(hass):
|
async def test_invalid_identifier_geography_name(hass):
|
||||||
"""Test migrating from version 1 to the current version."""
|
"""Test that an invalid location name throws an error."""
|
||||||
conf = {
|
with patch(
|
||||||
CONF_API_KEY: "abcde12345",
|
"pyairvisual.air_quality.AirQuality.city",
|
||||||
CONF_GEOGRAPHIES: [
|
side_effect=NotFoundError,
|
||||||
{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.air_quality.AirQuality.nearest_city"), patch.object(
|
|
||||||
hass.config_entries, "async_forward_entry_setup"
|
|
||||||
):
|
):
|
||||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: conf})
|
result = await hass.config_entries.flow.async_init(
|
||||||
await hass.async_block_till_done()
|
DOMAIN,
|
||||||
|
context={"source": SOURCE_USER},
|
||||||
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME},
|
||||||
|
)
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_API_KEY: "abcde12345",
|
||||||
|
CONF_CITY: "Beijing",
|
||||||
|
CONF_STATE: "Beijing",
|
||||||
|
CONF_COUNTRY: "China",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
config_entries = hass.config_entries.async_entries(DOMAIN)
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
assert result["errors"] == {CONF_CITY: "location_not_found"}
|
||||||
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,
|
|
||||||
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY,
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_node_pro_error(hass):
|
async def test_invalid_identifier_geography_unknown(hass):
|
||||||
"""Test that an invalid Node/Pro ID shows an error."""
|
"""Test that an unknown identifier issue throws an error."""
|
||||||
|
with patch(
|
||||||
|
"pyairvisual.air_quality.AirQuality.city",
|
||||||
|
side_effect=AirVisualError,
|
||||||
|
):
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": SOURCE_USER},
|
||||||
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME},
|
||||||
|
)
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
user_input={
|
||||||
|
CONF_API_KEY: "abcde12345",
|
||||||
|
CONF_CITY: "Beijing",
|
||||||
|
CONF_STATE: "Beijing",
|
||||||
|
CONF_COUNTRY: "China",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
assert result["errors"] == {"base": "unknown"}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_invalid_identifier_node_pro(hass):
|
||||||
|
"""Test that an invalid Node/Pro identifier shows an error."""
|
||||||
node_pro_conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PASSWORD: "my_password"}
|
node_pro_conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PASSWORD: "my_password"}
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
@ -153,6 +167,53 @@ async def test_node_pro_error(hass):
|
|||||||
assert result["errors"] == {CONF_IP_ADDRESS: "cannot_connect"}
|
assert result["errors"] == {CONF_IP_ADDRESS: "cannot_connect"}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_migration(hass):
|
||||||
|
"""Test migrating from version 1 to the current version."""
|
||||||
|
conf = {
|
||||||
|
CONF_API_KEY: "abcde12345",
|
||||||
|
CONF_GEOGRAPHIES: [
|
||||||
|
{CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765},
|
||||||
|
{CONF_CITY: "Beijing", CONF_STATE: "Beijing", CONF_COUNTRY: "China"},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
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.air_quality.AirQuality.city"), patch(
|
||||||
|
"pyairvisual.air_quality.AirQuality.nearest_city"
|
||||||
|
), patch.object(hass.config_entries, "async_forward_entry_setup"):
|
||||||
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: conf})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
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,
|
||||||
|
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert config_entries[1].unique_id == "Beijing, Beijing, China"
|
||||||
|
assert config_entries[1].title == "Cloud API (Beijing, Beijing, China)"
|
||||||
|
assert config_entries[1].data == {
|
||||||
|
CONF_API_KEY: "abcde12345",
|
||||||
|
CONF_CITY: "Beijing",
|
||||||
|
CONF_STATE: "Beijing",
|
||||||
|
CONF_COUNTRY: "China",
|
||||||
|
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_options_flow(hass):
|
async def test_options_flow(hass):
|
||||||
"""Test config flow options."""
|
"""Test config flow options."""
|
||||||
geography_conf = {
|
geography_conf = {
|
||||||
@ -186,8 +247,8 @@ async def test_options_flow(hass):
|
|||||||
assert config_entry.options == {CONF_SHOW_ON_MAP: False}
|
assert config_entry.options == {CONF_SHOW_ON_MAP: False}
|
||||||
|
|
||||||
|
|
||||||
async def test_step_geography(hass):
|
async def test_step_geography_by_coords(hass):
|
||||||
"""Test the geograph (cloud API) step."""
|
"""Test setting up a geopgraphy entry by latitude/longitude."""
|
||||||
conf = {
|
conf = {
|
||||||
CONF_API_KEY: "abcde12345",
|
CONF_API_KEY: "abcde12345",
|
||||||
CONF_LATITUDE: 51.528308,
|
CONF_LATITUDE: 51.528308,
|
||||||
@ -200,7 +261,7 @@ async def test_step_geography(hass):
|
|||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
data={"type": "Geographical Location"},
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS},
|
||||||
)
|
)
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input=conf
|
result["flow_id"], user_input=conf
|
||||||
@ -212,7 +273,39 @@ async def test_step_geography(hass):
|
|||||||
CONF_API_KEY: "abcde12345",
|
CONF_API_KEY: "abcde12345",
|
||||||
CONF_LATITUDE: 51.528308,
|
CONF_LATITUDE: 51.528308,
|
||||||
CONF_LONGITUDE: -0.3817765,
|
CONF_LONGITUDE: -0.3817765,
|
||||||
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY,
|
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_step_geography_by_name(hass):
|
||||||
|
"""Test setting up a geopgraphy entry by city/state/country."""
|
||||||
|
conf = {
|
||||||
|
CONF_API_KEY: "abcde12345",
|
||||||
|
CONF_CITY: "Beijing",
|
||||||
|
CONF_STATE: "Beijing",
|
||||||
|
CONF_COUNTRY: "China",
|
||||||
|
}
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.airvisual.async_setup_entry", return_value=True
|
||||||
|
), patch("pyairvisual.air_quality.AirQuality.city"):
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": SOURCE_USER},
|
||||||
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME},
|
||||||
|
)
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"], user_input=conf
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
assert result["title"] == "Cloud API (Beijing, Beijing, China)"
|
||||||
|
assert result["data"] == {
|
||||||
|
CONF_API_KEY: "abcde12345",
|
||||||
|
CONF_CITY: "Beijing",
|
||||||
|
CONF_STATE: "Beijing",
|
||||||
|
CONF_COUNTRY: "China",
|
||||||
|
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -244,18 +337,19 @@ async def test_step_node_pro(hass):
|
|||||||
|
|
||||||
async def test_step_reauth(hass):
|
async def test_step_reauth(hass):
|
||||||
"""Test that the reauth step works."""
|
"""Test that the reauth step works."""
|
||||||
geography_conf = {
|
entry_data = {
|
||||||
CONF_API_KEY: "abcde12345",
|
CONF_API_KEY: "abcde12345",
|
||||||
CONF_LATITUDE: 51.528308,
|
CONF_LATITUDE: 51.528308,
|
||||||
CONF_LONGITUDE: -0.3817765,
|
CONF_LONGITUDE: -0.3817765,
|
||||||
|
CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
||||||
}
|
}
|
||||||
|
|
||||||
MockConfigEntry(
|
MockConfigEntry(
|
||||||
domain=DOMAIN, unique_id="51.528308, -0.3817765", data=geography_conf
|
domain=DOMAIN, unique_id="51.528308, -0.3817765", data=entry_data
|
||||||
).add_to_hass(hass)
|
).add_to_hass(hass)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": "reauth"}, data=geography_conf
|
DOMAIN, context={"source": "reauth"}, data=entry_data
|
||||||
)
|
)
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
|
|
||||||
@ -287,11 +381,20 @@ async def test_step_user(hass):
|
|||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
data={"type": INTEGRATION_TYPE_GEOGRAPHY},
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["step_id"] == "geography"
|
assert result["step_id"] == "geography_by_coords"
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN,
|
||||||
|
context={"source": SOURCE_USER},
|
||||||
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
assert result["step_id"] == "geography_by_name"
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user