Move upnp update_interval options flow (#35474)
This commit is contained in:
parent
d2502f649f
commit
48899c7a1c
4 changed files with 113 additions and 96 deletions
|
@ -1,4 +1,5 @@
|
|||
"""Config flow for UPNP."""
|
||||
from datetime import timedelta
|
||||
from typing import Mapping, Optional
|
||||
|
||||
import voluptuous as vol
|
||||
|
@ -6,6 +7,7 @@ import voluptuous as vol
|
|||
from homeassistant import config_entries
|
||||
from homeassistant.components import ssdp
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL
|
||||
from homeassistant.core import callback
|
||||
|
||||
from .const import ( # pylint: disable=unused-import
|
||||
CONFIG_ENTRY_SCAN_INTERVAL,
|
||||
|
@ -57,9 +59,7 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
await self.async_set_unique_id(
|
||||
discovery[DISCOVERY_USN], raise_on_progress=False
|
||||
)
|
||||
return await self._async_create_entry_from_discovery(
|
||||
discovery, user_input[CONF_SCAN_INTERVAL]
|
||||
)
|
||||
return await self._async_create_entry_from_discovery(discovery)
|
||||
|
||||
# Discover devices.
|
||||
discoveries = await Device.async_discover(self.hass)
|
||||
|
@ -87,9 +87,6 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
for discovery in self._discoveries
|
||||
}
|
||||
),
|
||||
vol.Optional(
|
||||
CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL,
|
||||
): vol.All(vol.Coerce(int), vol.Range(min=30)),
|
||||
}
|
||||
)
|
||||
return self.async_show_form(step_id="user", data_schema=data_schema,)
|
||||
|
@ -127,9 +124,7 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
return self.async_abort(reason="no_devices_found")
|
||||
|
||||
discovery = self._discoveries[0]
|
||||
return await self._async_create_entry_from_discovery(
|
||||
discovery, DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
return await self._async_create_entry_from_discovery(discovery)
|
||||
|
||||
async def async_step_ssdp(self, discovery_info: Mapping):
|
||||
"""Handle a discovered UPnP/IGD device.
|
||||
|
@ -170,18 +165,20 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
return self.async_show_form(step_id="ssdp_confirm")
|
||||
|
||||
discovery = self._discoveries[0]
|
||||
return await self._async_create_entry_from_discovery(
|
||||
discovery, DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
return await self._async_create_entry_from_discovery(discovery)
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(config_entry):
|
||||
"""Define the config flow to handle options."""
|
||||
return UpnpOptionsFlowHandler(config_entry)
|
||||
|
||||
async def _async_create_entry_from_discovery(
|
||||
self, discovery: Mapping, scan_interval
|
||||
self, discovery: Mapping,
|
||||
):
|
||||
"""Create an entry from discovery."""
|
||||
_LOGGER.debug(
|
||||
"_async_create_entry_from_data: discovery: %s, scan_interval: %s",
|
||||
discovery,
|
||||
scan_interval,
|
||||
"_async_create_entry_from_data: discovery: %s", discovery,
|
||||
)
|
||||
# Get name from device, if not found already.
|
||||
if DISCOVERY_NAME not in discovery and DISCOVERY_LOCATION in discovery:
|
||||
|
@ -193,7 +190,6 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
data = {
|
||||
CONFIG_ENTRY_UDN: discovery[DISCOVERY_UDN],
|
||||
CONFIG_ENTRY_ST: discovery[DISCOVERY_ST],
|
||||
CONFIG_ENTRY_SCAN_INTERVAL: scan_interval,
|
||||
}
|
||||
return self.async_create_entry(title=title, data=data)
|
||||
|
||||
|
@ -204,3 +200,37 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
self.hass, discovery[DISCOVERY_LOCATION]
|
||||
)
|
||||
return device.name
|
||||
|
||||
|
||||
class UpnpOptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Handle a UPnP 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:
|
||||
udn = self.config_entry.data.get(CONFIG_ENTRY_UDN)
|
||||
coordinator = self.hass.data[DOMAIN]["coordinators"][udn]
|
||||
update_interval_sec = user_input.get(
|
||||
CONFIG_ENTRY_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
update_interval = timedelta(seconds=update_interval_sec)
|
||||
coordinator.update_interval = update_interval
|
||||
return self.async_create_entry(title="", data=user_input)
|
||||
|
||||
scan_interval = self.config_entry.options.get(
|
||||
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_SCAN_INTERVAL, default=scan_interval,): vol.All(
|
||||
vol.Coerce(int), vol.Range(min=30)
|
||||
),
|
||||
}
|
||||
),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue