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