Fallback to mac in Shelly config flow title (#40783)

This commit is contained in:
On Freund 2020-10-21 16:05:06 +03:00 committed by GitHub
parent c1ebad8c97
commit a07c0d1605
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 18 deletions

View file

@ -11,6 +11,21 @@ from homeassistant.components.shelly.const import DOMAIN
from tests.async_mock import AsyncMock, Mock, patch
from tests.common import MockConfigEntry
MOCK_SETTINGS = {
"name": "Test name",
"device": {"mac": "test-mac", "hostname": "test-host"},
}
DISCOVERY_INFO = {
"host": "1.1.1.1",
"name": "shelly1pm-12345",
"properties": {"id": "shelly1pm-12345"},
}
SWITCH25_DISCOVERY_INFO = {
"host": "1.1.1.1",
"name": "shellyswitch25-12345",
"properties": {"id": "shellyswitch25-12345"},
}
async def test_form(hass):
"""Test we get the form."""
@ -29,7 +44,7 @@ async def test_form(hass):
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
settings=MOCK_SETTINGS,
)
),
), patch(
@ -53,6 +68,51 @@ async def test_form(hass):
assert len(mock_setup_entry.mock_calls) == 1
async def test_title_without_name_and_prefix(hass):
"""Test we set the title to the hostname when the device doesn't have a name."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
settings = MOCK_SETTINGS.copy()
settings["name"] = None
settings["device"] = settings["device"].copy()
settings["device"]["hostname"] = "shelly1pm-12345"
with patch(
"aioshelly.get_info",
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
), patch(
"aioshelly.Device.create",
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
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"},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "shelly1pm-12345"
assert result2["data"] == {
"host": "1.1.1.1",
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_auth(hass):
"""Test manual configuration if auth is required."""
result = await hass.config_entries.flow.async_init(
@ -78,7 +138,7 @@ async def test_form_auth(hass):
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
settings=MOCK_SETTINGS,
)
),
), patch(
@ -234,18 +294,23 @@ async def test_zeroconf(hass):
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={"host": "1.1.1.1", "name": "shelly1pm-12345"},
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert 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(
"aioshelly.Device.create",
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
settings=MOCK_SETTINGS,
)
),
), patch(
@ -269,6 +334,29 @@ async def test_zeroconf(hass):
assert len(mock_setup_entry.mock_calls) == 1
async def test_zeroconf_with_switch_prefix(hass):
"""Test we get remove shelly from the prefix."""
await setup.async_setup_component(hass, "persistent_notification", {})
with patch(
"aioshelly.get_info",
return_value={"mac": "test-mac", "type": "SHSW-1", "auth": False},
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=SWITCH25_DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert 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"] == "switch25-12345"
@pytest.mark.parametrize(
"error", [(asyncio.TimeoutError, "cannot_connect"), (ValueError, "unknown")]
)
@ -283,7 +371,7 @@ async def test_zeroconf_confirm_error(hass, error):
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={"host": "1.1.1.1", "name": "shelly1pm-12345"},
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "form"
@ -316,7 +404,7 @@ async def test_zeroconf_already_configured(hass):
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={"host": "1.1.1.1", "name": "shelly1pm-12345"},
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "abort"
@ -331,7 +419,7 @@ async def test_zeroconf_firmware_unsupported(hass):
with patch("aioshelly.get_info", side_effect=aioshelly.FirmwareUnsupported):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={"host": "1.1.1.1", "name": "shelly1pm-12345"},
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
@ -344,7 +432,7 @@ async def test_zeroconf_cannot_connect(hass):
with patch("aioshelly.get_info", side_effect=asyncio.TimeoutError):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={"host": "1.1.1.1", "name": "shelly1pm-12345"},
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "abort"
@ -361,7 +449,7 @@ async def test_zeroconf_require_auth(hass):
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={"host": "1.1.1.1", "name": "shelly1pm-12345"},
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] == "form"
@ -379,7 +467,7 @@ async def test_zeroconf_require_auth(hass):
new=AsyncMock(
return_value=Mock(
shutdown=AsyncMock(),
settings={"name": "Test name", "device": {"mac": "test-mac"}},
settings=MOCK_SETTINGS,
)
),
), patch(