Merge pull request #1260 from balloob/chore/tests-cleanup

Chore/tests cleanup
This commit is contained in:
Paulus Schoutsen 2016-02-14 15:21:23 -08:00
commit 00486dccea
57 changed files with 313 additions and 220 deletions

View File

@ -5,7 +5,7 @@ universal = 1
testpaths = tests
[flake8]
exclude = .venv,.git,.tox,docs,www_static,tests,venv,bin,lib
exclude = .venv,.git,.tox,docs,www_static,venv,bin,lib
[pep257]
ignore = D203,D105

View File

@ -13,9 +13,11 @@ from homeassistant.helpers.entity import ToggleEntity
from homeassistant.const import (
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED,
EVENT_STATE_CHANGED, EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE,
ATTR_DISCOVERED)
ATTR_DISCOVERED, SERVER_PORT)
from homeassistant.components import sun, mqtt
_TEST_INSTANCE_PORT = SERVER_PORT
def get_test_config_dir():
""" Returns a path to a test config dir. """
@ -43,6 +45,18 @@ def get_test_home_assistant(num_threads=None):
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):
"""
Sets up a fake service.

View File

@ -8,14 +8,13 @@ from datetime import timedelta
import unittest
from unittest.mock import patch
import homeassistant.core as ha
from homeassistant.const import (
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED)
from homeassistant.components import alarm_control_panel
import homeassistant.util.dt as dt_util
from tests.common import fire_time_changed
from tests.common import fire_time_changed, get_test_home_assistant
CODE = 'HELLO_CODE'
@ -24,7 +23,7 @@ class TestAlarmControlPanelManual(unittest.TestCase):
""" Test the manual alarm module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """

View File

@ -7,13 +7,13 @@ Tests manual alarm control panel component.
import unittest
from unittest.mock import patch
import homeassistant.core as ha
from homeassistant.const import (
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED, STATE_UNKNOWN)
from homeassistant.components import alarm_control_panel
from tests.common import mock_mqtt_component, fire_mqtt_message
from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant)
CODE = 'HELLO_CODE'
@ -22,7 +22,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
""" Test the manual alarm module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.mock_publish = mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name

View File

@ -6,15 +6,16 @@ Tests event automation.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.automation as automation
from tests.common import get_test_home_assistant
class TestAutomationEvent(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.calls = []
def record_call(service):

View File

@ -6,16 +6,17 @@ Tests automation component.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.automation as automation
from homeassistant.const import ATTR_ENTITY_ID
from tests.common import get_test_home_assistant
class TestAutomation(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.calls = []
def record_call(service):

View File

@ -6,16 +6,16 @@ Tests mqtt automation.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.automation as automation
from tests.common import mock_mqtt_component, fire_mqtt_message
from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant)
class TestAutomationMQTT(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
mock_mqtt_component(self.hass)
self.calls = []

View File

@ -6,15 +6,16 @@ Tests numeric state automation.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.automation as automation
from tests.common import get_test_home_assistant
class TestAutomationNumericState(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.calls = []
def record_call(service):
@ -365,7 +366,7 @@ class TestAutomationNumericState(unittest.TestCase):
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))
def test_if_fires_on_attribute_change_with_attribute_below_multiple_attributes(self):
def test_fires_on_attr_change_with_attribute_below_and_multiple_attr(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'trigger': {
@ -380,7 +381,8 @@ class TestAutomationNumericState(unittest.TestCase):
}
}))
# 9 is not below 10
self.hass.states.set('test.entity', 'entity', {'test_attribute': 9, 'not_test_attribute': 11})
self.hass.states.set('test.entity', 'entity',
{'test_attribute': 9, 'not_test_attribute': 11})
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
@ -390,7 +392,8 @@ class TestAutomationNumericState(unittest.TestCase):
'trigger': {
'platform': 'numeric_state',
'entity_id': 'test.entity',
'value_template': '{{ state.attributes.test_attribute[2] }}',
'value_template':
'{{ state.attributes.test_attribute[2] }}',
'below': 10,
},
'action': {
@ -399,7 +402,8 @@ class TestAutomationNumericState(unittest.TestCase):
}
}))
# 3 is below 10
self.hass.states.set('test.entity', 'entity', {'test_attribute': [11, 15, 3]})
self.hass.states.set('test.entity', 'entity',
{'test_attribute': [11, 15, 3]})
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
@ -409,7 +413,8 @@ class TestAutomationNumericState(unittest.TestCase):
'trigger': {
'platform': 'numeric_state',
'entity_id': 'test.entity',
'value_template': '{{ state.attributes.test_attribute | multiply(10) }}',
'value_template':
'{{ state.attributes.test_attribute | multiply(10) }}',
'below': 10,
},
'action': {
@ -418,11 +423,12 @@ class TestAutomationNumericState(unittest.TestCase):
}
}))
# 9 is below 10
self.hass.states.set('test.entity', 'entity', {'test_attribute': '0.9'})
self.hass.states.set('test.entity', 'entity',
{'test_attribute': '0.9'})
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_not_fires_on_attribute_change_with_attribute_not_below_multiple_attributes(self):
def test_not_fires_on_attr_change_with_attr_not_below_multiple_attr(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'trigger': {
@ -437,7 +443,8 @@ class TestAutomationNumericState(unittest.TestCase):
}
}))
# 11 is not below 10
self.hass.states.set('test.entity', 'entity', {'test_attribute': 11, 'not_test_attribute': 9})
self.hass.states.set('test.entity', 'entity',
{'test_attribute': 11, 'not_test_attribute': 9})
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))

View File

@ -6,16 +6,17 @@ Tests state automation.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.automation as automation
import homeassistant.components.automation.state as state
from tests.common import get_test_home_assistant
class TestAutomationState(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.states.set('test.entity', 'hello')
self.calls = []

View File

@ -8,19 +8,18 @@ from datetime import datetime
import unittest
from unittest.mock import patch
import homeassistant.core as ha
from homeassistant.components import sun
import homeassistant.components.automation as automation
import homeassistant.util.dt as dt_util
from tests.common import fire_time_changed
from tests.common import fire_time_changed, get_test_home_assistant
class TestAutomationSun(unittest.TestCase):
""" Test the sun automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.config.components.append('sun')
self.calls = []

View File

@ -6,15 +6,16 @@ Tests template automation.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.automation as automation
from tests.common import get_test_home_assistant
class TestAutomationTemplate(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.states.set('test.entity', 'hello')
self.calls = []
@ -256,7 +257,8 @@ class TestAutomationTemplate(unittest.TestCase):
automation.DOMAIN: {
'trigger': {
'platform': 'template',
'value_template': '{{ not is_state("test.entity", "world") }}',
'value_template':
'{{ not is_state("test.entity", "world") }}',
},
'action': {
'service': 'test.automation'

View File

@ -8,20 +8,19 @@ from datetime import timedelta
import unittest
from unittest.mock import patch
import homeassistant.core as ha
import homeassistant.util.dt as dt_util
import homeassistant.components.automation as automation
from homeassistant.components.automation import time, event
from homeassistant.const import CONF_PLATFORM
from tests.common import fire_time_changed
from tests.common import fire_time_changed, get_test_home_assistant
class TestAutomationTime(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.calls = []
def record_call(service):

View File

@ -6,16 +6,17 @@ Tests command binary sensor.
"""
import unittest
import homeassistant.core as ha
from homeassistant.const import (STATE_ON, STATE_OFF)
from homeassistant.components.binary_sensor import command_sensor
from tests.common import get_test_home_assistant
class TestCommandSensorBinarySensor(unittest.TestCase):
""" Test the Template sensor. """
def setUp(self):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self):
""" Stop down stuff we started. """

View File

@ -6,17 +6,18 @@ Tests MQTT binary sensor.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.binary_sensor as binary_sensor
from tests.common import mock_mqtt_component, fire_mqtt_message
from homeassistant.const import (STATE_OFF, STATE_ON)
from tests.common import get_test_home_assistant
class TestSensorMQTT(unittest.TestCase):
""" Test the MQTT sensor. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name

View File

@ -123,12 +123,14 @@ class TestComponentsDeviceTracker(unittest.TestCase):
scanner = get_component('device_tracker.test').SCANNER
scanner.reset()
scanner.come_home('DEV1')
self.assertTrue(device_tracker.setup(self.hass, {
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
config = device_tracker.load_config(self.yaml_devices, self.hass,
timedelta(seconds=0), 0)[0]
self.assertEqual('dev1', config.dev_id)
self.assertEqual(True, config.track)
timedelta(seconds=0), 0)
assert len(config) == 1
assert config[0].dev_id == 'dev1'
assert config[0].track
def test_discovery(self):
scanner = get_component('device_tracker.test').SCANNER

View File

@ -13,9 +13,9 @@ from homeassistant import bootstrap, const
import homeassistant.components.device_tracker as device_tracker
import homeassistant.components.http as http
from tests.common import get_test_home_assistant
from tests.common import get_test_home_assistant, get_test_instance_port
SERVER_PORT = 8126
SERVER_PORT = get_test_instance_port()
HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT)
hass = None
@ -128,7 +128,8 @@ class TestLocative(unittest.TestCase):
# Enter the Home
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
state_name = hass.states.get('{}.{}'.format('device_tracker',
data['device'])).state
self.assertEqual(state_name, 'home')
data['id'] = 'HOME'
@ -137,7 +138,8 @@ class TestLocative(unittest.TestCase):
# Exit Home
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
state_name = hass.states.get('{}.{}'.format('device_tracker',
data['device'])).state
self.assertEqual(state_name, 'not_home')
data['id'] = 'hOmE'
@ -146,7 +148,8 @@ class TestLocative(unittest.TestCase):
# Enter Home again
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
state_name = hass.states.get('{}.{}'.format('device_tracker',
data['device'])).state
self.assertEqual(state_name, 'home')
data['trigger'] = 'exit'
@ -154,7 +157,8 @@ class TestLocative(unittest.TestCase):
# Exit Home
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
state_name = hass.states.get('{}.{}'.format('device_tracker',
data['device'])).state
self.assertEqual(state_name, 'not_home')
data['id'] = 'work'
@ -163,7 +167,8 @@ class TestLocative(unittest.TestCase):
# Enter Work
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state_name = hass.states.get('{}.{}'.format('device_tracker', data['device'])).state
state_name = hass.states.get('{}.{}'.format('device_tracker',
data['device'])).state
self.assertEqual(state_name, 'work')
def test_exit_after_enter(self, update_config):
@ -181,7 +186,8 @@ class TestLocative(unittest.TestCase):
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state = hass.states.get('{}.{}'.format('device_tracker', data['device']))
state = hass.states.get('{}.{}'.format('device_tracker',
data['device']))
self.assertEqual(state.state, 'home')
data['id'] = 'Work'
@ -190,7 +196,8 @@ class TestLocative(unittest.TestCase):
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state = hass.states.get('{}.{}'.format('device_tracker', data['device']))
state = hass.states.get('{}.{}'.format('device_tracker',
data['device']))
self.assertEqual(state.state, 'work')
data['id'] = 'Home'
@ -200,7 +207,8 @@ class TestLocative(unittest.TestCase):
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state = hass.states.get('{}.{}'.format('device_tracker', data['device']))
state = hass.states.get('{}.{}'.format('device_tracker',
data['device']))
self.assertEqual(state.state, 'work')
def test_exit_first(self, update_config):
@ -218,5 +226,6 @@ class TestLocative(unittest.TestCase):
req = requests.get(_url(data))
self.assertEqual(200, req.status_code)
state = hass.states.get('{}.{}'.format('device_tracker', data['device']))
state = hass.states.get('{}.{}'.format('device_tracker',
data['device']))
self.assertEqual(state.state, 'not_home')

View File

@ -6,9 +6,10 @@ Tests demo garage door component.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.garage_door as gd
from tests.common import get_test_home_assistant
LEFT = 'garage_door.left_garage_door'
RIGHT = 'garage_door.right_garage_door'
@ -18,7 +19,7 @@ class TestGarageDoorDemo(unittest.TestCase):
""" Test the demo garage door. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.assertTrue(gd.setup(self.hass, {
'garage_door': {
'platform': 'demo'

View File

@ -46,16 +46,16 @@ light:
import unittest
from homeassistant.const import STATE_ON, STATE_OFF
import homeassistant.core as ha
import homeassistant.components.light as light
from tests.common import mock_mqtt_component, fire_mqtt_message
from tests.common import (
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
class TestLightMQTT(unittest.TestCase):
""" Test the MQTT light. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.mock_publish = mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name

View File

@ -6,9 +6,10 @@ Tests demo lock component.
"""
import unittest
import homeassistant.core as ha
from homeassistant.components import lock
from tests.common import get_test_home_assistant
FRONT = 'lock.front_door'
KITCHEN = 'lock.kitchen_door'
@ -18,7 +19,7 @@ class TestLockDemo(unittest.TestCase):
""" Test the demo lock. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.assertTrue(lock.setup(self.hass, {
'lock': {
'platform': 'demo'

View File

@ -6,9 +6,10 @@ Tests demo media_player component.
"""
import unittest
from unittest.mock import patch
import homeassistant.core as ha
import homeassistant.components.media_player as mp
from tests.common import get_test_home_assistant
entity_id = 'media_player.walkman'
@ -16,7 +17,7 @@ class TestDemoMediaPlayer(unittest.TestCase):
""" Test the media_player module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
@ -114,7 +115,8 @@ class TestDemoMediaPlayer(unittest.TestCase):
assert 0 < (mp.SUPPORT_PREVIOUS_TRACK &
state.attributes.get('supported_media_commands'))
@patch('homeassistant.components.media_player.demo.DemoYoutubePlayer.media_seek')
@patch('homeassistant.components.media_player.demo.DemoYoutubePlayer.'
'media_seek')
def test_play_media(self, mock_seek):
assert mp.setup(self.hass, {'media_player': {'platform': 'demo'}})
ent_id = 'media_player.living_room'

View File

@ -7,14 +7,13 @@ Tests universal media_player component.
from copy import copy
import unittest
import homeassistant.core as ha
from homeassistant.const import (
STATE_OFF, STATE_ON, STATE_UNKNOWN, STATE_PLAYING, STATE_PAUSED)
import homeassistant.components.switch as switch
import homeassistant.components.media_player as media_player
import homeassistant.components.media_player.universal as universal
from tests.common import mock_service
from tests.common import mock_service, get_test_home_assistant
class MockMediaPlayer(media_player.MediaPlayerDevice):
@ -87,7 +86,7 @@ class TestMediaPlayer(unittest.TestCase):
""" Test the media_player module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.mock_mp_1 = MockMediaPlayer(self.hass, 'mock1')
self.mock_mp_1.update_ha_state()

View File

@ -6,16 +6,17 @@ Tests notify demo component.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.notify as notify
from homeassistant.components.notify import demo
from tests.common import get_test_home_assistant
class TestNotifyDemo(unittest.TestCase):
""" Test the demo notify. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.assertTrue(notify.setup(self.hass, {
'notify': {
'platform': 'demo'

View File

@ -7,16 +7,17 @@ Tests MQTT rollershutter.
import unittest
from homeassistant.const import STATE_OPEN, STATE_CLOSED, STATE_UNKNOWN
import homeassistant.core as ha
import homeassistant.components.rollershutter as rollershutter
from tests.common import mock_mqtt_component, fire_mqtt_message
from tests.common import get_test_home_assistant
class TestRollershutterMQTT(unittest.TestCase):
""" Test the MQTT rollershutter. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.mock_publish = mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name

View File

@ -6,15 +6,16 @@ Tests command sensor.
"""
import unittest
import homeassistant.core as ha
from homeassistant.components.sensor import command_sensor
from tests.common import get_test_home_assistant
class TestCommandSensorSensor(unittest.TestCase):
""" Test the Command line sensor. """
def setUp(self):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self):
""" Stop down stuff we started. """

View File

@ -7,11 +7,12 @@ Tests mFi sensor.
import unittest
import unittest.mock as mock
import homeassistant.core as ha
import homeassistant.components.sensor as sensor
import homeassistant.components.sensor.mfi as mfi
from homeassistant.const import TEMP_CELCIUS
from tests.common import get_test_home_assistant
class TestMfiSensorSetup(unittest.TestCase):
PLATFORM = mfi
@ -28,7 +29,7 @@ class TestMfiSensorSetup(unittest.TestCase):
}
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.config.latitude = 32.87336
self.hass.config.longitude = 117.22743
@ -87,7 +88,7 @@ class TestMfiSensorSetup(unittest.TestCase):
class TestMfiSensor(unittest.TestCase):
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.config.latitude = 32.87336
self.hass.config.longitude = 117.22743
self.port = mock.MagicMock()

View File

@ -6,16 +6,17 @@ Tests MQTT sensor.
"""
import unittest
import homeassistant.core as ha
import homeassistant.components.sensor as sensor
from tests.common import mock_mqtt_component, fire_mqtt_message
from tests.common import get_test_home_assistant
class TestSensorMQTT(unittest.TestCase):
""" Test the MQTT sensor. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name

View File

@ -4,15 +4,16 @@ tests.components.sensor.test_template
Tests template sensor.
"""
import homeassistant.core as ha
import homeassistant.components.sensor as sensor
from tests.common import get_test_home_assistant
class TestTemplateSensor:
""" Test the Template sensor. """
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def teardown_method(self, method):
""" Stop down stuff we started. """
@ -64,7 +65,7 @@ class TestTemplateSensor:
'sensors': {
'test_template_sensor': {
'value_template':
"It {{ states.sensor.test_state.attributes.missing }}."
"It {{ states.sensor.test_state.attributes.missing }}."
}
}
}

View File

@ -9,17 +9,18 @@ from unittest.mock import patch
import pytest
import homeassistant.core as ha
import homeassistant.components.sensor as sensor
import homeassistant.util.dt as dt_util
from tests.common import get_test_home_assistant
@pytest.mark.usefixtures('betamax_session')
class TestSensorYr:
""" Test the Yr sensor. """
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.config.latitude = 32.87336
self.hass.config.longitude = 117.22743

View File

@ -9,16 +9,17 @@ import os
import tempfile
import unittest
from homeassistant import core
from homeassistant.const import STATE_ON, STATE_OFF
import homeassistant.components.switch as switch
from tests.common import get_test_home_assistant
class TestCommandSwitch(unittest.TestCase):
""" Test the command switch. """
def setUp(self): # pylint: disable=invalid-name
self.hass = core.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """

View File

@ -7,11 +7,12 @@ Tests mFi switch.
import unittest
import unittest.mock as mock
import homeassistant.core as ha
import homeassistant.components.switch as switch
import homeassistant.components.switch.mfi as mfi
from tests.components.sensor import test_mfi as test_mfi_sensor
from tests.common import get_test_home_assistant
class TestMfiSwitchSetup(test_mfi_sensor.TestMfiSensorSetup):
PLATFORM = mfi
@ -45,7 +46,7 @@ class TestMfiSwitchSetup(test_mfi_sensor.TestMfiSensorSetup):
class TestMfiSwitch(unittest.TestCase):
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.config.latitude = 32.87336
self.hass.config.longitude = 117.22743
self.port = mock.MagicMock()

View File

@ -7,16 +7,16 @@ Tests MQTT switch.
import unittest
from homeassistant.const import STATE_ON, STATE_OFF
import homeassistant.core as ha
import homeassistant.components.switch as switch
from tests.common import mock_mqtt_component, fire_mqtt_message
from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant)
class TestSensorMQTT(unittest.TestCase):
""" Test the MQTT switch. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.mock_publish = mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name

View File

@ -4,7 +4,6 @@ tests.components.switch.template
Tests template switch.
"""
import homeassistant.core as ha
import homeassistant.components as core
import homeassistant.components.switch as switch
@ -12,12 +11,14 @@ from homeassistant.const import (
STATE_ON,
STATE_OFF)
from tests.common import get_test_home_assistant
class TestTemplateSwitch:
""" Test the Template switch. """
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.calls = []

View File

@ -12,20 +12,19 @@ from unittest.mock import patch
import requests
from homeassistant import bootstrap, const
import homeassistant.core as ha
from homeassistant.components import alexa, http
from tests.common import get_test_instance_port, get_test_home_assistant
API_PASSWORD = "test1234"
# Somehow the socket that holds the default port does not get released
# when we close down HA in a different test case. Until I have figured
# out what is going on, let's run this test on a different port.
SERVER_PORT = 8119
SERVER_PORT = get_test_instance_port()
API_URL = "http://127.0.0.1:{}{}".format(SERVER_PORT, alexa.API_ENDPOINT)
HA_HEADERS = {const.HTTP_HEADER_HA_AUTH: API_PASSWORD}
SESSION_ID = 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000'
APPLICATION_ID = 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe'
REQUEST_ID = 'amzn1.echo-api.request.0000000-0000-0000-0000-00000000000'
hass = None
calls = []
@ -36,7 +35,7 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
""" Initalize a Home Assistant server for testing this module. """
global hass
hass = ha.HomeAssistant()
hass = get_test_home_assistant()
bootstrap.setup_component(
hass, http.DOMAIN,
@ -53,10 +52,16 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
'type': 'plaintext',
'text':
"""
{%- if is_state('device_tracker.paulus', 'home') and is_state('device_tracker.anne_therese', 'home') -%}
{%- if is_state('device_tracker.paulus', 'home')
and is_state('device_tracker.anne_therese',
'home') -%}
You are both home, you silly
{%- else -%}
Anne Therese is at {{ states("device_tracker.anne_therese") }} and Paulus is at {{ states("device_tracker.paulus") }}
Anne Therese is at {{
states("device_tracker.anne_therese")
}} and Paulus is at {{
states("device_tracker.paulus")
}}
{% endif %}
""",
}
@ -105,9 +110,9 @@ class TestAlexa(unittest.TestCase):
'version': '1.0',
'session': {
'new': True,
'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000',
'sessionId': SESSION_ID,
'application': {
'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe'
'applicationId': APPLICATION_ID
},
'attributes': {},
'user': {
@ -116,7 +121,7 @@ class TestAlexa(unittest.TestCase):
},
'request': {
'type': 'LaunchRequest',
'requestId': 'amzn1.echo-api.request.0000000-0000-0000-0000-00000000000',
'requestId': REQUEST_ID,
'timestamp': '2015-05-13T12:34:56Z'
}
}
@ -130,9 +135,9 @@ class TestAlexa(unittest.TestCase):
'version': '1.0',
'session': {
'new': False,
'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000',
'sessionId': SESSION_ID,
'application': {
'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe'
'applicationId': APPLICATION_ID
},
'attributes': {
'supportedHoroscopePeriods': {
@ -147,7 +152,7 @@ class TestAlexa(unittest.TestCase):
},
'request': {
'type': 'IntentRequest',
'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000',
'requestId': REQUEST_ID,
'timestamp': '2015-05-13T12:34:56Z',
'intent': {
'name': 'GetZodiacHoroscopeIntent',
@ -162,7 +167,8 @@ class TestAlexa(unittest.TestCase):
}
req = _req(data)
self.assertEqual(200, req.status_code)
text = req.json().get('response', {}).get('outputSpeech', {}).get('text')
text = req.json().get('response', {}).get('outputSpeech',
{}).get('text')
self.assertEqual('You told us your sign is virgo.', text)
def test_intent_request_with_slots_but_no_value(self):
@ -170,9 +176,9 @@ class TestAlexa(unittest.TestCase):
'version': '1.0',
'session': {
'new': False,
'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000',
'sessionId': SESSION_ID,
'application': {
'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe'
'applicationId': APPLICATION_ID
},
'attributes': {
'supportedHoroscopePeriods': {
@ -187,7 +193,7 @@ class TestAlexa(unittest.TestCase):
},
'request': {
'type': 'IntentRequest',
'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000',
'requestId': REQUEST_ID,
'timestamp': '2015-05-13T12:34:56Z',
'intent': {
'name': 'GetZodiacHoroscopeIntent',
@ -201,7 +207,8 @@ class TestAlexa(unittest.TestCase):
}
req = _req(data)
self.assertEqual(200, req.status_code)
text = req.json().get('response', {}).get('outputSpeech', {}).get('text')
text = req.json().get('response', {}).get('outputSpeech',
{}).get('text')
self.assertEqual('You told us your sign is .', text)
def test_intent_request_without_slots(self):
@ -209,9 +216,9 @@ class TestAlexa(unittest.TestCase):
'version': '1.0',
'session': {
'new': False,
'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000',
'sessionId': SESSION_ID,
'application': {
'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe'
'applicationId': APPLICATION_ID
},
'attributes': {
'supportedHoroscopePeriods': {
@ -226,7 +233,7 @@ class TestAlexa(unittest.TestCase):
},
'request': {
'type': 'IntentRequest',
'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000',
'requestId': REQUEST_ID,
'timestamp': '2015-05-13T12:34:56Z',
'intent': {
'name': 'WhereAreWeIntent',
@ -235,16 +242,19 @@ class TestAlexa(unittest.TestCase):
}
req = _req(data)
self.assertEqual(200, req.status_code)
text = req.json().get('response', {}).get('outputSpeech', {}).get('text')
text = req.json().get('response', {}).get('outputSpeech',
{}).get('text')
self.assertEqual('Anne Therese is at unknown and Paulus is at unknown', text)
self.assertEqual('Anne Therese is at unknown and Paulus is at unknown',
text)
hass.states.set('device_tracker.paulus', 'home')
hass.states.set('device_tracker.anne_therese', 'home')
req = _req(data)
self.assertEqual(200, req.status_code)
text = req.json().get('response', {}).get('outputSpeech', {}).get('text')
text = req.json().get('response', {}).get('outputSpeech',
{}).get('text')
self.assertEqual('You are both home, you silly', text)
def test_intent_request_calling_service(self):
@ -252,9 +262,9 @@ class TestAlexa(unittest.TestCase):
'version': '1.0',
'session': {
'new': False,
'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000',
'sessionId': SESSION_ID,
'application': {
'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe'
'applicationId': APPLICATION_ID
},
'attributes': {},
'user': {
@ -263,7 +273,7 @@ class TestAlexa(unittest.TestCase):
},
'request': {
'type': 'IntentRequest',
'requestId': ' amzn1.echo-api.request.0000000-0000-0000-0000-00000000000',
'requestId': REQUEST_ID,
'timestamp': '2015-05-13T12:34:56Z',
'intent': {
'name': 'CallServiceIntent',
@ -285,9 +295,9 @@ class TestAlexa(unittest.TestCase):
'version': '1.0',
'session': {
'new': False,
'sessionId': 'amzn1.echo-api.session.0000000-0000-0000-0000-00000000000',
'sessionId': SESSION_ID,
'application': {
'applicationId': 'amzn1.echo-sdk-ams.app.000000-d0ed-0000-ad00-000000d00ebe'
'applicationId': APPLICATION_ID
},
'attributes': {
'supportedHoroscopePeriods': {
@ -302,7 +312,7 @@ class TestAlexa(unittest.TestCase):
},
'request': {
'type': 'SessionEndedRequest',
'requestId': 'amzn1.echo-api.request.0000000-0000-0000-0000-00000000000',
'requestId': REQUEST_ID,
'timestamp': '2015-05-13T12:34:56Z',
'reason': 'USER_INITIATED'
}

View File

@ -17,15 +17,11 @@ from homeassistant import bootstrap, const
import homeassistant.core as ha
import homeassistant.components.http as http
from tests.common import get_test_instance_port, get_test_home_assistant
API_PASSWORD = "test1234"
# Somehow the socket that holds the default port does not get released
# when we close down HA in a different test case. Until I have figured
# out what is going on, let's run this test on a different port.
SERVER_PORT = 8120
SERVER_PORT = get_test_instance_port()
HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT)
HA_HEADERS = {const.HTTP_HEADER_HA_AUTH: API_PASSWORD}
hass = None
@ -42,7 +38,7 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
""" Initializes a Home Assistant server. """
global hass
hass = ha.HomeAssistant()
hass = get_test_home_assistant()
hass.bus.listen('test_event', lambda _: _)
hass.states.set('test.test', 'a_state')
@ -386,7 +382,7 @@ class TestAPI(unittest.TestCase):
data=json.dumps({
'api_password': 'bla-di-bla',
'host': '127.0.0.1',
'port': '8125'
'port': get_test_instance_port()
}),
headers=HA_HEADERS)
self.assertEqual(422, req.status_code)

View File

@ -7,16 +7,17 @@ Tests Configurator component.
# pylint: disable=too-many-public-methods,protected-access
import unittest
import homeassistant.core as ha
import homeassistant.components.configurator as configurator
from homeassistant.const import EVENT_TIME_CHANGED
from tests.common import get_test_home_assistant
class TestConfigurator(unittest.TestCase):
""" Test the chromecast module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """

View File

@ -5,14 +5,14 @@ tests.test_component_demo
Tests demo component.
"""
import json
import os
import unittest
from unittest.mock import patch
import homeassistant.core as ha
import homeassistant.components.demo as demo
from homeassistant.components import demo, device_tracker
from homeassistant.remote import JSONEncoder
from tests.common import mock_http_component
from tests.common import mock_http_component, get_test_home_assistant
@patch('homeassistant.components.sun.setup')
@ -20,13 +20,18 @@ class TestDemo(unittest.TestCase):
""" Test the demo module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
mock_http_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
self.hass.stop()
try:
os.remove(self.hass.config.path(device_tracker.YAML_DEVICES))
except FileNotFoundError:
pass
def test_if_demo_state_shows_by_default(self, mock_sun_setup):
""" Test if demo state shows if we give no configuration. """
demo.setup(self.hass, {demo.DOMAIN: {}})

View File

@ -19,17 +19,15 @@ from tests.common import (
ensure_sun_set)
KNOWN_DEV_PATH = None
KNOWN_DEV_CSV_PATH = os.path.join(get_test_config_dir(),
device_tracker.CSV_DEVICES)
KNOWN_DEV_YAML_PATH = os.path.join(get_test_config_dir(),
device_tracker.YAML_DEVICES)
def setUpModule(): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server. """
global KNOWN_DEV_PATH
KNOWN_DEV_PATH = os.path.join(get_test_config_dir(),
device_tracker.CSV_DEVICES)
with open(KNOWN_DEV_PATH, 'w') as fil:
with open(KNOWN_DEV_CSV_PATH, 'w') as fil:
fil.write('device,name,track,picture\n')
fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n')
fil.write('DEV2,device 2,1,http://example.com/dev2.jpg\n')
@ -37,8 +35,9 @@ def setUpModule(): # pylint: disable=invalid-name
def tearDownModule(): # pylint: disable=invalid-name
""" Stops the Home Assistant server. """
os.remove(os.path.join(get_test_config_dir(),
device_tracker.YAML_DEVICES))
for fil in (KNOWN_DEV_CSV_PATH, KNOWN_DEV_YAML_PATH):
if os.path.isfile(fil):
os.remove(fil)
class TestDeviceSunLightTrigger(unittest.TestCase):

View File

@ -11,20 +11,15 @@ from unittest.mock import patch
import requests
import homeassistant.core as ha
import homeassistant.bootstrap as bootstrap
import homeassistant.components.http as http
from homeassistant.const import HTTP_HEADER_HA_AUTH
from tests.common import get_test_instance_port, get_test_home_assistant
API_PASSWORD = "test1234"
# Somehow the socket that holds the default port does not get released
# when we close down HA in a different test case. Until I have figured
# out what is going on, let's run this test on a different port.
SERVER_PORT = 8121
SERVER_PORT = get_test_instance_port()
HTTP_BASE_URL = "http://127.0.0.1:{}".format(SERVER_PORT)
HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD}
hass = None
@ -41,7 +36,7 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server. """
global hass
hass = ha.HomeAssistant()
hass = get_test_home_assistant()
hass.bus.listen('test_event', lambda _: _)
hass.states.set('test.test', 'a_state')

View File

@ -15,10 +15,12 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
STATE_ON, STATE_OFF)
from tests.common import get_test_home_assistant
class TestGraphite(unittest.TestCase):
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.config.latitude = 32.87336
self.hass.config.longitude = 117.22743
self.gf = graphite.GraphiteFeeder(self.hass, 'foo', 123, 'ha')

View File

@ -6,17 +6,12 @@ Tests the group compoments.
"""
# pylint: disable=protected-access,too-many-public-methods
import unittest
import logging
import homeassistant.core as ha
from homeassistant.const import (
STATE_ON, STATE_OFF, STATE_HOME, STATE_UNKNOWN, ATTR_ICON, ATTR_HIDDEN)
import homeassistant.components.group as group
def setUpModule(): # pylint: disable=invalid-name
""" Setup to ignore group errors. """
logging.disable(logging.CRITICAL)
from tests.common import get_test_home_assistant
class TestComponentsGroup(unittest.TestCase):
@ -24,7 +19,7 @@ class TestComponentsGroup(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.states.set('light.Bowl', STATE_ON)
self.hass.states.set('light.Ceiling', STATE_OFF)

View File

@ -6,15 +6,16 @@ Test introduction.
"""
import unittest
import homeassistant.core as ha
from homeassistant.components import introduction
from tests.common import get_test_home_assistant
class TestIntroduction(unittest.TestCase):
""" Test Introduction. """
def setUp(self):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self):
""" Stop down stuff we started. """

View File

@ -14,7 +14,7 @@ from homeassistant.const import (
import homeassistant.util.dt as dt_util
from homeassistant.components import logbook
from tests.common import mock_http_component
from tests.common import mock_http_component, get_test_home_assistant
class TestComponentHistory(unittest.TestCase):
@ -22,7 +22,7 @@ class TestComponentHistory(unittest.TestCase):
def setUp(self):
""" Test setup method. """
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
mock_http_component(self.hass)
self.assertTrue(logbook.setup(self.hass, {}))

View File

@ -4,15 +4,16 @@ tests.components.test_proximity
Tests proximity component.
"""
import homeassistant.core as ha
from homeassistant.components import proximity
from tests.common import get_test_home_assistant
class TestProximity:
""" Test the Proximity component. """
def setup_method(self, method):
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.states.set(
'zone.home', 'zoning',
{
@ -162,7 +163,8 @@ class TestProximity:
self.hass.pool.block_till_done()
state = self.hass.states.get('proximity.home')
assert state.state == '0'
assert (state.attributes.get('nearest') == 'test1, test2') or (state.attributes.get('nearest') == 'test2, test1')
assert ((state.attributes.get('nearest') == 'test1, test2') or
(state.attributes.get('nearest') == 'test2, test1'))
assert state.attributes.get('dir_of_travel') == 'arrived'
def test_device_tracker_test1_away(self):
@ -447,7 +449,7 @@ class TestProximity:
assert state.attributes.get('nearest') == 'test1'
assert state.attributes.get('dir_of_travel') == 'unknown'
def test_device_tracker_test1_awayfurther_than_test2_first_test1_than_test2_than_test1(self):
def test_device_tracker_test1_awayfurther_test2_first(self):
self.hass.states.set(
'device_tracker.test1', 'not_home',
{

View File

@ -10,15 +10,16 @@ import unittest
from unittest.mock import patch
from subprocess import SubprocessError
from homeassistant import core
from homeassistant.components import shell_command
from tests.common import get_test_home_assistant
class TestShellCommand(unittest.TestCase):
""" Test the demo module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = core.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """

View File

@ -14,12 +14,14 @@ import homeassistant.core as ha
import homeassistant.util.dt as dt_util
import homeassistant.components.sun as sun
from tests.common import get_test_home_assistant
class TestSun(unittest.TestCase):
""" Test the sun module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """

View File

@ -9,11 +9,10 @@ from unittest.mock import patch
import requests
import homeassistant.core as ha
from homeassistant.const import __version__ as CURRENT_VERSION
from homeassistant.components import updater
import homeassistant.util.dt as dt_util
from tests.common import fire_time_changed
from tests.common import fire_time_changed, get_test_home_assistant
NEW_VERSION = '10000.0'
@ -22,7 +21,7 @@ class TestUpdater(unittest.TestCase):
""" Test the demo lock. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """

View File

@ -6,21 +6,22 @@ Tests weblink component.
"""
import unittest
import homeassistant.core as ha
from homeassistant.components import weblink
from tests.common import get_test_home_assistant
class TestComponentHistory(unittest.TestCase):
class TestComponentWeblink(unittest.TestCase):
""" Tests homeassistant.components.history module. """
def setUp(self):
""" Test setup method. """
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self):
self.hass.stop()
def test_setup(self):
def test_entities_get_created(self):
self.assertTrue(weblink.setup(self.hass, {
weblink.DOMAIN: {
'entities': [
@ -32,3 +33,8 @@ class TestComponentHistory(unittest.TestCase):
]
}
}))
state = self.hass.states.get('weblink.my_router')
assert state is not None
assert state.state == 'http://127.0.0.1/'

View File

@ -14,9 +14,10 @@ from homeassistant.const import (
STATE_OFF,
TEMP_CELCIUS,
)
import homeassistant.core as ha
from homeassistant.components import thermostat
from tests.common import get_test_home_assistant
entity = 'thermostat.test'
ent_sensor = 'sensor.test'
@ -30,7 +31,7 @@ class TestThermostatHeatControl(unittest.TestCase):
""" Test the Heat Control thermostat. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.config.temperature_unit = TEMP_CELCIUS
thermostat.setup(self.hass, {'thermostat': {
'platform': 'heat_control',

View File

@ -131,7 +131,6 @@ class TestHoneywell(unittest.TestCase):
devices = [x[0][1].deviceid for x in result]
self.assertEqual([mock.sentinel.loc2dev1], devices)
@mock.patch('evohomeclient.EvohomeClient')
@mock.patch('homeassistant.components.thermostat.honeywell.'
'RoundThermostat')

View File

@ -7,10 +7,11 @@ Tests the entity helper.
# pylint: disable=protected-access,too-many-public-methods
import unittest
import homeassistant.core as ha
import homeassistant.helpers.entity as entity
from homeassistant.const import ATTR_HIDDEN
from tests.common import get_test_home_assistant
class TestHelpersEntity(unittest.TestCase):
""" Tests homeassistant.helpers.entity module. """
@ -19,7 +20,7 @@ class TestHelpersEntity(unittest.TestCase):
""" Init needed objects. """
self.entity = entity.Entity()
self.entity.entity_id = 'test.overwrite_hidden_true'
self.hass = self.entity.hass = ha.HomeAssistant()
self.hass = self.entity.hass = get_test_home_assistant()
self.entity.update_ha_state()
def tearDown(self): # pylint: disable=invalid-name

View File

@ -7,13 +7,24 @@ Tests event helpers.
# pylint: disable=protected-access,too-many-public-methods
# pylint: disable=too-few-public-methods
import unittest
from datetime import datetime
from datetime import datetime, timedelta
from astral import Astral
import homeassistant.core as ha
from homeassistant.helpers.event import *
from homeassistant.helpers.event import (
track_point_in_utc_time,
track_point_in_time,
track_utc_time_change,
track_time_change,
track_state_change,
track_sunrise,
track_sunset,
)
from homeassistant.components import sun
import homeassistant.util.dt as dt_util
from tests.common import get_test_home_assistant
class TestEventHelpers(unittest.TestCase):
@ -23,7 +34,7 @@ class TestEventHelpers(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
""" things to be run when tests are started. """
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """

View File

@ -19,6 +19,8 @@ from homeassistant.helpers.event_decorators import (
track_sunrise, track_sunset)
from homeassistant.components import sun
from tests.common import get_test_home_assistant
class TestEventDecoratorHelpers(unittest.TestCase):
"""
@ -27,7 +29,7 @@ class TestEventDecoratorHelpers(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
""" things to be run when tests are started. """
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.states.set("light.Bowl", "on")
self.hass.states.set("switch.AC", "off")

View File

@ -14,8 +14,8 @@ from homeassistant.const import SERVICE_TURN_ON
from homeassistant.util import dt as dt_util
from homeassistant.helpers import state
from homeassistant.const import (
STATE_OFF, STATE_OPEN, STATE_CLOSED,
STATE_LOCKED, STATE_UNLOCKED, STATE_UNKNOWN,
STATE_OPEN, STATE_CLOSED,
STATE_LOCKED, STATE_UNLOCKED,
STATE_ON, STATE_OFF)
from homeassistant.components.sun import (STATE_ABOVE_HORIZON,
STATE_BELOW_HORIZON)

View File

@ -9,12 +9,14 @@ import os
import tempfile
import unittest
from homeassistant import core, bootstrap
from homeassistant import bootstrap
from homeassistant.const import (__version__, CONF_LATITUDE, CONF_LONGITUDE,
CONF_NAME, CONF_CUSTOMIZE)
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity
from tests.common import get_test_home_assistant
class TestBootstrap(unittest.TestCase):
""" Test the bootstrap utils. """
@ -53,7 +55,7 @@ class TestBootstrap(unittest.TestCase):
with open(check_file, 'w'):
pass
hass = core.HomeAssistant()
hass = get_test_home_assistant()
hass.config.config_dir = config_dir
self.assertTrue(os.path.isfile(check_file))
@ -74,7 +76,7 @@ class TestBootstrap(unittest.TestCase):
with open(check_file, 'w'):
pass
hass = core.HomeAssistant()
hass = get_test_home_assistant()
hass.config.config_dir = config_dir
bootstrap.process_ha_config_upgrade(hass)
@ -88,7 +90,7 @@ class TestBootstrap(unittest.TestCase):
CONF_NAME: 'Test',
CONF_CUSTOMIZE: {'test.test': {'hidden': True}}}
hass = core.HomeAssistant()
hass = get_test_home_assistant()
bootstrap.process_ha_core_config(hass, config)

View File

@ -25,6 +25,8 @@ from homeassistant.const import (
EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, TEMP_CELCIUS,
TEMP_FAHRENHEIT)
from tests.common import get_test_home_assistant
PST = pytz.timezone('America/Los_Angeles')
@ -35,7 +37,7 @@ class TestHomeAssistant(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
""" things to be run when tests are started. """
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
self.hass.states.set("light.Bowl", "on")
self.hass.states.set("switch.AC", "off")

View File

@ -3,8 +3,6 @@ tests.remote
~~~~~~~~~~~~
Tests Home Assistant remote methods and classes.
Uses port 8122 for master, 8123 for slave
Uses port 8125 as a port that nothing runs on
"""
# pylint: disable=protected-access,too-many-public-methods
import unittest
@ -16,9 +14,13 @@ import homeassistant.remote as remote
import homeassistant.components.http as http
from homeassistant.const import HTTP_HEADER_HA_AUTH
API_PASSWORD = "test1234"
from tests.common import get_test_instance_port, get_test_home_assistant
HTTP_BASE_URL = "http://127.0.0.1:8122"
API_PASSWORD = "test1234"
MASTER_PORT = get_test_instance_port()
SLAVE_PORT = get_test_instance_port()
BROKEN_PORT = get_test_instance_port()
HTTP_BASE_URL = "http://127.0.0.1:{}".format(MASTER_PORT)
HA_HEADERS = {HTTP_HEADER_HA_AUTH: API_PASSWORD}
@ -36,7 +38,7 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server and Slave instance. """
global hass, slave, master_api, broken_api
hass = ha.HomeAssistant()
hass = get_test_home_assistant()
hass.bus.listen('test_event', lambda _: _)
hass.states.set('test.test', 'a_state')
@ -44,25 +46,25 @@ def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
bootstrap.setup_component(
hass, http.DOMAIN,
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
http.CONF_SERVER_PORT: 8122}})
http.CONF_SERVER_PORT: MASTER_PORT}})
bootstrap.setup_component(hass, 'api')
hass.start()
master_api = remote.API("127.0.0.1", API_PASSWORD, 8122)
master_api = remote.API("127.0.0.1", API_PASSWORD, MASTER_PORT)
# Start slave
slave = remote.HomeAssistant(master_api)
bootstrap.setup_component(
slave, http.DOMAIN,
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
http.CONF_SERVER_PORT: 8130}})
http.CONF_SERVER_PORT: SLAVE_PORT}})
slave.start()
# Setup API pointing at nothing
broken_api = remote.API("127.0.0.1", "", 8125)
broken_api = remote.API("127.0.0.1", "", BROKEN_PORT)
def tearDownModule(): # pylint: disable=invalid-name
@ -83,7 +85,7 @@ class TestRemoteMethods(unittest.TestCase):
self.assertEqual(
remote.APIStatus.INVALID_PASSWORD,
remote.validate_api(
remote.API("127.0.0.1", API_PASSWORD + "A", 8122)))
remote.API("127.0.0.1", API_PASSWORD + "A", MASTER_PORT)))
self.assertEqual(
remote.APIStatus.CANNOT_CONNECT, remote.validate_api(broken_api))
@ -210,7 +212,7 @@ class TestRemoteClasses(unittest.TestCase):
# Wrong port
self.assertRaises(
ha.HomeAssistantError, remote.HomeAssistant,
remote.API('127.0.0.1', API_PASSWORD, 8125))
remote.API('127.0.0.1', API_PASSWORD, BROKEN_PORT))
def test_statemachine_init(self):
""" Tests if remote.StateMachine copies all states on init. """

View File

@ -6,15 +6,16 @@ Tests Home Assistant template util methods.
"""
# pylint: disable=too-many-public-methods
import unittest
import homeassistant.core as ha
from homeassistant.exceptions import TemplateError
from homeassistant.util import template
from tests.common import get_test_home_assistant
class TestUtilTemplate(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
@ -45,7 +46,9 @@ class TestUtilTemplate(unittest.TestCase):
'open10',
template.render(
self.hass,
'{% for state in states.sensor %}{{ state.state }}{% endfor %}'))
"""
{% for state in states.sensor %}{{ state.state }}{% endfor %}
"""))
def test_rounding_value(self):
self.hass.states.set('sensor.temperature', 12.78)
@ -63,7 +66,8 @@ class TestUtilTemplate(unittest.TestCase):
'128',
template.render(
self.hass,
'{{ states.sensor.temperature.state | multiply(10) | round }}'))
'{{ states.sensor.temperature.state | multiply(10) | round }}'
))
def test_passing_vars_as_keywords(self):
self.assertEqual(
@ -91,7 +95,7 @@ class TestUtilTemplate(unittest.TestCase):
template.render_with_possible_json_value(
self.hass, '{{ value_json', 'hello'))
def test_render_with_possible_json_value_with_template_error_error_value(self):
def test_render_with_possible_json_value_with_template_error_value(self):
self.assertEqual(
'-',
template.render_with_possible_json_value(
@ -107,7 +111,9 @@ class TestUtilTemplate(unittest.TestCase):
'exists',
template.render(
self.hass,
'{% if states.test.object %}exists{% else %}not exists{% endif %}'))
"""
{% if states.test.object %}exists{% else %}not exists{% endif %}
"""))
def test_is_state(self):
self.hass.states.set('test.object', 'available')
@ -115,7 +121,9 @@ class TestUtilTemplate(unittest.TestCase):
'yes',
template.render(
self.hass,
'{% if is_state("test.object", "available") %}yes{% else %}no{% endif %}'))
"""
{% if is_state("test.object", "available") %}yes{% else %}no{% endif %}
"""))
def test_is_state_attr(self):
self.hass.states.set('test.object', 'available', {'mode': 'on'})
@ -123,7 +131,9 @@ class TestUtilTemplate(unittest.TestCase):
'yes',
template.render(
self.hass,
'{% if is_state_attr("test.object", "mode", "on") %}yes{% else %}no{% endif %}'))
"""
{% if is_state_attr("test.object", "mode", "on") %}yes{% else %}no{% endif %}
"""))
def test_states_function(self):
self.hass.states.set('test.object', 'available')