Add Safe Mode (#30723)
* Store last working HTTP settings * Add safe mode * Fix tests * Add cloud to safe mode * Update logging text * Fix camera tests leaving files behind * Make emulated_hue tests not leave files behind * Make logbook tests not leave files behind * Make tts tests not leave files behind * Make image_processing tests not leave files behind * Make manual_mqtt tests not leave files behind
This commit is contained in:
1 parent
c4673ddee1
commit
5fdc60e067
35 files changed
+480
-430
No files matched your search
@@ -496,14 +496,13 @@ async def test_reload_config_service(hass, calls, hass_admin_user, hass_read_onl
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
with pytest.raises(Unauthorized):
|
||||
await common.async_reload(hass, Context(user_id=hass_read_only_user.id))
|
||||
await hass.async_block_till_done()
|
||||
await common.async_reload(hass, Context(user_id=hass_admin_user.id))
|
||||
await hass.async_block_till_done()
|
||||
# De-flake ?!
|
||||
with pytest.raises(Unauthorized):
|
||||
await common.async_reload(hass, Context(user_id=hass_read_only_user.id))
|
||||
await hass.async_block_till_done()
|
||||
await common.async_reload(hass, Context(user_id=hass_admin_user.id))
|
||||
await hass.async_block_till_done()
|
||||
# De-flake ?!
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("automation.hello") is None
|
||||
assert hass.states.get("automation.bye") is not None
|
||||
@@ -551,9 +550,8 @@ async def test_reload_config_when_invalid_config(hass, calls):
|
||||
autospec=True,
|
||||
return_value={automation.DOMAIN: "not valid"},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
await common.async_reload(hass)
|
||||
await hass.async_block_till_done()
|
||||
await common.async_reload(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("automation.hello") is None
|
||||
|
||||
@@ -590,9 +588,8 @@ async def test_reload_config_handles_load_fails(hass, calls):
|
||||
"homeassistant.config.load_yaml_config_file",
|
||||
side_effect=HomeAssistantError("bla"),
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
await common.async_reload(hass)
|
||||
await hass.async_block_till_done()
|
||||
await common.async_reload(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("automation.hello") is not None
|
||||
|
||||
|
||||
@@ -6,24 +6,15 @@ from unittest.mock import PropertyMock, mock_open, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import camera, http
|
||||
from homeassistant.components import camera
|
||||
from homeassistant.components.camera.const import DOMAIN, PREF_PRELOAD_STREAM
|
||||
from homeassistant.components.camera.prefs import CameraEntityPreferences
|
||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_ENTITY_PICTURE,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_START
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import (
|
||||
assert_setup_component,
|
||||
get_test_home_assistant,
|
||||
get_test_instance_port,
|
||||
mock_coro,
|
||||
)
|
||||
from tests.common import mock_coro
|
||||
from tests.components.camera import common
|
||||
|
||||
|
||||
@@ -55,96 +46,53 @@ def setup_camera_prefs(hass):
|
||||
return common.mock_camera_prefs(hass, "camera.demo_camera")
|
||||
|
||||
|
||||
class TestSetupCamera:
|
||||
"""Test class for setup camera."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_component(self):
|
||||
"""Set up demo platform on camera component."""
|
||||
config = {camera.DOMAIN: {"platform": "demo"}}
|
||||
|
||||
with assert_setup_component(1, camera.DOMAIN):
|
||||
setup_component(self.hass, camera.DOMAIN, config)
|
||||
@pytest.fixture
|
||||
async def image_mock_url(hass):
|
||||
"""Fixture for get_image tests."""
|
||||
await async_setup_component(
|
||||
hass, camera.DOMAIN, {camera.DOMAIN: {"platform": "demo"}}
|
||||
)
|
||||
|
||||
|
||||
class TestGetImage:
|
||||
"""Test class for camera."""
|
||||
async def test_get_image_from_camera(hass, image_mock_url):
|
||||
"""Grab an image from camera entity."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
setup_component(
|
||||
self.hass,
|
||||
http.DOMAIN,
|
||||
{http.DOMAIN: {http.CONF_SERVER_PORT: get_test_instance_port()}},
|
||||
)
|
||||
|
||||
config = {camera.DOMAIN: {"platform": "demo"}}
|
||||
|
||||
setup_component(self.hass, camera.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get("camera.demo_camera")
|
||||
self.url = "{0}{1}".format(
|
||||
self.hass.config.api.base_url, state.attributes.get(ATTR_ENTITY_PICTURE)
|
||||
)
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@patch(
|
||||
with patch(
|
||||
"homeassistant.components.demo.camera.DemoCamera.camera_image",
|
||||
autospec=True,
|
||||
return_value=b"Test",
|
||||
)
|
||||
def test_get_image_from_camera(self, mock_camera):
|
||||
"""Grab an image from camera entity."""
|
||||
self.hass.start()
|
||||
) as mock_camera:
|
||||
image = await camera.async_get_image(hass, "camera.demo_camera")
|
||||
|
||||
image = asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
assert mock_camera.called
|
||||
assert image.content == b"Test"
|
||||
|
||||
assert mock_camera.called
|
||||
assert image.content == b"Test"
|
||||
|
||||
def test_get_image_without_exists_camera(self):
|
||||
"""Try to get image without exists camera."""
|
||||
with patch(
|
||||
"homeassistant.helpers.entity_component.EntityComponent.get_entity",
|
||||
return_value=None,
|
||||
), pytest.raises(HomeAssistantError):
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
async def test_get_image_without_exists_camera(hass, image_mock_url):
|
||||
"""Try to get image without exists camera."""
|
||||
with patch(
|
||||
"homeassistant.helpers.entity_component.EntityComponent.get_entity",
|
||||
return_value=None,
|
||||
), pytest.raises(HomeAssistantError):
|
||||
await camera.async_get_image(hass, "camera.demo_camera")
|
||||
|
||||
def test_get_image_with_timeout(self):
|
||||
"""Try to get image with timeout."""
|
||||
with patch(
|
||||
"homeassistant.components.camera.Camera.async_camera_image",
|
||||
side_effect=asyncio.TimeoutError,
|
||||
), pytest.raises(HomeAssistantError):
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
|
||||
def test_get_image_fails(self):
|
||||
"""Try to get image with timeout."""
|
||||
with patch(
|
||||
"homeassistant.components.camera.Camera.async_camera_image",
|
||||
return_value=mock_coro(None),
|
||||
), pytest.raises(HomeAssistantError):
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
async def test_get_image_with_timeout(hass, image_mock_url):
|
||||
"""Try to get image with timeout."""
|
||||
with patch(
|
||||
"homeassistant.components.camera.Camera.async_camera_image",
|
||||
side_effect=asyncio.TimeoutError,
|
||||
), pytest.raises(HomeAssistantError):
|
||||
await camera.async_get_image(hass, "camera.demo_camera")
|
||||
|
||||
|
||||
async def test_get_image_fails(hass, image_mock_url):
|
||||
"""Try to get image with timeout."""
|
||||
with patch(
|
||||
"homeassistant.components.camera.Camera.async_camera_image",
|
||||
return_value=mock_coro(None),
|
||||
), pytest.raises(HomeAssistantError):
|
||||
await camera.async_get_image(hass, "camera.demo_camera")
|
||||
|
||||
|
||||
async def test_snapshot_service(hass, mock_camera):
|
||||
|
||||
@@ -4,10 +4,11 @@ import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from aiohttp.hdrs import CONTENT_TYPE
|
||||
import defusedxml.ElementTree as ET
|
||||
import requests
|
||||
|
||||
from homeassistant import const, setup
|
||||
from homeassistant.components import emulated_hue, http
|
||||
from homeassistant.components import emulated_hue
|
||||
|
||||
from tests.common import get_test_home_assistant, get_test_instance_port
|
||||
|
||||
@@ -28,10 +29,6 @@ class TestEmulatedHue(unittest.TestCase):
|
||||
"""Set up the class."""
|
||||
cls.hass = hass = get_test_home_assistant()
|
||||
|
||||
setup.setup_component(
|
||||
hass, http.DOMAIN, {http.DOMAIN: {http.CONF_SERVER_PORT: HTTP_SERVER_PORT}}
|
||||
)
|
||||
|
||||
with patch("homeassistant.components.emulated_hue.UPNPResponderThread"):
|
||||
setup.setup_component(
|
||||
hass,
|
||||
@@ -52,8 +49,6 @@ class TestEmulatedHue(unittest.TestCase):
|
||||
|
||||
def test_description_xml(self):
|
||||
"""Test the description."""
|
||||
import defusedxml.ElementTree as ET
|
||||
|
||||
result = requests.get(BRIDGE_URL_BASE.format("/description.xml"), timeout=5)
|
||||
|
||||
assert result.status_code == 200
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""The tests for Home Assistant frontend."""
|
||||
import re
|
||||
from unittest.mock import patch
|
||||
|
||||
from asynctest import patch
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.frontend import (
|
||||
@@ -173,7 +173,7 @@ async def test_themes_reload_themes(hass, hass_ws_client):
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.frontend.load_yaml_config_file",
|
||||
"homeassistant.components.frontend.async_hass_config_yaml",
|
||||
return_value={DOMAIN: {CONF_THEMES: {"sad": {"primary-color": "blue"}}}},
|
||||
):
|
||||
await hass.services.async_call(
|
||||
|
||||
@@ -429,9 +429,8 @@ class TestComponentsGroup(unittest.TestCase):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
common.reload(self.hass)
|
||||
self.hass.block_till_done()
|
||||
common.reload(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert sorted(self.hass.states.entity_ids()) == [
|
||||
"group.all_tests",
|
||||
|
||||
@@ -18,7 +18,7 @@ async def test_reload_config_service(hass):
|
||||
"homeassistant.config.load_yaml_config_file",
|
||||
autospec=True,
|
||||
return_value={"scene": {"name": "Hallo", "entities": {"light.kitchen": "on"}}},
|
||||
), patch("homeassistant.config.find_config_file", return_value=""):
|
||||
):
|
||||
await hass.services.async_call("scene", "reload", blocking=True)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -28,7 +28,7 @@ async def test_reload_config_service(hass):
|
||||
"homeassistant.config.load_yaml_config_file",
|
||||
autospec=True,
|
||||
return_value={"scene": {"name": "Bye", "entities": {"light.kitchen": "on"}}},
|
||||
), patch("homeassistant.config.find_config_file", return_value=""):
|
||||
):
|
||||
await hass.services.async_call("scene", "reload", blocking=True)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@@ -240,3 +240,16 @@ async def test_cors_defaults(hass):
|
||||
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert mock_setup.mock_calls[0][1][1] == ["https://cast.home-assistant.io"]
|
||||
|
||||
|
||||
async def test_storing_config(hass, aiohttp_client, aiohttp_unused_port):
|
||||
"""Test that we store last working config."""
|
||||
config = {http.CONF_SERVER_PORT: aiohttp_unused_port()}
|
||||
|
||||
await async_setup_component(hass, http.DOMAIN, {http.DOMAIN: config})
|
||||
|
||||
await hass.async_start()
|
||||
|
||||
assert await hass.components.http.async_get_last_config() == http.HTTP_SCHEMA(
|
||||
config
|
||||
)
|
||||
@@ -77,8 +77,6 @@ class TestImageProcessing:
|
||||
)
|
||||
def test_get_image_from_camera(self, mock_camera):
|
||||
"""Grab an image from camera entity."""
|
||||
self.hass.start()
|
||||
|
||||
common.scan(self.hass, entity_id="image_processing.test")
|
||||
self.hass.block_till_done()
|
||||
|
||||
|
||||
@@ -240,14 +240,13 @@ async def test_reload(hass, hass_admin_user):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
|
||||
@@ -345,21 +345,20 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
|
||||
@@ -338,21 +338,20 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
|
||||
@@ -415,21 +415,20 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
|
||||
@@ -288,21 +288,20 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
self.hass = get_test_home_assistant()
|
||||
init_recorder_component(self.hass) # Force an in memory DB
|
||||
assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG)
|
||||
self.hass.start()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
@@ -90,7 +89,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
dt_util.utcnow() + timedelta(hours=1),
|
||||
)
|
||||
)
|
||||
assert len(events) == 2
|
||||
assert len(events) == 1
|
||||
|
||||
assert 1 == len(calls)
|
||||
last_call = calls[-1]
|
||||
|
||||
@@ -269,9 +269,6 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase):
|
||||
|
||||
entity_id = "alarm_control_panel.test"
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert STATE_ALARM_DISARMED == self.hass.states.get(entity_id).state
|
||||
|
||||
common.alarm_arm_home(self.hass, "abc")
|
||||
@@ -1471,9 +1468,6 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase):
|
||||
|
||||
entity_id = "alarm_control_panel.test"
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert STATE_ALARM_DISARMED == self.hass.states.get(entity_id).state
|
||||
|
||||
common.alarm_arm_home(self.hass, "def")
|
||||
|
||||
@@ -753,7 +753,7 @@ async def test_reload(hass, hass_admin_user):
|
||||
{"name": "Person 3", "id": "id-3"},
|
||||
]
|
||||
},
|
||||
), patch("homeassistant.config.find_config_file", return_value=""):
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
"""Tests for the Safe Mode integration."""
|
||||
@@ -0,0 +1,9 @@
|
||||
"""Tests for safe mode integration."""
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
async def test_works(hass):
|
||||
"""Test safe mode works."""
|
||||
assert await async_setup_component(hass, "safe_mode", {})
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
@@ -229,9 +229,8 @@ class TestScriptComponent(unittest.TestCase):
|
||||
"script": {"test2": {"sequence": [{"delay": {"seconds": 5}}]}}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
reload(self.hass)
|
||||
self.hass.block_till_done()
|
||||
reload(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.get(ENTITY_ID) is None
|
||||
assert not self.hass.services.has_service(script.DOMAIN, "test")
|
||||
@@ -262,7 +261,6 @@ async def test_service_descriptions(hass):
|
||||
assert not descriptions[DOMAIN]["test"]["fields"]
|
||||
|
||||
# Test 2: has "fields" but no "description"
|
||||
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
|
||||
with patch(
|
||||
"homeassistant.config.load_yaml_config_file",
|
||||
return_value={
|
||||
@@ -279,8 +277,7 @@ async def test_service_descriptions(hass):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
|
||||
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
|
||||
|
||||
descriptions = await async_get_all_descriptions(hass)
|
||||
|
||||
|
||||
@@ -259,21 +259,20 @@ async def test_config_reload(hass, hass_admin_user, hass_read_only_user):
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ from tests.common import (
|
||||
get_test_home_assistant,
|
||||
get_test_instance_port,
|
||||
mock_service,
|
||||
mock_storage,
|
||||
)
|
||||
|
||||
|
||||
@@ -45,6 +46,8 @@ class TestTTS:
|
||||
self.hass = get_test_home_assistant()
|
||||
self.demo_provider = DemoProvider("en")
|
||||
self.default_tts_cache = self.hass.config.path(tts.DEFAULT_CACHE_DIR)
|
||||
self.mock_storage = mock_storage()
|
||||
self.mock_storage.__enter__()
|
||||
|
||||
setup_component(
|
||||
self.hass,
|
||||
@@ -55,6 +58,7 @@ class TestTTS:
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
self.mock_storage.__exit__(None, None, None)
|
||||
|
||||
if os.path.isdir(self.default_tts_cache):
|
||||
shutil.rmtree(self.default_tts_cache)
|
||||
|
||||
@@ -120,7 +120,7 @@ def test_secrets(isfile_patch, loop):
|
||||
|
||||
@patch("os.path.isfile", return_value=True)
|
||||
def test_package_invalid(isfile_patch, loop):
|
||||
"""Test a valid platform setup."""
|
||||
"""Test an invalid package."""
|
||||
files = {
|
||||
YAML_CONFIG_FILE: BASE_CONFIG + (" packages:\n p1:\n" ' group: ["a"]')
|
||||
}
|
||||
|
||||
+173
-49
@@ -3,18 +3,23 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import Mock
|
||||
|
||||
from asynctest import patch
|
||||
import pytest
|
||||
|
||||
from homeassistant import bootstrap
|
||||
import homeassistant.config as config_util
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
MockConfigEntry,
|
||||
MockModule,
|
||||
flush_store,
|
||||
get_test_config_dir,
|
||||
mock_coro,
|
||||
mock_integration,
|
||||
patch_yaml_files,
|
||||
)
|
||||
|
||||
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
|
||||
@@ -23,26 +28,6 @@ VERSION_PATH = os.path.join(get_test_config_dir(), config_util.VERSION_FILE)
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# prevent .HA_VERSION file from being written
|
||||
@patch("homeassistant.bootstrap.conf_util.process_ha_config_upgrade", Mock())
|
||||
@patch(
|
||||
"homeassistant.util.location.async_detect_location_info",
|
||||
Mock(return_value=mock_coro(None)),
|
||||
)
|
||||
@patch("os.path.isfile", Mock(return_value=True))
|
||||
@patch("os.access", Mock(return_value=True))
|
||||
@patch("homeassistant.bootstrap.async_enable_logging", Mock(return_value=True))
|
||||
def test_from_config_file(hass):
|
||||
"""Test with configuration file."""
|
||||
components = set(["browser", "conversation", "script"])
|
||||
files = {"config.yaml": "".join(f"{comp}:\n" for comp in components)}
|
||||
|
||||
with patch_yaml_files(files, True):
|
||||
yield from bootstrap.async_from_config_file("config.yaml", hass)
|
||||
|
||||
assert components == hass.config.components
|
||||
|
||||
|
||||
@patch("homeassistant.bootstrap.async_enable_logging", Mock())
|
||||
@asyncio.coroutine
|
||||
def test_home_assistant_core_config_validation(hass):
|
||||
@@ -54,33 +39,6 @@ def test_home_assistant_core_config_validation(hass):
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_async_from_config_file_not_mount_deps_folder(loop):
|
||||
"""Test that we not mount the deps folder inside async_from_config_file."""
|
||||
hass = Mock(async_add_executor_job=Mock(side_effect=lambda *args: mock_coro()))
|
||||
|
||||
with patch("homeassistant.bootstrap.is_virtual_env", return_value=False), patch(
|
||||
"homeassistant.bootstrap.async_enable_logging", return_value=mock_coro()
|
||||
), patch(
|
||||
"homeassistant.bootstrap.async_mount_local_lib_path", return_value=mock_coro()
|
||||
) as mock_mount, patch(
|
||||
"homeassistant.bootstrap.async_from_config_dict", return_value=mock_coro()
|
||||
):
|
||||
|
||||
await bootstrap.async_from_config_file("mock-path", hass)
|
||||
assert len(mock_mount.mock_calls) == 1
|
||||
|
||||
with patch("homeassistant.bootstrap.is_virtual_env", return_value=True), patch(
|
||||
"homeassistant.bootstrap.async_enable_logging", return_value=mock_coro()
|
||||
), patch(
|
||||
"homeassistant.bootstrap.async_mount_local_lib_path", return_value=mock_coro()
|
||||
) as mock_mount, patch(
|
||||
"homeassistant.bootstrap.async_from_config_dict", return_value=mock_coro()
|
||||
):
|
||||
|
||||
await bootstrap.async_from_config_file("mock-path", hass)
|
||||
assert len(mock_mount.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_load_hassio(hass):
|
||||
"""Test that we load Hass.io component."""
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
@@ -233,3 +191,169 @@ async def test_setup_after_deps_not_present(hass, caplog):
|
||||
assert "first_dep" not in hass.config.components
|
||||
assert "second_dep" in hass.config.components
|
||||
assert order == ["root", "second_dep"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_is_virtual_env():
|
||||
"""Mock enable logging."""
|
||||
with patch(
|
||||
"homeassistant.bootstrap.is_virtual_env", return_value=False
|
||||
) as is_virtual_env:
|
||||
yield is_virtual_env
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_enable_logging():
|
||||
"""Mock enable logging."""
|
||||
with patch("homeassistant.bootstrap.async_enable_logging") as enable_logging:
|
||||
yield enable_logging
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_mount_local_lib_path():
|
||||
"""Mock enable logging."""
|
||||
with patch(
|
||||
"homeassistant.bootstrap.async_mount_local_lib_path"
|
||||
) as mount_local_lib_path:
|
||||
yield mount_local_lib_path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_process_ha_config_upgrade():
|
||||
"""Mock enable logging."""
|
||||
with patch(
|
||||
"homeassistant.config.process_ha_config_upgrade"
|
||||
) as process_ha_config_upgrade:
|
||||
yield process_ha_config_upgrade
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ensure_config_exists():
|
||||
"""Mock enable logging."""
|
||||
with patch(
|
||||
"homeassistant.config.async_ensure_config_exists", return_value=True
|
||||
) as ensure_config_exists:
|
||||
yield ensure_config_exists
|
||||
|
||||
|
||||
async def test_setup_hass(
|
||||
mock_enable_logging,
|
||||
mock_is_virtual_env,
|
||||
mock_mount_local_lib_path,
|
||||
mock_ensure_config_exists,
|
||||
mock_process_ha_config_upgrade,
|
||||
):
|
||||
"""Test it works."""
|
||||
verbose = Mock()
|
||||
log_rotate_days = Mock()
|
||||
log_file = Mock()
|
||||
log_no_color = Mock()
|
||||
|
||||
with patch(
|
||||
"homeassistant.config.async_hass_config_yaml", return_value={"browser": {}}
|
||||
):
|
||||
hass = await bootstrap.async_setup_hass(
|
||||
config_dir=get_test_config_dir(),
|
||||
verbose=verbose,
|
||||
log_rotate_days=log_rotate_days,
|
||||
log_file=log_file,
|
||||
log_no_color=log_no_color,
|
||||
skip_pip=True,
|
||||
safe_mode=False,
|
||||
)
|
||||
|
||||
assert "browser" in hass.config.components
|
||||
|
||||
assert len(mock_enable_logging.mock_calls) == 1
|
||||
assert mock_enable_logging.mock_calls[0][1] == (
|
||||
hass,
|
||||
verbose,
|
||||
log_rotate_days,
|
||||
log_file,
|
||||
log_no_color,
|
||||
)
|
||||
assert len(mock_mount_local_lib_path.mock_calls) == 1
|
||||
assert len(mock_ensure_config_exists.mock_calls) == 1
|
||||
assert len(mock_process_ha_config_upgrade.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_hass_invalid_yaml(
|
||||
mock_enable_logging,
|
||||
mock_is_virtual_env,
|
||||
mock_mount_local_lib_path,
|
||||
mock_ensure_config_exists,
|
||||
mock_process_ha_config_upgrade,
|
||||
):
|
||||
"""Test it works."""
|
||||
with patch(
|
||||
"homeassistant.config.async_hass_config_yaml", side_effect=HomeAssistantError
|
||||
):
|
||||
hass = await bootstrap.async_setup_hass(
|
||||
config_dir=get_test_config_dir(),
|
||||
verbose=False,
|
||||
log_rotate_days=10,
|
||||
log_file="",
|
||||
log_no_color=False,
|
||||
skip_pip=True,
|
||||
safe_mode=False,
|
||||
)
|
||||
|
||||
assert "safe_mode" in hass.config.components
|
||||
assert len(mock_mount_local_lib_path.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_setup_hass_config_dir_nonexistent(
|
||||
mock_enable_logging,
|
||||
mock_is_virtual_env,
|
||||
mock_mount_local_lib_path,
|
||||
mock_ensure_config_exists,
|
||||
mock_process_ha_config_upgrade,
|
||||
):
|
||||
"""Test it works."""
|
||||
mock_ensure_config_exists.return_value = False
|
||||
|
||||
assert (
|
||||
await bootstrap.async_setup_hass(
|
||||
config_dir=get_test_config_dir(),
|
||||
verbose=False,
|
||||
log_rotate_days=10,
|
||||
log_file="",
|
||||
log_no_color=False,
|
||||
skip_pip=True,
|
||||
safe_mode=False,
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
async def test_setup_hass_safe_mode(
|
||||
hass,
|
||||
mock_enable_logging,
|
||||
mock_is_virtual_env,
|
||||
mock_mount_local_lib_path,
|
||||
mock_ensure_config_exists,
|
||||
mock_process_ha_config_upgrade,
|
||||
):
|
||||
"""Test it works."""
|
||||
# Add a config entry to storage.
|
||||
MockConfigEntry(domain="browser").add_to_hass(hass)
|
||||
hass.config_entries._async_schedule_save()
|
||||
await flush_store(hass.config_entries._store)
|
||||
|
||||
with patch("homeassistant.components.browser.setup") as browser_setup:
|
||||
hass = await bootstrap.async_setup_hass(
|
||||
config_dir=get_test_config_dir(),
|
||||
verbose=False,
|
||||
log_rotate_days=10,
|
||||
log_file="",
|
||||
log_no_color=False,
|
||||
skip_pip=True,
|
||||
safe_mode=True,
|
||||
)
|
||||
|
||||
assert "safe_mode" in hass.config.components
|
||||
assert len(mock_mount_local_lib_path.mock_calls) == 0
|
||||
|
||||
# Validate we didn't try to set up config entry.
|
||||
assert "browser" not in hass.config.components
|
||||
assert len(browser_setup.mock_calls) == 0
|
||||
+5
-17
@@ -82,7 +82,7 @@ def teardown():
|
||||
|
||||
async def test_create_default_config(hass):
|
||||
"""Test creation of default config."""
|
||||
await config_util.async_create_default_config(hass, CONFIG_DIR)
|
||||
await config_util.async_create_default_config(hass)
|
||||
|
||||
assert os.path.isfile(YAML_PATH)
|
||||
assert os.path.isfile(SECRET_PATH)
|
||||
@@ -91,20 +91,13 @@ async def test_create_default_config(hass):
|
||||
assert os.path.isfile(AUTOMATIONS_PATH)
|
||||
|
||||
|
||||
def test_find_config_file_yaml():
|
||||
"""Test if it finds a YAML config file."""
|
||||
create_file(YAML_PATH)
|
||||
|
||||
assert YAML_PATH == config_util.find_config_file(CONFIG_DIR)
|
||||
|
||||
|
||||
async def test_ensure_config_exists_creates_config(hass):
|
||||
"""Test that calling ensure_config_exists.
|
||||
|
||||
If not creates a new config file.
|
||||
"""
|
||||
with mock.patch("builtins.print") as mock_print:
|
||||
await config_util.async_ensure_config_exists(hass, CONFIG_DIR)
|
||||
await config_util.async_ensure_config_exists(hass)
|
||||
|
||||
assert os.path.isfile(YAML_PATH)
|
||||
assert mock_print.called
|
||||
@@ -113,7 +106,7 @@ async def test_ensure_config_exists_creates_config(hass):
|
||||
async def test_ensure_config_exists_uses_existing_config(hass):
|
||||
"""Test that calling ensure_config_exists uses existing config."""
|
||||
create_file(YAML_PATH)
|
||||
await config_util.async_ensure_config_exists(hass, CONFIG_DIR)
|
||||
await config_util.async_ensure_config_exists(hass)
|
||||
|
||||
with open(YAML_PATH) as f:
|
||||
content = f.read()
|
||||
@@ -172,13 +165,9 @@ async def test_create_default_config_returns_none_if_write_error(hass):
|
||||
|
||||
Non existing folder returns None.
|
||||
"""
|
||||
hass.config.config_dir = os.path.join(CONFIG_DIR, "non_existing_dir/")
|
||||
with mock.patch("builtins.print") as mock_print:
|
||||
assert (
|
||||
await config_util.async_create_default_config(
|
||||
hass, os.path.join(CONFIG_DIR, "non_existing_dir/")
|
||||
)
|
||||
is None
|
||||
)
|
||||
assert await config_util.async_create_default_config(hass) is False
|
||||
assert mock_print.called
|
||||
|
||||
|
||||
@@ -331,7 +320,6 @@ def test_config_upgrade_same_version(hass):
|
||||
assert opened_file.write.call_count == 0
|
||||
|
||||
|
||||
@mock.patch("homeassistant.config.find_config_file", mock.Mock())
|
||||
def test_config_upgrade_no_file(hass):
|
||||
"""Test update of version on upgrade, with no version file."""
|
||||
mock_open = mock.mock_open()
|
||||
|
||||
Reference in new issue
Block a user