Fix PEP257 issues

This commit is contained in:
Fabian Affolter committed 2016-03-09 10:25:50 +01:00
1 parent d6eab03a61
commit 9838697d2b
120 files changed
+1447 -1297

No files matched your search

@@ -0,0 +1 @@
"""The tests for Binary sensor platforms."""
@@ -1,8 +1,4 @@
"""
tests.components.binary_sensor.test_binary_sensor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test the binary_sensor base class
"""
"""The tests for the Binary sensor component."""
import unittest
from unittest import mock
@@ -1,9 +1,4 @@
"""
tests.components.binary_sensor.command_line
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests command binary sensor.
"""
"""The tests fr the Command line Binary sensor platform."""
import unittest
from homeassistant.const import (STATE_ON, STATE_OFF)
@@ -13,17 +8,18 @@ from tests.common import get_test_home_assistant
class TestCommandSensorBinarySensor(unittest.TestCase):
""" Test the Template sensor. """
"""Test the Command line Binary sensor."""
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self):
""" Stop down stuff we started. """
"""Stop everything that was started."""
self.hass.stop()
def test_setup(self):
""" Test sensor setup """
"""Test sensor setup."""
config = {'name': 'Test',
'command': 'echo 1',
'payload_on': '1',
@@ -31,7 +27,7 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
devices = []
def add_dev_callback(devs):
""" callback to add device """
"""Add callback to add devices."""
for dev in devs:
devices.append(dev)
@@ -44,13 +40,13 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
self.assertEqual(STATE_ON, entity.state)
def test_setup_bad_config(self):
""" Test setup with a bad config """
"""Test the setup with a bad configuration."""
config = {}
devices = []
def add_dev_callback(devs):
""" callback to add device """
"""Add callback to add devices."""
for dev in devs:
devices.append(dev)
@@ -60,7 +56,7 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
self.assertEqual(0, len(devices))
def test_template(self):
""" Test command sensor with template """
"""Test setting the state with a template."""
data = command_line.CommandSensorData('echo 10')
entity = command_line.CommandBinarySensor(
@@ -69,7 +65,7 @@ class TestCommandSensorBinarySensor(unittest.TestCase):
self.assertEqual(STATE_ON, entity.state)
def test_sensor_off(self):
""" Test command sensor with template """
"""Test setting the state with a template."""
data = command_line.CommandSensorData('echo 0')
entity = command_line.CommandBinarySensor(
+5 -8
View File
@@ -1,9 +1,4 @@
"""
tests.components.binary_sensor.test_mqtt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests MQTT binary sensor.
"""
"""The tests for the MQTT binary sensor platform."""
import unittest
import homeassistant.components.binary_sensor as binary_sensor
@@ -14,17 +9,19 @@ from tests.common import get_test_home_assistant
class TestSensorMQTT(unittest.TestCase):
""" Test the MQTT sensor. """
"""Test the MQTT sensor."""
def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
"""Stop everything that was started."""
self.hass.stop()
def test_setting_sensor_value_via_mqtt_message(self):
"""Test the setting of the value via MQTT."""
self.assertTrue(binary_sensor.setup(self.hass, {
'binary_sensor': {
'platform': 'mqtt',
+24 -7
View File
@@ -1,9 +1,4 @@
"""
tests.components.binary_sensor.nx584
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests for nx584 sensor.
"""
"""The tests for the nx584 sensor platform."""
import requests
import unittest
from unittest import mock
@@ -13,11 +8,16 @@ from nx584 import client as nx584_client
class StopMe(Exception):
"""Stop helper."""
pass
class TestNX584SensorSetup(unittest.TestCase):
"""Test the nx584 sensor platform."""
def setUp(self):
"""Setup things to be run when tests are started."""
self._mock_client = mock.patch.object(nx584_client, 'Client')
self._mock_client.start()
@@ -32,11 +32,13 @@ class TestNX584SensorSetup(unittest.TestCase):
client.get_version.return_value = '1.1'
def tearDown(self):
"""Stop everything that was started."""
self._mock_client.stop()
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584Watcher')
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor')
def test_setup_no_config(self, mock_nx, mock_watcher):
"""Test the setup with no configuration."""
add_devices = mock.MagicMock()
hass = mock.MagicMock()
self.assertTrue(nx584.setup_platform(hass, {}, add_devices))
@@ -49,6 +51,7 @@ class TestNX584SensorSetup(unittest.TestCase):
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584Watcher')
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor')
def test_setup_full_config(self, mock_nx, mock_watcher):
"""Test the setup with full configuration."""
config = {
'host': 'foo:123',
'exclude_zones': [2],
@@ -66,12 +69,14 @@ class TestNX584SensorSetup(unittest.TestCase):
self.assertTrue(mock_watcher.called)
def _test_assert_graceful_fail(self, config):
"""Test the failing."""
hass = add_devices = mock.MagicMock()
self.assertFalse(nx584.setup_platform(hass, config,
add_devices))
self.assertFalse(add_devices.called)
def test_setup_bad_config(self):
"""Test the setup with bad configuration."""
bad_configs = [
{'exclude_zones': ['a']},
{'zone_types': {'a': 'b'}},
@@ -82,15 +87,18 @@ class TestNX584SensorSetup(unittest.TestCase):
self._test_assert_graceful_fail(config)
def test_setup_connect_failed(self):
"""Test the setup with connection failure."""
nx584_client.Client.return_value.list_zones.side_effect = \
requests.exceptions.ConnectionError
self._test_assert_graceful_fail({})
def test_setup_version_too_old(self):
""""Test if version is too old."""
nx584_client.Client.return_value.get_version.return_value = '1.0'
self._test_assert_graceful_fail({})
def test_setup_no_zones(self):
"""Test the setup with no zones."""
nx584_client.Client.return_value.list_zones.return_value = []
hass = add_devices = mock.MagicMock()
self.assertTrue(nx584.setup_platform(hass, {},
@@ -99,7 +107,10 @@ class TestNX584SensorSetup(unittest.TestCase):
class TestNX584ZoneSensor(unittest.TestCase):
"""Test for the nx584 zone sensor."""
def test_sensor_normal(self):
"""Test the sensor."""
zone = {'number': 1, 'name': 'foo', 'state': True}
sensor = nx584.NX584ZoneSensor(zone, 'motion')
self.assertEqual('foo', sensor.name)
@@ -111,8 +122,11 @@ class TestNX584ZoneSensor(unittest.TestCase):
class TestNX584Watcher(unittest.TestCase):
"""Test the nx584 watcher."""
@mock.patch.object(nx584.NX584ZoneSensor, 'update_ha_state')
def test_process_zone_event(self, mock_update):
"""Test the processing of zone events."""
zone1 = {'number': 1, 'name': 'foo', 'state': True}
zone2 = {'number': 2, 'name': 'bar', 'state': True}
zones = {
@@ -126,15 +140,17 @@ class TestNX584Watcher(unittest.TestCase):
@mock.patch.object(nx584.NX584ZoneSensor, 'update_ha_state')
def test_process_zone_event_missing_zone(self, mock_update):
"""Test the processing of zone events with missing zones."""
watcher = nx584.NX584Watcher(None, {})
watcher._process_zone_event({'zone': 1, 'zone_state': False})
self.assertFalse(mock_update.called)
def test_run_with_zone_events(self):
"""Test the zone events."""
empty_me = [1, 2]
def fake_get_events():
"""Return nothing twice, then some events"""
"""Return nothing twice, then some events."""
if empty_me:
empty_me.pop()
else:
@@ -159,6 +175,7 @@ class TestNX584Watcher(unittest.TestCase):
@mock.patch('time.sleep')
def test_run_retries_failures(self, mock_sleep):
"""Test the retries with failures."""
empty_me = [1, 2]
def fake_run():
+9 -12
View File
@@ -1,9 +1,4 @@
"""
tests.components.sensor.tcp
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests TCP sensor.
"""
"""The tests for the TCP binary sensor platform."""
from copy import copy
from unittest.mock import patch, Mock
@@ -15,7 +10,7 @@ from tests.components.sensor import test_tcp
@patch('homeassistant.components.sensor.tcp.Sensor.update')
def test_setup_platform_valid_config(mock_update):
""" Should check the supplied config and call add_entities with Sensor. """
"""Should check the supplied config and call add_entities with Sensor."""
add_entities = Mock()
ret = bin_tcp.setup_platform(None, test_tcp.TEST_CONFIG, add_entities)
assert ret is None, "setup_platform() should return None if successful."
@@ -24,23 +19,25 @@ def test_setup_platform_valid_config(mock_update):
def test_setup_platform_invalid_config():
""" Should check the supplied config and return False if it is invalid. """
"""Should check the supplied config and return False if it is invalid."""
config = copy(test_tcp.TEST_CONFIG)
del config[tcp.CONF_HOST]
assert bin_tcp.setup_platform(None, config, None) is False
class TestTCPBinarySensor():
""" Test the TCP Binary Sensor. """
"""Test the TCP Binary Sensor."""
def setup_class(cls):
"""Setup things to be run when tests are started."""
cls.hass = get_test_home_assistant()
def teardown_class(cls):
"""Stop down everything that was started."""
cls.hass.stop()
def test_requires_additional_values(self):
""" Should require the additional config values specified. """
"""Should require the additional config values specified."""
config = copy(test_tcp.TEST_CONFIG)
for key in bin_tcp.BinarySensor.required:
del config[key]
@@ -49,14 +46,14 @@ class TestTCPBinarySensor():
@patch('homeassistant.components.sensor.tcp.Sensor.update')
def test_is_on_true(self, mock_update):
""" Should return True if _state is the same as value_on. """
"""Should return True if _state is the same as value_on."""
sensor = bin_tcp.BinarySensor(self.hass, test_tcp.TEST_CONFIG)
sensor._state = test_tcp.TEST_CONFIG[tcp.CONF_VALUE_ON]
assert sensor.is_on
@patch('homeassistant.components.sensor.tcp.Sensor.update')
def test_is_on_false(self, mock_update):
""" Should return False if _state is not the same as value_on. """
"""Should return False if _state is not the same as value_on."""
sensor = bin_tcp.BinarySensor(self.hass, test_tcp.TEST_CONFIG)
sensor._state = "%s abc" % test_tcp.TEST_CONFIG[tcp.CONF_VALUE_ON]
assert not sensor.is_on
@@ -1,9 +1,4 @@
"""
tests.components.binary_sensor.test_template
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests for template binary_sensor.
"""
"""The tests for the Template Binary sensor platform."""
import unittest
from unittest import mock
@@ -12,8 +7,11 @@ from homeassistant.exceptions import TemplateError
class TestBinarySensorTemplate(unittest.TestCase):
"""Test for Binary sensor template platform."""
@mock.patch.object(template, 'BinarySensorTemplate')
def test_setup(self, mock_template):
""""Test the setup."""
config = {
'sensors': {
'test': {
@@ -32,11 +30,13 @@ class TestBinarySensorTemplate(unittest.TestCase):
add_devices.assert_called_once_with([mock_template.return_value])
def test_setup_no_sensors(self):
""""Test setup with no sensors."""
config = {}
result = template.setup_platform(None, config, None)
self.assertFalse(result)
def test_setup_invalid_device(self):
""""Test the setup with invalid devices."""
config = {
'sensors': {
'foo bar': {},
@@ -46,6 +46,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
self.assertFalse(result)
def test_setup_invalid_sensor_class(self):
""""Test setup with invalid sensor class."""
config = {
'sensors': {
'test': {
@@ -58,6 +59,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
self.assertFalse(result)
def test_setup_invalid_missing_template(self):
""""Test setup with invalid and missing template."""
config = {
'sensors': {
'test': {
@@ -69,6 +71,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
self.assertFalse(result)
def test_attributes(self):
""""Test the attributes."""
hass = mock.MagicMock()
vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
'motion', '{{ 1 > 1 }}')
@@ -84,6 +87,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
self.assertTrue(vs.is_on)
def test_event(self):
""""Test the event."""
hass = mock.MagicMock()
vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
'motion', '{{ 1 > 1 }}')
@@ -92,6 +96,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
mock_update.assert_called_once_with(True)
def test_update(self):
""""Test the update."""
hass = mock.MagicMock()
vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
'motion', '{{ 2 > 1 }}')
@@ -101,6 +106,7 @@ class TestBinarySensorTemplate(unittest.TestCase):
@mock.patch('homeassistant.helpers.template.render')
def test_update_template_error(self, mock_render):
""""Test the template update error."""
hass = mock.MagicMock()
vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
'motion', '{{ 1 > 1 }}')