Hassio cleanup part2 (#12588)
* Update handler.py * Update handler.py * Update __init__.py * Update handler.py * Update handler.py * Update __init__.py * Update tests
This commit is contained in:
1 parent
51c06e35cf
commit
b228695907
6 files changed
+260
-129
No files matched your search
@@ -1,3 +1,4 @@
|
||||
"""Tests for Hassio component."""
|
||||
|
||||
API_PASSWORD = 'pass1234'
|
||||
HASSIO_TOKEN = '123456'
|
||||
@@ -5,9 +5,10 @@ from unittest.mock import patch, Mock
|
||||
import pytest
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.hassio.handler import HassIO
|
||||
|
||||
from tests.common import mock_coro
|
||||
from . import API_PASSWORD
|
||||
from . import API_PASSWORD, HASSIO_TOKEN
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -38,3 +39,12 @@ def hassio_client(hassio_env, hass, test_client):
|
||||
}
|
||||
}))
|
||||
yield hass.loop.run_until_complete(test_client(hass.http.app))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hassio_handler(hass, aioclient_mock):
|
||||
"""Create mock hassio handler."""
|
||||
websession = hass.helpers.aiohttp_client.async_get_clientsession()
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO_TOKEN': HASSIO_TOKEN}):
|
||||
yield HassIO(hass.loop, websession, "127.0.0.1")
|
||||
@@ -1,151 +1,90 @@
|
||||
"""The tests for the hassio component."""
|
||||
import asyncio
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
import aiohttp
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_ping(hass, aioclient_mock):
|
||||
def test_api_ping(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API ping."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert hass.components.hassio.get_homeassistant_version() == "10.0"
|
||||
assert hass.components.hassio.is_hassio()
|
||||
assert (yield from hassio_handler.is_connected())
|
||||
assert aioclient_mock.call_count == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_push_api_data(hass, aioclient_mock):
|
||||
"""Test setup with API push."""
|
||||
def test_api_ping_error(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API ping error."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'error'})
|
||||
|
||||
assert not (yield from hassio_handler.is_connected())
|
||||
assert aioclient_mock.call_count == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_api_ping_exeption(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API ping exception."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", exc=aiohttp.ClientError())
|
||||
|
||||
assert not (yield from hassio_handler.is_connected())
|
||||
assert aioclient_mock.call_count == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_api_homeassistant_info(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API homeassistant info."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
|
||||
data = yield from hassio_handler.get_homeassistant_info()
|
||||
assert aioclient_mock.call_count == 1
|
||||
assert data['last_version'] == "10.0"
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_api_homeassistant_info_error(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API homeassistant info error."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'error', 'message': None})
|
||||
|
||||
data = yield from hassio_handler.get_homeassistant_info()
|
||||
assert aioclient_mock.call_count == 1
|
||||
assert data is None
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_api_homeassistant_stop(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API HomeAssistant stop."""
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/homeassistant/options", json={'result': 'ok'})
|
||||
"http://127.0.0.1/homeassistant/stop", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'http': {
|
||||
'api_password': "123456",
|
||||
'server_port': 9999
|
||||
},
|
||||
'hassio': {}
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert not aioclient_mock.mock_calls[1][2]['ssl']
|
||||
assert aioclient_mock.mock_calls[1][2]['password'] == "123456"
|
||||
assert aioclient_mock.mock_calls[1][2]['port'] == 9999
|
||||
assert aioclient_mock.mock_calls[1][2]['watchdog']
|
||||
assert (yield from hassio_handler.stop_homeassistant())
|
||||
assert aioclient_mock.call_count == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_push_api_data_server_host(hass, aioclient_mock):
|
||||
"""Test setup with API push with active server host."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
def test_api_homeassistant_restart(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API HomeAssistant restart."""
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/homeassistant/options", json={'result': 'ok'})
|
||||
"http://127.0.0.1/homeassistant/restart", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'http': {
|
||||
'api_password': "123456",
|
||||
'server_port': 9999,
|
||||
'server_host': "127.0.0.1"
|
||||
},
|
||||
'hassio': {}
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert not aioclient_mock.mock_calls[1][2]['ssl']
|
||||
assert aioclient_mock.mock_calls[1][2]['password'] == "123456"
|
||||
assert aioclient_mock.mock_calls[1][2]['port'] == 9999
|
||||
assert not aioclient_mock.mock_calls[1][2]['watchdog']
|
||||
assert (yield from hassio_handler.restart_homeassistant())
|
||||
assert aioclient_mock.call_count == 1
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_push_api_data_default(hass, aioclient_mock):
|
||||
"""Test setup with API push default data."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
def test_api_homeassistant_config(hassio_handler, aioclient_mock):
|
||||
"""Test setup with API HomeAssistant restart."""
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/homeassistant/options", json={'result': 'ok'})
|
||||
"http://127.0.0.1/homeassistant/check", json={
|
||||
'result': 'ok', 'data': {'test': 'bla'}})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'http': {},
|
||||
'hassio': {}
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert not aioclient_mock.mock_calls[1][2]['ssl']
|
||||
assert aioclient_mock.mock_calls[1][2]['password'] is None
|
||||
assert aioclient_mock.mock_calls[1][2]['port'] == 8123
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_core_push_timezone(hass, aioclient_mock):
|
||||
"""Test setup with API push default data."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/supervisor/options", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'hassio': {},
|
||||
'homeassistant': {
|
||||
'time_zone': 'testzone',
|
||||
},
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert aioclient_mock.mock_calls[1][2]['timezone'] == "testzone"
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_hassio_no_additional_data(hass, aioclient_mock):
|
||||
"""Test setup with API push default data."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}), \
|
||||
patch.dict(os.environ, {'HASSIO_TOKEN': "123456"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'hassio': {},
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert aioclient_mock.mock_calls[-1][3]['X-HASSIO-KEY'] == "123456"
|
||||
data = yield from hassio_handler.check_homeassistant_config()
|
||||
assert data['data']['test'] == 'bla'
|
||||
assert aioclient_mock.call_count == 1
|
||||
@@ -9,6 +9,151 @@ from homeassistant.components.hassio import async_check_config
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_ping(hass, aioclient_mock):
|
||||
"""Test setup with API ping."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert hass.components.hassio.get_homeassistant_version() == "10.0"
|
||||
assert hass.components.hassio.is_hassio()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_push_api_data(hass, aioclient_mock):
|
||||
"""Test setup with API push."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/homeassistant/options", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'http': {
|
||||
'api_password': "123456",
|
||||
'server_port': 9999
|
||||
},
|
||||
'hassio': {}
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert not aioclient_mock.mock_calls[1][2]['ssl']
|
||||
assert aioclient_mock.mock_calls[1][2]['password'] == "123456"
|
||||
assert aioclient_mock.mock_calls[1][2]['port'] == 9999
|
||||
assert aioclient_mock.mock_calls[1][2]['watchdog']
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_push_api_data_server_host(hass, aioclient_mock):
|
||||
"""Test setup with API push with active server host."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/homeassistant/options", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'http': {
|
||||
'api_password': "123456",
|
||||
'server_port': 9999,
|
||||
'server_host': "127.0.0.1"
|
||||
},
|
||||
'hassio': {}
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert not aioclient_mock.mock_calls[1][2]['ssl']
|
||||
assert aioclient_mock.mock_calls[1][2]['password'] == "123456"
|
||||
assert aioclient_mock.mock_calls[1][2]['port'] == 9999
|
||||
assert not aioclient_mock.mock_calls[1][2]['watchdog']
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_api_push_api_data_default(hass, aioclient_mock):
|
||||
"""Test setup with API push default data."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/homeassistant/options", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'http': {},
|
||||
'hassio': {}
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert not aioclient_mock.mock_calls[1][2]['ssl']
|
||||
assert aioclient_mock.mock_calls[1][2]['password'] is None
|
||||
assert aioclient_mock.mock_calls[1][2]['port'] == 8123
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_core_push_timezone(hass, aioclient_mock):
|
||||
"""Test setup with API push default data."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
aioclient_mock.post(
|
||||
"http://127.0.0.1/supervisor/options", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'hassio': {},
|
||||
'homeassistant': {
|
||||
'time_zone': 'testzone',
|
||||
},
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 3
|
||||
assert aioclient_mock.mock_calls[1][2]['timezone'] == "testzone"
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_hassio_no_additional_data(hass, aioclient_mock):
|
||||
"""Test setup with API push default data."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/supervisor/ping", json={'result': 'ok'})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={
|
||||
'result': 'ok', 'data': {'last_version': '10.0'}})
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/homeassistant/info", json={'result': 'ok'})
|
||||
|
||||
with patch.dict(os.environ, {'HASSIO': "127.0.0.1"}), \
|
||||
patch.dict(os.environ, {'HASSIO_TOKEN': "123456"}):
|
||||
result = yield from async_setup_component(hass, 'hassio', {
|
||||
'hassio': {},
|
||||
})
|
||||
assert result
|
||||
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert aioclient_mock.mock_calls[-1][3]['X-HASSIO-KEY'] == "123456"
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_fail_setup_without_environ_var(hass):
|
||||
"""Fail setup if no environ variable set."""
|
||||
|
||||
Reference in new issue
Block a user