Fix tests for smarla config_flow

This commit is contained in:
Robin Lintermann 2025-05-06 09:34:53 +00:00
parent 73d37972df
commit 2bdcdaf446

View File

@ -1,6 +1,5 @@
"""Test config flow for Swing2Sleep Smarla integration."""
import json
from unittest.mock import AsyncMock, patch
from homeassistant import config_entries
@ -13,11 +12,7 @@ from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
MOCK_ACCESS_TOKEN = "eyJyZWZyZXNoVG9rZW4iOiJ0ZXN0IiwiYXBwSWRlbnRpZmllciI6IkhBLXRlc3QiLCJzZXJpYWxOdW1iZXIiOiJBQkNEIn0="
MOCK_ACCESS_TOKEN_JSON = {
"refreshToken": "test",
"appIdentifier": "HA-test",
"serialNumber": "ABCD",
}
MOCK_SERIAL_NUMBER = "ABCD"
async def test_show_form(hass: HomeAssistant) -> None:
@ -32,7 +27,7 @@ async def test_show_form(hass: HomeAssistant) -> None:
async def test_create_entry(hass: HomeAssistant) -> None:
"""Test creating a config entry."""
with patch.object(Connection, "get_token", new=AsyncMock(return_value=True)):
with patch.object(Connection, "refresh_token", new=AsyncMock(return_value=True)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
@ -40,13 +35,13 @@ async def test_create_entry(hass: HomeAssistant) -> None:
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == MOCK_ACCESS_TOKEN_JSON["serialNumber"]
assert result["data"] == {CONF_ACCESS_TOKEN: json.dumps(MOCK_ACCESS_TOKEN_JSON)}
assert result["title"] == MOCK_SERIAL_NUMBER
assert result["data"] == {CONF_ACCESS_TOKEN: MOCK_ACCESS_TOKEN}
async def test_invalid_auth(hass: HomeAssistant) -> None:
"""Test we show user form on invalid auth."""
with patch.object(Connection, "get_token", new=AsyncMock(return_value=None)):
with patch.object(Connection, "refresh_token", new=AsyncMock(return_value=None)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
@ -73,12 +68,12 @@ async def test_device_exists_abort(hass: HomeAssistant) -> None:
"""Test we abort config flow if Smarla device already configured."""
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=MOCK_ACCESS_TOKEN_JSON["serialNumber"],
unique_id=MOCK_SERIAL_NUMBER,
source=config_entries.SOURCE_USER,
)
config_entry.add_to_hass(hass)
with patch.object(Connection, "get_token", new=AsyncMock(return_value=True)):
with patch.object(Connection, "refresh_token", new=AsyncMock(return_value=True)):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},