2018-02-20 23:24:31 +00:00
|
|
|
"""Fixtures for Hass.io."""
|
|
|
|
import os
|
|
|
|
from unittest.mock import patch, Mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2018-10-10 11:50:11 +00:00
|
|
|
from homeassistant.core import CoreState
|
2018-02-20 23:24:31 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-10-03 11:10:38 +00:00
|
|
|
from homeassistant.components.hassio.handler import HassIO, HassioAPIError
|
2018-02-20 23:24:31 +00:00
|
|
|
|
|
|
|
from tests.common import mock_coro
|
2018-02-21 21:42:55 +00:00
|
|
|
from . import API_PASSWORD, 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",
|
|
|
|
Mock(return_value=mock_coro({"result": "ok", "data": {}})),
|
|
|
|
), patch.dict(os.environ, {"HASSIO_TOKEN": "123456"}), patch(
|
|
|
|
"homeassistant.components.hassio.HassIO." "get_homeassistant_info",
|
|
|
|
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",
|
|
|
|
return_value=mock_coro({"result": "ok"}),
|
2019-06-10 23:05:43 +00:00
|
|
|
), patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant.components.hassio.HassIO.update_hass_timezone",
|
|
|
|
return_value=mock_coro({"result": "ok"}),
|
2019-06-10 23:05:43 +00:00
|
|
|
), patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant.components.hassio.HassIO.get_homeassistant_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-07-31 19:25:30 +00:00
|
|
|
hass.loop.run_until_complete(
|
|
|
|
async_setup_component(
|
|
|
|
hass, "hassio", {"http": {"api_password": API_PASSWORD}}
|
|
|
|
)
|
|
|
|
)
|
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."""
|
2018-12-17 10:27:03 +00:00
|
|
|
yield hass.loop.run_until_complete(hass_client())
|
|
|
|
|
|
|
|
|
|
|
|
@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."""
|
2018-03-15 20:49:49 +00:00
|
|
|
yield hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
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():
|
|
|
|
return hass.helpers.aiohttp_client.async_get_clientsession()
|
|
|
|
|
|
|
|
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")
|