2014-11-25 08:20:36 +00:00
|
|
|
"""
|
2015-01-09 04:05:12 +00:00
|
|
|
tests.helper
|
2014-12-01 07:14:08 +00:00
|
|
|
~~~~~~~~~~~~~
|
2014-11-25 08:20:36 +00:00
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
Helper method for writing tests.
|
2014-11-25 08:20:36 +00:00
|
|
|
"""
|
2014-12-01 07:14:08 +00:00
|
|
|
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
|
2014-12-01 07:14:08 +00:00
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
import homeassistant.core as ha
|
2015-08-11 15:20:13 +00:00
|
|
|
import homeassistant.util.location as location_util
|
2015-04-30 05:26:54 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-03-22 02:37:18 +00:00
|
|
|
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,
|
|
|
|
EVENT_STATE_CHANGED)
|
2015-08-11 06:11:46 +00:00
|
|
|
from homeassistant.components import sun, mqtt
|
2014-11-25 08:20:36 +00:00
|
|
|
|
|
|
|
|
2015-04-26 17:05:01 +00:00
|
|
|
def get_test_config_dir():
|
|
|
|
""" Returns 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):
|
2014-12-01 07:14:08 +00:00
|
|
|
""" Returns 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
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
hass = ha.HomeAssistant()
|
2015-05-01 04:03:01 +00:00
|
|
|
|
|
|
|
if num_threads:
|
|
|
|
ha.MIN_WORKER_THREAD = orig_num_threads
|
|
|
|
|
2015-04-26 17:05:01 +00:00
|
|
|
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
|
2014-12-01 07:14:08 +00:00
|
|
|
|
|
|
|
return hass
|
|
|
|
|
|
|
|
|
2015-08-11 15:20:13 +00:00
|
|
|
def mock_detect_location_info():
|
|
|
|
""" Mock implementation of util.detect_location_info. """
|
|
|
|
return location_util.LocationInfo(
|
|
|
|
ip='1.1.1.1',
|
|
|
|
country_code='US',
|
|
|
|
country_name='United States',
|
|
|
|
region_code='CA',
|
|
|
|
region_name='California',
|
|
|
|
city='San Diego',
|
|
|
|
zip_code='92122',
|
|
|
|
time_zone='America/Los_Angeles',
|
|
|
|
latitude='2.0',
|
|
|
|
longitude='1.0',
|
|
|
|
use_fahrenheit=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
def mock_service(hass, domain, service):
|
|
|
|
"""
|
|
|
|
Sets up a fake service.
|
|
|
|
Returns 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):
|
|
|
|
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):
|
|
|
|
hass.bus.fire(EVENT_TIME_CHANGED, {'now': time})
|
|
|
|
|
|
|
|
|
2015-04-30 05:26:54 +00:00
|
|
|
def trigger_device_tracker_scan(hass):
|
|
|
|
""" Triggers the device tracker to scan. """
|
2015-08-03 15:57:12 +00:00
|
|
|
fire_time_changed(
|
|
|
|
hass, dt_util.utcnow().replace(second=0) + timedelta(hours=1))
|
2015-04-30 05:26:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ensure_sun_risen(hass):
|
|
|
|
""" 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):
|
|
|
|
""" 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):
|
|
|
|
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):
|
|
|
|
hass.http = MockHTTP()
|
|
|
|
hass.config.components.append('http')
|
|
|
|
|
|
|
|
|
2015-08-11 06:11:46 +00:00
|
|
|
def mock_mqtt_component(hass):
|
|
|
|
with mock.patch('homeassistant.components.mqtt.MQTT'):
|
|
|
|
mqtt.setup(hass, {
|
|
|
|
mqtt.DOMAIN: {
|
|
|
|
mqtt.CONF_BROKER: 'mock-broker',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
hass.config.components.append(mqtt.DOMAIN)
|
|
|
|
|
|
|
|
|
2015-07-11 07:02:52 +00:00
|
|
|
class MockHTTP(object):
|
|
|
|
""" Mocks the HTTP module. """
|
|
|
|
|
|
|
|
def register_path(self, method, url, callback, require_auth=True):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-12-01 07:14:08 +00:00
|
|
|
class MockModule(object):
|
|
|
|
""" Provides a fake module. """
|
|
|
|
|
|
|
|
def __init__(self, domain, dependencies=[], setup=None):
|
|
|
|
self.DOMAIN = domain
|
|
|
|
self.DEPENDENCIES = dependencies
|
|
|
|
# Setup a mock setup if none given.
|
|
|
|
self.setup = lambda hass, config: False if setup is None else setup
|
|
|
|
|
|
|
|
|
2015-03-22 02:37:18 +00:00
|
|
|
class MockToggleDevice(ToggleEntity):
|
2014-12-01 07:14:08 +00:00
|
|
|
""" Provides a mock toggle device. """
|
2014-11-25 08:20:36 +00:00
|
|
|
def __init__(self, name, state):
|
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):
|
2014-11-25 08:20:36 +00:00
|
|
|
""" Returns 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):
|
|
|
|
""" Returns the name of the device if any. """
|
|
|
|
self.calls.append(('state', {}))
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
""" 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):
|
|
|
|
""" 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):
|
|
|
|
""" 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):
|
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
|