home-assistant-core/tests/common.py

229 lines
6.2 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""Test the helper method for writing tests."""
import os
2015-04-30 05:26:54 +00:00
from datetime import timedelta
2015-08-11 06:11:46 +00:00
from unittest import mock
2015-09-01 07:18:26 +00:00
from homeassistant import core as ha, loader
from homeassistant.helpers.entity import ToggleEntity
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,
ATTR_DISCOVERED, SERVER_PORT)
2015-08-11 06:11:46 +00:00
from homeassistant.components import sun, mqtt
2014-11-25 08:20:36 +00:00
_TEST_INSTANCE_PORT = SERVER_PORT
2014-11-25 08:20:36 +00:00
def get_test_config_dir():
2016-03-09 09:25:50 +00:00
"""Return a path to a test config dir."""
return os.path.join(os.path.dirname(__file__), "config")
2015-05-01 04:03:01 +00:00
def get_test_home_assistant(num_threads=None):
2016-03-09 09:25:50 +00:00
"""Return a Home Assistant object pointing at test config dir."""
2015-05-01 04:03:01 +00:00
if num_threads:
orig_num_threads = ha.MIN_WORKER_THREAD
ha.MIN_WORKER_THREAD = num_threads
hass = ha.HomeAssistant()
2015-05-01 04:03:01 +00:00
if num_threads:
ha.MIN_WORKER_THREAD = orig_num_threads
hass.config.config_dir = get_test_config_dir()
2015-04-30 05:26:54 +00:00
hass.config.latitude = 32.87336
hass.config.longitude = -117.22743
2015-09-13 05:56:49 +00:00
if 'custom_components.test' not in loader.AVAILABLE_COMPONENTS:
loader.prepare(hass)
2015-09-01 07:18:26 +00:00
return hass
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
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.
"""
calls = []
hass.services.register(
domain, service, lambda call: calls.append(call))
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
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."""
2015-07-11 07:02:52 +00:00
hass.http = MockHTTP()
hass.config.components.append('http')
2015-10-03 06:57:26 +00:00
@mock.patch('homeassistant.components.mqtt.MQTT')
2015-10-14 06:08:12 +00:00
def mock_mqtt_component(hass, mock_mqtt):
2016-03-09 09:25:50 +00:00
"""Mock the MQTT component."""
2015-10-03 06:57:26 +00:00
mqtt.setup(hass, {
mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'mock-broker',
}
})
hass.config.components.append(mqtt.DOMAIN)
2015-10-14 06:08:12 +00:00
return mock_mqtt
2015-08-11 06:11:46 +00:00
2015-07-11 07:02:52 +00:00
class MockHTTP(object):
2016-03-09 09:25:50 +00:00
"""Mock the HTTP module."""
2016-03-09 10:15:04 +00:00
2015-07-11 07:02:52 +00:00
def register_path(self, method, url, callback, require_auth=True):
2016-03-09 10:15:04 +00:00
"""Register a path."""
2015-07-11 07:02:52 +00:00
pass
class MockModule(object):
2016-03-09 10:15:04 +00:00
"""Representation of a fake module."""
def __init__(self, domain=None, dependencies=[], setup=None,
2016-03-30 05:50:38 +00:00
requirements=[], config_schema=None, platform_schema=None):
2016-03-09 10:15:04 +00:00
"""Initialize the mock module."""
self.DOMAIN = domain
self.DEPENDENCIES = dependencies
2016-03-30 05:50:38 +00:00
self.REQUIREMENTS = requirements
if config_schema is not None:
self.CONFIG_SCHEMA = config_schema
if platform_schema is not None:
self.PLATFORM_SCHEMA = platform_schema
# Setup a mock setup if none given.
2016-01-31 02:55:52 +00:00
if setup is None:
2016-02-20 07:20:14 +00:00
self.setup = lambda hass, config: True
2016-01-31 02:55:52 +00:00
else:
self.setup = setup
class MockPlatform(object):
2016-03-09 09:25:50 +00:00
"""Provide a fake platform."""
2016-01-31 02:55:52 +00:00
def __init__(self, setup_platform=None, dependencies=[]):
2016-03-09 09:25:50 +00:00
"""Initialize the platform."""
2016-01-31 02:55:52 +00:00
self.DEPENDENCIES = dependencies
self._setup_platform = setup_platform
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)
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."""
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
@property
def name(self):
2016-03-09 09:25:50 +00:00
"""Return the name of the device if any."""
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."""
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."""
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))
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))
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."""
if not self.calls:
return None
elif method is None:
2014-11-26 05:28:43 +00:00
return self.calls[-1]
else:
try:
return next(call for call in reversed(self.calls)
if call[0] == method)
except StopIteration:
return None