Skip to content

Commit

Permalink
Fix config imports (home-assistant#27669)
Browse files Browse the repository at this point in the history
* Fix config imports

* Remove old migration

* Remove migrate tests
  • Loading branch information
balloob authored Oct 15, 2019
1 parent c700085 commit 93f9afc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 90 deletions.
5 changes: 2 additions & 3 deletions homeassistant/components/config/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from homeassistant.components.automation import DOMAIN, PLATFORM_SCHEMA
from homeassistant.components.automation.config import async_validate_config_item
from homeassistant.const import CONF_ID, SERVICE_RELOAD
from homeassistant.config import AUTOMATION_CONFIG_PATH
import homeassistant.helpers.config_validation as cv

from . import EditIdBasedConfigView

CONFIG_PATH = "automations.yaml"


async def async_setup(hass):
"""Set up the Automation config API."""
Expand All @@ -23,7 +22,7 @@ async def hook(hass):
EditAutomationConfigView(
DOMAIN,
"config",
CONFIG_PATH,
AUTOMATION_CONFIG_PATH,
cv.string,
PLATFORM_SCHEMA,
post_write_hook=hook,
Expand Down
10 changes: 7 additions & 3 deletions homeassistant/components/config/group.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Provide configuration end points for Groups."""
from homeassistant.components.group import DOMAIN, GROUP_SCHEMA
from homeassistant.const import SERVICE_RELOAD
from homeassistant.config import GROUP_CONFIG_PATH
import homeassistant.helpers.config_validation as cv

from . import EditKeyBasedConfigView

CONFIG_PATH = "groups.yaml"


async def async_setup(hass):
"""Set up the Group config API."""
Expand All @@ -17,7 +16,12 @@ async def hook(hass):

hass.http.register_view(
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
5 changes: 2 additions & 3 deletions homeassistant/components/config/script.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Provide configuration end points for scripts."""
from homeassistant.components.script import DOMAIN, SCRIPT_ENTRY_SCHEMA
from homeassistant.const import SERVICE_RELOAD
from homeassistant.config import SCRIPT_CONFIG_PATH
import homeassistant.helpers.config_validation as cv

from . import EditKeyBasedConfigView

CONFIG_PATH = "scripts.yaml"


async def async_setup(hass):
"""Set up the script config API."""
Expand All @@ -19,7 +18,7 @@ async def hook(hass):
EditKeyBasedConfigView(
"script",
"config",
CONFIG_PATH,
SCRIPT_CONFIG_PATH,
cv.slug,
SCRIPT_ENTRY_SCHEMA,
post_write_hook=hook,
Expand Down
24 changes: 7 additions & 17 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@
CONFIG_DIR_NAME = ".homeassistant"
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)
default_config:
Expand All @@ -80,9 +82,9 @@
tts:
- platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
group: !include {GROUP_CONFIG_PATH}
automation: !include {AUTOMATION_CONFIG_PATH}
script: !include {SCRIPT_CONFIG_PATH}
"""
DEFAULT_SECRETS = """
# Use this file to store secrets like usernames and passwords.
Expand Down Expand Up @@ -253,12 +255,6 @@ async def async_create_default_config(

def _write_default_config(config_dir: str) -> Optional[str]:
"""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)
secret_path = os.path.join(config_dir, SECRET_YAML)
version_path = os.path.join(config_dir, VERSION_FILE)
Expand Down Expand Up @@ -407,12 +403,6 @@ def process_ha_config_upgrade(hass: HomeAssistant) -> None:
with open(version_path, "wt") as outp:
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
def async_log_exception(
Expand Down
67 changes: 3 additions & 64 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
from homeassistant.util import dt as dt_util
from homeassistant.util.yaml import SECRET_YAML
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

from tests.common import get_test_config_dir, patch_yaml_files
Expand All @@ -46,9 +41,9 @@
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
SECRET_PATH = os.path.join(CONFIG_DIR, SECRET_YAML)
VERSION_PATH = os.path.join(CONFIG_DIR, config_util.VERSION_FILE)
GROUP_PATH = os.path.join(CONFIG_DIR, GROUP_CONFIG_PATH)
AUTOMATIONS_PATH = os.path.join(CONFIG_DIR, AUTOMATIONS_CONFIG_PATH)
SCRIPTS_PATH = os.path.join(CONFIG_DIR, SCRIPTS_CONFIG_PATH)
GROUP_PATH = os.path.join(CONFIG_DIR, config_util.GROUP_CONFIG_PATH)
AUTOMATIONS_PATH = os.path.join(CONFIG_DIR, config_util.AUTOMATION_CONFIG_PATH)
SCRIPTS_PATH = os.path.join(CONFIG_DIR, config_util.SCRIPT_CONFIG_PATH)
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE


Expand Down Expand Up @@ -345,62 +340,6 @@ def test_config_upgrade_no_file(hass):
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):
"""Test loading core config onto hass object."""
hass_storage["core.config"] = {
Expand Down

0 comments on commit 93f9afc

Please sign in to comment.