mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Add risco options flow (#39154)
This commit is contained in:
parent
4af90e41ce
commit
cdb6161d3d
@ -6,16 +6,21 @@ import logging
|
|||||||
from pyrisco import CannotConnectError, OperationError, RiscoAPI, UnauthorizedError
|
from pyrisco import CannotConnectError, OperationError, RiscoAPI, UnauthorizedError
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_PIN, CONF_USERNAME
|
from homeassistant.const import (
|
||||||
|
CONF_PASSWORD,
|
||||||
|
CONF_PIN,
|
||||||
|
CONF_SCAN_INTERVAL,
|
||||||
|
CONF_USERNAME,
|
||||||
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DOMAIN
|
from .const import DATA_COORDINATOR, DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||||
|
|
||||||
PLATFORMS = ["alarm_control_panel"]
|
PLATFORMS = ["alarm_control_panel"]
|
||||||
|
UNDO_UPDATE_LISTENER = "undo_update_listener"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -38,11 +43,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
_LOGGER.exception("Failed to login to Risco cloud")
|
_LOGGER.exception("Failed to login to Risco cloud")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
coordinator = RiscoDataUpdateCoordinator(hass, risco)
|
scan_interval = entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||||
|
coordinator = RiscoDataUpdateCoordinator(hass, risco, scan_interval)
|
||||||
await coordinator.async_refresh()
|
await coordinator.async_refresh()
|
||||||
|
|
||||||
|
undo_listener = entry.add_update_listener(_update_listener)
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {
|
hass.data[DOMAIN][entry.entry_id] = {
|
||||||
DATA_COORDINATOR: coordinator,
|
DATA_COORDINATOR: coordinator,
|
||||||
|
UNDO_UPDATE_LISTENER: undo_listener,
|
||||||
}
|
}
|
||||||
|
|
||||||
for component in PLATFORMS:
|
for component in PLATFORMS:
|
||||||
@ -65,18 +74,24 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
|
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
|
async def _update_listener(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
|
"""Handle options update."""
|
||||||
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
class RiscoDataUpdateCoordinator(DataUpdateCoordinator):
|
class RiscoDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
"""Class to manage fetching risco data."""
|
"""Class to manage fetching risco data."""
|
||||||
|
|
||||||
def __init__(self, hass, risco):
|
def __init__(self, hass, risco, scan_interval):
|
||||||
"""Initialize global risco data updater."""
|
"""Initialize global risco data updater."""
|
||||||
self.risco = risco
|
self.risco = risco
|
||||||
interval = timedelta(seconds=30)
|
interval = timedelta(seconds=scan_interval)
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass, _LOGGER, name=DOMAIN, update_interval=interval,
|
hass, _LOGGER, name=DOMAIN, update_interval=interval,
|
||||||
)
|
)
|
||||||
|
@ -5,10 +5,15 @@ from pyrisco import CannotConnectError, RiscoAPI, UnauthorizedError
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries, core
|
from homeassistant import config_entries, core
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_PIN, CONF_USERNAME
|
from homeassistant.const import (
|
||||||
|
CONF_PASSWORD,
|
||||||
|
CONF_PIN,
|
||||||
|
CONF_SCAN_INTERVAL,
|
||||||
|
CONF_USERNAME,
|
||||||
|
)
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import DOMAIN # pylint:disable=unused-import
|
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN # pylint:disable=unused-import
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -37,6 +42,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
VERSION = 1
|
VERSION = 1
|
||||||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@core.callback
|
||||||
|
def async_get_options_flow(config_entry):
|
||||||
|
"""Define the config flow to handle options."""
|
||||||
|
return RiscoOptionsFlowHandler(config_entry)
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(self, user_input=None):
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
errors = {}
|
errors = {}
|
||||||
@ -59,3 +70,26 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RiscoOptionsFlowHandler(config_entries.OptionsFlow):
|
||||||
|
"""Handle a Risco options flow."""
|
||||||
|
|
||||||
|
def __init__(self, config_entry):
|
||||||
|
"""Initialize."""
|
||||||
|
self.config_entry = config_entry
|
||||||
|
|
||||||
|
async def async_step_init(self, user_input=None):
|
||||||
|
"""Manage the options."""
|
||||||
|
if user_input is not None:
|
||||||
|
return self.async_create_entry(
|
||||||
|
title="", data={CONF_SCAN_INTERVAL: user_input[CONF_SCAN_INTERVAL]}
|
||||||
|
)
|
||||||
|
|
||||||
|
current = self.config_entry.options.get(
|
||||||
|
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
||||||
|
)
|
||||||
|
|
||||||
|
options = vol.Schema({vol.Required(CONF_SCAN_INTERVAL, default=current): int})
|
||||||
|
|
||||||
|
return self.async_show_form(step_id="init", data_schema=options)
|
||||||
|
@ -3,3 +3,5 @@
|
|||||||
DOMAIN = "risco"
|
DOMAIN = "risco"
|
||||||
|
|
||||||
DATA_COORDINATOR = "risco"
|
DATA_COORDINATOR = "risco"
|
||||||
|
|
||||||
|
DEFAULT_SCAN_INTERVAL = 30
|
||||||
|
@ -17,5 +17,15 @@
|
|||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"options": {
|
||||||
|
"step": {
|
||||||
|
"init": {
|
||||||
|
"title": "Configure options",
|
||||||
|
"data": {
|
||||||
|
"scan_interval": "How often to poll Risco (in seconds)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
"""Test the Risco config flow."""
|
"""Test the Risco config flow."""
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries, data_entry_flow
|
||||||
from homeassistant.components.risco.config_flow import (
|
from homeassistant.components.risco.config_flow import (
|
||||||
CannotConnectError,
|
CannotConnectError,
|
||||||
UnauthorizedError,
|
UnauthorizedError,
|
||||||
@ -114,7 +114,6 @@ async def test_form_already_exists(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
@ -126,3 +125,27 @@ async def test_form_already_exists(hass):
|
|||||||
|
|
||||||
assert result2["type"] == "abort"
|
assert result2["type"] == "abort"
|
||||||
assert result2["reason"] == "already_configured"
|
assert result2["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_options_flow(hass):
|
||||||
|
"""Test options flow."""
|
||||||
|
conf = {"scan_interval": 10}
|
||||||
|
|
||||||
|
entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN, unique_id=TEST_DATA["username"], data=TEST_DATA,
|
||||||
|
)
|
||||||
|
|
||||||
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
with patch("homeassistant.components.risco.async_setup_entry", return_value=True):
|
||||||
|
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
assert result["step_id"] == "init"
|
||||||
|
|
||||||
|
result = await hass.config_entries.options.async_configure(
|
||||||
|
result["flow_id"], user_input=conf,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
|
assert entry.options == conf
|
||||||
|
Loading…
x
Reference in New Issue
Block a user