-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from firstof9/add_config_flow
Add Config Flow option
- Loading branch information
Showing
9 changed files
with
477 additions
and
199 deletions.
There are no files selected for viewing
This file contains 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,64 @@ | ||
""" NWS Alerts """ | ||
import logging | ||
from homeassistant import config_entries | ||
from .const import ( | ||
DOMAIN, | ||
VERSION, | ||
ISSUE_URL, | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup(hass, config_entry): | ||
"""Set up this component using YAML.""" | ||
if config_entry.get(DOMAIN) is None: | ||
# We get here if the integration is set up using config flow | ||
return True | ||
|
||
# Print startup message | ||
_LOGGER.info('Version %s is starting, if you have any issues please report' | ||
' them here: %s', VERSION, ISSUE_URL) | ||
|
||
hass.async_create_task( | ||
hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={} | ||
) | ||
) | ||
|
||
return True | ||
|
||
|
||
async def async_setup_entry(hass, config_entry): | ||
"""Load the saved entities.""" | ||
# Print startup message | ||
_LOGGER.info('Version %s is starting, if you have any issues please report' | ||
' them here: %s', VERSION, ISSUE_URL) | ||
config_entry.options = config_entry.data | ||
config_entry.add_update_listener(update_listener) | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(config_entry, "sensor") | ||
) | ||
|
||
return True | ||
|
||
|
||
async def async_unload_entry(hass, config_entry): | ||
"""Handle removal of an entry.""" | ||
try: | ||
await hass.config_entries.async_forward_entry_unload(config_entry, | ||
"sensor") | ||
_LOGGER.info( | ||
"Successfully removed sensor from the " + DOMAIN + " integration" | ||
) | ||
except ValueError: | ||
pass | ||
return True | ||
|
||
|
||
async def update_listener(hass, entry): | ||
"""Update listener.""" | ||
entry.data = entry.options | ||
await hass.config_entries.async_forward_entry_unload(entry, "sensor") | ||
hass.async_add_job(hass.config_entries.async_forward_entry_setup(entry, | ||
"sensor")) |
This file contains 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,107 @@ | ||
"""Adds config flow for NWS Alerts.""" | ||
import logging | ||
from collections import OrderedDict | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.core import callback | ||
from homeassistant import config_entries | ||
from .const import ( | ||
DOMAIN, | ||
CONF_ZONE_ID, | ||
DEFAULT_NAME, | ||
) | ||
|
||
from homeassistant.const import CONF_NAME | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@config_entries.HANDLERS.register(DOMAIN) | ||
class NWSAlertsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): | ||
"""Config flow for NWS Alerts.""" | ||
|
||
VERSION = 1 | ||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL | ||
|
||
def __init__(self): | ||
"""Initialize.""" | ||
self._data = {} | ||
self._errors = {} | ||
|
||
async def async_step_user(self, user_input={}): | ||
"""Handle a flow initialized by the user.""" | ||
self._errors = {} | ||
|
||
if user_input is not None: | ||
self._data.update(user_input) | ||
return self.async_create_entry(title=self._data[CONF_NAME], | ||
data=self._data) | ||
return await self._show_config_form(user_input) | ||
|
||
return await self._show_config_form(user_input) | ||
|
||
async def _show_config_form(self, user_input): | ||
"""Show the configuration form to edit location data.""" | ||
|
||
# Defaults | ||
name = DEFAULT_NAME | ||
|
||
if user_input is not None: | ||
if "name" in user_input: | ||
name = user_input["name"] | ||
if "zone_id" in user_input: | ||
zone_id = user_input["zone_id"] | ||
|
||
data_schema = OrderedDict() | ||
data_schema[vol.Required("name", default=name)] = str | ||
data_schema[vol.Required("zone_id")] = str | ||
return self.async_show_form( | ||
step_id="user", data_schema=vol.Schema(data_schema), | ||
errors=self._errors) | ||
|
||
@staticmethod | ||
@callback | ||
def async_get_options_flow(config_entry): | ||
return NWSAlertsOptionsFlow(config_entry) | ||
|
||
|
||
class NWSAlertsOptionsFlow(config_entries.OptionsFlow): | ||
"""Options flow for NWS Alerts.""" | ||
|
||
def __init__(self, config_entry): | ||
"""Initialize.""" | ||
self.config = config_entry | ||
self._data = dict(config_entry.options) | ||
self._errors = {} | ||
|
||
async def async_step_init(self, user_input=None): | ||
"""Manage Mail and Packages options.""" | ||
if user_input is not None: | ||
self._data.update(user_input) | ||
return self.async_create_entry(title=self._data[CONF_NAME], | ||
data=self._data) | ||
|
||
return await self._show_options_form(user_input) | ||
|
||
return await self._show_options_form(user_input) | ||
|
||
async def _show_options_form(self, user_input): | ||
"""Show the configuration form to edit location data.""" | ||
|
||
# Defaults | ||
name = self.config.options.get(CONF_NAME) | ||
zone_id = self.config.options.get(CONF_ZONE_ID) | ||
|
||
if user_input is not None: | ||
if "name" in user_input: | ||
name = user_input["name"] | ||
if "zone_id" in user_input: | ||
zone_id = user_input["zone_id"] | ||
|
||
data_schema = OrderedDict() | ||
data_schema[vol.Required("name", default=name)] = str | ||
data_schema[vol.Required("zone_id", default=zone_id)] = str | ||
return self.async_show_form( | ||
step_id="init", data_schema=vol.Schema(data_schema), | ||
errors=self._errors) |
This file contains 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,11 @@ | ||
API_ENDPOINT = 'https://api.weather.gov' | ||
USER_AGENT = 'Home Assistant' | ||
DEFAULT_ICON = 'mdi:alert' | ||
DEFAULT_NAME = 'NWS Alerts' | ||
CONF_ZONE_ID = 'zone_id' | ||
ZONE_ID = '' | ||
VERSION = '1.0' | ||
ISSUE_URL = 'https://github.com/finity69x2/nws_alert' | ||
DOMAIN = 'nws_alerts' | ||
PLATFORM = 'sensor' | ||
ATTRIBUTION = 'Data provided by Weather.gov' |
This file contains 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,9 @@ | ||
{ | ||
"domain": "nws_alerts", | ||
"name": "NWS Alerts", | ||
"documentation": "https://github.com/finity69x2/nws_alerts/", | ||
"dependencies": [], | ||
"codeowners": ["@finity69x2"], | ||
"config_flow": true, | ||
"requirements": [] | ||
} |
Oops, something went wrong.