2020-08-24 10:43:31 +00:00
|
|
|
"""Test the Shelly config flow."""
|
|
|
|
import asyncio
|
2021-10-23 18:49:04 +00:00
|
|
|
from http import HTTPStatus
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2020-09-01 12:08:37 +00:00
|
|
|
import aiohttp
|
2020-09-26 20:39:02 +00:00
|
|
|
import aioshelly
|
2020-08-24 11:39:23 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2021-11-21 22:33:20 +00:00
|
|
|
from homeassistant.components import zeroconf
|
2020-08-24 10:43:31 +00:00
|
|
|
from homeassistant.components.shelly.const import DOMAIN
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2020-10-21 13:05:06 +00:00
|
|
|
MOCK_SETTINGS = {
|
|
|
|
"name": "Test name",
|
2021-02-03 16:03:22 +00:00
|
|
|
"device": {"mac": "test-mac", "hostname": "test-host", "type": "SHSW-1"},
|
2020-10-21 13:05:06 +00:00
|
|
|
}
|
2021-11-21 22:33:20 +00:00
|
|
|
DISCOVERY_INFO = zeroconf.ZeroconfServiceInfo(
|
|
|
|
host="1.1.1.1",
|
2022-02-11 22:46:17 +00:00
|
|
|
addresses=["1.1.1.1"],
|
2021-11-23 16:35:44 +00:00
|
|
|
hostname="mock_hostname",
|
2021-11-21 22:33:20 +00:00
|
|
|
name="shelly1pm-12345",
|
2021-11-23 16:35:44 +00:00
|
|
|
port=None,
|
|
|
|
properties={zeroconf.ATTR_PROPERTIES_ID: "shelly1pm-12345"},
|
|
|
|
type="mock_type",
|
2021-11-21 22:33:20 +00:00
|
|
|
)
|
2021-09-11 20:28:33 +00:00
|
|
|
MOCK_CONFIG = {
|
2021-11-29 15:16:49 +00:00
|
|
|
"sys": {
|
|
|
|
"device": {"name": "Test name"},
|
|
|
|
},
|
2021-09-11 20:28:33 +00:00
|
|
|
}
|
2020-10-21 13:05:06 +00:00
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2021-09-11 20:28:33 +00:00
|
|
|
@pytest.mark.parametrize("gen", [1, 2])
|
|
|
|
async def test_form(hass, gen):
|
2020-08-24 10:43:31 +00:00
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2020-12-01 18:26:43 +00:00
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2021-09-11 20:28:33 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False, "gen": gen},
|
2020-08-24 10:43:31 +00:00
|
|
|
), patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2020-09-17 05:20:00 +00:00
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
2021-09-11 20:28:33 +00:00
|
|
|
model="SHSW-1",
|
2020-10-21 13:05:06 +00:00
|
|
|
settings=MOCK_SETTINGS,
|
2020-09-17 05:20:00 +00:00
|
|
|
)
|
2020-08-24 10:43:31 +00:00
|
|
|
),
|
2021-09-11 20:28:33 +00:00
|
|
|
), patch(
|
|
|
|
"aioshelly.rpc_device.RpcDevice.create",
|
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
|
|
|
model="SHSW-1",
|
|
|
|
config=MOCK_CONFIG,
|
|
|
|
shutdown=AsyncMock(),
|
|
|
|
)
|
|
|
|
),
|
2020-08-24 10:43:31 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.shelly.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.shelly.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-08-24 10:43:31 +00:00
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
2020-08-24 10:43:31 +00:00
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-08-24 10:43:31 +00:00
|
|
|
assert result2["title"] == "Test name"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
2021-02-03 16:03:22 +00:00
|
|
|
"model": "SHSW-1",
|
|
|
|
"sleep_period": 0,
|
2021-09-11 20:28:33 +00:00
|
|
|
"gen": gen,
|
2020-08-24 10:43:31 +00:00
|
|
|
}
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2020-12-16 20:28:59 +00:00
|
|
|
async def test_title_without_name(hass):
|
2020-10-21 13:05:06 +00:00
|
|
|
"""Test we set the title to the hostname when the device doesn't have a name."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-10-21 13:05:06 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-10-21 13:05:06 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
settings = MOCK_SETTINGS.copy()
|
|
|
|
settings["name"] = None
|
|
|
|
settings["device"] = settings["device"].copy()
|
|
|
|
settings["device"]["hostname"] = "shelly1pm-12345"
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2020-10-21 13:05:06 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
|
|
|
|
), patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2020-10-21 13:05:06 +00:00
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
2021-09-11 20:28:33 +00:00
|
|
|
model="SHSW-1",
|
2020-10-21 13:05:06 +00:00
|
|
|
settings=settings,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.shelly.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
|
|
|
"homeassistant.components.shelly.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-10-21 13:05:06 +00:00
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-10-21 13:05:06 +00:00
|
|
|
assert result2["title"] == "shelly1pm-12345"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
2021-02-03 16:03:22 +00:00
|
|
|
"model": "SHSW-1",
|
|
|
|
"sleep_period": 0,
|
2021-09-11 20:28:33 +00:00
|
|
|
"gen": 1,
|
2020-10-21 13:05:06 +00:00
|
|
|
}
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
async def test_form_auth(hass):
|
2020-09-01 12:08:37 +00:00
|
|
|
"""Test manual configuration if auth is required."""
|
2020-08-24 10:43:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-08-24 10:43:31 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2020-08-24 10:43:31 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": True},
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
2020-08-24 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-09-01 12:08:37 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2020-09-17 05:20:00 +00:00
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
2021-09-11 20:28:33 +00:00
|
|
|
model="SHSW-1",
|
2020-10-21 13:05:06 +00:00
|
|
|
settings=MOCK_SETTINGS,
|
2020-09-17 05:20:00 +00:00
|
|
|
)
|
2020-09-01 12:08:37 +00:00
|
|
|
),
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.shelly.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
|
|
|
"homeassistant.components.shelly.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result2["flow_id"],
|
|
|
|
{"username": "test username", "password": "test password"},
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-09-01 12:08:37 +00:00
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-09-01 12:08:37 +00:00
|
|
|
assert result3["title"] == "Test name"
|
|
|
|
assert result3["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
2021-02-03 16:03:22 +00:00
|
|
|
"model": "SHSW-1",
|
|
|
|
"sleep_period": 0,
|
2021-09-11 20:28:33 +00:00
|
|
|
"gen": 1,
|
2020-09-01 12:08:37 +00:00
|
|
|
"username": "test username",
|
|
|
|
"password": "test password",
|
|
|
|
}
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
|
2020-08-24 11:39:23 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"error", [(asyncio.TimeoutError, "cannot_connect"), (ValueError, "unknown")]
|
|
|
|
)
|
|
|
|
async def test_form_errors_get_info(hass, error):
|
|
|
|
"""Test we handle errors."""
|
|
|
|
exc, base_error = error
|
2020-08-24 10:43:31 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2021-09-10 21:48:55 +00:00
|
|
|
with patch("aioshelly.common.get_info", side_effect=exc):
|
2020-08-24 11:39:23 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
2020-08-24 11:39:23 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-08-24 11:39:23 +00:00
|
|
|
assert result2["errors"] == {"base": base_error}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"error", [(asyncio.TimeoutError, "cannot_connect"), (ValueError, "unknown")]
|
|
|
|
)
|
|
|
|
async def test_form_errors_test_connection(hass, error):
|
|
|
|
"""Test we handle errors."""
|
|
|
|
exc, base_error = error
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2020-09-01 12:08:37 +00:00
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info", return_value={"mac": "test-mac", "auth": False}
|
|
|
|
), patch(
|
|
|
|
"aioshelly.block_device.BlockDevice.create", new=AsyncMock(side_effect=exc)
|
|
|
|
):
|
2020-08-24 10:43:31 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
2020-08-24 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-08-24 11:39:23 +00:00
|
|
|
assert result2["errors"] == {"base": base_error}
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
|
2020-09-01 12:08:37 +00:00
|
|
|
async def test_form_already_configured(hass):
|
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-09-01 12:08:37 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0"}
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2020-09-01 12:08:37 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2020-09-01 12:08:37 +00:00
|
|
|
assert result2["reason"] == "already_configured"
|
|
|
|
|
|
|
|
# Test config entry got updated with latest IP
|
|
|
|
assert entry.data["host"] == "1.1.1.1"
|
|
|
|
|
|
|
|
|
2020-12-07 08:25:04 +00:00
|
|
|
async def test_user_setup_ignored_device(hass):
|
|
|
|
"""Test user can successfully setup an ignored device."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-12-07 08:25:04 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="shelly",
|
|
|
|
unique_id="test-mac",
|
|
|
|
data={"host": "0.0.0.0"},
|
|
|
|
source=config_entries.SOURCE_IGNORE,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
settings = MOCK_SETTINGS.copy()
|
|
|
|
settings["device"]["type"] = "SHSW-1"
|
|
|
|
settings["fw"] = "20201124-092534/v1.9.0@57ac4ad8"
|
|
|
|
|
2020-12-07 08:25:04 +00:00
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2020-12-07 08:25:04 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
|
2021-01-04 13:04:40 +00:00
|
|
|
), patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2021-01-04 13:04:40 +00:00
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
2021-09-11 20:28:33 +00:00
|
|
|
model="SHSW-1",
|
2021-01-04 13:04:40 +00:00
|
|
|
settings=settings,
|
|
|
|
)
|
|
|
|
),
|
2021-01-05 17:35:54 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.shelly.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
|
|
|
"homeassistant.components.shelly.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
2021-01-04 13:04:40 +00:00
|
|
|
|
2020-12-07 08:25:04 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-12-07 08:25:04 +00:00
|
|
|
|
|
|
|
# Test config entry got updated with latest IP
|
|
|
|
assert entry.data["host"] == "1.1.1.1"
|
2021-01-05 17:35:54 +00:00
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2020-12-07 08:25:04 +00:00
|
|
|
|
|
|
|
|
2020-09-26 20:39:02 +00:00
|
|
|
async def test_form_firmware_unsupported(hass):
|
|
|
|
"""Test we abort if device firmware is unsupported."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2021-09-10 21:48:55 +00:00
|
|
|
with patch(
|
|
|
|
"aioshelly.common.get_info",
|
|
|
|
side_effect=aioshelly.exceptions.FirmwareUnsupported,
|
|
|
|
):
|
2020-09-26 20:39:02 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2020-09-26 20:39:02 +00:00
|
|
|
assert result2["reason"] == "unsupported_firmware"
|
|
|
|
|
|
|
|
|
2020-09-01 12:08:37 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"error",
|
|
|
|
[
|
2021-10-23 18:49:04 +00:00
|
|
|
(
|
|
|
|
aiohttp.ClientResponseError(Mock(), (), status=HTTPStatus.BAD_REQUEST),
|
|
|
|
"cannot_connect",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
aiohttp.ClientResponseError(Mock(), (), status=HTTPStatus.UNAUTHORIZED),
|
|
|
|
"invalid_auth",
|
|
|
|
),
|
2020-09-01 12:08:37 +00:00
|
|
|
(asyncio.TimeoutError, "cannot_connect"),
|
|
|
|
(ValueError, "unknown"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_form_auth_errors_test_connection(hass, error):
|
|
|
|
"""Test we handle errors in authenticated devices."""
|
|
|
|
exc, base_error = error
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2021-09-10 21:48:55 +00:00
|
|
|
with patch(
|
|
|
|
"aioshelly.common.get_info",
|
|
|
|
return_value={"mac": "test-mac", "auth": True},
|
|
|
|
):
|
2020-09-01 12:08:37 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{"host": "1.1.1.1"},
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2020-09-17 05:20:00 +00:00
|
|
|
new=AsyncMock(side_effect=exc),
|
2020-09-01 12:08:37 +00:00
|
|
|
):
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
|
|
|
result2["flow_id"],
|
|
|
|
{"username": "test username", "password": "test password"},
|
|
|
|
)
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result3["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-09-01 12:08:37 +00:00
|
|
|
assert result3["errors"] == {"base": base_error}
|
|
|
|
|
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
async def test_zeroconf(hass):
|
|
|
|
"""Test we get the form."""
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2020-08-24 10:43:31 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
|
2021-03-09 12:13:43 +00:00
|
|
|
), patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2021-03-09 12:13:43 +00:00
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
2021-09-11 20:28:33 +00:00
|
|
|
model="SHSW-1",
|
2021-03-09 12:13:43 +00:00
|
|
|
settings=MOCK_SETTINGS,
|
|
|
|
)
|
|
|
|
),
|
2020-08-24 10:43:31 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2020-10-21 13:05:06 +00:00
|
|
|
data=DISCOVERY_INFO,
|
2020-08-24 10:43:31 +00:00
|
|
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
|
|
|
)
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-08-24 10:43:31 +00:00
|
|
|
assert result["errors"] == {}
|
2020-10-21 13:05:06 +00:00
|
|
|
context = next(
|
|
|
|
flow["context"]
|
|
|
|
for flow in hass.config_entries.flow.async_progress()
|
|
|
|
if flow["flow_id"] == result["flow_id"]
|
|
|
|
)
|
|
|
|
assert context["title_placeholders"]["name"] == "shelly1pm-12345"
|
2021-03-09 12:13:43 +00:00
|
|
|
assert context["confirm_only"] is True
|
2020-08-24 10:43:31 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.shelly.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.shelly.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-08-24 10:43:31 +00:00
|
|
|
) as mock_setup_entry:
|
2020-08-27 11:56:20 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{},
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-08-24 10:43:31 +00:00
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-08-24 10:43:31 +00:00
|
|
|
assert result2["title"] == "Test name"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
2021-02-03 16:03:22 +00:00
|
|
|
"model": "SHSW-1",
|
|
|
|
"sleep_period": 0,
|
2021-09-11 20:28:33 +00:00
|
|
|
"gen": 1,
|
2020-08-24 10:43:31 +00:00
|
|
|
}
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2021-02-09 08:28:40 +00:00
|
|
|
async def test_zeroconf_sleeping_device(hass):
|
|
|
|
"""Test sleeping device configuration via zeroconf."""
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2021-02-09 08:28:40 +00:00
|
|
|
return_value={
|
|
|
|
"mac": "test-mac",
|
|
|
|
"type": "SHSW-1",
|
|
|
|
"auth": False,
|
|
|
|
"sleep_mode": True,
|
|
|
|
},
|
|
|
|
), patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2021-02-09 08:28:40 +00:00
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
2021-09-11 20:28:33 +00:00
|
|
|
model="SHSW-1",
|
2021-02-09 08:28:40 +00:00
|
|
|
settings={
|
|
|
|
"name": "Test name",
|
|
|
|
"device": {
|
|
|
|
"mac": "test-mac",
|
|
|
|
"hostname": "test-host",
|
|
|
|
"type": "SHSW-1",
|
|
|
|
},
|
|
|
|
"sleep_mode": {"period": 10, "unit": "m"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
),
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
data=DISCOVERY_INFO,
|
|
|
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["errors"] == {}
|
|
|
|
context = next(
|
|
|
|
flow["context"]
|
|
|
|
for flow in hass.config_entries.flow.async_progress()
|
|
|
|
if flow["flow_id"] == result["flow_id"]
|
|
|
|
)
|
|
|
|
assert context["title_placeholders"]["name"] == "shelly1pm-12345"
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.shelly.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
|
|
|
"homeassistant.components.shelly.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result2["title"] == "Test name"
|
|
|
|
assert result2["data"] == {
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"model": "SHSW-1",
|
|
|
|
"sleep_period": 600,
|
2021-09-11 20:28:33 +00:00
|
|
|
"gen": 1,
|
2021-02-09 08:28:40 +00:00
|
|
|
}
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"error",
|
|
|
|
[
|
2021-10-23 18:49:04 +00:00
|
|
|
(
|
|
|
|
aiohttp.ClientResponseError(Mock(), (), status=HTTPStatus.BAD_REQUEST),
|
|
|
|
"cannot_connect",
|
|
|
|
),
|
2021-02-09 08:28:40 +00:00
|
|
|
(asyncio.TimeoutError, "cannot_connect"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_zeroconf_sleeping_device_error(hass, error):
|
|
|
|
"""Test sleeping device configuration via zeroconf with error."""
|
|
|
|
exc = error
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2021-02-09 08:28:40 +00:00
|
|
|
return_value={
|
|
|
|
"mac": "test-mac",
|
|
|
|
"type": "SHSW-1",
|
|
|
|
"auth": False,
|
|
|
|
"sleep_mode": True,
|
|
|
|
},
|
|
|
|
), patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2021-02-09 08:28:40 +00:00
|
|
|
new=AsyncMock(side_effect=exc),
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
data=DISCOVERY_INFO,
|
|
|
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "cannot_connect"
|
|
|
|
|
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
async def test_zeroconf_already_configured(hass):
|
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0"}
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2020-08-24 10:43:31 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2020-10-21 13:05:06 +00:00
|
|
|
data=DISCOVERY_INFO,
|
2020-08-24 10:43:31 +00:00
|
|
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
|
|
|
)
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2020-08-24 10:43:31 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
# Test config entry got updated with latest IP
|
|
|
|
assert entry.data["host"] == "1.1.1.1"
|
|
|
|
|
|
|
|
|
2020-09-26 20:39:02 +00:00
|
|
|
async def test_zeroconf_firmware_unsupported(hass):
|
|
|
|
"""Test we abort if device firmware is unsupported."""
|
2021-09-10 21:48:55 +00:00
|
|
|
with patch(
|
|
|
|
"aioshelly.common.get_info",
|
|
|
|
side_effect=aioshelly.exceptions.FirmwareUnsupported,
|
|
|
|
):
|
2020-09-26 20:39:02 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2020-10-21 13:05:06 +00:00
|
|
|
data=DISCOVERY_INFO,
|
2020-09-26 20:39:02 +00:00
|
|
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2020-09-26 20:39:02 +00:00
|
|
|
assert result["reason"] == "unsupported_firmware"
|
|
|
|
|
|
|
|
|
2020-08-24 11:39:23 +00:00
|
|
|
async def test_zeroconf_cannot_connect(hass):
|
|
|
|
"""Test we get the form."""
|
2021-09-10 21:48:55 +00:00
|
|
|
with patch("aioshelly.common.get_info", side_effect=asyncio.TimeoutError):
|
2020-08-24 11:39:23 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2020-10-21 13:05:06 +00:00
|
|
|
data=DISCOVERY_INFO,
|
2020-08-24 11:39:23 +00:00
|
|
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
|
|
|
)
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2020-08-24 11:39:23 +00:00
|
|
|
assert result["reason"] == "cannot_connect"
|
|
|
|
|
|
|
|
|
2020-08-24 10:43:31 +00:00
|
|
|
async def test_zeroconf_require_auth(hass):
|
2020-09-01 12:08:37 +00:00
|
|
|
"""Test zeroconf if auth is required."""
|
2020-08-24 10:43:31 +00:00
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.common.get_info",
|
2020-08-24 10:43:31 +00:00
|
|
|
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": True},
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2020-10-21 13:05:06 +00:00
|
|
|
data=DISCOVERY_INFO,
|
2020-08-24 10:43:31 +00:00
|
|
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
|
|
|
)
|
2021-01-04 13:04:40 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2020-09-01 12:08:37 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
2021-09-10 21:48:55 +00:00
|
|
|
"aioshelly.block_device.BlockDevice.create",
|
2020-09-17 05:20:00 +00:00
|
|
|
new=AsyncMock(
|
|
|
|
return_value=Mock(
|
2021-09-11 20:28:33 +00:00
|
|
|
model="SHSW-1",
|
2020-10-21 13:05:06 +00:00
|
|
|
settings=MOCK_SETTINGS,
|
2020-09-17 05:20:00 +00:00
|
|
|
)
|
2020-09-01 12:08:37 +00:00
|
|
|
),
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.shelly.async_setup", return_value=True
|
|
|
|
) as mock_setup, patch(
|
|
|
|
"homeassistant.components.shelly.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
2021-03-09 12:13:43 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
2020-09-01 12:08:37 +00:00
|
|
|
{"username": "test username", "password": "test password"},
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-09-01 12:08:37 +00:00
|
|
|
|
2021-03-09 12:13:43 +00:00
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result2["title"] == "Test name"
|
|
|
|
assert result2["data"] == {
|
2020-09-01 12:08:37 +00:00
|
|
|
"host": "1.1.1.1",
|
2021-02-03 16:03:22 +00:00
|
|
|
"model": "SHSW-1",
|
|
|
|
"sleep_period": 0,
|
2021-09-11 20:28:33 +00:00
|
|
|
"gen": 1,
|
2020-09-01 12:08:37 +00:00
|
|
|
"username": "test username",
|
|
|
|
"password": "test password",
|
|
|
|
}
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|