2017-11-13 16:32:23 +00:00
|
|
|
"""The tests for google-assistant init."""
|
2024-03-08 13:50:04 +00:00
|
|
|
|
2021-10-23 18:34:53 +00:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
2019-12-09 19:00:14 +00:00
|
|
|
from homeassistant.components import google_assistant as ga
|
2022-06-27 18:24:15 +00:00
|
|
|
from homeassistant.core import Context, HomeAssistant
|
2017-11-13 16:32:23 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2020-02-05 17:50:00 +00:00
|
|
|
from .test_http import DUMMY_CONFIG
|
2017-11-13 16:32:23 +00:00
|
|
|
|
2022-06-27 18:24:15 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2023-02-13 10:13:48 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2022-06-27 18:24:15 +00:00
|
|
|
|
|
|
|
|
2023-02-13 10:13:48 +00:00
|
|
|
async def test_import(hass: HomeAssistant) -> None:
|
2022-06-27 18:24:15 +00:00
|
|
|
"""Test import."""
|
|
|
|
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
ga.DOMAIN,
|
|
|
|
{"google_assistant": DUMMY_CONFIG},
|
|
|
|
)
|
|
|
|
|
|
|
|
entries = hass.config_entries.async_entries("google_assistant")
|
|
|
|
assert len(entries) == 1
|
|
|
|
assert entries[0].data[ga.const.CONF_PROJECT_ID] == "1234"
|
|
|
|
|
|
|
|
|
2023-02-13 10:13:48 +00:00
|
|
|
async def test_import_changed(hass: HomeAssistant) -> None:
|
2022-06-27 18:24:15 +00:00
|
|
|
"""Test import with changed project id."""
|
|
|
|
|
|
|
|
old_entry = MockConfigEntry(
|
|
|
|
domain=ga.DOMAIN, data={ga.const.CONF_PROJECT_ID: "4321"}, source="import"
|
|
|
|
)
|
|
|
|
old_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
await async_setup_component(
|
|
|
|
hass,
|
|
|
|
ga.DOMAIN,
|
|
|
|
{"google_assistant": DUMMY_CONFIG},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
entries = hass.config_entries.async_entries("google_assistant")
|
|
|
|
assert len(entries) == 1
|
|
|
|
assert entries[0].data[ga.const.CONF_PROJECT_ID] == "1234"
|
|
|
|
|
2017-11-13 16:32:23 +00:00
|
|
|
|
2023-02-13 10:13:48 +00:00
|
|
|
async def test_request_sync_service(
|
|
|
|
aioclient_mock: AiohttpClientMocker, hass: HomeAssistant
|
|
|
|
) -> None:
|
2017-11-13 16:32:23 +00:00
|
|
|
"""Test that it posts to the request_sync url."""
|
2020-02-05 17:50:00 +00:00
|
|
|
aioclient_mock.post(
|
|
|
|
ga.const.HOMEGRAPH_TOKEN_URL,
|
2021-10-23 18:34:53 +00:00
|
|
|
status=HTTPStatus.OK,
|
2020-02-05 17:50:00 +00:00
|
|
|
json={"access_token": "1234", "expires_in": 3600},
|
|
|
|
)
|
|
|
|
|
2021-10-23 18:34:53 +00:00
|
|
|
aioclient_mock.post(ga.const.REQUEST_SYNC_BASE_URL, status=HTTPStatus.OK)
|
2017-11-13 16:32:23 +00:00
|
|
|
|
2020-01-01 23:24:30 +00:00
|
|
|
await async_setup_component(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass,
|
|
|
|
"google_assistant",
|
|
|
|
{"google_assistant": DUMMY_CONFIG},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-11-13 16:32:23 +00:00
|
|
|
|
|
|
|
assert aioclient_mock.call_count == 0
|
2020-01-01 23:24:30 +00:00
|
|
|
await hass.services.async_call(
|
2019-10-03 11:02:38 +00:00
|
|
|
ga.const.DOMAIN,
|
|
|
|
ga.const.SERVICE_REQUEST_SYNC,
|
|
|
|
blocking=True,
|
|
|
|
context=Context(user_id="123"),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-11-13 16:32:23 +00:00
|
|
|
|
2020-02-05 17:50:00 +00:00
|
|
|
assert aioclient_mock.call_count == 2 # token + request
|