mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Cleanup timeouts values for Shelly (#44790)
* Updated timeouts * Small cleanup * Fix + small cleanup of test code
This commit is contained in:
parent
f771d8ff14
commit
f07bf6a88e
@ -24,6 +24,7 @@ from homeassistant.helpers import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
AIOSHELLY_DEVICE_TIMEOUT_SEC,
|
||||||
BATTERY_DEVICES_WITH_PERMANENT_CONNECTION,
|
BATTERY_DEVICES_WITH_PERMANENT_CONNECTION,
|
||||||
COAP,
|
COAP,
|
||||||
DATA_CONFIG_ENTRY,
|
DATA_CONFIG_ENTRY,
|
||||||
@ -32,7 +33,6 @@ from .const import (
|
|||||||
POLLING_TIMEOUT_MULTIPLIER,
|
POLLING_TIMEOUT_MULTIPLIER,
|
||||||
REST,
|
REST,
|
||||||
REST_SENSORS_UPDATE_INTERVAL,
|
REST_SENSORS_UPDATE_INTERVAL,
|
||||||
SETUP_ENTRY_TIMEOUT_SEC,
|
|
||||||
SLEEP_PERIOD_MULTIPLIER,
|
SLEEP_PERIOD_MULTIPLIER,
|
||||||
UPDATE_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)
|
coap_context = await get_coap_context(hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(SETUP_ENTRY_TIMEOUT_SEC):
|
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
|
||||||
device = await aioshelly.Device.create(
|
device = await aioshelly.Device.create(
|
||||||
aiohttp_client.async_get_clientsession(hass),
|
aiohttp_client.async_get_clientsession(hass),
|
||||||
coap_context,
|
coap_context,
|
||||||
@ -263,7 +263,7 @@ class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator):
|
|||||||
async def _async_update_data(self):
|
async def _async_update_data(self):
|
||||||
"""Fetch data."""
|
"""Fetch data."""
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(5):
|
async with async_timeout.timeout(AIOSHELLY_DEVICE_TIMEOUT_SEC):
|
||||||
_LOGGER.debug("REST update for %s", self.name)
|
_LOGGER.debug("REST update for %s", self.name)
|
||||||
return await self.device.update_status()
|
return await self.device.update_status()
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
|
@ -18,6 +18,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.helpers import aiohttp_client
|
from homeassistant.helpers import aiohttp_client
|
||||||
|
|
||||||
from . import get_coap_context
|
from . import get_coap_context
|
||||||
|
from .const import AIOSHELLY_DEVICE_TIMEOUT_SEC
|
||||||
from .const import DOMAIN # pylint:disable=unused-import
|
from .const import DOMAIN # pylint:disable=unused-import
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -39,7 +40,7 @@ async def validate_input(hass: core.HomeAssistant, host, data):
|
|||||||
)
|
)
|
||||||
coap_context = await get_coap_context(hass)
|
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(
|
device = await aioshelly.Device.create(
|
||||||
aiohttp_client.async_get_clientsession(hass),
|
aiohttp_client.async_get_clientsession(hass),
|
||||||
coap_context,
|
coap_context,
|
||||||
@ -187,7 +188,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
async def _async_get_info(self, host):
|
async def _async_get_info(self, host):
|
||||||
"""Get info from shelly device."""
|
"""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(
|
return await aioshelly.get_info(
|
||||||
aiohttp_client.async_get_clientsession(self.hass),
|
aiohttp_client.async_get_clientsession(self.hass),
|
||||||
host,
|
host,
|
||||||
|
@ -11,8 +11,8 @@ POLLING_TIMEOUT_MULTIPLIER = 1.2
|
|||||||
# Refresh interval for REST sensors
|
# Refresh interval for REST sensors
|
||||||
REST_SENSORS_UPDATE_INTERVAL = 60
|
REST_SENSORS_UPDATE_INTERVAL = 60
|
||||||
|
|
||||||
# Timeout used for initial entry setup in "async_setup_entry".
|
# Timeout used for aioshelly calls
|
||||||
SETUP_ENTRY_TIMEOUT_SEC = 10
|
AIOSHELLY_DEVICE_TIMEOUT_SEC = 10
|
||||||
|
|
||||||
# Multiplier used to calculate the "update_interval" for sleeping devices.
|
# Multiplier used to calculate the "update_interval" for sleeping devices.
|
||||||
SLEEP_PERIOD_MULTIPLIER = 1.2
|
SLEEP_PERIOD_MULTIPLIER = 1.2
|
||||||
|
@ -53,7 +53,7 @@ async def test_form(hass):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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["title"] == "Test name"
|
||||||
assert result2["data"] == {
|
assert result2["data"] == {
|
||||||
"host": "1.1.1.1",
|
"host": "1.1.1.1",
|
||||||
@ -68,7 +68,7 @@ async def test_title_without_name(hass):
|
|||||||
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}
|
||||||
)
|
)
|
||||||
assert result["type"] == "form"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
settings = MOCK_SETTINGS.copy()
|
settings = MOCK_SETTINGS.copy()
|
||||||
@ -97,7 +97,7 @@ async def test_title_without_name(hass):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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["title"] == "shelly1pm-12345"
|
||||||
assert result2["data"] == {
|
assert result2["data"] == {
|
||||||
"host": "1.1.1.1",
|
"host": "1.1.1.1",
|
||||||
@ -111,7 +111,7 @@ async def test_form_auth(hass):
|
|||||||
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}
|
||||||
)
|
)
|
||||||
assert result["type"] == "form"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
@ -123,7 +123,7 @@ async def test_form_auth(hass):
|
|||||||
{"host": "1.1.1.1"},
|
{"host": "1.1.1.1"},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == "form"
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
@ -145,7 +145,7 @@ async def test_form_auth(hass):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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["title"] == "Test name"
|
||||||
assert result3["data"] == {
|
assert result3["data"] == {
|
||||||
"host": "1.1.1.1",
|
"host": "1.1.1.1",
|
||||||
@ -172,7 +172,7 @@ async def test_form_errors_get_info(hass, error):
|
|||||||
{"host": "1.1.1.1"},
|
{"host": "1.1.1.1"},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == "form"
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result2["errors"] == {"base": base_error}
|
assert result2["errors"] == {"base": base_error}
|
||||||
|
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ async def test_form_errors_test_connection(hass, error):
|
|||||||
{"host": "1.1.1.1"},
|
{"host": "1.1.1.1"},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == "form"
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result2["errors"] == {"base": base_error}
|
assert result2["errors"] == {"base": base_error}
|
||||||
|
|
||||||
|
|
||||||
@ -219,7 +219,7 @@ async def test_form_already_configured(hass):
|
|||||||
{"host": "1.1.1.1"},
|
{"host": "1.1.1.1"},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == "abort"
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result2["reason"] == "already_configured"
|
assert result2["reason"] == "already_configured"
|
||||||
|
|
||||||
# Test config entry got updated with latest IP
|
# 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}
|
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(
|
with patch(
|
||||||
"aioshelly.get_info",
|
"aioshelly.get_info",
|
||||||
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
|
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(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{"host": "1.1.1.1"},
|
{"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
|
# Test config entry got updated with latest IP
|
||||||
assert entry.data["host"] == "1.1.1.1"
|
assert entry.data["host"] == "1.1.1.1"
|
||||||
@ -268,7 +280,7 @@ async def test_form_firmware_unsupported(hass):
|
|||||||
{"host": "1.1.1.1"},
|
{"host": "1.1.1.1"},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == "abort"
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result2["reason"] == "unsupported_firmware"
|
assert result2["reason"] == "unsupported_firmware"
|
||||||
|
|
||||||
|
|
||||||
@ -302,7 +314,7 @@ async def test_form_auth_errors_test_connection(hass, error):
|
|||||||
result2["flow_id"],
|
result2["flow_id"],
|
||||||
{"username": "test username", "password": "test password"},
|
{"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}
|
assert result3["errors"] == {"base": base_error}
|
||||||
|
|
||||||
|
|
||||||
@ -319,7 +331,7 @@ async def test_zeroconf(hass):
|
|||||||
data=DISCOVERY_INFO,
|
data=DISCOVERY_INFO,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
)
|
)
|
||||||
assert result["type"] == "form"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
context = next(
|
context = next(
|
||||||
flow["context"]
|
flow["context"]
|
||||||
@ -346,7 +358,7 @@ async def test_zeroconf(hass):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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["title"] == "Test name"
|
||||||
assert result2["data"] == {
|
assert result2["data"] == {
|
||||||
"host": "1.1.1.1",
|
"host": "1.1.1.1",
|
||||||
@ -372,7 +384,7 @@ async def test_zeroconf_confirm_error(hass, error):
|
|||||||
data=DISCOVERY_INFO,
|
data=DISCOVERY_INFO,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
)
|
)
|
||||||
assert result["type"] == "form"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
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}
|
assert result2["errors"] == {"base": base_error}
|
||||||
|
|
||||||
|
|
||||||
@ -405,7 +417,7 @@ async def test_zeroconf_already_configured(hass):
|
|||||||
data=DISCOVERY_INFO,
|
data=DISCOVERY_INFO,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
)
|
)
|
||||||
assert result["type"] == "abort"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
# Test config entry got updated with latest IP
|
# 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},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == "abort"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result["reason"] == "unsupported_firmware"
|
assert result["reason"] == "unsupported_firmware"
|
||||||
|
|
||||||
|
|
||||||
@ -433,7 +445,7 @@ async def test_zeroconf_cannot_connect(hass):
|
|||||||
data=DISCOVERY_INFO,
|
data=DISCOVERY_INFO,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
)
|
)
|
||||||
assert result["type"] == "abort"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||||
assert result["reason"] == "cannot_connect"
|
assert result["reason"] == "cannot_connect"
|
||||||
|
|
||||||
|
|
||||||
@ -450,14 +462,14 @@ async def test_zeroconf_require_auth(hass):
|
|||||||
data=DISCOVERY_INFO,
|
data=DISCOVERY_INFO,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
)
|
)
|
||||||
assert result["type"] == "form"
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{},
|
{},
|
||||||
)
|
)
|
||||||
assert result2["type"] == "form"
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
assert result2["errors"] == {}
|
assert result2["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
@ -479,7 +491,7 @@ async def test_zeroconf_require_auth(hass):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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["title"] == "Test name"
|
||||||
assert result3["data"] == {
|
assert result3["data"] == {
|
||||||
"host": "1.1.1.1",
|
"host": "1.1.1.1",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user