-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add config flow to nws and remove yaml configuration #34267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
76b838b
add config flow to nws and remove yaml
MatthewFlamm 569a16b
Don't duplicate scan_time
MatthewFlamm ee7a3f4
Use _abort_if_unique_id_configured
MatthewFlamm 9649983
fix abort
MatthewFlamm fe2e14f
Add unavailable tests
MatthewFlamm 28ccb16
update and use better strings
MatthewFlamm ed9bb12
lint
MatthewFlamm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "config": { | ||
| "abort": { | ||
| "already_configured": "Device is already configured" | ||
| }, | ||
| "error": { | ||
| "cannot_connect": "Failed to connect, please try again", | ||
| "unknown": "Unexpected error" | ||
| }, | ||
| "step": { | ||
| "user": { | ||
| "data": { | ||
| "api_key": "API key (email)", | ||
| "latitude": "Latitude", | ||
| "longitude": "Longitude", | ||
| "station": "METAR station code" | ||
| }, | ||
| "description": "If a METAR station code is not specified, the latitude and longitude will be used to find the closest station.", | ||
| "title": "Connect to the National Weather Service" | ||
| } | ||
| } | ||
| }, | ||
| "title": "National Weather Service (NWS)" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """Config flow for National Weather Service (NWS) integration.""" | ||
| import logging | ||
|
|
||
| import aiohttp | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant import config_entries, core, exceptions | ||
| from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
|
|
||
| from . import NwsData, base_unique_id | ||
| from .const import CONF_STATION, DOMAIN # pylint:disable=unused-import | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def validate_input(hass: core.HomeAssistant, data): | ||
| """Validate the user input allows us to connect. | ||
|
|
||
| Data has the keys from DATA_SCHEMA with values provided by the user. | ||
| """ | ||
| latitude = data[CONF_LATITUDE] | ||
| longitude = data[CONF_LONGITUDE] | ||
| api_key = data[CONF_API_KEY] | ||
| station = data.get(CONF_STATION) | ||
|
|
||
| client_session = async_get_clientsession(hass) | ||
| ha_api_key = f"{api_key} homeassistant" | ||
| nws = NwsData(hass, latitude, longitude, ha_api_key, client_session) | ||
|
|
||
| try: | ||
| await nws.async_set_station(station) | ||
| except aiohttp.ClientError as err: | ||
| _LOGGER.error("Could not connect: %s", err) | ||
| raise CannotConnect | ||
|
|
||
| return {"title": nws.station} | ||
|
|
||
|
|
||
| class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): | ||
| """Handle a config flow for National Weather Service (NWS).""" | ||
|
|
||
| VERSION = 1 | ||
| CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL | ||
|
|
||
| async def async_step_user(self, user_input=None): | ||
| """Handle the initial step.""" | ||
| errors = {} | ||
| if user_input is not None: | ||
| await self.async_set_unique_id( | ||
| base_unique_id(user_input[CONF_LATITUDE], user_input[CONF_LONGITUDE]) | ||
| ) | ||
| self._abort_if_unique_id_configured() | ||
| try: | ||
| info = await validate_input(self.hass, user_input) | ||
| user_input[CONF_STATION] = info["title"] | ||
| return self.async_create_entry(title=info["title"], data=user_input) | ||
| except CannotConnect: | ||
| errors["base"] = "cannot_connect" | ||
| except Exception: # pylint: disable=broad-except | ||
| _LOGGER.exception("Unexpected exception") | ||
| errors["base"] = "unknown" | ||
|
|
||
| data_schema = vol.Schema( | ||
| { | ||
| vol.Required(CONF_API_KEY): str, | ||
| vol.Required( | ||
| CONF_LATITUDE, default=self.hass.config.latitude | ||
| ): cv.latitude, | ||
| vol.Required( | ||
| CONF_LONGITUDE, default=self.hass.config.longitude | ||
| ): cv.longitude, | ||
| vol.Optional(CONF_STATION): str, | ||
| } | ||
| ) | ||
|
|
||
| return self.async_show_form( | ||
| step_id="user", data_schema=data_schema, errors=errors | ||
| ) | ||
|
|
||
|
|
||
| class CannotConnect(exceptions.HomeAssistantError): | ||
| """Error to indicate we cannot connect.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "title": "National Weather Service (NWS)", | ||
| "config": { | ||
| "step": { | ||
| "user": { | ||
| "description": "If a METAR station code is not specified, the latitude and longitude will be used to find the closest station.", | ||
| "title": "Connect to the National Weather Service", | ||
| "data": { | ||
| "api_key": "API key (email)", | ||
| "latitude": "Latitude", | ||
| "longitude": "Longitude", | ||
| "station": "METAR station code" | ||
| } | ||
| } | ||
| }, | ||
| "error": { | ||
| "cannot_connect": "Failed to connect, please try again", | ||
| "unknown": "Unexpected error" | ||
| }, | ||
| "abort": { | ||
| "already_configured": "Device is already configured" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,7 @@ | |
| "notion", | ||
| "nuheat", | ||
| "nut", | ||
| "nws", | ||
| "opentherm_gw", | ||
| "openuv", | ||
| "owntracks", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.