2023-04-20 14:02:58 +00:00
|
|
|
"""Test for Roborock init."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2023-11-21 23:21:31 +00:00
|
|
|
from roborock import RoborockException, RoborockInvalidCredentials
|
|
|
|
|
2023-04-20 14:02:58 +00:00
|
|
|
from homeassistant.components.roborock.const import DOMAIN
|
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-04-24 14:46:00 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2023-04-20 14:02:58 +00:00
|
|
|
|
2023-04-24 14:46:00 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2023-04-20 14:02:58 +00:00
|
|
|
|
|
|
|
|
2023-04-24 14:46:00 +00:00
|
|
|
async def test_unload_entry(
|
|
|
|
hass: HomeAssistant, bypass_api_fixture, setup_entry: MockConfigEntry
|
|
|
|
) -> None:
|
2023-04-20 14:02:58 +00:00
|
|
|
"""Test unloading roboorck integration."""
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
2023-04-24 14:46:00 +00:00
|
|
|
assert setup_entry.state is ConfigEntryState.LOADED
|
2023-04-20 14:02:58 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.roborock.coordinator.RoborockLocalClient.async_disconnect"
|
|
|
|
) as mock_disconnect:
|
2023-04-24 14:46:00 +00:00
|
|
|
assert await hass.config_entries.async_unload(setup_entry.entry_id)
|
2023-04-20 14:02:58 +00:00
|
|
|
await hass.async_block_till_done()
|
2023-09-20 15:17:32 +00:00
|
|
|
assert mock_disconnect.call_count == 2
|
2023-04-24 14:46:00 +00:00
|
|
|
assert setup_entry.state is ConfigEntryState.NOT_LOADED
|
2023-04-20 14:02:58 +00:00
|
|
|
assert not hass.data.get(DOMAIN)
|
|
|
|
|
|
|
|
|
2023-04-24 14:46:00 +00:00
|
|
|
async def test_config_entry_not_ready(
|
|
|
|
hass: HomeAssistant, mock_roborock_entry: MockConfigEntry
|
|
|
|
) -> None:
|
2023-04-20 14:02:58 +00:00
|
|
|
"""Test that when coordinator update fails, entry retries."""
|
|
|
|
with patch(
|
2023-04-24 14:46:00 +00:00
|
|
|
"homeassistant.components.roborock.RoborockApiClient.get_home_data",
|
|
|
|
), patch(
|
2023-11-22 16:34:20 +00:00
|
|
|
"homeassistant.components.roborock.coordinator.RoborockLocalClient.get_prop",
|
|
|
|
side_effect=RoborockException(),
|
2023-04-20 14:02:58 +00:00
|
|
|
):
|
2023-04-24 14:46:00 +00:00
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
assert mock_roborock_entry.state is ConfigEntryState.SETUP_RETRY
|
2023-11-21 23:21:31 +00:00
|
|
|
|
|
|
|
|
2023-11-22 16:34:20 +00:00
|
|
|
async def test_config_entry_not_ready_home_data(
|
|
|
|
hass: HomeAssistant, mock_roborock_entry: MockConfigEntry
|
2023-11-21 23:21:31 +00:00
|
|
|
) -> None:
|
2023-11-22 16:34:20 +00:00
|
|
|
"""Test that when we fail to get home data, entry retries."""
|
2023-11-21 23:21:31 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.roborock.RoborockApiClient.get_home_data",
|
2023-11-22 16:34:20 +00:00
|
|
|
side_effect=RoborockException(),
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.roborock.coordinator.RoborockLocalClient.get_prop",
|
|
|
|
side_effect=RoborockException(),
|
2023-11-21 23:21:31 +00:00
|
|
|
):
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
2023-11-22 16:34:20 +00:00
|
|
|
assert mock_roborock_entry.state is ConfigEntryState.SETUP_RETRY
|
2023-11-21 23:21:31 +00:00
|
|
|
|
|
|
|
|
2023-11-22 16:34:20 +00:00
|
|
|
async def test_get_networking_fails(
|
|
|
|
hass: HomeAssistant, mock_roborock_entry: MockConfigEntry, bypass_api_fixture
|
2023-11-21 23:21:31 +00:00
|
|
|
) -> None:
|
2023-11-22 16:34:20 +00:00
|
|
|
"""Test that when networking fails, we attempt to retry."""
|
2023-11-21 23:21:31 +00:00
|
|
|
with patch(
|
2023-11-22 16:34:20 +00:00
|
|
|
"homeassistant.components.roborock.RoborockMqttClient.get_networking",
|
|
|
|
side_effect=RoborockException(),
|
|
|
|
):
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
assert mock_roborock_entry.state is ConfigEntryState.SETUP_RETRY
|
|
|
|
|
|
|
|
|
|
|
|
async def test_get_networking_fails_none(
|
|
|
|
hass: HomeAssistant, mock_roborock_entry: MockConfigEntry, bypass_api_fixture
|
|
|
|
) -> None:
|
|
|
|
"""Test that when networking returns None, we attempt to retry."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.roborock.RoborockMqttClient.get_networking",
|
|
|
|
return_value=None,
|
|
|
|
):
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
assert mock_roborock_entry.state is ConfigEntryState.SETUP_RETRY
|
|
|
|
|
|
|
|
|
|
|
|
async def test_cloud_client_fails_props(
|
|
|
|
hass: HomeAssistant, mock_roborock_entry: MockConfigEntry, bypass_api_fixture
|
|
|
|
) -> None:
|
|
|
|
"""Test that if networking succeeds, but we can't communicate with the vacuum, we can't get props, fail."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.roborock.coordinator.RoborockLocalClient.ping",
|
|
|
|
side_effect=RoborockException(),
|
|
|
|
), patch(
|
|
|
|
"homeassistant.components.roborock.coordinator.RoborockMqttClient.get_prop",
|
|
|
|
side_effect=RoborockException(),
|
|
|
|
):
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
assert mock_roborock_entry.state is ConfigEntryState.SETUP_RETRY
|
|
|
|
|
|
|
|
|
|
|
|
async def test_local_client_fails_props(
|
|
|
|
hass: HomeAssistant, mock_roborock_entry: MockConfigEntry, bypass_api_fixture
|
|
|
|
) -> None:
|
|
|
|
"""Test that if networking succeeds, but we can't communicate locally with the vacuum, we can't get props, fail."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.roborock.coordinator.RoborockLocalClient.get_prop",
|
2023-11-21 23:21:31 +00:00
|
|
|
side_effect=RoborockException(),
|
|
|
|
):
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
assert mock_roborock_entry.state is ConfigEntryState.SETUP_RETRY
|
2023-11-22 16:34:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_reauth_started(
|
|
|
|
hass: HomeAssistant, bypass_api_fixture, mock_roborock_entry: MockConfigEntry
|
|
|
|
) -> None:
|
|
|
|
"""Test reauth flow started."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.roborock.RoborockApiClient.get_home_data",
|
|
|
|
side_effect=RoborockInvalidCredentials(),
|
|
|
|
):
|
|
|
|
await async_setup_component(hass, DOMAIN, {})
|
|
|
|
assert mock_roborock_entry.state is ConfigEntryState.SETUP_ERROR
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
assert flows[0]["step_id"] == "reauth_confirm"
|