Cleanup lamarzocco tests (#143176)

This commit is contained in:
Josef Zweck 2025-04-17 20:53:47 +02:00 committed by GitHub
parent 8355727eb1
commit b88bf74e13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 63 additions and 86 deletions

View File

@ -1,7 +1,7 @@
"""Lamarzocco session fixtures.""" """Lamarzocco session fixtures."""
from collections.abc import Generator from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import MagicMock, patch
from bleak.backends.device import BLEDevice from bleak.backends.device import BLEDevice
from pylamarzocco.const import ModelName from pylamarzocco.const import ModelName
@ -22,15 +22,6 @@ from . import SERIAL_DICT, USER_INPUT, async_init_integration
from tests.common import MockConfigEntry, load_json_object_fixture from tests.common import MockConfigEntry, load_json_object_fixture
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.lamarzocco.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture @pytest.fixture
def mock_config_entry( def mock_config_entry(
hass: HomeAssistant, mock_lamarzocco: MagicMock hass: HomeAssistant, mock_lamarzocco: MagicMock

View File

@ -18,7 +18,6 @@ from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_plat
async def test_binary_sensors( async def test_binary_sensors(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,

View File

@ -27,6 +27,15 @@ from . import USER_INPUT, async_init_integration, get_bluetooth_service_info
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.fixture(autouse=True)
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.lamarzocco.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry
async def __do_successful_user_step( async def __do_successful_user_step(
hass: HomeAssistant, result: ConfigFlowResult, mock_cloud_client: MagicMock hass: HomeAssistant, result: ConfigFlowResult, mock_cloud_client: MagicMock
) -> ConfigFlowResult: ) -> ConfigFlowResult:
@ -47,25 +56,24 @@ async def __do_sucessful_machine_selection_step(
) -> None: ) -> None:
"""Successfully configure the machine selection step.""" """Successfully configure the machine selection step."""
result3 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result2["flow_id"], result2["flow_id"],
{CONF_MACHINE: "GS012345"}, {CONF_MACHINE: "GS012345"},
) )
assert result3["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result3["title"] == "GS012345" assert result["title"] == "GS012345"
assert result3["data"] == { assert result["data"] == {
**USER_INPUT, **USER_INPUT,
CONF_TOKEN: None, CONF_TOKEN: None,
} }
assert result3["result"].unique_id == "GS012345" assert result["result"].unique_id == "GS012345"
async def test_form( async def test_form(
hass: HomeAssistant, hass: HomeAssistant,
mock_cloud_client: MagicMock, mock_cloud_client: MagicMock,
mock_setup_entry: Generator[AsyncMock],
) -> None: ) -> None:
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -75,13 +83,12 @@ async def test_form(
assert result["errors"] == {} assert result["errors"] == {}
assert result["step_id"] == "user" assert result["step_id"] == "user"
result2 = await __do_successful_user_step(hass, result, mock_cloud_client) result = await __do_successful_user_step(hass, result, mock_cloud_client)
await __do_sucessful_machine_selection_step(hass, result2) await __do_sucessful_machine_selection_step(hass, result)
async def test_form_abort_already_configured( async def test_form_abort_already_configured(
hass: HomeAssistant, hass: HomeAssistant,
mock_cloud_client: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test we abort if already configured.""" """Test we abort if already configured."""
@ -93,25 +100,25 @@ async def test_form_abort_already_configured(
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["errors"] == {} assert result["errors"] == {}
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result2["step_id"] == "machine_selection" assert result["step_id"] == "machine_selection"
result3 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result2["flow_id"], result["flow_id"],
{ {
CONF_MACHINE: "GS012345", CONF_MACHINE: "GS012345",
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result3["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
assert result3["reason"] == "already_configured" assert result["reason"] == "already_configured"
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -124,7 +131,6 @@ async def test_form_abort_already_configured(
async def test_form_invalid_auth( async def test_form_invalid_auth(
hass: HomeAssistant, hass: HomeAssistant,
mock_cloud_client: MagicMock, mock_cloud_client: MagicMock,
mock_setup_entry: Generator[AsyncMock],
side_effect: Exception, side_effect: Exception,
error: str, error: str,
) -> None: ) -> None:
@ -135,25 +141,24 @@ async def test_form_invalid_auth(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
) )
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
) )
assert result2["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": error} assert result["errors"] == {"base": error}
assert len(mock_cloud_client.list_things.mock_calls) == 1 assert len(mock_cloud_client.list_things.mock_calls) == 1
# test recovery from failure # test recovery from failure
mock_cloud_client.list_things.side_effect = None mock_cloud_client.list_things.side_effect = None
result2 = await __do_successful_user_step(hass, result, mock_cloud_client) result = await __do_successful_user_step(hass, result, mock_cloud_client)
await __do_sucessful_machine_selection_step(hass, result2) await __do_sucessful_machine_selection_step(hass, result)
async def test_form_no_machines( async def test_form_no_machines(
hass: HomeAssistant, hass: HomeAssistant,
mock_cloud_client: MagicMock, mock_cloud_client: MagicMock,
mock_setup_entry: Generator[AsyncMock],
) -> None: ) -> None:
"""Test we don't have any devices.""" """Test we don't have any devices."""
@ -164,20 +169,20 @@ async def test_form_no_machines(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
) )
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
) )
assert result2["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "no_machines"} assert result["errors"] == {"base": "no_machines"}
assert len(mock_cloud_client.list_things.mock_calls) == 1 assert len(mock_cloud_client.list_things.mock_calls) == 1
# test recovery from failure # test recovery from failure
mock_cloud_client.list_things.return_value = original_return mock_cloud_client.list_things.return_value = original_return
result2 = await __do_successful_user_step(hass, result, mock_cloud_client) result = await __do_successful_user_step(hass, result, mock_cloud_client)
await __do_sucessful_machine_selection_step(hass, result2) await __do_sucessful_machine_selection_step(hass, result)
async def test_reauth_flow( async def test_reauth_flow(
@ -194,14 +199,14 @@ async def test_reauth_flow(
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reauth_confirm" assert result["step_id"] == "reauth_confirm"
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{CONF_PASSWORD: "new_password"}, {CONF_PASSWORD: "new_password"},
) )
assert result2["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["reason"] == "reauth_successful" assert result["reason"] == "reauth_successful"
assert len(mock_cloud_client.list_things.mock_calls) == 1 assert len(mock_cloud_client.list_things.mock_calls) == 1
assert mock_config_entry.data[CONF_PASSWORD] == "new_password" assert mock_config_entry.data[CONF_PASSWORD] == "new_password"
@ -210,7 +215,6 @@ async def test_reconfigure_flow(
hass: HomeAssistant, hass: HomeAssistant,
mock_cloud_client: MagicMock, mock_cloud_client: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_setup_entry: Generator[AsyncMock],
) -> None: ) -> None:
"""Testing reconfgure flow.""" """Testing reconfgure flow."""
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)
@ -220,7 +224,7 @@ async def test_reconfigure_flow(
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure" assert result["step_id"] == "reconfigure"
result2 = await __do_successful_user_step(hass, result, mock_cloud_client) result = await __do_successful_user_step(hass, result, mock_cloud_client)
service_info = get_bluetooth_service_info(ModelName.GS3_MP, "GS012345") service_info = get_bluetooth_service_info(ModelName.GS3_MP, "GS012345")
with ( with (
@ -229,24 +233,24 @@ async def test_reconfigure_flow(
return_value=[service_info], return_value=[service_info],
), ),
): ):
result3 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result2["flow_id"], result["flow_id"],
{ {
CONF_MACHINE: "GS012345", CONF_MACHINE: "GS012345",
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result3["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result3["step_id"] == "bluetooth_selection" assert result["step_id"] == "bluetooth_selection"
result4 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result3["flow_id"], result["flow_id"],
{CONF_MAC: service_info.address}, {CONF_MAC: service_info.address},
) )
assert result4["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
assert result4["reason"] == "reconfigure_successful" assert result["reason"] == "reconfigure_successful"
assert mock_config_entry.title == "My LaMarzocco" assert mock_config_entry.title == "My LaMarzocco"
assert mock_config_entry.data == { assert mock_config_entry.data == {
@ -259,7 +263,6 @@ async def test_bluetooth_discovery(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock, mock_lamarzocco: MagicMock,
mock_cloud_client: MagicMock, mock_cloud_client: MagicMock,
mock_setup_entry: Generator[AsyncMock],
) -> None: ) -> None:
"""Test bluetooth discovery.""" """Test bluetooth discovery."""
service_info = get_bluetooth_service_info( service_info = get_bluetooth_service_info(
@ -274,14 +277,14 @@ async def test_bluetooth_discovery(
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
) )
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "GS012345" assert result["title"] == "GS012345"
assert result2["data"] == { assert result["data"] == {
**USER_INPUT, **USER_INPUT,
CONF_MAC: "aa:bb:cc:dd:ee:ff", CONF_MAC: "aa:bb:cc:dd:ee:ff",
CONF_TOKEN: "dummyToken", CONF_TOKEN: "dummyToken",
@ -291,8 +294,6 @@ async def test_bluetooth_discovery(
async def test_bluetooth_discovery_already_configured( async def test_bluetooth_discovery_already_configured(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock, mock_lamarzocco: MagicMock,
mock_cloud_client: MagicMock,
mock_setup_entry: Generator[AsyncMock],
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test bluetooth discovery.""" """Test bluetooth discovery."""
@ -312,7 +313,6 @@ async def test_bluetooth_discovery_errors(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock, mock_lamarzocco: MagicMock,
mock_cloud_client: MagicMock, mock_cloud_client: MagicMock,
mock_setup_entry: Generator[AsyncMock],
) -> None: ) -> None:
"""Test bluetooth discovery errors.""" """Test bluetooth discovery errors."""
service_info = get_bluetooth_service_info( service_info = get_bluetooth_service_info(
@ -330,24 +330,24 @@ async def test_bluetooth_discovery_errors(
original_return = deepcopy(mock_cloud_client.list_things.return_value) original_return = deepcopy(mock_cloud_client.list_things.return_value)
mock_cloud_client.list_things.return_value[0].serial_number = "GS98765" mock_cloud_client.list_things.return_value[0].serial_number = "GS98765"
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
) )
assert result2["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "machine_not_found"} assert result["errors"] == {"base": "machine_not_found"}
assert len(mock_cloud_client.list_things.mock_calls) == 1 assert len(mock_cloud_client.list_things.mock_calls) == 1
mock_cloud_client.list_things.return_value = original_return mock_cloud_client.list_things.return_value = original_return
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
) )
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "GS012345" assert result["title"] == "GS012345"
assert result2["data"] == { assert result["data"] == {
**USER_INPUT, **USER_INPUT,
CONF_MAC: "aa:bb:cc:dd:ee:ff", CONF_MAC: "aa:bb:cc:dd:ee:ff",
CONF_TOKEN: None, CONF_TOKEN: None,
@ -357,8 +357,6 @@ async def test_bluetooth_discovery_errors(
async def test_dhcp_discovery( async def test_dhcp_discovery(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock, mock_lamarzocco: MagicMock,
mock_cloud_client: MagicMock,
mock_setup_entry: Generator[AsyncMock],
) -> None: ) -> None:
"""Test dhcp discovery.""" """Test dhcp discovery."""
@ -375,12 +373,12 @@ async def test_dhcp_discovery(
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
result2 = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
USER_INPUT, USER_INPUT,
) )
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result2["data"] == { assert result["data"] == {
**USER_INPUT, **USER_INPUT,
CONF_ADDRESS: "aa:bb:cc:dd:ee:ff", CONF_ADDRESS: "aa:bb:cc:dd:ee:ff",
CONF_TOKEN: None, CONF_TOKEN: None,
@ -389,8 +387,6 @@ async def test_dhcp_discovery(
async def test_dhcp_discovery_abort_on_hostname_changed( async def test_dhcp_discovery_abort_on_hostname_changed(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock,
mock_cloud_client: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test dhcp discovery aborts when hostname was changed manually.""" """Test dhcp discovery aborts when hostname was changed manually."""
@ -411,7 +407,6 @@ async def test_dhcp_discovery_abort_on_hostname_changed(
async def test_dhcp_already_configured_and_update( async def test_dhcp_already_configured_and_update(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock, mock_lamarzocco: MagicMock,
mock_cloud_client: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test discovered IP address change.""" """Test discovered IP address change."""
@ -436,9 +431,7 @@ async def test_dhcp_already_configured_and_update(
async def test_options_flow( async def test_options_flow(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_setup_entry: Generator[AsyncMock],
) -> None: ) -> None:
"""Test options flow.""" """Test options flow."""
await async_init_integration(hass, mock_config_entry) await async_init_integration(hass, mock_config_entry)
@ -449,7 +442,7 @@ async def test_options_flow(
assert result["type"] is FlowResultType.FORM assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init" assert result["step_id"] == "init"
result2 = await hass.config_entries.options.async_configure( result = await hass.config_entries.options.async_configure(
result["flow_id"], result["flow_id"],
user_input={ user_input={
CONF_USE_BLUETOOTH: False, CONF_USE_BLUETOOTH: False,
@ -457,7 +450,7 @@ async def test_options_flow(
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert result2["type"] is FlowResultType.CREATE_ENTRY assert result["type"] is FlowResultType.CREATE_ENTRY
assert result2["data"] == { assert result["data"] == {
CONF_USE_BLUETOOTH: False, CONF_USE_BLUETOOTH: False,
} }

View File

@ -35,7 +35,6 @@ from tests.common import MockConfigEntry
async def test_load_unload_config_entry( async def test_load_unload_config_entry(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_lamarzocco: MagicMock,
) -> None: ) -> None:
"""Test loading and unloading the integration.""" """Test loading and unloading the integration."""
await async_init_integration(hass, mock_config_entry) await async_init_integration(hass, mock_config_entry)
@ -111,7 +110,6 @@ async def test_invalid_auth(
async def test_v1_migration_fails( async def test_v1_migration_fails(
hass: HomeAssistant, hass: HomeAssistant,
mock_cloud_client: MagicMock,
mock_lamarzocco: MagicMock, mock_lamarzocco: MagicMock,
) -> None: ) -> None:
"""Test v1 -> v2 Migration.""" """Test v1 -> v2 Migration."""
@ -131,7 +129,6 @@ async def test_v1_migration_fails(
async def test_v2_migration( async def test_v2_migration(
hass: HomeAssistant, hass: HomeAssistant,
mock_cloud_client: MagicMock,
mock_lamarzocco: MagicMock, mock_lamarzocco: MagicMock,
) -> None: ) -> None:
"""Test v2 -> v3 Migration.""" """Test v2 -> v3 Migration."""
@ -256,7 +253,6 @@ async def test_websocket_closed_on_unload(
async def test_gateway_version_issue( async def test_gateway_version_issue(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_lamarzocco: MagicMock,
mock_cloud_client: MagicMock, mock_cloud_client: MagicMock,
version: str, version: str,
issue_exists: bool, issue_exists: bool,

View File

@ -25,7 +25,6 @@ from tests.common import MockConfigEntry, snapshot_platform
async def test_switches( async def test_switches(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,

View File

@ -19,7 +19,6 @@ from tests.common import MockConfigEntry, snapshot_platform
async def test_update( async def test_update(
hass: HomeAssistant, hass: HomeAssistant,
mock_lamarzocco: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,