init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1 @@
"""Tests for the Google Cloud integration."""

View File

@@ -0,0 +1,124 @@
"""Tests helpers."""
from collections.abc import Generator
import json
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
from google.cloud.texttospeech_v1.types import cloud_tts
import pytest
from homeassistant.components.google_cloud.const import (
CONF_SERVICE_ACCOUNT_INFO,
DOMAIN,
)
from tests.common import MockConfigEntry
VALID_SERVICE_ACCOUNT_INFO = {
"type": "service_account",
"project_id": "my project id",
"private_key_id": "my private key if",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKYscIlwm7soDsHAz6L6YvUkCvkrX19rS6yeYOmovvhoK5WeYGWUsd8V72zmsyHB7XO94YgJVjvxfzn5K8bLePjFzwoSJjZvhBJ/ZQ05d8VmbvgyWUoPdG9oEa4fZ/lCYrXoaFdTot2xcJvrb/ZuiRl4s4eZpNeFYvVK/Am7UeFPAgMBAAECgYAUetOfzLYUudofvPCaKHu7tKZ5kQPfEa0w6BAPnBF1Mfl1JiDBRDMryFtKs6AOIAVwx00dY/Ex0BCbB3+Cr58H7t4NaPTJxCpmR09pK7o17B7xAdQv8+SynFNud9/5vQ5AEXMOLNwKiU7wpXT6Z7ZIibUBOR7ewsWgsHCDpN1iqQJBAOMODPTPSiQMwRAUHIc6GPleFSJnIz2PAoG3JOG9KFAL6RtIc19lob2ZXdbQdzKtjSkWo+O5W20WDNAl1k32h6MCQQC7W4ZCIY67mPbL6CxXfHjpSGF4Dr9VWJ7ZrKHr6XUoOIcEvsn/pHvWonjMdy93rQMSfOE8BKd/I1+GHRmNVgplAkAnSo4paxmsZVyfeKt7Jy2dMY+8tVZe17maUuQaAE7Sk00SgJYegwrbMYgQnWCTL39HBfj0dmYA2Zj8CCAuu6O7AkEAryFiYjaUAO9+4iNoL27+ZrFtypeeadyov7gKs0ZKaQpNyzW8A+Zwi7TbTeSqzic/E+z/bOa82q7p/6b7141xsQJBANCAcIwMcVb6KVCHlQbOtKspo5Eh4ZQi8bGl+IcwbQ6JSxeTx915IfAldgbuU047wOB04dYCFB2yLDiUGVXTifU=\n-----END PRIVATE KEY-----\n",
"client_email": "my client email",
"client_id": "my client id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account",
"universe_domain": "googleapis.com",
}
@pytest.fixture
def create_google_credentials_json(tmp_path: Path) -> str:
"""Create googlecredentials.json."""
file_path = tmp_path / "googlecredentials.json"
with open(file_path, "w", encoding="utf8") as f:
json.dump(VALID_SERVICE_ACCOUNT_INFO, f)
return str(file_path)
@pytest.fixture
def create_invalid_google_credentials_json(create_google_credentials_json: str) -> str:
"""Create invalid googlecredentials.json."""
invalid_service_account_info = VALID_SERVICE_ACCOUNT_INFO.copy()
invalid_service_account_info.pop("client_email")
with open(create_google_credentials_json, "w", encoding="utf8") as f:
json.dump(invalid_service_account_info, f)
return create_google_credentials_json
@pytest.fixture
def mock_process_uploaded_file(
create_google_credentials_json: str,
) -> Generator[MagicMock]:
"""Mock upload certificate files."""
ctx_mock = MagicMock()
ctx_mock.__enter__.return_value = Path(create_google_credentials_json)
with patch(
"homeassistant.components.google_cloud.config_flow.process_uploaded_file",
return_value=ctx_mock,
) as mock_upload:
yield mock_upload
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
title="my Google Cloud title",
domain=DOMAIN,
data={CONF_SERVICE_ACCOUNT_INFO: VALID_SERVICE_ACCOUNT_INFO},
)
@pytest.fixture
def mock_api_tts() -> AsyncMock:
"""Return a mocked TTS client."""
mock_client = AsyncMock()
mock_client.list_voices.return_value = cloud_tts.ListVoicesResponse(
voices=[
cloud_tts.Voice(language_codes=["en-US"], name="en-US-Standard-A"),
cloud_tts.Voice(language_codes=["en-US"], name="en-US-Standard-B"),
cloud_tts.Voice(language_codes=["el-GR"], name="el-GR-Standard-A"),
]
)
return mock_client
@pytest.fixture
def mock_api_tts_from_service_account_info(
mock_api_tts: AsyncMock,
) -> Generator[AsyncMock]:
"""Return a mocked TTS client created with from_service_account_info."""
with (
patch(
"google.cloud.texttospeech.TextToSpeechAsyncClient.from_service_account_info",
return_value=mock_api_tts,
),
):
yield mock_api_tts
@pytest.fixture
def mock_api_tts_from_service_account_file(
mock_api_tts: AsyncMock,
) -> Generator[AsyncMock]:
"""Return a mocked TTS client created with from_service_account_file."""
with (
patch(
"google.cloud.texttospeech.TextToSpeechAsyncClient.from_service_account_file",
return_value=mock_api_tts,
),
):
yield mock_api_tts
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.google_cloud.async_setup_entry", return_value=True
) as mock_setup_entry:
yield mock_setup_entry

View File

@@ -0,0 +1,185 @@
"""Test the Google Cloud config flow."""
from unittest.mock import AsyncMock, MagicMock
from uuid import uuid4
from homeassistant import config_entries
from homeassistant.components import tts
from homeassistant.components.google_cloud.config_flow import UPLOADED_KEY_FILE
from homeassistant.components.google_cloud.const import (
CONF_KEY_FILE,
CONF_SERVICE_ACCOUNT_INFO,
DOMAIN,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_PLATFORM
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.setup import async_setup_component
from .conftest import VALID_SERVICE_ACCOUNT_INFO
from tests.common import MockConfigEntry
async def test_user_flow_success(
hass: HomeAssistant,
mock_process_uploaded_file: MagicMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test user flow creates entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert not result["errors"]
uploaded_file = str(uuid4())
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{UPLOADED_KEY_FILE: uploaded_file},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "Google Cloud"
assert result["data"] == {CONF_SERVICE_ACCOUNT_INFO: VALID_SERVICE_ACCOUNT_INFO}
mock_process_uploaded_file.assert_called_with(hass, uploaded_file)
assert len(mock_setup_entry.mock_calls) == 1
async def test_user_flow_missing_file(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
) -> None:
"""Test user flow when uploaded file is missing."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{UPLOADED_KEY_FILE: str(uuid4())},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_file"}
assert len(mock_setup_entry.mock_calls) == 0
async def test_user_flow_invalid_file(
hass: HomeAssistant,
create_invalid_google_credentials_json: str,
mock_process_uploaded_file: MagicMock,
mock_setup_entry: AsyncMock,
) -> None:
"""Test user flow when uploaded file is invalid."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {}
uploaded_file = str(uuid4())
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{UPLOADED_KEY_FILE: uploaded_file},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_file"}
mock_process_uploaded_file.assert_called_with(hass, uploaded_file)
assert len(mock_setup_entry.mock_calls) == 0
async def test_import_flow(
hass: HomeAssistant,
create_google_credentials_json: str,
mock_api_tts_from_service_account_file: AsyncMock,
mock_api_tts_from_service_account_info: AsyncMock,
) -> None:
"""Test the import flow."""
assert not hass.config_entries.async_entries(DOMAIN)
assert await async_setup_component(
hass,
tts.DOMAIN,
{
tts.DOMAIN: {CONF_PLATFORM: DOMAIN}
| {CONF_KEY_FILE: create_google_credentials_json}
},
)
await hass.async_block_till_done()
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
assert config_entry.state is config_entries.ConfigEntryState.LOADED
async def test_import_flow_invalid_file(
hass: HomeAssistant,
create_invalid_google_credentials_json: str,
mock_api_tts_from_service_account_file: AsyncMock,
) -> None:
"""Test the import flow when the key file is invalid."""
assert not hass.config_entries.async_entries(DOMAIN)
assert await async_setup_component(
hass,
tts.DOMAIN,
{
tts.DOMAIN: {CONF_PLATFORM: DOMAIN}
| {CONF_KEY_FILE: create_invalid_google_credentials_json}
},
)
await hass.async_block_till_done()
assert not hass.config_entries.async_entries(DOMAIN)
assert mock_api_tts_from_service_account_file.list_voices.call_count == 1
async def test_options_flow(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_api_tts_from_service_account_info: AsyncMock,
) -> None:
"""Test options flow."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_api_tts_from_service_account_info.list_voices.call_count == 1
assert mock_config_entry.options == {}
result = await hass.config_entries.options.async_init(mock_config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
data_schema = result["data_schema"].schema
assert set(data_schema) == {
"language",
"gender",
"voice",
"encoding",
"speed",
"pitch",
"gain",
"profiles",
"text_type",
"stt_model",
}
assert mock_api_tts_from_service_account_info.list_voices.call_count == 2
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"language": "el-GR"},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert mock_config_entry.options == {
"language": "el-GR",
"gender": "NEUTRAL",
"voice": "",
"encoding": "MP3",
"speed": 1.0,
"pitch": 0.0,
"gain": 0.0,
"profiles": [],
"text_type": "text",
"stt_model": "latest_short",
}
assert mock_api_tts_from_service_account_info.list_voices.call_count == 3