From 82090f5060cb263aeda068ac35c01cf8b83e6a73 Mon Sep 17 00:00:00 2001 From: Kevin Fronczak Date: Wed, 10 Jun 2020 12:38:17 -0400 Subject: [PATCH] Add options flow to Blink (#35645) --- homeassistant/components/blink/__init__.py | 15 ++++++- homeassistant/components/blink/config_flow.py | 44 ++++++++++++++++++- homeassistant/components/blink/strings.json | 13 +++++- .../components/blink/translations/en.json | 13 +++++- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/blink/__init__.py b/homeassistant/components/blink/__init__.py index 04f9652bcb5..2344ce7b432 100644 --- a/homeassistant/components/blink/__init__.py +++ b/homeassistant/components/blink/__init__.py @@ -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( diff --git a/homeassistant/components/blink/config_flow.py b/homeassistant/components/blink/config_flow.py index 281dee17cb1..4cd89175ab6 100644 --- a/homeassistant/components/blink/config_flow.py +++ b/homeassistant/components/blink/config_flow.py @@ -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.""" diff --git a/homeassistant/components/blink/strings.json b/homeassistant/components/blink/strings.json index dcd4a488c5c..e3bbe4006f3 100644 --- a/homeassistant/components/blink/strings.json +++ b/homeassistant/components/blink/strings.json @@ -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" + } + } + } } diff --git a/homeassistant/components/blink/translations/en.json b/homeassistant/components/blink/translations/en.json index 2187e91f09e..431afa50d9d 100644 --- a/homeassistant/components/blink/translations/en.json +++ b/homeassistant/components/blink/translations/en.json @@ -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" + } + } } -} \ No newline at end of file +}