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

+27 -60
View File
@@ -1,9 +1,4 @@
"""
tests.components.sensor.tcp
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests TCP sensor.
"""
"""The tests for the TCP sensor platform."""
import socket
from copy import copy
from uuid import uuid4
@@ -37,7 +32,7 @@ KEYS_AND_DEFAULTS = {
@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 = tcp.setup_platform(None, TEST_CONFIG, add_entities)
assert ret is None, "setup_platform() should return None if successful."
@@ -46,30 +41,32 @@ 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_CONFIG)
del config[tcp.CONF_HOST]
assert tcp.setup_platform(None, config, None) is False
class TestTCPSensor():
""" Test the TCP Sensor. """
"""Test the TCP 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 everything that was started."""
cls.hass.stop()
@patch('homeassistant.components.sensor.tcp.Sensor.update')
def test_name(self, mock_update):
""" Should return the name if set in the config. """
"""Should return the name if set in the config."""
sensor = tcp.Sensor(self.hass, TEST_CONFIG)
assert sensor.name == TEST_CONFIG[tcp.CONF_NAME]
@patch('homeassistant.components.sensor.tcp.Sensor.update')
def test_name_not_set(self, mock_update):
""" Should return the superclass name property if not set in config """
"""Should return the superclass name property if not set in config."""
config = copy(TEST_CONFIG)
del config[tcp.CONF_NAME]
entity = Entity()
@@ -78,7 +75,7 @@ class TestTCPSensor():
@patch('homeassistant.components.sensor.tcp.Sensor.update')
def test_state(self, mock_update):
""" Should return the contents of _state. """
"""Should return the contents of _state."""
sensor = tcp.Sensor(self.hass, TEST_CONFIG)
uuid = str(uuid4())
sensor._state = uuid
@@ -86,30 +83,24 @@ class TestTCPSensor():
@patch('homeassistant.components.sensor.tcp.Sensor.update')
def test_unit_of_measurement(self, mock_update):
""" Should return the configured unit of measurement. """
"""Should return the configured unit of measurement."""
sensor = tcp.Sensor(self.hass, TEST_CONFIG)
assert sensor.unit_of_measurement == TEST_CONFIG[tcp.CONF_UNIT]
@patch("homeassistant.components.sensor.tcp.Sensor.update")
def test_config_valid_keys(self, *args):
"""
Should store valid keys in _config.
"""
"""Should store valid keys in _config."""
sensor = tcp.Sensor(self.hass, TEST_CONFIG)
for key in TEST_CONFIG:
assert key in sensor._config
def test_validate_config_valid_keys(self):
"""
Should return True when provided with the correct keys.
"""
"""Should return True when provided with the correct keys."""
assert tcp.Sensor.validate_config(TEST_CONFIG)
@patch("homeassistant.components.sensor.tcp.Sensor.update")
def test_config_invalid_keys(self, mock_update):
"""
Shouldn't store invalid keys in _config.
"""
"""Shouldn't store invalid keys in _config."""
config = copy(TEST_CONFIG)
config.update({
"a": "test_a",
@@ -121,9 +112,7 @@ class TestTCPSensor():
assert invalid_key not in sensor._config
def test_validate_config_invalid_keys(self):
"""
Should return True when provided with the correct keys plus some extra.
"""
"""Test with invalid keys plus some extra."""
config = copy(TEST_CONFIG)
config.update({
"a": "test_a",
@@ -134,9 +123,7 @@ class TestTCPSensor():
@patch("homeassistant.components.sensor.tcp.Sensor.update")
def test_config_uses_defaults(self, mock_update):
"""
Should use defaults where appropriate.
"""
"""Should use defaults where appropriate."""
config = copy(TEST_CONFIG)
for key in KEYS_AND_DEFAULTS.keys():
del config[key]
@@ -145,18 +132,14 @@ class TestTCPSensor():
assert sensor._config[key] == default
def test_validate_config_missing_defaults(self):
"""
Should return True when defaulted keys are not provided.
"""
"""Should return True when defaulted keys are not provided."""
config = copy(TEST_CONFIG)
for key in KEYS_AND_DEFAULTS.keys():
del config[key]
assert tcp.Sensor.validate_config(config)
def test_validate_config_missing_required(self):
"""
Should return False when required config items are missing.
"""
"""Should return False when required config items are missing."""
for key in TEST_CONFIG:
if key in KEYS_AND_DEFAULTS:
continue
@@ -168,18 +151,14 @@ class TestTCPSensor():
@patch("homeassistant.components.sensor.tcp.Sensor.update")
def test_init_calls_update(self, mock_update):
"""
Should call update() method during __init__().
"""
"""Should call update() method during __init__()."""
tcp.Sensor(self.hass, TEST_CONFIG)
assert mock_update.called
@patch("socket.socket")
@patch("select.select", return_value=(True, False, False))
def test_update_connects_to_host_and_port(self, mock_select, mock_socket):
"""
Should connect to the configured host and port.
"""
"""Should connect to the configured host and port."""
tcp.Sensor(self.hass, TEST_CONFIG)
mock_socket = mock_socket().__enter__()
assert mock_socket.connect.mock_calls[0][1] == ((
@@ -188,9 +167,7 @@ class TestTCPSensor():
@patch("socket.socket.connect", side_effect=socket.error())
def test_update_returns_if_connecting_fails(self, *args):
"""
Should return if connecting to host fails.
"""
"""Should return if connecting to host fails."""
with patch("homeassistant.components.sensor.tcp.Sensor.update"):
sensor = tcp.Sensor(self.hass, TEST_CONFIG)
assert sensor.update() is None
@@ -198,9 +175,7 @@ class TestTCPSensor():
@patch("socket.socket.connect")
@patch("socket.socket.send", side_effect=socket.error())
def test_update_returns_if_sending_fails(self, *args):
"""
Should return if sending fails.
"""
"""Should return if sending fails."""
with patch("homeassistant.components.sensor.tcp.Sensor.update"):
sensor = tcp.Sensor(self.hass, TEST_CONFIG)
assert sensor.update() is None
@@ -209,7 +184,7 @@ class TestTCPSensor():
@patch("socket.socket.send")
@patch("select.select", return_value=(False, False, False))
def test_update_returns_if_select_fails(self, *args):
""" Should return if select fails to return a socket. """
"""Should return if select fails to return a socket."""
with patch("homeassistant.components.sensor.tcp.Sensor.update"):
sensor = tcp.Sensor(self.hass, TEST_CONFIG)
assert sensor.update() is None
@@ -217,9 +192,7 @@ class TestTCPSensor():
@patch("socket.socket")
@patch("select.select", return_value=(True, False, False))
def test_update_sends_payload(self, mock_select, mock_socket):
"""
Should send the configured payload as bytes.
"""
"""Should send the configured payload as bytes."""
tcp.Sensor(self.hass, TEST_CONFIG)
mock_socket = mock_socket().__enter__()
mock_socket.send.assert_called_with(
@@ -229,9 +202,7 @@ class TestTCPSensor():
@patch("socket.socket")
@patch("select.select", return_value=(True, False, False))
def test_update_calls_select_with_timeout(self, mock_select, mock_socket):
"""
Should provide the timeout argument to select.
"""
"""Should provide the timeout argument to select."""
tcp.Sensor(self.hass, TEST_CONFIG)
mock_socket = mock_socket().__enter__()
mock_select.assert_called_with(
@@ -241,9 +212,7 @@ class TestTCPSensor():
@patch("select.select", return_value=(True, False, False))
def test_update_receives_packet_and_sets_as_state(
self, mock_select, mock_socket):
"""
Should receive the response from the socket and set it as the state.
"""
"""Test the response from the socket and set it as the state."""
test_value = "test_value"
mock_socket = mock_socket().__enter__()
mock_socket.recv.return_value = test_value.encode()
@@ -255,9 +224,7 @@ class TestTCPSensor():
@patch("socket.socket")
@patch("select.select", return_value=(True, False, False))
def test_update_renders_value_in_template(self, mock_select, mock_socket):
"""
Should render the value in the provided template.
"""
"""Should render the value in the provided template."""
test_value = "test_value"
mock_socket = mock_socket().__enter__()
mock_socket.recv.return_value = test_value.encode()
@@ -270,7 +237,7 @@ class TestTCPSensor():
@patch("select.select", return_value=(True, False, False))
def test_update_returns_if_template_render_fails(
self, mock_select, mock_socket):
""" Should return None if rendering the template fails. """
"""Should return None if rendering the template fails."""
test_value = "test_value"
mock_socket = mock_socket().__enter__()
mock_socket.recv.return_value = test_value.encode()