Cleanup Discovergy a bit (#104552)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
ad17acc6ca
commit
b314df272f
7 changed files with 45 additions and 66 deletions
|
@ -2,7 +2,6 @@
|
|||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from pydiscovergy import Discovergy
|
||||
from pydiscovergy.models import Reading
|
||||
import pytest
|
||||
|
||||
|
@ -27,14 +26,16 @@ def _meter_last_reading(meter_id: str) -> Reading:
|
|||
@pytest.fixture(name="discovergy")
|
||||
def mock_discovergy() -> Generator[AsyncMock, None, None]:
|
||||
"""Mock the pydiscovergy client."""
|
||||
mock = AsyncMock(spec=Discovergy)
|
||||
mock.meters.return_value = GET_METERS
|
||||
mock.meter_last_reading.side_effect = _meter_last_reading
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.discovergy.pydiscovergy.Discovergy",
|
||||
return_value=mock,
|
||||
"homeassistant.components.discovergy.Discovergy",
|
||||
autospec=True,
|
||||
) as mock_discovergy, patch(
|
||||
"homeassistant.components.discovergy.config_flow.Discovergy",
|
||||
new=mock_discovergy,
|
||||
):
|
||||
mock = mock_discovergy.return_value
|
||||
mock.meters.return_value = GET_METERS
|
||||
mock.meter_last_reading.side_effect = _meter_last_reading
|
||||
yield mock
|
||||
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
|||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.discovergy.const import GET_METERS
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant, discovergy: AsyncMock) -> None:
|
||||
|
@ -25,10 +24,7 @@ async def test_form(hass: HomeAssistant, discovergy: AsyncMock) -> None:
|
|||
with patch(
|
||||
"homeassistant.components.discovergy.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry, patch(
|
||||
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy",
|
||||
return_value=discovergy,
|
||||
):
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
|
@ -65,10 +61,7 @@ async def test_reauth(
|
|||
with patch(
|
||||
"homeassistant.components.discovergy.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry, patch(
|
||||
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy",
|
||||
return_value=discovergy,
|
||||
):
|
||||
) as mock_setup_entry:
|
||||
configure_result = await hass.config_entries.flow.async_configure(
|
||||
init_result["flow_id"],
|
||||
{
|
||||
|
@ -92,38 +85,34 @@ async def test_reauth(
|
|||
(Exception, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) -> None:
|
||||
async def test_form_fail(
|
||||
hass: HomeAssistant, discovergy: AsyncMock, error: Exception, message: str
|
||||
) -> None:
|
||||
"""Test to handle exceptions."""
|
||||
discovergy.meters.side_effect = error
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={
|
||||
CONF_EMAIL: "test@example.com",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy.meters",
|
||||
side_effect=error,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={
|
||||
CONF_EMAIL: "test@example.com",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": message}
|
||||
|
||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": message}
|
||||
# reset and test for success
|
||||
discovergy.meters.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_EMAIL: "test@example.com",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy.meters",
|
||||
return_value=GET_METERS,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_EMAIL: "test@example.com",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "test@example.com"
|
||||
assert "errors" not in result
|
||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "test@example.com"
|
||||
assert "errors" not in result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue