Cleanup timeouts values for Shelly (#44790)

* Updated timeouts

* Small cleanup

* Fix + small cleanup of test code
This commit is contained in:
Simone Chemelli 2021-01-04 14:04:40 +01:00 committed by GitHub
parent f771d8ff14
commit f07bf6a88e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 29 deletions

View File

@ -24,6 +24,7 @@ from homeassistant.helpers import (
)
from .const import (
AIOSHELLY_DEVICE_TIMEOUT_SEC,
BATTERY_DEVICES_WITH_PERMANENT_CONNECTION,
COAP,
DATA_CONFIG_ENTRY,
@ -32,7 +33,6 @@ from .const import (
POLLING_TIMEOUT_MULTIPLIER,
REST,
REST_SENSORS_UPDATE_INTERVAL,
SETUP_ENTRY_TIMEOUT_SEC,
SLEEP_PERIOD_MULTIPLIER,
UPDATE_PERIOD_MULTIPLIER,
)
@ -79,7 +79,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
coap_context = await get_coap_context(hass)
try:
async with async_timeout.timeout(SETUP_ENTRY_TIMEOUT_SEC):
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
device = await aioshelly.Device.create(
aiohttp_client.async_get_clientsession(hass),
coap_context,
@ -263,7 +263,7 @@ class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator):
async def _async_update_data(self):
"""Fetch data."""
try:
async with async_timeout.timeout(5):
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
_LOGGER.debug("REST update for %s", self.name)
return await self.device.update_status()
except OSError as err:

View File

@ -18,6 +18,7 @@ from homeassistant.const import (
from homeassistant.helpers import aiohttp_client
from . import get_coap_context
from .const import AIOSHELLY_DEVICE_TIMEOUT_SEC
from .const import DOMAIN # pylint:disable=unused-import
_LOGGER = logging.getLogger(__name__)
@ -39,7 +40,7 @@ async def validate_input(hass: core.HomeAssistant, host, data):
)
coap_context = await get_coap_context(hass)
async with async_timeout.timeout(5):
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
device = await aioshelly.Device.create(
aiohttp_client.async_get_clientsession(hass),
coap_context,
@ -187,7 +188,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def _async_get_info(self, host):
"""Get info from shelly device."""
async with async_timeout.timeout(5):
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
return await aioshelly.get_info(
aiohttp_client.async_get_clientsession(self.hass),
host,

View File

@ -11,8 +11,8 @@ POLLING_TIMEOUT_MULTIPLIER = 1.2
# Refresh interval for REST sensors
REST_SENSORS_UPDATE_INTERVAL = 60
# Timeout used for initial entry setup in "async_setup_entry".
SETUP_ENTRY_TIMEOUT_SEC = 10
# Timeout used for aioshelly calls
AIOSHELLY_DEVICE_TIMEOUT_SEC = 10
# Multiplier used to calculate the "update_interval" for sleeping devices.
SLEEP_PERIOD_MULTIPLIER = 1.2

View File

@ -53,7 +53,7 @@ async def test_form(hass):
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == "Test name"
assert result2["data"] == {
"host": "1.1.1.1",
@ -68,7 +68,7 @@ async def test_title_without_name(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {}
settings = MOCK_SETTINGS.copy()
@ -97,7 +97,7 @@ async def test_title_without_name(hass):
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == "shelly1pm-12345"
assert result2["data"] == {
"host": "1.1.1.1",
@ -111,7 +111,7 @@ async def test_form_auth(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {}
with patch(
@ -123,7 +123,7 @@ async def test_form_auth(hass):
{"host": "1.1.1.1"},
)
assert result2["type"] == "form"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {}
with patch(
@ -145,7 +145,7 @@ async def test_form_auth(hass):
)
await hass.async_block_till_done()
assert result3["type"] == "create_entry"
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "Test name"
assert result3["data"] == {
"host": "1.1.1.1",
@ -172,7 +172,7 @@ async def test_form_errors_get_info(hass, error):
{"host": "1.1.1.1"},
)
assert result2["type"] == "form"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] == {"base": base_error}
@ -194,7 +194,7 @@ async def test_form_errors_test_connection(hass, error):
{"host": "1.1.1.1"},
)
assert result2["type"] == "form"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] == {"base": base_error}
@ -219,7 +219,7 @@ async def test_form_already_configured(hass):
{"host": "1.1.1.1"},
)
assert result2["type"] == "abort"
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result2["reason"] == "already_configured"
# Test config entry got updated with latest IP
@ -241,16 +241,28 @@ async def test_user_setup_ignored_device(hass):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
settings = MOCK_SETTINGS.copy()
settings["device"]["type"] = "SHSW-1"
settings["fw"] = "20201124-092534/v1.9.0@57ac4ad8"
with patch(
"aioshelly.get_info",
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
), patch(
"aioshelly.Device.create",
new=AsyncMock(
return_value=Mock(
settings=settings,
)
),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"host": "1.1.1.1"},
)
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
# Test config entry got updated with latest IP
assert entry.data["host"] == "1.1.1.1"
@ -268,7 +280,7 @@ async def test_form_firmware_unsupported(hass):
{"host": "1.1.1.1"},
)
assert result2["type"] == "abort"
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result2["reason"] == "unsupported_firmware"
@ -302,7 +314,7 @@ async def test_form_auth_errors_test_connection(hass, error):
result2["flow_id"],
{"username": "test username", "password": "test password"},
)
assert result3["type"] == "form"
assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result3["errors"] == {"base": base_error}
@ -319,7 +331,7 @@ async def test_zeroconf(hass):
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {}
context = next(
flow["context"]
@ -346,7 +358,7 @@ async def test_zeroconf(hass):
)
await hass.async_block_till_done()
assert result2["type"] == "create_entry"
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["title"] == "Test name"
assert result2["data"] == {
"host": "1.1.1.1",
@ -372,7 +384,7 @@ async def test_zeroconf_confirm_error(hass, error):
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {}
with patch(
@ -384,7 +396,7 @@ async def test_zeroconf_confirm_error(hass, error):
{},
)
assert result2["type"] == "form"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] == {"base": base_error}
@ -405,7 +417,7 @@ async def test_zeroconf_already_configured(hass):
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "abort"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
# Test config entry got updated with latest IP
@ -421,7 +433,7 @@ async def test_zeroconf_firmware_unsupported(hass):
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "abort"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "unsupported_firmware"
@ -433,7 +445,7 @@ async def test_zeroconf_cannot_connect(hass):
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "abort"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "cannot_connect"
@ -450,14 +462,14 @@ async def test_zeroconf_require_auth(hass):
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "form"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {}
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{},
)
assert result2["type"] == "form"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] == {}
with patch(
@ -479,7 +491,7 @@ async def test_zeroconf_require_auth(hass):
)
await hass.async_block_till_done()
assert result3["type"] == "create_entry"
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "Test name"
assert result3["data"] == {
"host": "1.1.1.1",