Remove options flow from Plugwise (#65808)
parent
ae3b337d3e
commit
5c6d019d6b
|
@ -15,17 +15,14 @@ from homeassistant.const import (
|
|||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_USERNAME,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import (
|
||||
API,
|
||||
DEFAULT_PORT,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DEFAULT_USERNAME,
|
||||
DOMAIN,
|
||||
FLOW_NET,
|
||||
|
@ -186,37 +183,6 @@ class PlugwiseConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
errors=errors,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(config_entry):
|
||||
"""Get the options flow for this handler."""
|
||||
return PlugwiseOptionsFlowHandler(config_entry)
|
||||
|
||||
|
||||
class PlugwiseOptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Plugwise option flow."""
|
||||
|
||||
def __init__(self, config_entry):
|
||||
"""Initialize options flow."""
|
||||
self.config_entry = config_entry
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
"""Manage the Plugwise options."""
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title="", data=user_input)
|
||||
|
||||
api = self.hass.data[DOMAIN][self.config_entry.entry_id][API]
|
||||
interval = DEFAULT_SCAN_INTERVAL[api.smile_type]
|
||||
|
||||
data = {
|
||||
vol.Optional(
|
||||
CONF_SCAN_INTERVAL,
|
||||
default=self.config_entry.options.get(CONF_SCAN_INTERVAL, interval),
|
||||
): int
|
||||
}
|
||||
|
||||
return self.async_show_form(step_id="init", data_schema=vol.Schema(data))
|
||||
|
||||
|
||||
class CannotConnect(exceptions.HomeAssistantError):
|
||||
"""Error to indicate we cannot connect."""
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
"""Constants for Plugwise component."""
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.const import Platform
|
||||
|
||||
API = "api"
|
||||
|
@ -18,7 +20,6 @@ SCHEDULE_ON = "true"
|
|||
SMILE = "smile"
|
||||
STRETCH = "stretch"
|
||||
STRETCH_USERNAME = "stretch"
|
||||
UNDO_UPDATE_LISTENER = "undo_update_listener"
|
||||
UNIT_LUMEN = "lm"
|
||||
|
||||
PLATFORMS_GATEWAY = [
|
||||
|
@ -47,9 +48,9 @@ DEFAULT_MIN_TEMP = 4
|
|||
DEFAULT_NAME = "Smile"
|
||||
DEFAULT_PORT = 80
|
||||
DEFAULT_SCAN_INTERVAL = {
|
||||
"power": 10,
|
||||
"stretch": 60,
|
||||
"thermostat": 60,
|
||||
"power": timedelta(seconds=10),
|
||||
"stretch": timedelta(seconds=60),
|
||||
"thermostat": timedelta(seconds=60),
|
||||
}
|
||||
DEFAULT_TIMEOUT = 60
|
||||
DEFAULT_USERNAME = "smile"
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import async_timeout
|
||||
|
@ -21,7 +20,6 @@ from homeassistant.const import (
|
|||
CONF_HOST,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_USERNAME,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
@ -36,7 +34,6 @@ from homeassistant.helpers.update_coordinator import (
|
|||
)
|
||||
|
||||
from .const import (
|
||||
API,
|
||||
COORDINATOR,
|
||||
DEFAULT_PORT,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
|
@ -47,7 +44,6 @@ from .const import (
|
|||
PLATFORMS_GATEWAY,
|
||||
PW_TYPE,
|
||||
SENSOR_PLATFORMS,
|
||||
UNDO_UPDATE_LISTENER,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -85,12 +81,6 @@ async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
_LOGGER.error("Timeout while connecting to Smile %s", api.smile_name)
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
update_interval = timedelta(
|
||||
seconds=entry.options.get(
|
||||
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL[api.smile_type]
|
||||
)
|
||||
)
|
||||
|
||||
async def async_update_data():
|
||||
"""Update data via API endpoint."""
|
||||
try:
|
||||
|
@ -105,7 +95,7 @@ async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
_LOGGER,
|
||||
name=f"Smile {api.smile_name}",
|
||||
update_method=async_update_data,
|
||||
update_interval=update_interval,
|
||||
update_interval=DEFAULT_SCAN_INTERVAL[api.smile_type],
|
||||
)
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
@ -115,13 +105,10 @@ async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
if entry.unique_id is None and api.smile_version[0] != "1.8.0":
|
||||
hass.config_entries.async_update_entry(entry, unique_id=api.smile_hostname)
|
||||
|
||||
undo_listener = entry.add_update_listener(_update_listener)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
|
||||
"api": api,
|
||||
COORDINATOR: coordinator,
|
||||
PW_TYPE: GATEWAY,
|
||||
UNDO_UPDATE_LISTENER: undo_listener,
|
||||
}
|
||||
|
||||
device_registry = dr.async_get(hass)
|
||||
|
@ -145,28 +132,12 @@ async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
async def _update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
"""Handle options update."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id][COORDINATOR]
|
||||
update_interval = entry.options.get(CONF_SCAN_INTERVAL)
|
||||
if update_interval is None:
|
||||
api = hass.data[DOMAIN][entry.entry_id][API]
|
||||
update_interval = DEFAULT_SCAN_INTERVAL[api.smile_type]
|
||||
|
||||
coordinator.update_interval = timedelta(seconds=update_interval)
|
||||
|
||||
|
||||
async def async_unload_entry_gw(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(
|
||||
entry, PLATFORMS_GATEWAY
|
||||
)
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id][UNDO_UPDATE_LISTENER]()
|
||||
|
||||
if unload_ok:
|
||||
):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
"""Test the Plugwise config flow."""
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from plugwise.exceptions import (
|
||||
ConnectionFailedError,
|
||||
|
@ -12,7 +12,6 @@ from homeassistant.components import zeroconf
|
|||
from homeassistant.components.plugwise.const import (
|
||||
API,
|
||||
DEFAULT_PORT,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
DOMAIN,
|
||||
FLOW_NET,
|
||||
FLOW_TYPE,
|
||||
|
@ -24,7 +23,6 @@ from homeassistant.const import (
|
|||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_SOURCE,
|
||||
CONF_USERNAME,
|
||||
)
|
||||
|
@ -375,68 +373,3 @@ async def test_form_other_problem(hass, mock_smile):
|
|||
|
||||
assert result2["type"] == RESULT_TYPE_FORM
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_options_flow_power(hass, mock_smile) -> None:
|
||||
"""Test config flow options DSMR environments."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=CONF_NAME,
|
||||
data={CONF_HOST: TEST_HOST, CONF_PASSWORD: TEST_PASSWORD},
|
||||
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
||||
)
|
||||
|
||||
hass.data[DOMAIN] = {entry.entry_id: {"api": MagicMock(smile_type="power")}}
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.plugwise.async_setup_entry", return_value=True
|
||||
):
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], user_input={CONF_SCAN_INTERVAL: 10}
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
CONF_SCAN_INTERVAL: 10,
|
||||
}
|
||||
|
||||
|
||||
async def test_options_flow_thermo(hass, mock_smile) -> None:
|
||||
"""Test config flow options for thermostatic environments."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=CONF_NAME,
|
||||
data={CONF_HOST: TEST_HOST, CONF_PASSWORD: TEST_PASSWORD},
|
||||
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
|
||||
)
|
||||
|
||||
hass.data[DOMAIN] = {entry.entry_id: {"api": MagicMock(smile_type="thermostat")}}
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.plugwise.async_setup_entry", return_value=True
|
||||
):
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"], user_input={CONF_SCAN_INTERVAL: 60}
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
CONF_SCAN_INTERVAL: 60,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue