Fix config imports (#27669)
* Fix config imports * Remove old migration * Remove migrate testspull/27734/head
parent
c700085490
commit
93f9afcd21
|
@ -5,12 +5,11 @@ import uuid
|
||||||
from homeassistant.components.automation import DOMAIN, PLATFORM_SCHEMA
|
from homeassistant.components.automation import DOMAIN, PLATFORM_SCHEMA
|
||||||
from homeassistant.components.automation.config import async_validate_config_item
|
from homeassistant.components.automation.config import async_validate_config_item
|
||||||
from homeassistant.const import CONF_ID, SERVICE_RELOAD
|
from homeassistant.const import CONF_ID, SERVICE_RELOAD
|
||||||
|
from homeassistant.config import AUTOMATION_CONFIG_PATH
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import EditIdBasedConfigView
|
from . import EditIdBasedConfigView
|
||||||
|
|
||||||
CONFIG_PATH = "automations.yaml"
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass):
|
async def async_setup(hass):
|
||||||
"""Set up the Automation config API."""
|
"""Set up the Automation config API."""
|
||||||
|
@ -23,7 +22,7 @@ async def async_setup(hass):
|
||||||
EditAutomationConfigView(
|
EditAutomationConfigView(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
"config",
|
"config",
|
||||||
CONFIG_PATH,
|
AUTOMATION_CONFIG_PATH,
|
||||||
cv.string,
|
cv.string,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
post_write_hook=hook,
|
post_write_hook=hook,
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
"""Provide configuration end points for Groups."""
|
"""Provide configuration end points for Groups."""
|
||||||
from homeassistant.components.group import DOMAIN, GROUP_SCHEMA
|
from homeassistant.components.group import DOMAIN, GROUP_SCHEMA
|
||||||
from homeassistant.const import SERVICE_RELOAD
|
from homeassistant.const import SERVICE_RELOAD
|
||||||
|
from homeassistant.config import GROUP_CONFIG_PATH
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import EditKeyBasedConfigView
|
from . import EditKeyBasedConfigView
|
||||||
|
|
||||||
CONFIG_PATH = "groups.yaml"
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass):
|
async def async_setup(hass):
|
||||||
"""Set up the Group config API."""
|
"""Set up the Group config API."""
|
||||||
|
@ -17,7 +16,12 @@ async def async_setup(hass):
|
||||||
|
|
||||||
hass.http.register_view(
|
hass.http.register_view(
|
||||||
EditKeyBasedConfigView(
|
EditKeyBasedConfigView(
|
||||||
"group", "config", CONFIG_PATH, cv.slug, GROUP_SCHEMA, post_write_hook=hook
|
"group",
|
||||||
|
"config",
|
||||||
|
GROUP_CONFIG_PATH,
|
||||||
|
cv.slug,
|
||||||
|
GROUP_SCHEMA,
|
||||||
|
post_write_hook=hook,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
"""Provide configuration end points for scripts."""
|
"""Provide configuration end points for scripts."""
|
||||||
from homeassistant.components.script import DOMAIN, SCRIPT_ENTRY_SCHEMA
|
from homeassistant.components.script import DOMAIN, SCRIPT_ENTRY_SCHEMA
|
||||||
from homeassistant.const import SERVICE_RELOAD
|
from homeassistant.const import SERVICE_RELOAD
|
||||||
|
from homeassistant.config import SCRIPT_CONFIG_PATH
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import EditKeyBasedConfigView
|
from . import EditKeyBasedConfigView
|
||||||
|
|
||||||
CONFIG_PATH = "scripts.yaml"
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass):
|
async def async_setup(hass):
|
||||||
"""Set up the script config API."""
|
"""Set up the script config API."""
|
||||||
|
@ -19,7 +18,7 @@ async def async_setup(hass):
|
||||||
EditKeyBasedConfigView(
|
EditKeyBasedConfigView(
|
||||||
"script",
|
"script",
|
||||||
"config",
|
"config",
|
||||||
CONFIG_PATH,
|
SCRIPT_CONFIG_PATH,
|
||||||
cv.slug,
|
cv.slug,
|
||||||
SCRIPT_ENTRY_SCHEMA,
|
SCRIPT_ENTRY_SCHEMA,
|
||||||
post_write_hook=hook,
|
post_write_hook=hook,
|
||||||
|
|
|
@ -66,9 +66,11 @@ VERSION_FILE = ".HA_VERSION"
|
||||||
CONFIG_DIR_NAME = ".homeassistant"
|
CONFIG_DIR_NAME = ".homeassistant"
|
||||||
DATA_CUSTOMIZE = "hass_customize"
|
DATA_CUSTOMIZE = "hass_customize"
|
||||||
|
|
||||||
FILE_MIGRATION = (("ios.conf", ".ios.conf"),)
|
GROUP_CONFIG_PATH = "groups.yaml"
|
||||||
|
AUTOMATION_CONFIG_PATH = "automations.yaml"
|
||||||
|
SCRIPT_CONFIG_PATH = "scripts.yaml"
|
||||||
|
|
||||||
DEFAULT_CONFIG = """
|
DEFAULT_CONFIG = f"""
|
||||||
# Configure a default setup of Home Assistant (frontend, api, etc)
|
# Configure a default setup of Home Assistant (frontend, api, etc)
|
||||||
default_config:
|
default_config:
|
||||||
|
|
||||||
|
@ -80,9 +82,9 @@ default_config:
|
||||||
tts:
|
tts:
|
||||||
- platform: google_translate
|
- platform: google_translate
|
||||||
|
|
||||||
group: !include groups.yaml
|
group: !include {GROUP_CONFIG_PATH}
|
||||||
automation: !include automations.yaml
|
automation: !include {AUTOMATION_CONFIG_PATH}
|
||||||
script: !include scripts.yaml
|
script: !include {SCRIPT_CONFIG_PATH}
|
||||||
"""
|
"""
|
||||||
DEFAULT_SECRETS = """
|
DEFAULT_SECRETS = """
|
||||||
# Use this file to store secrets like usernames and passwords.
|
# Use this file to store secrets like usernames and passwords.
|
||||||
|
@ -253,12 +255,6 @@ async def async_create_default_config(
|
||||||
|
|
||||||
def _write_default_config(config_dir: str) -> Optional[str]:
|
def _write_default_config(config_dir: str) -> Optional[str]:
|
||||||
"""Write the default config."""
|
"""Write the default config."""
|
||||||
from homeassistant.components.config.group import CONFIG_PATH as GROUP_CONFIG_PATH
|
|
||||||
from homeassistant.components.config.automation import (
|
|
||||||
CONFIG_PATH as AUTOMATION_CONFIG_PATH,
|
|
||||||
)
|
|
||||||
from homeassistant.components.config.script import CONFIG_PATH as SCRIPT_CONFIG_PATH
|
|
||||||
|
|
||||||
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
|
config_path = os.path.join(config_dir, YAML_CONFIG_FILE)
|
||||||
secret_path = os.path.join(config_dir, SECRET_YAML)
|
secret_path = os.path.join(config_dir, SECRET_YAML)
|
||||||
version_path = os.path.join(config_dir, VERSION_FILE)
|
version_path = os.path.join(config_dir, VERSION_FILE)
|
||||||
|
@ -407,12 +403,6 @@ def process_ha_config_upgrade(hass: HomeAssistant) -> None:
|
||||||
with open(version_path, "wt") as outp:
|
with open(version_path, "wt") as outp:
|
||||||
outp.write(__version__)
|
outp.write(__version__)
|
||||||
|
|
||||||
_LOGGER.debug("Migrating old system configuration files to new locations")
|
|
||||||
for oldf, newf in FILE_MIGRATION:
|
|
||||||
if os.path.isfile(hass.config.path(oldf)):
|
|
||||||
_LOGGER.info("Migrating %s to %s", oldf, newf)
|
|
||||||
os.rename(hass.config.path(oldf), hass.config.path(newf))
|
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_log_exception(
|
def async_log_exception(
|
||||||
|
|
|
@ -33,11 +33,6 @@ from homeassistant.const import (
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
from homeassistant.util.yaml import SECRET_YAML
|
from homeassistant.util.yaml import SECRET_YAML
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.components.config.group import CONFIG_PATH as GROUP_CONFIG_PATH
|
|
||||||
from homeassistant.components.config.automation import (
|
|
||||||
CONFIG_PATH as AUTOMATIONS_CONFIG_PATH,
|
|
||||||
)
|
|
||||||
from homeassistant.components.config.script import CONFIG_PATH as SCRIPTS_CONFIG_PATH
|
|
||||||
import homeassistant.helpers.check_config as check_config
|
import homeassistant.helpers.check_config as check_config
|
||||||
|
|
||||||
from tests.common import get_test_config_dir, patch_yaml_files
|
from tests.common import get_test_config_dir, patch_yaml_files
|
||||||
|
@ -46,9 +41,9 @@ CONFIG_DIR = get_test_config_dir()
|
||||||
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
|
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
|
||||||
SECRET_PATH = os.path.join(CONFIG_DIR, SECRET_YAML)
|
SECRET_PATH = os.path.join(CONFIG_DIR, SECRET_YAML)
|
||||||
VERSION_PATH = os.path.join(CONFIG_DIR, config_util.VERSION_FILE)
|
VERSION_PATH = os.path.join(CONFIG_DIR, config_util.VERSION_FILE)
|
||||||
GROUP_PATH = os.path.join(CONFIG_DIR, GROUP_CONFIG_PATH)
|
GROUP_PATH = os.path.join(CONFIG_DIR, config_util.GROUP_CONFIG_PATH)
|
||||||
AUTOMATIONS_PATH = os.path.join(CONFIG_DIR, AUTOMATIONS_CONFIG_PATH)
|
AUTOMATIONS_PATH = os.path.join(CONFIG_DIR, config_util.AUTOMATION_CONFIG_PATH)
|
||||||
SCRIPTS_PATH = os.path.join(CONFIG_DIR, SCRIPTS_CONFIG_PATH)
|
SCRIPTS_PATH = os.path.join(CONFIG_DIR, config_util.SCRIPT_CONFIG_PATH)
|
||||||
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
|
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
|
||||||
|
|
||||||
|
|
||||||
|
@ -345,62 +340,6 @@ def test_config_upgrade_no_file(hass):
|
||||||
assert opened_file.write.call_args == mock.call(__version__)
|
assert opened_file.write.call_args == mock.call(__version__)
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("homeassistant.config.shutil")
|
|
||||||
@mock.patch("homeassistant.config.os")
|
|
||||||
@mock.patch("homeassistant.config.find_config_file", mock.Mock())
|
|
||||||
def test_migrate_file_on_upgrade(mock_os, mock_shutil, hass):
|
|
||||||
"""Test migrate of config files on upgrade."""
|
|
||||||
ha_version = "0.7.0"
|
|
||||||
|
|
||||||
mock_os.path.isdir = mock.Mock(return_value=True)
|
|
||||||
|
|
||||||
mock_open = mock.mock_open()
|
|
||||||
|
|
||||||
def _mock_isfile(filename):
|
|
||||||
return True
|
|
||||||
|
|
||||||
with mock.patch("homeassistant.config.open", mock_open, create=True), mock.patch(
|
|
||||||
"homeassistant.config.os.path.isfile", _mock_isfile
|
|
||||||
):
|
|
||||||
opened_file = mock_open.return_value
|
|
||||||
# pylint: disable=no-member
|
|
||||||
opened_file.readline.return_value = ha_version
|
|
||||||
|
|
||||||
hass.config.path = mock.Mock()
|
|
||||||
|
|
||||||
config_util.process_ha_config_upgrade(hass)
|
|
||||||
|
|
||||||
assert mock_os.rename.call_count == 1
|
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("homeassistant.config.shutil")
|
|
||||||
@mock.patch("homeassistant.config.os")
|
|
||||||
@mock.patch("homeassistant.config.find_config_file", mock.Mock())
|
|
||||||
def test_migrate_no_file_on_upgrade(mock_os, mock_shutil, hass):
|
|
||||||
"""Test not migrating config files on upgrade."""
|
|
||||||
ha_version = "0.7.0"
|
|
||||||
|
|
||||||
mock_os.path.isdir = mock.Mock(return_value=True)
|
|
||||||
|
|
||||||
mock_open = mock.mock_open()
|
|
||||||
|
|
||||||
def _mock_isfile(filename):
|
|
||||||
return False
|
|
||||||
|
|
||||||
with mock.patch("homeassistant.config.open", mock_open, create=True), mock.patch(
|
|
||||||
"homeassistant.config.os.path.isfile", _mock_isfile
|
|
||||||
):
|
|
||||||
opened_file = mock_open.return_value
|
|
||||||
# pylint: disable=no-member
|
|
||||||
opened_file.readline.return_value = ha_version
|
|
||||||
|
|
||||||
hass.config.path = mock.Mock()
|
|
||||||
|
|
||||||
config_util.process_ha_config_upgrade(hass)
|
|
||||||
|
|
||||||
assert mock_os.rename.call_count == 0
|
|
||||||
|
|
||||||
|
|
||||||
async def test_loading_configuration_from_storage(hass, hass_storage):
|
async def test_loading_configuration_from_storage(hass, hass_storage):
|
||||||
"""Test loading core config onto hass object."""
|
"""Test loading core config onto hass object."""
|
||||||
hass_storage["core.config"] = {
|
hass_storage["core.config"] = {
|
||||||
|
|
Loading…
Reference in New Issue