2016-03-09 09:25:50 +00:00
|
|
|
"""Test the helper method for writing tests."""
|
2016-09-13 02:16:14 +00:00
|
|
|
import asyncio
|
2014-12-01 07:14:08 +00:00
|
|
|
import os
|
2016-11-02 20:53:52 +00:00
|
|
|
import sys
|
2015-04-30 05:26:54 +00:00
|
|
|
from datetime import timedelta
|
2016-11-27 02:23:28 +00:00
|
|
|
from unittest.mock import patch, MagicMock
|
2016-08-23 04:42:05 +00:00
|
|
|
from io import StringIO
|
|
|
|
import logging
|
2016-09-13 02:16:14 +00:00
|
|
|
import threading
|
2016-10-08 18:27:35 +00:00
|
|
|
from contextlib import contextmanager
|
2014-12-01 07:14:08 +00:00
|
|
|
|
2016-11-25 21:04:06 +00:00
|
|
|
from aiohttp import web
|
|
|
|
|
2015-09-01 07:18:26 +00:00
|
|
|
from homeassistant import core as ha, loader
|
2016-10-27 07:16:23 +00:00
|
|
|
from homeassistant.bootstrap import (
|
|
|
|
setup_component, async_prepare_setup_component)
|
2015-03-22 02:37:18 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2016-08-09 03:42:25 +00:00
|
|
|
from homeassistant.util.unit_system import METRIC_SYSTEM
|
2016-04-08 01:32:21 +00:00
|
|
|
import homeassistant.util.dt as date_util
|
2016-08-23 04:42:05 +00:00
|
|
|
import homeassistant.util.yaml as yaml
|
2015-04-30 05:26:54 +00:00
|
|
|
from homeassistant.const import (
|
2015-05-01 04:03:01 +00:00
|
|
|
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED,
|
2015-09-12 16:15:28 +00:00
|
|
|
EVENT_STATE_CHANGED, EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE,
|
2016-07-31 20:24:49 +00:00
|
|
|
ATTR_DISCOVERED, SERVER_PORT)
|
2015-08-11 06:11:46 +00:00
|
|
|
from homeassistant.components import sun, mqtt
|
2016-11-25 21:04:06 +00:00
|
|
|
from homeassistant.components.http.auth import auth_middleware
|
|
|
|
from homeassistant.components.http.const import (
|
2016-11-27 02:23:28 +00:00
|
|
|
KEY_USE_X_FORWARDED_FOR, KEY_BANS_ENABLED, KEY_TRUSTED_NETWORKS)
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2016-02-14 20:54:16 +00:00
|
|
|
_TEST_INSTANCE_PORT = SERVER_PORT
|
2016-08-23 04:42:05 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-02-14 20:54:16 +00:00
|
|
|
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2016-08-23 04:42:05 +00:00
|
|
|
def get_test_config_dir(*add_path):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Return a path to a test config dir."""
|
2016-11-18 22:05:03 +00:00
|
|
|
return os.path.join(os.path.dirname(__file__), 'testing_config', *add_path)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
|
|
|
|
2016-10-31 15:47:29 +00:00
|
|
|
def get_test_home_assistant():
|
2016-11-18 22:05:03 +00:00
|
|
|
"""Return a Home Assistant object pointing at test config directory."""
|
2016-11-02 20:53:52 +00:00
|
|
|
if sys.platform == "win32":
|
|
|
|
loop = asyncio.ProactorEventLoop()
|
|
|
|
else:
|
|
|
|
loop = asyncio.new_event_loop()
|
2016-09-13 02:16:14 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
hass = loop.run_until_complete(async_test_home_assistant(loop))
|
2015-05-01 04:03:01 +00:00
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
# FIXME should not be a daemon. Means hass.stop() not called in teardown
|
2016-09-19 03:35:58 +00:00
|
|
|
stop_event = threading.Event()
|
|
|
|
|
|
|
|
def run_loop():
|
2016-10-08 18:27:35 +00:00
|
|
|
"""Run event loop."""
|
|
|
|
# pylint: disable=protected-access
|
2016-10-02 22:07:23 +00:00
|
|
|
loop._thread_ident = threading.get_ident()
|
2016-09-19 03:35:58 +00:00
|
|
|
loop.run_forever()
|
|
|
|
loop.close()
|
|
|
|
stop_event.set()
|
|
|
|
|
|
|
|
threading.Thread(name="LoopThread", target=run_loop, daemon=True).start()
|
2016-09-13 02:16:14 +00:00
|
|
|
|
|
|
|
orig_start = hass.start
|
2016-09-19 03:35:58 +00:00
|
|
|
orig_stop = hass.stop
|
2016-09-13 02:16:14 +00:00
|
|
|
|
2016-10-08 16:56:36 +00:00
|
|
|
@patch.object(hass.loop, 'run_forever')
|
|
|
|
@patch.object(hass.loop, 'close')
|
|
|
|
def start_hass(*mocks):
|
2016-09-13 02:16:14 +00:00
|
|
|
"""Helper to start hass."""
|
2016-10-08 16:56:36 +00:00
|
|
|
orig_start()
|
|
|
|
hass.block_till_done()
|
2016-09-13 02:16:14 +00:00
|
|
|
|
2016-09-19 03:35:58 +00:00
|
|
|
def stop_hass():
|
2016-10-08 18:27:35 +00:00
|
|
|
"""Stop hass."""
|
2016-09-19 03:35:58 +00:00
|
|
|
orig_stop()
|
|
|
|
stop_event.wait()
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
hass.start = start_hass
|
2016-09-19 03:35:58 +00:00
|
|
|
hass.stop = stop_hass
|
2016-09-13 02:16:14 +00:00
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
return hass
|
|
|
|
|
|
|
|
|
2016-11-18 22:05:03 +00:00
|
|
|
# pylint: disable=protected-access
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_test_home_assistant(loop):
|
|
|
|
"""Return a Home Assistant object pointing at test config dir."""
|
|
|
|
loop._thread_ident = threading.get_ident()
|
|
|
|
|
2016-10-29 15:57:59 +00:00
|
|
|
hass = ha.HomeAssistant(loop)
|
2016-11-24 22:49:29 +00:00
|
|
|
hass.async_track_tasks()
|
2016-10-29 15:57:59 +00:00
|
|
|
|
|
|
|
hass.config.location_name = 'test home'
|
|
|
|
hass.config.config_dir = get_test_config_dir()
|
|
|
|
hass.config.latitude = 32.87336
|
|
|
|
hass.config.longitude = -117.22743
|
|
|
|
hass.config.elevation = 0
|
|
|
|
hass.config.time_zone = date_util.get_time_zone('US/Pacific')
|
|
|
|
hass.config.units = METRIC_SYSTEM
|
|
|
|
hass.config.skip_pip = True
|
|
|
|
|
|
|
|
if 'custom_components.test' not in loader.AVAILABLE_COMPONENTS:
|
|
|
|
yield from loop.run_in_executor(None, loader.prepare, hass)
|
|
|
|
|
|
|
|
hass.state = ha.CoreState.running
|
2016-10-24 06:48:01 +00:00
|
|
|
|
2016-11-03 02:16:59 +00:00
|
|
|
# Mock async_start
|
|
|
|
orig_start = hass.async_start
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def mock_async_start():
|
2016-11-18 22:05:03 +00:00
|
|
|
"""Start the mocking."""
|
2016-11-24 22:49:29 +00:00
|
|
|
with patch.object(loop, 'add_signal_handler'), \
|
|
|
|
patch('homeassistant.core._async_create_timer'):
|
2016-11-03 02:16:59 +00:00
|
|
|
yield from orig_start()
|
|
|
|
|
|
|
|
hass.async_start = mock_async_start
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
return hass
|
|
|
|
|
|
|
|
|
2016-02-14 20:54:16 +00:00
|
|
|
def get_test_instance_port():
|
|
|
|
"""Return unused port for running test instance.
|
|
|
|
|
|
|
|
The socket that holds the default port does not get released when we stop
|
|
|
|
HA in a different test case. Until I have figured out what is going on,
|
|
|
|
let's run each test on a different port.
|
|
|
|
"""
|
|
|
|
global _TEST_INSTANCE_PORT
|
|
|
|
_TEST_INSTANCE_PORT += 1
|
|
|
|
return _TEST_INSTANCE_PORT
|
|
|
|
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
def mock_service(hass, domain, service):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup a fake service.
|
|
|
|
|
|
|
|
Return a list that logs all calls to fake service.
|
2014-12-01 07:14:08 +00:00
|
|
|
"""
|
|
|
|
calls = []
|
|
|
|
|
2016-11-18 22:05:03 +00:00
|
|
|
# pylint: disable=redefined-outer-name
|
2016-11-05 23:36:20 +00:00
|
|
|
@ha.callback
|
|
|
|
def mock_service(call):
|
2016-11-18 22:05:03 +00:00
|
|
|
""""Mocked service call."""
|
2016-11-05 23:36:20 +00:00
|
|
|
calls.append(call)
|
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
# pylint: disable=unnecessary-lambda
|
2016-11-05 23:36:20 +00:00
|
|
|
hass.services.register(domain, service, mock_service)
|
2014-12-01 07:14:08 +00:00
|
|
|
|
|
|
|
return calls
|
|
|
|
|
|
|
|
|
2015-08-11 06:11:46 +00:00
|
|
|
def fire_mqtt_message(hass, topic, payload, qos=0):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Fire the MQTT message."""
|
2015-08-11 06:11:46 +00:00
|
|
|
hass.bus.fire(mqtt.EVENT_MQTT_MESSAGE_RECEIVED, {
|
|
|
|
mqtt.ATTR_TOPIC: topic,
|
|
|
|
mqtt.ATTR_PAYLOAD: payload,
|
|
|
|
mqtt.ATTR_QOS: qos,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2015-08-03 15:57:12 +00:00
|
|
|
def fire_time_changed(hass, time):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Fire a time changes event."""
|
2015-08-03 15:57:12 +00:00
|
|
|
hass.bus.fire(EVENT_TIME_CHANGED, {'now': time})
|
|
|
|
|
|
|
|
|
2015-09-12 16:15:28 +00:00
|
|
|
def fire_service_discovered(hass, service, info):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Fire the MQTT message."""
|
2015-09-12 16:15:28 +00:00
|
|
|
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
|
|
|
ATTR_SERVICE: service,
|
|
|
|
ATTR_DISCOVERED: info
|
|
|
|
})
|
2015-04-30 05:26:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ensure_sun_risen(hass):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Trigger sun to rise if below horizon."""
|
2015-08-03 15:57:12 +00:00
|
|
|
if sun.is_on(hass):
|
|
|
|
return
|
|
|
|
fire_time_changed(hass, sun.next_rising_utc(hass) + timedelta(seconds=10))
|
2015-04-30 05:26:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ensure_sun_set(hass):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Trigger sun to set if above horizon."""
|
2015-08-03 15:57:12 +00:00
|
|
|
if not sun.is_on(hass):
|
|
|
|
return
|
|
|
|
fire_time_changed(hass, sun.next_setting_utc(hass) + timedelta(seconds=10))
|
2015-04-30 05:26:54 +00:00
|
|
|
|
|
|
|
|
2016-06-27 16:02:45 +00:00
|
|
|
def load_fixture(filename):
|
|
|
|
"""Helper to load a fixture."""
|
|
|
|
path = os.path.join(os.path.dirname(__file__), 'fixtures', filename)
|
2016-08-23 04:42:05 +00:00
|
|
|
with open(path) as fptr:
|
|
|
|
return fptr.read()
|
2016-06-27 16:02:45 +00:00
|
|
|
|
|
|
|
|
2015-05-01 04:03:01 +00:00
|
|
|
def mock_state_change_event(hass, new_state, old_state=None):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Mock state change envent."""
|
2015-05-01 04:03:01 +00:00
|
|
|
event_data = {
|
|
|
|
'entity_id': new_state.entity_id,
|
|
|
|
'new_state': new_state,
|
|
|
|
}
|
|
|
|
|
|
|
|
if old_state:
|
|
|
|
event_data['old_state'] = old_state
|
|
|
|
|
|
|
|
hass.bus.fire(EVENT_STATE_CHANGED, event_data)
|
|
|
|
|
|
|
|
|
2015-07-11 07:02:52 +00:00
|
|
|
def mock_http_component(hass):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Mock the HTTP component."""
|
2016-11-27 02:23:28 +00:00
|
|
|
hass.http = MagicMock()
|
2015-07-11 07:02:52 +00:00
|
|
|
hass.config.components.append('http')
|
2016-10-24 06:48:01 +00:00
|
|
|
hass.http.views = {}
|
|
|
|
|
|
|
|
def mock_register_view(view):
|
|
|
|
"""Store registered view."""
|
|
|
|
if isinstance(view, type):
|
|
|
|
# Instantiate the view, if needed
|
2016-11-25 21:04:06 +00:00
|
|
|
view = view()
|
2016-10-24 06:48:01 +00:00
|
|
|
|
|
|
|
hass.http.views[view.name] = view
|
|
|
|
|
|
|
|
hass.http.register_view = mock_register_view
|
2015-07-11 07:02:52 +00:00
|
|
|
|
|
|
|
|
2016-11-27 02:23:28 +00:00
|
|
|
def mock_http_component_app(hass, api_password=None):
|
2016-11-25 21:04:06 +00:00
|
|
|
"""Create an aiohttp.web.Application instance for testing."""
|
2016-11-27 02:23:28 +00:00
|
|
|
hass.http = MagicMock(api_password=api_password)
|
2016-11-25 21:04:06 +00:00
|
|
|
app = web.Application(middlewares=[auth_middleware], loop=hass.loop)
|
|
|
|
app['hass'] = hass
|
|
|
|
app[KEY_USE_X_FORWARDED_FOR] = False
|
|
|
|
app[KEY_BANS_ENABLED] = False
|
2016-11-27 02:23:28 +00:00
|
|
|
app[KEY_TRUSTED_NETWORKS] = []
|
2016-11-25 21:04:06 +00:00
|
|
|
return app
|
|
|
|
|
|
|
|
|
2016-08-30 16:22:52 +00:00
|
|
|
def mock_mqtt_component(hass):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Mock the MQTT component."""
|
2016-11-27 02:23:28 +00:00
|
|
|
with patch('homeassistant.components.mqtt.MQTT') as mock_mqtt:
|
2016-08-30 16:22:52 +00:00
|
|
|
setup_component(hass, mqtt.DOMAIN, {
|
|
|
|
mqtt.DOMAIN: {
|
|
|
|
mqtt.CONF_BROKER: 'mock-broker',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return mock_mqtt
|
2015-08-11 06:11:46 +00:00
|
|
|
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
class MockModule(object):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Representation of a fake module."""
|
2014-12-01 07:14:08 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
2016-04-03 03:10:57 +00:00
|
|
|
def __init__(self, domain=None, dependencies=None, setup=None,
|
2016-10-31 15:47:29 +00:00
|
|
|
requirements=None, config_schema=None, platform_schema=None,
|
|
|
|
async_setup=None):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Initialize the mock module."""
|
2014-12-01 07:14:08 +00:00
|
|
|
self.DOMAIN = domain
|
2016-04-03 03:10:57 +00:00
|
|
|
self.DEPENDENCIES = dependencies or []
|
|
|
|
self.REQUIREMENTS = requirements or []
|
2016-10-27 07:16:23 +00:00
|
|
|
self._setup = setup
|
2016-03-29 07:17:53 +00:00
|
|
|
|
|
|
|
if config_schema is not None:
|
|
|
|
self.CONFIG_SCHEMA = config_schema
|
|
|
|
|
|
|
|
if platform_schema is not None:
|
|
|
|
self.PLATFORM_SCHEMA = platform_schema
|
|
|
|
|
2016-10-31 15:47:29 +00:00
|
|
|
if async_setup is not None:
|
|
|
|
self.async_setup = async_setup
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
def setup(self, hass, config):
|
2016-10-31 15:47:29 +00:00
|
|
|
"""Setup the component.
|
|
|
|
|
|
|
|
We always define this mock because MagicMock setups will be seen by the
|
|
|
|
executor as a coroutine, raising an exception.
|
|
|
|
"""
|
2016-10-27 07:16:23 +00:00
|
|
|
if self._setup is not None:
|
|
|
|
return self._setup(hass, config)
|
|
|
|
return True
|
2016-01-31 02:55:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MockPlatform(object):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Provide a fake platform."""
|
2016-01-31 02:55:52 +00:00
|
|
|
|
2016-10-30 21:18:53 +00:00
|
|
|
# pylint: disable=invalid-name
|
2016-04-03 03:10:57 +00:00
|
|
|
def __init__(self, setup_platform=None, dependencies=None,
|
|
|
|
platform_schema=None):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Initialize the platform."""
|
2016-04-03 03:10:57 +00:00
|
|
|
self.DEPENDENCIES = dependencies or []
|
2016-01-31 02:55:52 +00:00
|
|
|
self._setup_platform = setup_platform
|
|
|
|
|
2016-04-03 03:10:57 +00:00
|
|
|
if platform_schema is not None:
|
|
|
|
self.PLATFORM_SCHEMA = platform_schema
|
|
|
|
|
2016-01-31 02:55:52 +00:00
|
|
|
def setup_platform(self, hass, config, add_devices, discovery_info=None):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup the platform."""
|
2016-01-31 02:55:52 +00:00
|
|
|
if self._setup_platform is not None:
|
|
|
|
self._setup_platform(hass, config, add_devices, discovery_info)
|
2014-12-01 07:14:08 +00:00
|
|
|
|
|
|
|
|
2015-03-22 02:37:18 +00:00
|
|
|
class MockToggleDevice(ToggleEntity):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Provide a mock toggle device."""
|
2016-03-09 10:15:04 +00:00
|
|
|
|
2014-11-25 08:20:36 +00:00
|
|
|
def __init__(self, name, state):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Initialize the mock device."""
|
2015-01-11 17:20:41 +00:00
|
|
|
self._name = name or DEVICE_DEFAULT_NAME
|
|
|
|
self._state = state
|
2014-11-26 05:28:43 +00:00
|
|
|
self.calls = []
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Return the name of the device if any."""
|
2015-01-11 17:20:41 +00:00
|
|
|
self.calls.append(('name', {}))
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Return the name of the device if any."""
|
2015-01-11 17:20:41 +00:00
|
|
|
self.calls.append(('state', {}))
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Return true if device is on."""
|
2015-01-11 17:20:41 +00:00
|
|
|
self.calls.append(('is_on', {}))
|
|
|
|
return self._state == STATE_ON
|
2014-11-25 08:20:36 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Turn the device on."""
|
2014-11-26 05:28:43 +00:00
|
|
|
self.calls.append(('turn_on', kwargs))
|
2015-01-11 17:20:41 +00:00
|
|
|
self._state = STATE_ON
|
2014-11-25 08:20:36 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Turn the device off."""
|
2014-11-26 05:28:43 +00:00
|
|
|
self.calls.append(('turn_off', kwargs))
|
2015-01-11 17:20:41 +00:00
|
|
|
self._state = STATE_OFF
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2014-11-26 05:28:43 +00:00
|
|
|
def last_call(self, method=None):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Return the last call."""
|
2015-02-09 06:18:54 +00:00
|
|
|
if not self.calls:
|
|
|
|
return None
|
|
|
|
elif method is None:
|
2014-11-26 05:28:43 +00:00
|
|
|
return self.calls[-1]
|
|
|
|
else:
|
2015-02-09 06:18:54 +00:00
|
|
|
try:
|
|
|
|
return next(call for call in reversed(self.calls)
|
|
|
|
if call[0] == method)
|
|
|
|
except StopIteration:
|
|
|
|
return None
|
2016-08-23 04:42:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def patch_yaml_files(files_dict, endswith=True):
|
|
|
|
"""Patch load_yaml with a dictionary of yaml files."""
|
|
|
|
# match using endswith, start search with longest string
|
|
|
|
matchlist = sorted(list(files_dict.keys()), key=len) if endswith else []
|
|
|
|
|
|
|
|
def mock_open_f(fname, **_):
|
|
|
|
"""Mock open() in the yaml module, used by load_yaml."""
|
|
|
|
# Return the mocked file on full match
|
|
|
|
if fname in files_dict:
|
|
|
|
_LOGGER.debug('patch_yaml_files match %s', fname)
|
2016-09-08 20:20:38 +00:00
|
|
|
res = StringIO(files_dict[fname])
|
|
|
|
setattr(res, 'name', fname)
|
|
|
|
return res
|
2016-08-23 04:42:05 +00:00
|
|
|
|
|
|
|
# Match using endswith
|
|
|
|
for ends in matchlist:
|
|
|
|
if fname.endswith(ends):
|
|
|
|
_LOGGER.debug('patch_yaml_files end match %s: %s', ends, fname)
|
2016-09-08 20:20:38 +00:00
|
|
|
res = StringIO(files_dict[ends])
|
|
|
|
setattr(res, 'name', fname)
|
|
|
|
return res
|
2016-08-23 04:42:05 +00:00
|
|
|
|
|
|
|
# Fallback for hass.components (i.e. services.yaml)
|
|
|
|
if 'homeassistant/components' in fname:
|
|
|
|
_LOGGER.debug('patch_yaml_files using real file: %s', fname)
|
|
|
|
return open(fname, encoding='utf-8')
|
|
|
|
|
|
|
|
# Not found
|
2016-09-08 20:20:38 +00:00
|
|
|
raise FileNotFoundError('File not found: {}'.format(fname))
|
2016-08-23 04:42:05 +00:00
|
|
|
|
|
|
|
return patch.object(yaml, 'open', mock_open_f, create=True)
|
2016-10-08 18:27:35 +00:00
|
|
|
|
|
|
|
|
2016-10-29 19:54:47 +00:00
|
|
|
def mock_coro(return_value=None):
|
|
|
|
"""Helper method to return a coro that returns a value."""
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coro():
|
|
|
|
"""Fake coroutine."""
|
|
|
|
return return_value
|
|
|
|
|
|
|
|
return coro
|
|
|
|
|
|
|
|
|
2016-10-08 18:27:35 +00:00
|
|
|
@contextmanager
|
|
|
|
def assert_setup_component(count, domain=None):
|
|
|
|
"""Collect valid configuration from setup_component.
|
|
|
|
|
|
|
|
- count: The amount of valid platforms that should be setup
|
|
|
|
- domain: The domain to count is optional. It can be automatically
|
|
|
|
determined most of the time
|
|
|
|
|
|
|
|
Use as a context manager aroung bootstrap.setup_component
|
|
|
|
with assert_setup_component(0) as result_config:
|
|
|
|
setup_component(hass, start_config, domain)
|
|
|
|
# using result_config is optional
|
|
|
|
"""
|
|
|
|
config = {}
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
@asyncio.coroutine
|
2016-10-08 18:27:35 +00:00
|
|
|
def mock_psc(hass, config_input, domain):
|
|
|
|
"""Mock the prepare_setup_component to capture config."""
|
2016-10-27 07:16:23 +00:00
|
|
|
res = yield from async_prepare_setup_component(
|
|
|
|
hass, config_input, domain)
|
2016-10-08 18:27:35 +00:00
|
|
|
config[domain] = None if res is None else res.get(domain)
|
|
|
|
_LOGGER.debug('Configuration for %s, Validated: %s, Original %s',
|
|
|
|
domain, config[domain], config_input.get(domain))
|
|
|
|
return res
|
|
|
|
|
|
|
|
assert isinstance(config, dict)
|
2016-10-27 07:16:23 +00:00
|
|
|
with patch('homeassistant.bootstrap.async_prepare_setup_component',
|
|
|
|
mock_psc):
|
2016-10-08 18:27:35 +00:00
|
|
|
yield config
|
|
|
|
|
|
|
|
if domain is None:
|
|
|
|
assert len(config) == 1, ('assert_setup_component requires DOMAIN: {}'
|
|
|
|
.format(list(config.keys())))
|
|
|
|
domain = list(config.keys())[0]
|
|
|
|
|
|
|
|
res = config.get(domain)
|
|
|
|
res_len = 0 if res is None else len(res)
|
|
|
|
assert res_len == count, 'setup_component failed, expected {} got {}: {}' \
|
|
|
|
.format(count, res_len, res)
|