SMA code quality improvement and bugfix (#49346)
* Minor code quality improvements Thanks to @MartinHjelmare * Convert legacy dict config to list * Improved test * Typo * Test improvements * Create fixtures in conftest.py
This commit is contained in:
parent
a278ebd37b
commit
12853438c5
5 changed files with 142 additions and 100 deletions
|
@ -11,20 +11,18 @@ from homeassistant.data_entry_flow import (
|
|||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import (
|
||||
MOCK_DEVICE,
|
||||
MOCK_IMPORT,
|
||||
MOCK_LEGACY_ENTRY,
|
||||
MOCK_IMPORT_DICT,
|
||||
MOCK_SETUP_DATA,
|
||||
MOCK_USER_INPUT,
|
||||
_patch_async_setup_entry,
|
||||
_patch_validate_input,
|
||||
)
|
||||
|
||||
|
||||
async def test_form(hass, aioclient_mock):
|
||||
async def test_form(hass):
|
||||
"""Test we get the form."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
@ -49,30 +47,34 @@ async def test_form(hass, aioclient_mock):
|
|||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass, aioclient_mock):
|
||||
async def test_form_cannot_connect(hass):
|
||||
"""Test we handle cannot connect error."""
|
||||
aioclient_mock.get("https://1.1.1.1/data/l10n/en-US.json", exc=aiohttp.ClientError)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_INPUT,
|
||||
)
|
||||
with patch(
|
||||
"pysma.SMA.new_session", side_effect=aiohttp.ClientError
|
||||
), _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass, aioclient_mock):
|
||||
async def test_form_invalid_auth(hass):
|
||||
"""Test we handle invalid auth error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch("pysma.SMA.new_session", return_value=False):
|
||||
with patch(
|
||||
"pysma.SMA.new_session", return_value=False
|
||||
), _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_INPUT,
|
||||
|
@ -80,9 +82,10 @@ async def test_form_invalid_auth(hass, aioclient_mock):
|
|||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_form_cannot_retrieve_device_info(hass, aioclient_mock):
|
||||
async def test_form_cannot_retrieve_device_info(hass):
|
||||
"""Test we handle cannot retrieve device info error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
|
@ -90,7 +93,7 @@ async def test_form_cannot_retrieve_device_info(hass, aioclient_mock):
|
|||
|
||||
with patch("pysma.SMA.new_session", return_value=True), patch(
|
||||
"pysma.SMA.read", return_value=False
|
||||
):
|
||||
), _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_INPUT,
|
||||
|
@ -98,6 +101,7 @@ async def test_form_cannot_retrieve_device_info(hass, aioclient_mock):
|
|||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "cannot_retrieve_device_info"}
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_form_unexpected_exception(hass):
|
||||
|
@ -106,7 +110,9 @@ async def test_form_unexpected_exception(hass):
|
|||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with _patch_validate_input(side_effect=Exception):
|
||||
with patch(
|
||||
"pysma.SMA.new_session", side_effect=Exception
|
||||
), _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_INPUT,
|
||||
|
@ -114,28 +120,23 @@ async def test_form_unexpected_exception(hass):
|
|||
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_form_already_configured(hass):
|
||||
async def test_form_already_configured(hass, mock_config_entry):
|
||||
"""Test starting a flow by user when already configured."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with _patch_validate_input():
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["result"].unique_id == MOCK_DEVICE["serial"]
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with _patch_validate_input():
|
||||
with patch("pysma.SMA.new_session", return_value=True), patch(
|
||||
"pysma.SMA.device_info", return_value=MOCK_DEVICE
|
||||
), patch(
|
||||
"pysma.SMA.close_session", return_value=True
|
||||
), _patch_async_setup_entry() as mock_setup_entry:
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
MOCK_USER_INPUT,
|
||||
|
@ -143,16 +144,18 @@ async def test_form_already_configured(hass):
|
|||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_import(hass):
|
||||
"""Test we can import."""
|
||||
entity_registry = er.async_get(hass)
|
||||
entity_registry._register_entry(MOCK_LEGACY_ENTRY)
|
||||
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
with _patch_validate_input():
|
||||
with patch("pysma.SMA.new_session", return_value=True), patch(
|
||||
"pysma.SMA.device_info", return_value=MOCK_DEVICE
|
||||
), patch(
|
||||
"pysma.SMA.close_session", return_value=True
|
||||
), _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
|
@ -162,9 +165,25 @@ async def test_import(hass):
|
|||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == MOCK_USER_INPUT["host"]
|
||||
assert result["data"] == MOCK_IMPORT
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
assert MOCK_LEGACY_ENTRY.original_name not in result["data"]["sensors"]
|
||||
assert "pv_power_a" in result["data"]["sensors"]
|
||||
|
||||
entity = entity_registry.async_get(MOCK_LEGACY_ENTRY.entity_id)
|
||||
assert entity.unique_id == f"{MOCK_DEVICE['serial']}-6380_40251E00_0"
|
||||
async def test_import_sensor_dict(hass):
|
||||
"""Test we can import."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
with patch("pysma.SMA.new_session", return_value=True), patch(
|
||||
"pysma.SMA.device_info", return_value=MOCK_DEVICE
|
||||
), patch(
|
||||
"pysma.SMA.close_session", return_value=True
|
||||
), _patch_async_setup_entry() as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=MOCK_IMPORT_DICT,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == MOCK_USER_INPUT["host"]
|
||||
assert result["data"] == MOCK_IMPORT_DICT
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue