Fixes an issue with OpenUV config import failing (#17831)

* Fixes an issue with OpenUV config import failing

* Update

* Update __init__.py

* Update config_flow.py
pull/17883/head
Aaron Bach 2018-10-27 11:07:08 -06:00 committed by GitHub
parent 92e9c2aa72
commit a22aad50e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 37 deletions

View File

@ -109,26 +109,30 @@ async def async_setup(hass, config):
return True
conf = config[DOMAIN]
latitude = conf.get(CONF_LATITUDE)
longitude = conf.get(CONF_LONGITUDE)
identifier = '{0}, {1}'.format(latitude, longitude)
identifier = '{0}, {1}'.format(
conf.get(CONF_LATITUDE, hass.config.latitude),
conf.get(CONF_LONGITUDE, hass.config.longitude))
if identifier in configured_instances(hass):
return True
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={'source': SOURCE_IMPORT},
data = {
CONF_API_KEY: conf[CONF_API_KEY],
CONF_LATITUDE: latitude,
CONF_LONGITUDE: longitude,
CONF_ELEVATION: conf.get(CONF_ELEVATION),
CONF_BINARY_SENSORS: conf[CONF_BINARY_SENSORS],
CONF_SENSORS: conf[CONF_SENSORS],
CONF_SCAN_INTERVAL: conf[CONF_SCAN_INTERVAL],
}))
}
if CONF_LATITUDE in conf:
data[CONF_LATITUDE] = conf[CONF_LATITUDE]
if CONF_LONGITUDE in conf:
data[CONF_LONGITUDE] = conf[CONF_LONGITUDE]
if CONF_ELEVATION in conf:
data[CONF_ELEVATION] = conf[CONF_ELEVATION]
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={'source': SOURCE_IMPORT}, data=data))
return True
@ -143,10 +147,11 @@ async def async_setup_entry(hass, config_entry):
openuv = OpenUV(
Client(
config_entry.data[CONF_API_KEY],
config_entry.data[CONF_LATITUDE],
config_entry.data[CONF_LONGITUDE],
config_entry.data.get(CONF_LATITUDE, hass.config.latitude),
config_entry.data.get(CONF_LONGITUDE, hass.config.longitude),
websession,
altitude=config_entry.data[CONF_ELEVATION]),
altitude=config_entry.data.get(
CONF_ELEVATION, hass.config.elevation)),
config_entry.data.get(CONF_BINARY_SENSORS, {}).get(
CONF_MONITORED_CONDITIONS, list(BINARY_SENSORS)),
config_entry.data.get(CONF_SENSORS, {}).get(
@ -158,7 +163,8 @@ async def async_setup_entry(hass, config_entry):
raise ConfigEntryNotReady
for component in ('binary_sensor', 'sensor'):
hass.async_create_task(hass.config_entries.async_forward_entry_setup(
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
config_entry, component))
async def refresh(event_time):

View File

@ -17,7 +17,8 @@ def configured_instances(hass):
"""Return a set of configured OpenUV instances."""
return set(
'{0}, {1}'.format(
entry.data[CONF_LATITUDE], entry.data[CONF_LONGITUDE])
entry.data.get(CONF_LATITUDE, hass.config.latitude),
entry.data.get(CONF_LONGITUDE, hass.config.longitude))
for entry in hass.config_entries.async_entries(DOMAIN))
@ -36,12 +37,9 @@ class OpenUvFlowHandler(config_entries.ConfigFlow):
"""Show the form to the user."""
data_schema = vol.Schema({
vol.Required(CONF_API_KEY): str,
vol.Optional(CONF_LATITUDE, default=self.hass.config.latitude):
cv.latitude,
vol.Optional(CONF_LONGITUDE, default=self.hass.config.longitude):
cv.longitude,
vol.Optional(CONF_ELEVATION, default=self.hass.config.elevation):
vol.Coerce(float),
vol.Optional(CONF_LATITUDE): cv.latitude,
vol.Optional(CONF_LONGITUDE): cv.longitude,
vol.Optional(CONF_ELEVATION): vol.Coerce(float),
})
return self.async_show_form(
@ -61,11 +59,9 @@ class OpenUvFlowHandler(config_entries.ConfigFlow):
if not user_input:
return await self._show_form()
latitude = user_input[CONF_LATITUDE]
longitude = user_input[CONF_LONGITUDE]
elevation = user_input[CONF_ELEVATION]
identifier = '{0}, {1}'.format(latitude, longitude)
identifier = '{0}, {1}'.format(
user_input.get(CONF_LATITUDE, self.hass.config.latitude),
user_input.get(CONF_LONGITUDE, self.hass.config.longitude))
if identifier in configured_instances(self.hass):
return await self._show_form({CONF_LATITUDE: 'identifier_exists'})
@ -78,11 +74,6 @@ class OpenUvFlowHandler(config_entries.ConfigFlow):
scan_interval = user_input.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
user_input.update({
CONF_LATITUDE: latitude,
CONF_LONGITUDE: longitude,
CONF_ELEVATION: elevation,
CONF_SCAN_INTERVAL: scan_interval.seconds,
})
user_input[CONF_SCAN_INTERVAL] = scan_interval.seconds
return self.async_create_entry(title=identifier, data=user_input)