mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Modernize Ambient PWS config flow (#32164)
* Modernize Ambient PWS config flow * Linting
This commit is contained in:
parent
c97b1c60b0
commit
ba4cc373c8
@ -1,7 +1,9 @@
|
|||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "This app key is already in use."
|
||||||
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"identifier_exists": "Application Key and/or API Key already registered",
|
|
||||||
"invalid_key": "Invalid API Key and/or Application Key",
|
"invalid_key": "Invalid API Key and/or Application Key",
|
||||||
"no_devices": "No devices found in account"
|
"no_devices": "No devices found in account"
|
||||||
},
|
},
|
||||||
|
@ -25,7 +25,6 @@ from homeassistant.helpers.dispatcher import (
|
|||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.event import async_call_later
|
from homeassistant.helpers.event import async_call_later
|
||||||
|
|
||||||
from .config_flow import configured_instances
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_LAST_DATA,
|
ATTR_LAST_DATA,
|
||||||
ATTR_MONITORED_CONDITIONS,
|
ATTR_MONITORED_CONDITIONS,
|
||||||
@ -254,9 +253,6 @@ async def async_setup(hass, config):
|
|||||||
# Store config for use during entry setup:
|
# Store config for use during entry setup:
|
||||||
hass.data[DOMAIN][DATA_CONFIG] = conf
|
hass.data[DOMAIN][DATA_CONFIG] = conf
|
||||||
|
|
||||||
if conf[CONF_APP_KEY] in configured_instances(hass):
|
|
||||||
return True
|
|
||||||
|
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.flow.async_init(
|
hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@ -270,6 +266,11 @@ async def async_setup(hass, config):
|
|||||||
|
|
||||||
async def async_setup_entry(hass, config_entry):
|
async def async_setup_entry(hass, config_entry):
|
||||||
"""Set up the Ambient PWS as config entry."""
|
"""Set up the Ambient PWS as config entry."""
|
||||||
|
if not config_entry.unique_id:
|
||||||
|
hass.config_entries.async_update_entry(
|
||||||
|
config_entry, unique_id=config_entry.data[CONF_APP_KEY]
|
||||||
|
)
|
||||||
|
|
||||||
session = aiohttp_client.async_get_clientsession(hass)
|
session = aiohttp_client.async_get_clientsession(hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -5,35 +5,29 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.core import callback
|
|
||||||
from homeassistant.helpers import aiohttp_client
|
from homeassistant.helpers import aiohttp_client
|
||||||
|
|
||||||
from .const import CONF_APP_KEY, DOMAIN
|
from .const import CONF_APP_KEY, DOMAIN # pylint: disable=unused-import
|
||||||
|
|
||||||
|
|
||||||
@callback
|
class AmbientStationFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
def configured_instances(hass):
|
|
||||||
"""Return a set of configured Ambient PWS instances."""
|
|
||||||
return set(
|
|
||||||
entry.data[CONF_APP_KEY] for entry in hass.config_entries.async_entries(DOMAIN)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@config_entries.HANDLERS.register(DOMAIN)
|
|
||||||
class AmbientStationFlowHandler(config_entries.ConfigFlow):
|
|
||||||
"""Handle an Ambient PWS config flow."""
|
"""Handle an Ambient PWS config flow."""
|
||||||
|
|
||||||
VERSION = 2
|
VERSION = 2
|
||||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH
|
||||||
|
|
||||||
async def _show_form(self, errors=None):
|
def __init__(self):
|
||||||
"""Show the form to the user."""
|
"""Initialize the config flow."""
|
||||||
data_schema = vol.Schema(
|
self.data_schema = vol.Schema(
|
||||||
{vol.Required(CONF_API_KEY): str, vol.Required(CONF_APP_KEY): str}
|
{vol.Required(CONF_API_KEY): str, vol.Required(CONF_APP_KEY): str}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def _show_form(self, errors=None):
|
||||||
|
"""Show the form to the user."""
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=data_schema, errors=errors if errors else {}
|
step_id="user",
|
||||||
|
data_schema=self.data_schema,
|
||||||
|
errors=errors if errors else {},
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_import(self, import_config):
|
async def async_step_import(self, import_config):
|
||||||
@ -42,12 +36,11 @@ class AmbientStationFlowHandler(config_entries.ConfigFlow):
|
|||||||
|
|
||||||
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 await self._show_form()
|
return await self._show_form()
|
||||||
|
|
||||||
if user_input[CONF_APP_KEY] in configured_instances(self.hass):
|
await self.async_set_unique_id(user_input[CONF_APP_KEY])
|
||||||
return await self._show_form({CONF_APP_KEY: "identifier_exists"})
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
session = aiohttp_client.async_get_clientsession(self.hass)
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
||||||
client = Client(user_input[CONF_API_KEY], user_input[CONF_APP_KEY], session)
|
client = Client(user_input[CONF_API_KEY], user_input[CONF_APP_KEY], session)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
"name": "Ambient Weather Station",
|
"name": "Ambient Weather Station",
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/ambient_station",
|
"documentation": "https://www.home-assistant.io/integrations/ambient_station",
|
||||||
"requirements": ["aioambient==1.0.2"],
|
"requirements": ["aioambient==1.0.4"],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@bachya"]
|
"codeowners": ["@bachya"]
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"identifier_exists": "Application Key and/or API Key already registered",
|
|
||||||
"invalid_key": "Invalid API Key and/or Application Key",
|
"invalid_key": "Invalid API Key and/or Application Key",
|
||||||
"no_devices": "No devices found in account"
|
"no_devices": "No devices found in account"
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "This app key is already in use."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -136,7 +136,7 @@ aio_geojson_nsw_rfs_incidents==0.3
|
|||||||
aio_georss_gdacs==0.3
|
aio_georss_gdacs==0.3
|
||||||
|
|
||||||
# homeassistant.components.ambient_station
|
# homeassistant.components.ambient_station
|
||||||
aioambient==1.0.2
|
aioambient==1.0.4
|
||||||
|
|
||||||
# homeassistant.components.asuswrt
|
# homeassistant.components.asuswrt
|
||||||
aioasuswrt==1.2.2
|
aioasuswrt==1.2.2
|
||||||
|
@ -47,7 +47,7 @@ aio_geojson_nsw_rfs_incidents==0.3
|
|||||||
aio_georss_gdacs==0.3
|
aio_georss_gdacs==0.3
|
||||||
|
|
||||||
# homeassistant.components.ambient_station
|
# homeassistant.components.ambient_station
|
||||||
aioambient==1.0.2
|
aioambient==1.0.4
|
||||||
|
|
||||||
# homeassistant.components.asuswrt
|
# homeassistant.components.asuswrt
|
||||||
aioasuswrt==1.2.2
|
aioasuswrt==1.2.2
|
||||||
|
@ -7,6 +7,7 @@ import pytest
|
|||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.ambient_station import CONF_APP_KEY, DOMAIN, config_flow
|
from homeassistant.components.ambient_station import CONF_APP_KEY, DOMAIN, config_flow
|
||||||
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, load_fixture, mock_coro
|
from tests.common import MockConfigEntry, load_fixture, mock_coro
|
||||||
@ -30,12 +31,16 @@ async def test_duplicate_error(hass):
|
|||||||
"""Test that errors are shown when duplicates are added."""
|
"""Test that errors are shown when duplicates are added."""
|
||||||
conf = {CONF_API_KEY: "12345abcde12345abcde", CONF_APP_KEY: "67890fghij67890fghij"}
|
conf = {CONF_API_KEY: "12345abcde12345abcde", CONF_APP_KEY: "67890fghij67890fghij"}
|
||||||
|
|
||||||
MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass)
|
MockConfigEntry(
|
||||||
flow = config_flow.AmbientStationFlowHandler()
|
domain=DOMAIN, unique_id="67890fghij67890fghij", data=conf
|
||||||
flow.hass = hass
|
).add_to_hass(hass)
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=conf)
|
result = await hass.config_entries.flow.async_init(
|
||||||
assert result["errors"] == {CONF_APP_KEY: "identifier_exists"}
|
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -47,6 +52,7 @@ async def test_invalid_api_key(hass, mock_aioambient):
|
|||||||
|
|
||||||
flow = config_flow.AmbientStationFlowHandler()
|
flow = config_flow.AmbientStationFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=conf)
|
result = await flow.async_step_user(user_input=conf)
|
||||||
assert result["errors"] == {"base": "invalid_key"}
|
assert result["errors"] == {"base": "invalid_key"}
|
||||||
@ -59,6 +65,7 @@ async def test_no_devices(hass, mock_aioambient):
|
|||||||
|
|
||||||
flow = config_flow.AmbientStationFlowHandler()
|
flow = config_flow.AmbientStationFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=conf)
|
result = await flow.async_step_user(user_input=conf)
|
||||||
assert result["errors"] == {"base": "no_devices"}
|
assert result["errors"] == {"base": "no_devices"}
|
||||||
@ -68,6 +75,7 @@ async def test_show_form(hass):
|
|||||||
"""Test that the form is served with no input."""
|
"""Test that the form is served with no input."""
|
||||||
flow = config_flow.AmbientStationFlowHandler()
|
flow = config_flow.AmbientStationFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=None)
|
result = await flow.async_step_user(user_input=None)
|
||||||
|
|
||||||
@ -85,6 +93,7 @@ async def test_step_import(hass, mock_aioambient):
|
|||||||
|
|
||||||
flow = config_flow.AmbientStationFlowHandler()
|
flow = config_flow.AmbientStationFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_import(import_config=conf)
|
result = await flow.async_step_import(import_config=conf)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
@ -105,6 +114,7 @@ async def test_step_user(hass, mock_aioambient):
|
|||||||
|
|
||||||
flow = config_flow.AmbientStationFlowHandler()
|
flow = config_flow.AmbientStationFlowHandler()
|
||||||
flow.hass = hass
|
flow.hass = hass
|
||||||
|
flow.context = {"source": SOURCE_USER}
|
||||||
|
|
||||||
result = await flow.async_step_user(user_input=conf)
|
result = await flow.async_step_user(user_input=conf)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
Loading…
x
Reference in New Issue
Block a user