No longer rely on requests (#23685)

* No longer rely on requests

* Lint

* Missed a few parts

* Fix types

* Fix more types

* Update __main__.py

* Fix tests

* Lint

* Fix script
This commit is contained in:
Paulus Schoutsen 2019-05-08 11:15:04 -07:00 committed by GitHub
parent f019e2a204
commit cc13713abd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 271 additions and 197 deletions

View file

@ -34,7 +34,7 @@ from homeassistant.components.config.customize import (
import homeassistant.scripts.check_config as check_config
from tests.common import (
get_test_config_dir, patch_yaml_files)
get_test_config_dir, patch_yaml_files, mock_coro)
CONFIG_DIR = get_test_config_dir()
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
@ -79,9 +79,9 @@ def teardown():
os.remove(CUSTOMIZE_PATH)
def test_create_default_config():
async def test_create_default_config(hass):
"""Test creation of default config."""
config_util.create_default_config(CONFIG_DIR, False)
await config_util.async_create_default_config(hass, CONFIG_DIR, False)
assert os.path.isfile(YAML_PATH)
assert os.path.isfile(SECRET_PATH)
@ -98,22 +98,22 @@ def test_find_config_file_yaml():
assert YAML_PATH == config_util.find_config_file(CONFIG_DIR)
@mock.patch('builtins.print')
def test_ensure_config_exists_creates_config(mock_print):
async def test_ensure_config_exists_creates_config(hass):
"""Test that calling ensure_config_exists.
If not creates a new config file.
"""
config_util.ensure_config_exists(CONFIG_DIR, False)
with mock.patch('builtins.print') as mock_print:
await config_util.async_ensure_config_exists(hass, CONFIG_DIR, False)
assert os.path.isfile(YAML_PATH)
assert mock_print.called
def test_ensure_config_exists_uses_existing_config():
async def test_ensure_config_exists_uses_existing_config(hass):
"""Test that calling ensure_config_exists uses existing config."""
create_file(YAML_PATH)
config_util.ensure_config_exists(CONFIG_DIR, False)
await config_util.async_ensure_config_exists(hass, CONFIG_DIR, False)
with open(YAML_PATH) as f:
content = f.read()
@ -166,17 +166,17 @@ def test_load_yaml_config_preserves_key_order():
list(config_util.load_yaml_config_file(YAML_PATH).items())
@mock.patch('homeassistant.util.location.detect_location_info',
return_value=location_util.LocationInfo(
'0.0.0.0', 'US', 'United States', 'CA', 'California',
'San Diego', '92122', 'America/Los_Angeles', 32.8594,
-117.2073, True))
@mock.patch('homeassistant.util.location.elevation', return_value=101)
@mock.patch('builtins.print')
def test_create_default_config_detect_location(mock_detect,
mock_elev, mock_print):
async def test_create_default_config_detect_location(hass):
"""Test that detect location sets the correct config keys."""
config_util.ensure_config_exists(CONFIG_DIR)
with mock.patch('homeassistant.util.location.async_detect_location_info',
return_value=mock_coro(location_util.LocationInfo(
'0.0.0.0', 'US', 'United States', 'CA', 'California',
'San Diego', '92122', 'America/Los_Angeles', 32.8594,
-117.2073, True))), \
mock.patch('homeassistant.util.location.async_get_elevation',
return_value=mock_coro(101)), \
mock.patch('builtins.print') as mock_print:
await config_util.async_ensure_config_exists(hass, CONFIG_DIR)
config = config_util.load_yaml_config_file(YAML_PATH)
@ -198,14 +198,14 @@ def test_create_default_config_detect_location(mock_detect,
assert mock_print.called
@mock.patch('builtins.print')
def test_create_default_config_returns_none_if_write_error(mock_print):
async def test_create_default_config_returns_none_if_write_error(hass):
"""Test the writing of a default configuration.
Non existing folder returns None.
"""
assert config_util.create_default_config(
os.path.join(CONFIG_DIR, 'non_existing_dir/'), False) is None
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/'), False) is None
assert mock_print.called
@ -490,13 +490,14 @@ async def test_loading_configuration_from_packages(hass):
})
@asynctest.mock.patch('homeassistant.util.location.detect_location_info',
autospec=True, return_value=location_util.LocationInfo(
'0.0.0.0', 'US', 'United States', 'CA', 'California',
'San Diego', '92122', 'America/Los_Angeles', 32.8594,
-117.2073, True))
@asynctest.mock.patch('homeassistant.util.location.elevation',
autospec=True, return_value=101)
@asynctest.mock.patch(
'homeassistant.util.location.async_detect_location_info',
autospec=True, return_value=mock_coro(location_util.LocationInfo(
'0.0.0.0', 'US', 'United States', 'CA',
'California', 'San Diego', '92122',
'America/Los_Angeles', 32.8594, -117.2073, True)))
@asynctest.mock.patch('homeassistant.util.location.async_get_elevation',
autospec=True, return_value=mock_coro(101))
async def test_discovering_configuration(mock_detect, mock_elevation, hass):
"""Test auto discovery for missing core configs."""
hass.config.latitude = None
@ -516,9 +517,10 @@ async def test_discovering_configuration(mock_detect, mock_elevation, hass):
assert hass.config.time_zone.zone == 'America/Los_Angeles'
@asynctest.mock.patch('homeassistant.util.location.detect_location_info',
autospec=True, return_value=None)
@asynctest.mock.patch('homeassistant.util.location.elevation', return_value=0)
@asynctest.mock.patch('homeassistant.util.location.async_detect_location_info',
autospec=True, return_value=mock_coro(None))
@asynctest.mock.patch('homeassistant.util.location.async_get_elevation',
return_value=mock_coro(0))
async def test_discovering_configuration_auto_detect_fails(mock_detect,
mock_elevation,
hass):