Migrate entry unique id for Sensibo (#67119)

This commit is contained in:
G Johansson 2022-02-27 22:38:39 +01:00 committed by GitHub
parent 9c440d8aa6
commit bb4b7c96d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 212 additions and 74 deletions

View file

@ -5,7 +5,7 @@ import asyncio
from unittest.mock import patch
import aiohttp
from pysensibo import AuthenticationError, SensiboError
from pysensibo.exceptions import AuthenticationError, SensiboError
import pytest
from homeassistant import config_entries
@ -22,11 +22,6 @@ from tests.common import MockConfigEntry
DOMAIN = "sensibo"
def devices():
"""Return list of test devices."""
return (yield from [{"id": "xyzxyz"}, {"id": "abcabc"}])
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form."""
@ -38,8 +33,11 @@ async def test_form(hass: HomeAssistant) -> None:
assert result["errors"] == {}
with patch(
"homeassistant.components.sensibo.config_flow.SensiboClient.async_get_devices",
return_value=devices(),
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices",
return_value={"result": [{"id": "xyzxyz"}, {"id": "abcabc"}]},
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_me",
return_value={"result": {"username": "username"}},
), patch(
"homeassistant.components.sensibo.async_setup_entry",
return_value=True,
@ -53,6 +51,7 @@ async def test_form(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
assert result2["version"] == 2
assert result2["data"] == {
"api_key": "1234567890",
}
@ -64,8 +63,11 @@ async def test_import_flow_success(hass: HomeAssistant) -> None:
"""Test a successful import of yaml."""
with patch(
"homeassistant.components.sensibo.config_flow.SensiboClient.async_get_devices",
return_value=devices(),
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices",
return_value={"result": [{"id": "xyzxyz"}, {"id": "abcabc"}]},
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_me",
return_value={"result": {"username": "username"}},
), patch(
"homeassistant.components.sensibo.async_setup_entry",
return_value=True,
@ -95,15 +97,18 @@ async def test_import_flow_already_exist(hass: HomeAssistant) -> None:
data={
CONF_API_KEY: "1234567890",
},
unique_id="1234567890",
unique_id="username",
).add_to_hass(hass)
with patch(
"homeassistant.components.sensibo.async_setup_entry",
return_value=True,
), patch(
"homeassistant.components.sensibo.config_flow.SensiboClient.async_get_devices",
return_value=devices(),
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices",
return_value={"result": [{"id": "xyzxyz"}, {"id": "abcabc"}]},
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_me",
return_value={"result": {"username": "username"}},
):
result3 = await hass.config_entries.flow.async_init(
DOMAIN,
@ -119,33 +124,112 @@ async def test_import_flow_already_exist(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
"error_message",
"error_message, p_error",
[
(aiohttp.ClientConnectionError),
(asyncio.TimeoutError),
(AuthenticationError),
(SensiboError),
(aiohttp.ClientConnectionError, "cannot_connect"),
(asyncio.TimeoutError, "cannot_connect"),
(AuthenticationError, "invalid_auth"),
(SensiboError, "cannot_connect"),
],
)
async def test_flow_fails(hass: HomeAssistant, error_message) -> None:
async def test_flow_fails(
hass: HomeAssistant, error_message: Exception, p_error: str
) -> None:
"""Test config flow errors."""
result4 = await hass.config_entries.flow.async_init(
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result4["type"] == RESULT_TYPE_FORM
assert result4["step_id"] == config_entries.SOURCE_USER
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == config_entries.SOURCE_USER
with patch(
"homeassistant.components.sensibo.config_flow.SensiboClient.async_get_devices",
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices",
side_effect=error_message,
):
result4 = await hass.config_entries.flow.async_configure(
result4["flow_id"],
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "1234567890",
},
)
assert result4["errors"] == {"base": "cannot_connect"}
assert result2["errors"] == {"base": p_error}
with patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices",
return_value={"result": [{"id": "xyzxyz"}, {"id": "abcabc"}]},
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_me",
return_value={"result": {"username": "username"}},
), patch(
"homeassistant.components.sensibo.async_setup_entry",
return_value=True,
):
result3 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "1234567891",
},
)
assert result3["type"] == RESULT_TYPE_CREATE_ENTRY
assert result3["title"] == "Sensibo"
assert result3["data"] == {
"api_key": "1234567891",
}
async def test_flow_get_no_devices(hass: HomeAssistant) -> None:
"""Test config flow get no devices from api."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == config_entries.SOURCE_USER
with patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices",
return_value={"result": []},
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_me",
return_value={"result": {}},
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "1234567890",
},
)
assert result2["errors"] == {"base": "no_devices"}
async def test_flow_get_no_username(hass: HomeAssistant) -> None:
"""Test config flow get no username from api."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == config_entries.SOURCE_USER
with patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_devices",
return_value={"result": [{"id": "xyzxyz"}, {"id": "abcabc"}]},
), patch(
"homeassistant.components.sensibo.util.SensiboClient.async_get_me",
return_value={"result": {}},
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_API_KEY: "1234567890",
},
)
assert result2["errors"] == {"base": "no_username"}