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

@ -2,9 +2,19 @@
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from homeassistant.components import cloud from homeassistant.components.cloud.const import (
from homeassistant.components.cloud import const, prefs as cloud_prefs DATA_CLOUD,
from homeassistant.components.cloud.const import DATA_CLOUD DOMAIN,
PREF_ALEXA_SETTINGS_VERSION,
PREF_ENABLE_ALEXA,
PREF_ENABLE_GOOGLE,
PREF_GOOGLE_SECURE_DEVICES_PIN,
PREF_GOOGLE_SETTINGS_VERSION,
)
from homeassistant.components.cloud.prefs import (
ALEXA_SETTINGS_VERSION,
GOOGLE_SETTINGS_VERSION,
)
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
PIPELINE_DATA = { PIPELINE_DATA = {
@ -62,7 +72,7 @@ async def mock_cloud(hass, config=None):
# because it's always setup by bootstrap. Set it up manually in tests. # because it's always setup by bootstrap. Set it up manually in tests.
assert await async_setup_component(hass, "homeassistant", {}) assert await async_setup_component(hass, "homeassistant", {})
assert await async_setup_component(hass, cloud.DOMAIN, {"cloud": config or {}}) assert await async_setup_component(hass, DOMAIN, {"cloud": config or {}})
cloud_inst = hass.data[DATA_CLOUD] cloud_inst = hass.data[DATA_CLOUD]
with patch("hass_nabucasa.Cloud.run_executor", AsyncMock(return_value=None)): with patch("hass_nabucasa.Cloud.run_executor", AsyncMock(return_value=None)):
await cloud_inst.initialize() await cloud_inst.initialize()
@ -71,11 +81,11 @@ async def mock_cloud(hass, config=None):
def mock_cloud_prefs(hass, prefs): def mock_cloud_prefs(hass, prefs):
"""Fixture for cloud component.""" """Fixture for cloud component."""
prefs_to_set = { prefs_to_set = {
const.PREF_ALEXA_SETTINGS_VERSION: cloud_prefs.ALEXA_SETTINGS_VERSION, PREF_ALEXA_SETTINGS_VERSION: ALEXA_SETTINGS_VERSION,
const.PREF_ENABLE_ALEXA: True, PREF_ENABLE_ALEXA: True,
const.PREF_ENABLE_GOOGLE: True, PREF_ENABLE_GOOGLE: True,
const.PREF_GOOGLE_SECURE_DEVICES_PIN: None, PREF_GOOGLE_SECURE_DEVICES_PIN: None,
const.PREF_GOOGLE_SETTINGS_VERSION: cloud_prefs.GOOGLE_SETTINGS_VERSION, PREF_GOOGLE_SETTINGS_VERSION: GOOGLE_SETTINGS_VERSION,
} }
prefs_to_set.update(prefs) prefs_to_set.update(prefs)
hass.data[DATA_CLOUD].client._prefs._prefs = prefs_to_set hass.data[DATA_CLOUD].client._prefs._prefs = prefs_to_set

View file

@ -17,8 +17,13 @@ import jwt
import pytest import pytest
from typing_extensions import AsyncGenerator from typing_extensions import AsyncGenerator
from homeassistant.components.cloud import CloudClient, prefs from homeassistant.components.cloud.client import CloudClient
from homeassistant.components.cloud.const import DATA_CLOUD from homeassistant.components.cloud.const import DATA_CLOUD
from homeassistant.components.cloud.prefs import (
PREF_ALEXA_DEFAULT_EXPOSE,
PREF_GOOGLE_DEFAULT_EXPOSE,
CloudPreferences,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
@ -174,8 +179,8 @@ def set_cloud_prefs_fixture(
async def set_cloud_prefs(prefs_settings: dict[str, Any]) -> None: async def set_cloud_prefs(prefs_settings: dict[str, Any]) -> None:
"""Set cloud prefs.""" """Set cloud prefs."""
prefs_to_set = cloud.client.prefs.as_dict() prefs_to_set = cloud.client.prefs.as_dict()
prefs_to_set.pop(prefs.PREF_ALEXA_DEFAULT_EXPOSE) prefs_to_set.pop(PREF_ALEXA_DEFAULT_EXPOSE)
prefs_to_set.pop(prefs.PREF_GOOGLE_DEFAULT_EXPOSE) prefs_to_set.pop(PREF_GOOGLE_DEFAULT_EXPOSE)
prefs_to_set.update(prefs_settings) prefs_to_set.update(prefs_settings)
await cloud.client.prefs.async_update(**prefs_to_set) await cloud.client.prefs.async_update(**prefs_to_set)
@ -210,7 +215,7 @@ def mock_cloud_fixture(hass):
@pytest.fixture @pytest.fixture
async def cloud_prefs(hass): async def cloud_prefs(hass):
"""Fixture for cloud preferences.""" """Fixture for cloud preferences."""
cloud_prefs = prefs.CloudPreferences(hass) cloud_prefs = CloudPreferences(hass)
await cloud_prefs.async_initialize() await cloud_prefs.async_initialize()
return cloud_prefs return cloud_prefs

View file

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

View file

@ -6,8 +6,10 @@ from unittest.mock import patch
import pytest import pytest
from homeassistant.components.cloud import DOMAIN from homeassistant.components.cloud.const import DOMAIN
import homeassistant.components.cloud.repairs as cloud_repairs from homeassistant.components.cloud.repairs import (
async_manage_legacy_subscription_issue,
)
from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.issue_registry as ir import homeassistant.helpers.issue_registry as ir
@ -65,12 +67,12 @@ async def test_legacy_subscription_delete_issue_if_no_longer_legacy(
issue_registry: ir.IssueRegistry, issue_registry: ir.IssueRegistry,
) -> None: ) -> None:
"""Test that we delete the legacy subscription issue if no longer legacy.""" """Test that we delete the legacy subscription issue if no longer legacy."""
cloud_repairs.async_manage_legacy_subscription_issue(hass, {"provider": "legacy"}) async_manage_legacy_subscription_issue(hass, {"provider": "legacy"})
assert issue_registry.async_get_issue( assert issue_registry.async_get_issue(
domain="cloud", issue_id="legacy_subscription" domain="cloud", issue_id="legacy_subscription"
) )
cloud_repairs.async_manage_legacy_subscription_issue(hass, {}) async_manage_legacy_subscription_issue(hass, {})
assert not issue_registry.async_get_issue( assert not issue_registry.async_get_issue(
domain="cloud", issue_id="legacy_subscription" domain="cloud", issue_id="legacy_subscription"
) )
@ -93,7 +95,7 @@ async def test_legacy_subscription_repair_flow(
json={"url": "https://paypal.com"}, json={"url": "https://paypal.com"},
) )
cloud_repairs.async_manage_legacy_subscription_issue(hass, {"provider": "legacy"}) async_manage_legacy_subscription_issue(hass, {"provider": "legacy"})
repair_issue = issue_registry.async_get_issue( repair_issue = issue_registry.async_get_issue(
domain="cloud", issue_id="legacy_subscription" domain="cloud", issue_id="legacy_subscription"
) )
@ -174,7 +176,7 @@ async def test_legacy_subscription_repair_flow_timeout(
status=403, status=403,
) )
cloud_repairs.async_manage_legacy_subscription_issue(hass, {"provider": "legacy"}) async_manage_legacy_subscription_issue(hass, {"provider": "legacy"})
repair_issue = issue_registry.async_get_issue( repair_issue = issue_registry.async_get_issue(
domain="cloud", issue_id="legacy_subscription" domain="cloud", issue_id="legacy_subscription"
) )

View file

@ -10,7 +10,7 @@ import pytest
from typing_extensions import AsyncGenerator from typing_extensions import AsyncGenerator
from homeassistant.components.assist_pipeline.pipeline import STORAGE_KEY from homeassistant.components.assist_pipeline.pipeline import STORAGE_KEY
from homeassistant.components.cloud import DOMAIN from homeassistant.components.cloud.const import DOMAIN
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component

View file

@ -8,7 +8,7 @@ from unittest.mock import MagicMock
from aiohttp import ClientError from aiohttp import ClientError
from hass_nabucasa.remote import CertificateStatus from hass_nabucasa.remote import CertificateStatus
from homeassistant.components.cloud import DOMAIN from homeassistant.components.cloud.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component

View file

@ -12,7 +12,8 @@ from typing_extensions import AsyncGenerator
import voluptuous as vol import voluptuous as vol
from homeassistant.components.assist_pipeline.pipeline import STORAGE_KEY from homeassistant.components.assist_pipeline.pipeline import STORAGE_KEY
from homeassistant.components.cloud import DOMAIN, const, tts from homeassistant.components.cloud.const import DEFAULT_TTS_DEFAULT_VOICE, DOMAIN
from homeassistant.components.cloud.tts import PLATFORM_SCHEMA, SUPPORT_LANGUAGES, Voice
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_ID,
DOMAIN as DOMAIN_MP, DOMAIN as DOMAIN_MP,
@ -57,33 +58,30 @@ async def internal_url_mock(hass: HomeAssistant) -> None:
def test_default_exists() -> None: def test_default_exists() -> None:
"""Test our default language exists.""" """Test our default language exists."""
assert const.DEFAULT_TTS_DEFAULT_VOICE[0] in TTS_VOICES assert DEFAULT_TTS_DEFAULT_VOICE[0] in TTS_VOICES
assert ( assert DEFAULT_TTS_DEFAULT_VOICE[1] in TTS_VOICES[DEFAULT_TTS_DEFAULT_VOICE[0]]
const.DEFAULT_TTS_DEFAULT_VOICE[1]
in TTS_VOICES[const.DEFAULT_TTS_DEFAULT_VOICE[0]]
)
def test_schema() -> None: def test_schema() -> None:
"""Test schema.""" """Test schema."""
assert "nl-NL" in tts.SUPPORT_LANGUAGES assert "nl-NL" in SUPPORT_LANGUAGES
processed = tts.PLATFORM_SCHEMA({"platform": "cloud", "language": "nl-NL"}) processed = PLATFORM_SCHEMA({"platform": "cloud", "language": "nl-NL"})
assert processed["gender"] == "female" assert processed["gender"] == "female"
with pytest.raises(vol.Invalid): with pytest.raises(vol.Invalid):
tts.PLATFORM_SCHEMA( PLATFORM_SCHEMA(
{"platform": "cloud", "language": "non-existing", "gender": "female"} {"platform": "cloud", "language": "non-existing", "gender": "female"}
) )
with pytest.raises(vol.Invalid): with pytest.raises(vol.Invalid):
tts.PLATFORM_SCHEMA( PLATFORM_SCHEMA(
{"platform": "cloud", "language": "nl-NL", "gender": "not-supported"} {"platform": "cloud", "language": "nl-NL", "gender": "not-supported"}
) )
# Should not raise # Should not raise
tts.PLATFORM_SCHEMA({"platform": "cloud", "language": "nl-NL", "gender": "female"}) PLATFORM_SCHEMA({"platform": "cloud", "language": "nl-NL", "gender": "female"})
tts.PLATFORM_SCHEMA({"platform": "cloud"}) PLATFORM_SCHEMA({"platform": "cloud"})
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -188,7 +186,7 @@ async def test_provider_properties(
assert "nl-NL" in engine.supported_languages assert "nl-NL" in engine.supported_languages
supported_voices = engine.async_get_supported_voices("nl-NL") supported_voices = engine.async_get_supported_voices("nl-NL")
assert supported_voices is not None assert supported_voices is not None
assert tts.Voice("ColetteNeural", "ColetteNeural") in supported_voices assert Voice("ColetteNeural", "ColetteNeural") in supported_voices
supported_voices = engine.async_get_supported_voices("missing_language") supported_voices = engine.async_get_supported_voices("missing_language")
assert supported_voices is None assert supported_voices is None