2018-02-21 00:24:31 +01:00
|
|
|
"""Fixtures for Hass.io."""
|
|
|
|
import os
|
2021-01-01 22:31:56 +01:00
|
|
|
from unittest.mock import Mock, patch
|
2018-02-21 00:24:31 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2019-12-08 16:33:22 +01:00
|
|
|
from homeassistant.components.hassio.handler import HassIO, HassioAPIError
|
2018-10-10 13:50:11 +02:00
|
|
|
from homeassistant.core import CoreState
|
2022-01-11 17:33:50 +01:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2018-02-21 00:24:31 +01:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2022-05-30 12:00:13 +02:00
|
|
|
from . import SUPERVISOR_TOKEN
|
2018-02-21 00:24:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hassio_env():
|
|
|
|
"""Fixture to inject hassio env."""
|
2022-05-30 12:00:13 +02:00
|
|
|
with patch.dict(os.environ, {"SUPERVISOR": "127.0.0.1"}), patch(
|
2019-07-31 12:25:30 -07:00
|
|
|
"homeassistant.components.hassio.HassIO.is_connected",
|
2020-04-30 13:29:50 -07:00
|
|
|
return_value={"result": "ok", "data": {}},
|
2022-05-30 12:00:13 +02:00
|
|
|
), patch.dict(os.environ, {"SUPERVISOR_TOKEN": SUPERVISOR_TOKEN}), patch(
|
2020-05-13 00:27:34 +02:00
|
|
|
"homeassistant.components.hassio.HassIO.get_info",
|
2019-07-31 12:25:30 -07:00
|
|
|
Mock(side_effect=HassioAPIError()),
|
|
|
|
):
|
2018-02-21 00:24:31 +01:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2018-12-17 11:27:03 +01:00
|
|
|
def hassio_stubs(hassio_env, hass, hass_client, aioclient_mock):
|
2018-02-21 00:24:31 +01:00
|
|
|
"""Create mock hassio http client."""
|
2019-06-10 16:05:43 -07:00
|
|
|
with patch(
|
2019-07-31 12:25:30 -07:00
|
|
|
"homeassistant.components.hassio.HassIO.update_hass_api",
|
2020-04-30 13:29:50 -07:00
|
|
|
return_value={"result": "ok"},
|
2020-01-14 23:49:56 +01:00
|
|
|
) as hass_api, patch(
|
2019-07-31 12:25:30 -07:00
|
|
|
"homeassistant.components.hassio.HassIO.update_hass_timezone",
|
2020-04-30 13:29:50 -07:00
|
|
|
return_value={"result": "ok"},
|
2019-06-10 16:05:43 -07:00
|
|
|
), patch(
|
2020-08-27 13:56:20 +02:00
|
|
|
"homeassistant.components.hassio.HassIO.get_info",
|
|
|
|
side_effect=HassioAPIError(),
|
2019-06-10 16:05:43 -07:00
|
|
|
):
|
2018-10-10 13:50:11 +02:00
|
|
|
hass.state = CoreState.starting
|
2019-10-14 14:56:45 -07:00
|
|
|
hass.loop.run_until_complete(async_setup_component(hass, "hassio", {}))
|
2018-12-17 11:27:03 +01:00
|
|
|
|
2020-01-14 23:49:56 +01:00
|
|
|
return hass_api.call_args[0][1]
|
|
|
|
|
2018-12-17 11:27:03 +01:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hassio_client(hassio_stubs, hass, hass_client):
|
2019-01-11 10:10:36 -08:00
|
|
|
"""Return a Hass.io HTTP client."""
|
2019-10-14 14:56:45 -07:00
|
|
|
return hass.loop.run_until_complete(hass_client())
|
2018-12-17 11:27:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hassio_noauth_client(hassio_stubs, hass, aiohttp_client):
|
2019-01-11 10:10:36 -08:00
|
|
|
"""Return a Hass.io HTTP client without auth."""
|
2019-10-14 14:56:45 -07:00
|
|
|
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
2018-02-21 22:42:55 +01:00
|
|
|
|
|
|
|
|
2020-01-14 23:49:56 +01:00
|
|
|
@pytest.fixture
|
|
|
|
async def hassio_client_supervisor(hass, aiohttp_client, hassio_stubs):
|
|
|
|
"""Return an authenticated HTTP client."""
|
|
|
|
access_token = hass.auth.async_create_access_token(hassio_stubs)
|
|
|
|
return await aiohttp_client(
|
2020-08-27 13:56:20 +02:00
|
|
|
hass.http.app,
|
|
|
|
headers={"Authorization": f"Bearer {access_token}"},
|
2020-01-14 23:49:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-02-21 22:42:55 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def hassio_handler(hass, aioclient_mock):
|
|
|
|
"""Create mock hassio handler."""
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2019-01-11 10:10:36 -08:00
|
|
|
async def get_client_session():
|
2022-01-11 17:33:50 +01:00
|
|
|
return async_get_clientsession(hass)
|
2019-01-11 10:10:36 -08:00
|
|
|
|
|
|
|
websession = hass.loop.run_until_complete(get_client_session())
|
2018-02-21 22:42:55 +01:00
|
|
|
|
2022-05-30 12:00:13 +02:00
|
|
|
with patch.dict(os.environ, {"SUPERVISOR_TOKEN": SUPERVISOR_TOKEN}):
|
2018-02-21 22:42:55 +01:00
|
|
|
yield HassIO(hass.loop, websession, "127.0.0.1")
|