mirror of
https://github.com/home-assistant/core.git
synced 2026-09-24 23:41:48 -05:00
Remove user-configurable polling interval from UpCloud (#181327)
This commit is contained in:
@@ -1,25 +1,14 @@
|
||||
"""Support for UpCloud."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import requests.exceptions
|
||||
import upcloud_api
|
||||
|
||||
from homeassistant.const import (
|
||||
CONF_PASSWORD,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_USERNAME,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
|
||||
from .const import CONFIG_ENTRY_UPDATE_SIGNAL_TEMPLATE, DEFAULT_SCAN_INTERVAL
|
||||
from .coordinator import UpCloudConfigEntry, UpCloudDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -27,20 +16,6 @@ _LOGGER = logging.getLogger(__name__)
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SWITCH]
|
||||
|
||||
|
||||
def _config_entry_update_signal_name(config_entry: UpCloudConfigEntry) -> str:
|
||||
"""Get signal name for updates to a config entry."""
|
||||
return CONFIG_ENTRY_UPDATE_SIGNAL_TEMPLATE.format(config_entry.unique_id)
|
||||
|
||||
|
||||
async def _async_signal_options_update(
|
||||
hass: HomeAssistant, config_entry: UpCloudConfigEntry
|
||||
) -> None:
|
||||
"""Signal config entry options update."""
|
||||
async_dispatcher_send(
|
||||
hass, _config_entry_update_signal_name(config_entry), config_entry
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: UpCloudConfigEntry) -> bool:
|
||||
"""Set up the UpCloud config entry."""
|
||||
|
||||
@@ -57,15 +32,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: UpCloudConfigEntry) -> b
|
||||
_LOGGER.exception("Failed to connect")
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
if entry.options.get(CONF_SCAN_INTERVAL):
|
||||
update_interval = timedelta(seconds=entry.options[CONF_SCAN_INTERVAL])
|
||||
else:
|
||||
update_interval = DEFAULT_SCAN_INTERVAL
|
||||
|
||||
coordinator = UpCloudDataUpdateCoordinator(
|
||||
hass,
|
||||
config_entry=entry,
|
||||
update_interval=update_interval,
|
||||
cloud_manager=manager,
|
||||
username=entry.data[CONF_USERNAME],
|
||||
)
|
||||
@@ -74,16 +43,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: UpCloudConfigEntry) -> b
|
||||
# Call the UpCloud API to refresh data
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
# Listen to config entry updates
|
||||
entry.async_on_unload(entry.add_update_listener(_async_signal_options_update))
|
||||
entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
_config_entry_update_signal_name(entry),
|
||||
coordinator.async_update_config,
|
||||
)
|
||||
)
|
||||
|
||||
# Forward entry setup
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
|
||||
@@ -7,12 +7,11 @@ import requests.exceptions
|
||||
import upcloud_api
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import callback
|
||||
|
||||
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
from .coordinator import UpCloudConfigEntry
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -83,37 +82,3 @@ class UpCloudConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
),
|
||||
errors=errors or {},
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
@override
|
||||
def async_get_options_flow(
|
||||
config_entry: UpCloudConfigEntry,
|
||||
) -> UpCloudOptionsFlow:
|
||||
"""Get options flow."""
|
||||
return UpCloudOptionsFlow()
|
||||
|
||||
|
||||
class UpCloudOptionsFlow(OptionsFlow):
|
||||
"""UpCloud options flow."""
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle options flow."""
|
||||
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title="", data=user_input)
|
||||
|
||||
data_schema = vol.Schema(
|
||||
{
|
||||
# Polling interval is user-configurable, which is no longer allowed
|
||||
# pylint: disable-next=home-assistant-config-flow-polling-field
|
||||
vol.Optional(
|
||||
CONF_SCAN_INTERVAL,
|
||||
default=self.config_entry.options.get(CONF_SCAN_INTERVAL)
|
||||
or DEFAULT_SCAN_INTERVAL.total_seconds(),
|
||||
): vol.All(vol.Coerce(int), vol.Range(min=30)),
|
||||
}
|
||||
)
|
||||
return self.async_show_form(step_id="init", data_schema=data_schema)
|
||||
|
||||
@@ -4,4 +4,3 @@ from datetime import timedelta
|
||||
|
||||
DOMAIN = "upcloud"
|
||||
DEFAULT_SCAN_INTERVAL = timedelta(seconds=60)
|
||||
CONFIG_ENTRY_UPDATE_SIGNAL_TEMPLATE = f"{DOMAIN}_config_entry_update:{{}}"
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
"""Coordinator for UpCloud."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import override
|
||||
|
||||
import upcloud_api
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DEFAULT_SCAN_INTERVAL
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ class UpCloudDataUpdateCoordinator(
|
||||
*,
|
||||
config_entry: UpCloudConfigEntry,
|
||||
cloud_manager: upcloud_api.CloudManager,
|
||||
update_interval: timedelta,
|
||||
username: str,
|
||||
) -> None:
|
||||
"""Initialize coordinator."""
|
||||
@@ -37,16 +36,10 @@ class UpCloudDataUpdateCoordinator(
|
||||
_LOGGER,
|
||||
config_entry=config_entry,
|
||||
name=f"{username}@UpCloud",
|
||||
update_interval=update_interval,
|
||||
update_interval=DEFAULT_SCAN_INTERVAL,
|
||||
)
|
||||
self.cloud_manager = cloud_manager
|
||||
|
||||
async def async_update_config(self, config_entry: UpCloudConfigEntry) -> None:
|
||||
"""Handle config update."""
|
||||
self.update_interval = timedelta(
|
||||
seconds=config_entry.options[CONF_SCAN_INTERVAL]
|
||||
)
|
||||
|
||||
@override
|
||||
async def _async_update_data(self) -> dict[str, upcloud_api.Server]:
|
||||
return {
|
||||
|
||||
@@ -15,14 +15,5 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"data": {
|
||||
"scan_interval": "Update interval in seconds, minimum 30"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
"""Tests for the UpCloud config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import requests.exceptions
|
||||
import requests_mock
|
||||
from requests_mock import ANY
|
||||
@@ -9,7 +7,7 @@ from upcloud_api import UpCloudAPIError
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.upcloud.const import DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
@@ -20,10 +18,6 @@ FIXTURE_USER_INPUT = {
|
||||
CONF_PASSWORD: "pass",
|
||||
}
|
||||
|
||||
FIXTURE_USER_INPUT_OPTIONS = {
|
||||
CONF_SCAN_INTERVAL: "120",
|
||||
}
|
||||
|
||||
|
||||
async def test_show_set_form(hass: HomeAssistant) -> None:
|
||||
"""Test that the setup form is served."""
|
||||
@@ -86,31 +80,6 @@ async def test_success(
|
||||
assert result["data"][CONF_PASSWORD] == FIXTURE_USER_INPUT[CONF_PASSWORD]
|
||||
|
||||
|
||||
async def test_options(hass: HomeAssistant) -> None:
|
||||
"""Test options produce expected data."""
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data=FIXTURE_USER_INPUT, options=FIXTURE_USER_INPUT_OPTIONS
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.upcloud.async_setup_entry", return_value=True):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input=FIXTURE_USER_INPUT_OPTIONS,
|
||||
)
|
||||
assert result["data"][CONF_SCAN_INTERVAL] == int(
|
||||
FIXTURE_USER_INPUT_OPTIONS[CONF_SCAN_INTERVAL]
|
||||
)
|
||||
|
||||
|
||||
async def test_already_configured(
|
||||
hass: HomeAssistant, requests_mock: requests_mock.Mocker
|
||||
) -> None:
|
||||
@@ -120,7 +89,6 @@ async def test_already_configured(
|
||||
domain=DOMAIN,
|
||||
unique_id=FIXTURE_USER_INPUT[CONF_USERNAME],
|
||||
data=FIXTURE_USER_INPUT,
|
||||
options=FIXTURE_USER_INPUT_OPTIONS,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user