Skip to content

Commit

Permalink
Merge branch 'pep257-tests' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff committed Mar 9, 2016
2 parents d1256d4 + 9838697 commit 986c9c5
Show file tree
Hide file tree
Showing 120 changed files with 1,447 additions and 1,297 deletions.
7 changes: 1 addition & 6 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"""
tests.__init__
~~~~~~~~~~~~~~
Tests initialization.
"""
"""Test the initialization."""
import betamax

from homeassistant import util
Expand Down
46 changes: 24 additions & 22 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"""
tests.common
~~~~~~~~~~~~
Helper method for writing tests.
"""
"""Test the helper method for writing tests."""
import os
from datetime import timedelta
from unittest import mock
Expand All @@ -20,12 +15,12 @@


def get_test_config_dir():
""" Returns a path to a test config dir. """
"""Return a path to a test config dir."""
return os.path.join(os.path.dirname(__file__), "config")


def get_test_home_assistant(num_threads=None):
""" Returns a Home Assistant object pointing at test config dir. """
"""Return a Home Assistant object pointing at test config dir."""
if num_threads:
orig_num_threads = ha.MIN_WORKER_THREAD
ha.MIN_WORKER_THREAD = num_threads
Expand Down Expand Up @@ -58,9 +53,9 @@ def get_test_instance_port():


def mock_service(hass, domain, service):
"""
Sets up a fake service.
Returns a list that logs all calls to fake service.
"""Setup a fake service.
Return a list that logs all calls to fake service.
"""
calls = []

Expand All @@ -71,6 +66,7 @@ def mock_service(hass, domain, service):


def fire_mqtt_message(hass, topic, payload, qos=0):
"""Fire the MQTT message."""
hass.bus.fire(mqtt.EVENT_MQTT_MESSAGE_RECEIVED, {
mqtt.ATTR_TOPIC: topic,
mqtt.ATTR_PAYLOAD: payload,
Expand All @@ -79,25 +75,27 @@ def fire_mqtt_message(hass, topic, payload, qos=0):


def fire_time_changed(hass, time):
"""Fire a time changes event."""
hass.bus.fire(EVENT_TIME_CHANGED, {'now': time})


def fire_service_discovered(hass, service, info):
"""Fire the MQTT message."""
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
ATTR_SERVICE: service,
ATTR_DISCOVERED: info
})


def ensure_sun_risen(hass):
""" Trigger sun to rise if below horizon. """
"""Trigger sun to rise if below horizon."""
if sun.is_on(hass):
return
fire_time_changed(hass, sun.next_rising_utc(hass) + timedelta(seconds=10))


def ensure_sun_set(hass):
""" Trigger sun to set if above horizon. """
"""Trigger sun to set if above horizon."""
if not sun.is_on(hass):
return
fire_time_changed(hass, sun.next_setting_utc(hass) + timedelta(seconds=10))
Expand All @@ -116,12 +114,14 @@ def mock_state_change_event(hass, new_state, old_state=None):


def mock_http_component(hass):
"""Mock the HTTP component."""
hass.http = MockHTTP()
hass.config.components.append('http')


@mock.patch('homeassistant.components.mqtt.MQTT')
def mock_mqtt_component(hass, mock_mqtt):
"""Mock the MQTT component."""
mqtt.setup(hass, {
mqtt.DOMAIN: {
mqtt.CONF_BROKER: 'mock-broker',
Expand All @@ -132,8 +132,7 @@ def mock_mqtt_component(hass, mock_mqtt):


class MockHTTP(object):
""" Mocks the HTTP module. """

"""Mock the HTTP module."""
def register_path(self, method, url, callback, require_auth=True):
pass

Expand All @@ -152,53 +151,56 @@ def __init__(self, domain=None, dependencies=[], setup=None):


class MockPlatform(object):
""" Provides a fake platform. """
"""Provide a fake platform."""

def __init__(self, setup_platform=None, dependencies=[]):
"""Initialize the platform."""
self.DEPENDENCIES = dependencies
self._setup_platform = setup_platform

def setup_platform(self, hass, config, add_devices, discovery_info=None):
"""Setup the platform."""
if self._setup_platform is not None:
self._setup_platform(hass, config, add_devices, discovery_info)


class MockToggleDevice(ToggleEntity):
""" Provides a mock toggle device. """
"""Provide a mock toggle device."""
def __init__(self, name, state):
self._name = name or DEVICE_DEFAULT_NAME
self._state = state
self.calls = []

@property
def name(self):
""" Returns the name of the device if any. """
"""Return the name of the device if any."""
self.calls.append(('name', {}))
return self._name

@property
def state(self):
""" Returns the name of the device if any. """
"""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. """
"""Return true if device is on."""
self.calls.append(('is_on', {}))
return self._state == STATE_ON

def turn_on(self, **kwargs):
""" Turn the device on. """
"""Turn the device on."""
self.calls.append(('turn_on', kwargs))
self._state = STATE_ON

def turn_off(self, **kwargs):
""" Turn the device off. """
"""Turn the device off."""
self.calls.append(('turn_off', kwargs))
self._state = STATE_OFF

def last_call(self, method=None):
"""Return the last call."""
if not self.calls:
return None
elif method is None:
Expand Down
1 change: 1 addition & 0 deletions tests/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The tests for components."""
1 change: 1 addition & 0 deletions tests/components/alarm_control_panel/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The tests for Alarm control panel platforms."""
30 changes: 14 additions & 16 deletions tests/components/alarm_control_panel/test_manual.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"""
tests.components.alarm_control_panel.test_manual
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests manual alarm control panel component.
"""
"""The tests for the manual Alarm Control Panel component."""
from datetime import timedelta
import unittest
from unittest.mock import patch
Expand All @@ -20,17 +15,18 @@


class TestAlarmControlPanelManual(unittest.TestCase):
""" Test the manual alarm module. """
"""Test the manual alarm module."""

def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()

def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
"""Stop down everything that was started."""
self.hass.stop()

def test_arm_home_no_pending(self):
""" Test arm home method. """
"""Test arm home method."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand All @@ -51,7 +47,7 @@ def test_arm_home_no_pending(self):
self.hass.states.get(entity_id).state)

def test_arm_home_with_pending(self):
""" Test arm home method. """
"""Test arm home method."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand Down Expand Up @@ -81,7 +77,7 @@ def test_arm_home_with_pending(self):
self.hass.states.get(entity_id).state)

def test_arm_home_with_invalid_code(self):
""" Attempt to arm home without a valid code. """
"""Attempt to arm home without a valid code."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand All @@ -102,7 +98,7 @@ def test_arm_home_with_invalid_code(self):
self.hass.states.get(entity_id).state)

def test_arm_away_no_pending(self):
""" Test arm home method. """
"""Test arm home method."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand All @@ -123,7 +119,7 @@ def test_arm_away_no_pending(self):
self.hass.states.get(entity_id).state)

def test_arm_away_with_pending(self):
""" Test arm home method. """
"""Test arm home method."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand Down Expand Up @@ -153,7 +149,7 @@ def test_arm_away_with_pending(self):
self.hass.states.get(entity_id).state)

def test_arm_away_with_invalid_code(self):
""" Attempt to arm away without a valid code. """
"""Attempt to arm away without a valid code."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand All @@ -174,7 +170,7 @@ def test_arm_away_with_invalid_code(self):
self.hass.states.get(entity_id).state)

def test_trigger_no_pending(self):
""" Test arm home method. """
"""Test arm home method."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand All @@ -194,7 +190,7 @@ def test_trigger_no_pending(self):
self.hass.states.get(entity_id).state)

def test_trigger_with_pending(self):
""" Test arm home method. """
"""Test arm home method."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand Down Expand Up @@ -233,6 +229,7 @@ def test_trigger_with_pending(self):
self.hass.states.get(entity_id).state)

def test_disarm_while_pending_trigger(self):
"""Test disarming while pending state."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand Down Expand Up @@ -267,6 +264,7 @@ def test_disarm_while_pending_trigger(self):
self.hass.states.get(entity_id).state)

def test_disarm_during_trigger_with_invalid_code(self):
"""Test disarming while code is invalid."""
self.assertTrue(alarm_control_panel.setup(self.hass, {
'alarm_control_panel': {
'platform': 'manual',
Expand Down
Loading

0 comments on commit 986c9c5

Please sign in to comment.