Adjust imports in cloud tests (#120386)

This commit is contained in:
epenet 2024-06-25 13:13:14 +02:00 committed by GitHub
parent cbb3d48bd9
commit 76e890865e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 75 additions and 53 deletions

View file

@ -6,15 +6,22 @@ from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components import cloud
from homeassistant.components.cloud import (
CloudConnectionState,
CloudNotAvailable,
CloudNotConnected,
async_get_or_create_cloudhook,
async_listen_connection_change,
async_remote_ui_url,
)
from homeassistant.components.cloud.const import (
DATA_CLOUD,
DOMAIN,
MODE_DEV,
PREF_CLOUDHOOKS,
)
from homeassistant.components.cloud.const import DATA_CLOUD, DOMAIN, PREF_CLOUDHOOKS
from homeassistant.components.cloud.prefs import STORAGE_KEY
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.const import CONF_MODE, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Context, HomeAssistant
from homeassistant.exceptions import Unauthorized
from homeassistant.setup import async_setup_component
@ -31,7 +38,7 @@ async def test_constructor_loads_info_from_config(hass: HomeAssistant) -> None:
{
"http": {},
"cloud": {
cloud.CONF_MODE: cloud.MODE_DEV,
CONF_MODE: MODE_DEV,
"cognito_client_id": "test-cognito_client_id",
"user_pool_id": "test-user_pool_id",
"region": "test-region",
@ -47,7 +54,7 @@ async def test_constructor_loads_info_from_config(hass: HomeAssistant) -> None:
assert result
cl = hass.data[DATA_CLOUD]
assert cl.mode == cloud.MODE_DEV
assert cl.mode == MODE_DEV
assert cl.cognito_client_id == "test-cognito_client_id"
assert cl.user_pool_id == "test-user_pool_id"
assert cl.region == "test-region"
@ -129,7 +136,7 @@ async def test_setup_existing_cloud_user(
{
"http": {},
"cloud": {
cloud.CONF_MODE: cloud.MODE_DEV,
CONF_MODE: MODE_DEV,
"cognito_client_id": "test-cognito_client_id",
"user_pool_id": "test-user_pool_id",
"region": "test-region",
@ -156,7 +163,7 @@ async def test_on_connect(hass: HomeAssistant, mock_cloud_fixture) -> None:
nonlocal cloud_states
cloud_states.append(cloud_state)
cloud.async_listen_connection_change(hass, handle_state)
async_listen_connection_change(hass, handle_state)
assert "async_setup" in str(cl.iot._on_connect[-1])
await cl.iot._on_connect[-1]()
@ -178,12 +185,12 @@ async def test_on_connect(hass: HomeAssistant, mock_cloud_fixture) -> None:
assert len(mock_load.mock_calls) == 0
assert len(cloud_states) == 1
assert cloud_states[-1] == cloud.CloudConnectionState.CLOUD_CONNECTED
assert cloud_states[-1] == CloudConnectionState.CLOUD_CONNECTED
await cl.iot._on_connect[-1]()
await hass.async_block_till_done()
assert len(cloud_states) == 2
assert cloud_states[-1] == cloud.CloudConnectionState.CLOUD_CONNECTED
assert cloud_states[-1] == CloudConnectionState.CLOUD_CONNECTED
assert len(cl.iot._on_disconnect) == 2
assert "async_setup" in str(cl.iot._on_disconnect[-1])
@ -191,12 +198,12 @@ async def test_on_connect(hass: HomeAssistant, mock_cloud_fixture) -> None:
await hass.async_block_till_done()
assert len(cloud_states) == 3
assert cloud_states[-1] == cloud.CloudConnectionState.CLOUD_DISCONNECTED
assert cloud_states[-1] == CloudConnectionState.CLOUD_DISCONNECTED
await cl.iot._on_disconnect[-1]()
await hass.async_block_till_done()
assert len(cloud_states) == 4
assert cloud_states[-1] == cloud.CloudConnectionState.CLOUD_DISCONNECTED
assert cloud_states[-1] == CloudConnectionState.CLOUD_DISCONNECTED
async def test_remote_ui_url(hass: HomeAssistant, mock_cloud_fixture) -> None:
@ -204,26 +211,26 @@ async def test_remote_ui_url(hass: HomeAssistant, mock_cloud_fixture) -> None:
cl = hass.data[DATA_CLOUD]
# Not logged in
with pytest.raises(cloud.CloudNotAvailable):
cloud.async_remote_ui_url(hass)
with pytest.raises(CloudNotAvailable):
async_remote_ui_url(hass)
with patch.object(cloud, "async_is_logged_in", return_value=True):
with patch("homeassistant.components.cloud.async_is_logged_in", return_value=True):
# Remote not enabled
with pytest.raises(cloud.CloudNotAvailable):
cloud.async_remote_ui_url(hass)
with pytest.raises(CloudNotAvailable):
async_remote_ui_url(hass)
with patch.object(cl.remote, "connect"):
await cl.client.prefs.async_update(remote_enabled=True)
await hass.async_block_till_done()
# No instance domain
with pytest.raises(cloud.CloudNotAvailable):
cloud.async_remote_ui_url(hass)
with pytest.raises(CloudNotAvailable):
async_remote_ui_url(hass)
# Remote finished initializing
cl.client.prefs._prefs["remote_domain"] = "example.com"
assert cloud.async_remote_ui_url(hass) == "https://example.com"
assert async_remote_ui_url(hass) == "https://example.com"
async def test_async_get_or_create_cloudhook(