Skip to content

Commit

Permalink
Move elevation to core config and clean up HTTP mocking in tests (#2378)
Browse files Browse the repository at this point in the history
* Stick version numbers

* Move elevation to core config

* Migrate forecast test to requests-mock

* Migrate YR tests to requests-mock

* Add requests_mock to requirements_test.txt

* Move conf code from bootstrap to config

* More config fixes

* Fix some more issues

* Add test for set config and failing auto detect
  • Loading branch information
balloob authored Jun 27, 2016
1 parent dc75b28 commit 6714392
Show file tree
Hide file tree
Showing 26 changed files with 1,779 additions and 337 deletions.
129 changes: 14 additions & 115 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import logging.handlers
import os
import shutil
import sys
from collections import defaultdict
from threading import RLock
Expand All @@ -12,21 +11,15 @@

import homeassistant.components as core_components
from homeassistant.components import group, persistent_notification
import homeassistant.config as config_util
import homeassistant.config as conf_util
import homeassistant.core as core
import homeassistant.helpers.config_validation as cv
import homeassistant.loader as loader
import homeassistant.util.dt as date_util
import homeassistant.util.location as loc_util
import homeassistant.util.package as pkg_util
from homeassistant.const import (
CONF_CUSTOMIZE, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME,
CONF_TEMPERATURE_UNIT, CONF_TIME_ZONE, EVENT_COMPONENT_LOADED,
TEMP_CELSIUS, TEMP_FAHRENHEIT, PLATFORM_FORMAT, __version__)
from homeassistant.const import EVENT_COMPONENT_LOADED, PLATFORM_FORMAT
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import (
event_decorators, service, config_per_platform, extract_domain_configs,
entity)
event_decorators, service, config_per_platform, extract_domain_configs)

_LOGGER = logging.getLogger(__name__)
_SETUP_LOCK = RLock()
Expand Down Expand Up @@ -208,11 +201,6 @@ def prepare_setup_platform(hass, config, domain, platform_name):
return platform


def mount_local_lib_path(config_dir):
"""Add local library to Python Path."""
sys.path.insert(0, os.path.join(config_dir, 'deps'))


# pylint: disable=too-many-branches, too-many-statements, too-many-arguments
def from_config_dict(config, hass=None, config_dir=None, enable_log=True,
verbose=False, skip_pip=False,
Expand All @@ -226,18 +214,17 @@ def from_config_dict(config, hass=None, config_dir=None, enable_log=True,
if config_dir is not None:
config_dir = os.path.abspath(config_dir)
hass.config.config_dir = config_dir
mount_local_lib_path(config_dir)
_mount_local_lib_path(config_dir)

core_config = config.get(core.DOMAIN, {})

try:
process_ha_core_config(hass, config_util.CORE_CONFIG_SCHEMA(
core_config))
except vol.MultipleInvalid as ex:
conf_util.process_ha_core_config(hass, core_config)
except vol.Invalid as ex:
cv.log_exception(_LOGGER, ex, 'homeassistant', core_config)
return None

process_ha_config_upgrade(hass)
conf_util.process_ha_config_upgrade(hass)

if enable_log:
enable_logging(hass, verbose, log_rotate_days)
Expand Down Expand Up @@ -292,12 +279,12 @@ def from_config_file(config_path, hass=None, verbose=False, skip_pip=True,
# Set config dir to directory holding config file
config_dir = os.path.abspath(os.path.dirname(config_path))
hass.config.config_dir = config_dir
mount_local_lib_path(config_dir)
_mount_local_lib_path(config_dir)

enable_logging(hass, verbose, log_rotate_days)

try:
config_dict = config_util.load_yaml_config_file(config_path)
config_dict = conf_util.load_yaml_config_file(config_path)
except HomeAssistantError:
return None

Expand Down Expand Up @@ -356,100 +343,12 @@ def enable_logging(hass, verbose=False, log_rotate_days=None):
'Unable to setup error log %s (access denied)', err_log_path)


def process_ha_config_upgrade(hass):
"""Upgrade config if necessary."""
version_path = hass.config.path('.HA_VERSION')

try:
with open(version_path, 'rt') as inp:
conf_version = inp.readline().strip()
except FileNotFoundError:
# Last version to not have this file
conf_version = '0.7.7'

if conf_version == __version__:
return

_LOGGER.info('Upgrading config directory from %s to %s', conf_version,
__version__)

# This was where dependencies were installed before v0.18
# Probably should keep this around until ~v0.20.
lib_path = hass.config.path('lib')
if os.path.isdir(lib_path):
shutil.rmtree(lib_path)

lib_path = hass.config.path('deps')
if os.path.isdir(lib_path):
shutil.rmtree(lib_path)

with open(version_path, 'wt') as outp:
outp.write(__version__)


def process_ha_core_config(hass, config):
"""Process the [homeassistant] section from the config."""
hac = hass.config

def set_time_zone(time_zone_str):
"""Helper method to set time zone."""
if time_zone_str is None:
return

time_zone = date_util.get_time_zone(time_zone_str)

if time_zone:
hac.time_zone = time_zone
date_util.set_default_time_zone(time_zone)
else:
_LOGGER.error('Received invalid time zone %s', time_zone_str)

for key, attr in ((CONF_LATITUDE, 'latitude'),
(CONF_LONGITUDE, 'longitude'),
(CONF_NAME, 'location_name')):
if key in config:
setattr(hac, attr, config[key])

if CONF_TIME_ZONE in config:
set_time_zone(config.get(CONF_TIME_ZONE))

entity.set_customize(config.get(CONF_CUSTOMIZE))

if CONF_TEMPERATURE_UNIT in config:
hac.temperature_unit = config[CONF_TEMPERATURE_UNIT]

# If we miss some of the needed values, auto detect them
if None not in (
hac.latitude, hac.longitude, hac.temperature_unit, hac.time_zone):
return

_LOGGER.warning('Incomplete core config. Auto detecting location and '
'temperature unit')

info = loc_util.detect_location_info()

if info is None:
_LOGGER.error('Could not detect location information')
return

if hac.latitude is None and hac.longitude is None:
hac.latitude = info.latitude
hac.longitude = info.longitude

if hac.temperature_unit is None:
if info.use_fahrenheit:
hac.temperature_unit = TEMP_FAHRENHEIT
else:
hac.temperature_unit = TEMP_CELSIUS

if hac.location_name is None:
hac.location_name = info.city

if hac.time_zone is None:
set_time_zone(info.time_zone)


def _ensure_loader_prepared(hass):
"""Ensure Home Assistant loader is prepared."""
if not loader.PREPARED:
loader.prepare(hass)


def _mount_local_lib_path(config_dir):
"""Add local library to Python Path."""
sys.path.insert(0, os.path.join(config_dir, 'deps'))
8 changes: 4 additions & 4 deletions homeassistant/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ def handle_turn_service(service):
def handle_reload_config(call):
"""Service handler for reloading core config."""
from homeassistant.exceptions import HomeAssistantError
from homeassistant import config, bootstrap
from homeassistant import config as conf_util

try:
path = config.find_config_file(hass.config.config_dir)
conf = config.load_yaml_config_file(path)
path = conf_util.find_config_file(hass.config.config_dir)
conf = conf_util.load_yaml_config_file(path)
except HomeAssistantError as err:
_LOGGER.error(err)
return

bootstrap.process_ha_core_config(hass, conf.get(ha.DOMAIN) or {})
conf_util.process_ha_core_config(hass, conf.get(ha.DOMAIN) or {})

hass.services.register(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG,
handle_reload_config)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/media_player/cmus.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
CONF_PORT)

_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pycmus>=0.1.0']
REQUIREMENTS = ['pycmus==0.1.0']

SUPPORT_CMUS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
SUPPORT_TURN_ON | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
Expand Down
7 changes: 1 addition & 6 deletions homeassistant/components/sensor/yr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
)
from homeassistant.helpers.entity import Entity
from homeassistant.util import dt as dt_util
from homeassistant.util import location

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,16 +53,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Yr.no sensor."""
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
elevation = config.get(CONF_ELEVATION)
elevation = config.get(CONF_ELEVATION, hass.config.elevation or 0)

if None in (latitude, longitude):
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
return False

if elevation is None:
elevation = location.elevation(latitude,
longitude)

coordinates = dict(lat=latitude,
lon=longitude,
msl=elevation)
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from homeassistant.helpers.event import (
track_point_in_utc_time, track_utc_time_change)
from homeassistant.util import dt as dt_util
from homeassistant.util import location as location_util
from homeassistant.const import CONF_ELEVATION

REQUIREMENTS = ['astral==1.2']
Expand Down Expand Up @@ -108,7 +107,7 @@ def setup(hass, config):

elevation = platform_config.get(CONF_ELEVATION)
if elevation is None:
elevation = location_util.elevation(latitude, longitude)
elevation = hass.config.elevation or 0

from astral import Location

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/thermostat/eq3btsmart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.const import TEMP_CELCIUS
from homeassistant.helpers.temperature import convert

REQUIREMENTS = ['bluepy_devices>=0.2.0']
REQUIREMENTS = ['bluepy_devices==0.2.0']

CONF_MAC = 'mac'
CONF_DEVICES = 'devices'
Expand Down
Loading

0 comments on commit 6714392

Please sign in to comment.