Add options flow to Blink (#35645)

This commit is contained in:
Kevin Fronczak 2020-06-10 12:38:17 -04:00 committed by GitHub
parent 186a299215
commit 82090f5060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 5 deletions

View File

@ -14,6 +14,7 @@ from homeassistant.const import (
CONF_SCAN_INTERVAL,
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from .const import (
@ -58,7 +59,7 @@ def _blink_startup_wrapper(entry):
no_prompt=True,
device_id=DEVICE_ID,
)
blink.refresh_rate = entry.data[CONF_SCAN_INTERVAL]
blink.refresh_rate = entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
try:
blink.login_response = entry.data["login_response"]
@ -91,6 +92,8 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, entry):
"""Set up Blink via config entry."""
_async_import_options_from_data_if_missing(hass, entry)
hass.data[DOMAIN][entry.entry_id] = await hass.async_add_executor_job(
_blink_startup_wrapper, entry
)
@ -130,6 +133,16 @@ async def async_setup_entry(hass, entry):
return True
@callback
def _async_import_options_from_data_if_missing(hass, entry):
options = dict(entry.options)
if CONF_SCAN_INTERVAL not in entry.options:
options[CONF_SCAN_INTERVAL] = entry.data.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
)
hass.config_entries.async_update_entry(entry, options=options)
async def async_unload_entry(hass, entry):
"""Unload Blink entry."""
unload_ok = all(

View File

@ -11,6 +11,7 @@ from homeassistant.const import (
CONF_SCAN_INTERVAL,
CONF_USERNAME,
)
from homeassistant.core import callback
from .const import DEFAULT_OFFSET, DEFAULT_SCAN_INTERVAL, DEVICE_ID, DOMAIN
@ -40,10 +41,15 @@ class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self.data = {
CONF_USERNAME: "",
CONF_PASSWORD: "",
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
"login_response": None,
}
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get options flow for this handler."""
return BlinkOptionsFlowHandler(config_entry)
async def async_step_user(self, user_input=None):
"""Handle a flow initiated by the user."""
errors = {}
@ -54,7 +60,7 @@ class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
await self.async_set_unique_id(self.data[CONF_USERNAME])
if CONF_SCAN_INTERVAL in user_input:
self.data[CONF_SCAN_INTERVAL] = user_input["scan_interval"]
self.data[CONF_SCAN_INTERVAL] = user_input[CONF_SCAN_INTERVAL]
self.blink = Blink(
username=self.data[CONF_USERNAME],
@ -107,6 +113,40 @@ class BlinkConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return await self.async_step_user(import_data)
class BlinkOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle Blink options."""
def __init__(self, config_entry):
"""Initialize Blink options flow."""
self.config_entry = config_entry
self.options = dict(config_entry.options)
self.blink = None
async def async_step_init(self, user_input=None):
"""Manage the Blink options."""
self.blink = self.hass.data[DOMAIN][self.config_entry.entry_id]
self.options[CONF_SCAN_INTERVAL] = self.blink.refresh_rate
return await self.async_step_simple_options()
async def async_step_simple_options(self, user_input=None):
"""For simple options."""
if user_input is not None:
self.options.update(user_input)
self.blink.refresh_rate = user_input[CONF_SCAN_INTERVAL]
return self.async_create_entry(title="", data=self.options)
options = self.config_entry.options
scan_interval = options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
return self.async_show_form(
step_id="simple_options",
data_schema=vol.Schema(
{vol.Optional(CONF_SCAN_INTERVAL, default=scan_interval,): int}
),
)
class Require2FA(exceptions.HomeAssistantError):
"""Error to indicate we require 2FA."""

View File

@ -21,5 +21,16 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
},
"options": {
"step": {
"simple_options": {
"data": {
"scan_interval": "Scan Interval (seconds)"
},
"title": "Blink options",
"description": "Configure Blink integration"
}
}
}
}

View File

@ -23,5 +23,16 @@
"title": "Sign-in with Blink account"
}
}
},
"options": {
"step": {
"simple_options": {
"data": {
"scan_interval": "Scan Interval (seconds)"
},
"title": "Blink options",
"description": "Configure Blink integration"
}
}
}
}