Clean tcp tests (#41673)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Julian Engelhardt 2021-01-27 22:37:59 +01:00 committed by GitHub
parent 0338f5bccf
commit 3f948e027a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 170 additions and 254 deletions

View File

@ -78,10 +78,7 @@ class TcpSensor(Entity):
@property @property
def name(self): def name(self):
"""Return the name of this sensor.""" """Return the name of this sensor."""
name = self._config[CONF_NAME] return self._config[CONF_NAME]
if name is not None:
return name
return super().name
@property @property
def state(self): def state(self):

View File

@ -1,62 +1,83 @@
"""The tests for the TCP binary sensor platform.""" """The tests for the TCP binary sensor platform."""
import unittest from datetime import timedelta
from unittest.mock import Mock, patch from unittest.mock import call, patch
from homeassistant.components.tcp import binary_sensor as bin_tcp import pytest
import homeassistant.components.tcp.sensor as tcp
from homeassistant.setup import setup_component
from tests.common import assert_setup_component, get_test_home_assistant from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from tests.common import assert_setup_component, async_fire_time_changed
import tests.components.tcp.test_sensor as test_tcp import tests.components.tcp.test_sensor as test_tcp
BINARY_SENSOR_CONFIG = test_tcp.TEST_CONFIG["sensor"]
TEST_CONFIG = {"binary_sensor": BINARY_SENSOR_CONFIG}
TEST_ENTITY = "binary_sensor.test_name"
class TestTCPBinarySensor(unittest.TestCase):
"""Test the TCP Binary Sensor."""
def setup_method(self, method): @pytest.fixture(name="mock_socket")
"""Set up things to be run when tests are started.""" def mock_socket_fixture():
self.hass = get_test_home_assistant() """Mock the socket."""
with patch(
"homeassistant.components.tcp.sensor.socket.socket"
) as mock_socket, patch(
"homeassistant.components.tcp.sensor.select.select",
return_value=(True, False, False),
):
# yield the return value of the socket context manager
yield mock_socket.return_value.__enter__.return_value
def teardown_method(self, method):
"""Stop down everything that was started."""
self.hass.stop()
def test_setup_platform_valid_config(self): @pytest.fixture
"""Check a valid configuration.""" def now():
with assert_setup_component(0, "binary_sensor"): """Return datetime UTC now."""
assert setup_component(self.hass, "binary_sensor", test_tcp.TEST_CONFIG) return utcnow()
def test_setup_platform_invalid_config(self):
"""Check the invalid configuration."""
with assert_setup_component(0):
assert setup_component(
self.hass,
"binary_sensor",
{"binary_sensor": {"platform": "tcp", "porrt": 1234}},
)
@patch("homeassistant.components.tcp.sensor.TcpSensor.update") async def test_setup_platform_valid_config(hass, mock_socket):
def test_setup_platform_devices(self, mock_update): """Check a valid configuration."""
"""Check the supplied config and call add_entities with sensor.""" with assert_setup_component(1, "binary_sensor"):
add_entities = Mock() assert await async_setup_component(hass, "binary_sensor", TEST_CONFIG)
ret = bin_tcp.setup_platform(None, test_tcp.TEST_CONFIG, add_entities) await hass.async_block_till_done()
assert ret is None
assert add_entities.called
assert isinstance(add_entities.call_args[0][0][0], bin_tcp.TcpBinarySensor)
@patch("homeassistant.components.tcp.sensor.TcpSensor.update")
def test_is_on_true(self, mock_update):
"""Check the return that _state is value_on."""
sensor = bin_tcp.TcpBinarySensor(self.hass, test_tcp.TEST_CONFIG["sensor"])
sensor._state = test_tcp.TEST_CONFIG["sensor"][tcp.CONF_VALUE_ON]
print(sensor._state)
assert sensor.is_on
@patch("homeassistant.components.tcp.sensor.TcpSensor.update") async def test_setup_platform_invalid_config(hass, mock_socket):
def test_is_on_false(self, mock_update): """Check the invalid configuration."""
"""Check the return that _state is not the same as value_on.""" with assert_setup_component(0):
sensor = bin_tcp.TcpBinarySensor(self.hass, test_tcp.TEST_CONFIG["sensor"]) assert await async_setup_component(
sensor._state = "{} abc".format( hass,
test_tcp.TEST_CONFIG["sensor"][tcp.CONF_VALUE_ON] "binary_sensor",
{"binary_sensor": {"platform": "tcp", "porrt": 1234}},
) )
assert not sensor.is_on await hass.async_block_till_done()
async def test_state(hass, mock_socket, now):
"""Check the state and update of the binary sensor."""
mock_socket.recv.return_value = b"off"
assert await async_setup_component(hass, "binary_sensor", TEST_CONFIG)
await hass.async_block_till_done()
state = hass.states.get(TEST_ENTITY)
assert state
assert state.state == STATE_OFF
assert mock_socket.connect.called
assert mock_socket.connect.call_args == call(
(BINARY_SENSOR_CONFIG["host"], BINARY_SENSOR_CONFIG["port"])
)
assert mock_socket.send.called
assert mock_socket.send.call_args == call(BINARY_SENSOR_CONFIG["payload"].encode())
assert mock_socket.recv.called
assert mock_socket.recv.call_args == call(BINARY_SENSOR_CONFIG["buffer_size"])
mock_socket.recv.return_value = b"on"
async_fire_time_changed(hass, now + timedelta(seconds=45))
await hass.async_block_till_done()
state = hass.states.get(TEST_ENTITY)
assert state
assert state.state == STATE_ON

View File

@ -1,16 +1,13 @@
"""The tests for the TCP sensor platform.""" """The tests for the TCP sensor platform."""
from copy import copy from copy import copy
import socket from unittest.mock import call, patch
import unittest
from unittest.mock import Mock, patch import pytest
from uuid import uuid4
import homeassistant.components.tcp.sensor as tcp import homeassistant.components.tcp.sensor as tcp
from homeassistant.helpers.entity import Entity from homeassistant.setup import async_setup_component
from homeassistant.helpers.template import Template
from homeassistant.setup import setup_component
from tests.common import assert_setup_component, get_test_home_assistant from tests.common import assert_setup_component
TEST_CONFIG = { TEST_CONFIG = {
"sensor": { "sensor": {
@ -21,13 +18,16 @@ TEST_CONFIG = {
tcp.CONF_TIMEOUT: tcp.DEFAULT_TIMEOUT + 1, tcp.CONF_TIMEOUT: tcp.DEFAULT_TIMEOUT + 1,
tcp.CONF_PAYLOAD: "test_payload", tcp.CONF_PAYLOAD: "test_payload",
tcp.CONF_UNIT_OF_MEASUREMENT: "test_unit", tcp.CONF_UNIT_OF_MEASUREMENT: "test_unit",
tcp.CONF_VALUE_TEMPLATE: Template("test_template"), tcp.CONF_VALUE_TEMPLATE: "{{ 'test_' + value }}",
tcp.CONF_VALUE_ON: "test_on", tcp.CONF_VALUE_ON: "test_on",
tcp.CONF_BUFFER_SIZE: tcp.DEFAULT_BUFFER_SIZE + 1, tcp.CONF_BUFFER_SIZE: tcp.DEFAULT_BUFFER_SIZE + 1,
} }
} }
SENSOR_TEST_CONFIG = TEST_CONFIG["sensor"]
TEST_ENTITY = "sensor.test_name"
KEYS_AND_DEFAULTS = { KEYS_AND_DEFAULTS = {
tcp.CONF_NAME: tcp.DEFAULT_NAME,
tcp.CONF_TIMEOUT: tcp.DEFAULT_TIMEOUT, tcp.CONF_TIMEOUT: tcp.DEFAULT_TIMEOUT,
tcp.CONF_UNIT_OF_MEASUREMENT: None, tcp.CONF_UNIT_OF_MEASUREMENT: None,
tcp.CONF_VALUE_TEMPLATE: None, tcp.CONF_VALUE_TEMPLATE: None,
@ -35,229 +35,127 @@ KEYS_AND_DEFAULTS = {
tcp.CONF_BUFFER_SIZE: tcp.DEFAULT_BUFFER_SIZE, tcp.CONF_BUFFER_SIZE: tcp.DEFAULT_BUFFER_SIZE,
} }
socket_test_value = "value"
class TestTCPSensor(unittest.TestCase):
"""Test the TCP Sensor."""
def setup_method(self, method): @pytest.fixture(name="mock_socket")
"""Set up things to be run when tests are started.""" def mock_socket_fixture(mock_select):
self.hass = get_test_home_assistant() """Mock socket."""
with patch("homeassistant.components.tcp.sensor.socket.socket") as mock_socket:
socket_instance = mock_socket.return_value.__enter__.return_value
socket_instance.recv.return_value = socket_test_value.encode()
yield socket_instance
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
@patch("homeassistant.components.tcp.sensor.TcpSensor.update") @pytest.fixture(name="mock_select")
def test_setup_platform_valid_config(self, mock_update): def mock_select_fixture():
"""Check a valid configuration and call add_entities with sensor.""" """Mock select."""
with assert_setup_component(0, "sensor"): with patch(
assert setup_component(self.hass, "sensor", TEST_CONFIG) "homeassistant.components.tcp.sensor.select.select",
return_value=(True, False, False),
) as mock_select:
yield mock_select
add_entities = Mock()
tcp.setup_platform(None, TEST_CONFIG["sensor"], add_entities)
assert add_entities.called
assert isinstance(add_entities.call_args[0][0][0], tcp.TcpSensor)
def test_setup_platform_invalid_config(self): async def test_setup_platform_valid_config(hass, mock_socket):
"""Check an invalid configuration.""" """Check a valid configuration and call add_entities with sensor."""
with assert_setup_component(0): with assert_setup_component(1, "sensor"):
assert setup_component( assert await async_setup_component(hass, "sensor", TEST_CONFIG)
self.hass, "sensor", {"sensor": {"platform": "tcp", "porrt": 1234}} await hass.async_block_till_done()
)
@patch("homeassistant.components.tcp.sensor.TcpSensor.update")
def test_name(self, mock_update):
"""Return the name if set in the configuration."""
sensor = tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
assert sensor.name == TEST_CONFIG["sensor"][tcp.CONF_NAME]
@patch("homeassistant.components.tcp.sensor.TcpSensor.update") async def test_setup_platform_invalid_config(hass, mock_socket):
def test_name_not_set(self, mock_update): """Check an invalid configuration."""
"""Return the superclass name property if not set in configuration.""" with assert_setup_component(0):
config = copy(TEST_CONFIG["sensor"]) assert await async_setup_component(
del config[tcp.CONF_NAME] hass, "sensor", {"sensor": {"platform": "tcp", "porrt": 1234}}
entity = Entity()
sensor = tcp.TcpSensor(self.hass, config)
assert sensor.name == entity.name
@patch("homeassistant.components.tcp.sensor.TcpSensor.update")
def test_state(self, mock_update):
"""Return the contents of _state."""
sensor = tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
uuid = str(uuid4())
sensor._state = uuid
assert sensor.state == uuid
@patch("homeassistant.components.tcp.sensor.TcpSensor.update")
def test_unit_of_measurement(self, mock_update):
"""Return the configured unit of measurement."""
sensor = tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
assert (
sensor.unit_of_measurement
== TEST_CONFIG["sensor"][tcp.CONF_UNIT_OF_MEASUREMENT]
) )
await hass.async_block_till_done()
@patch("homeassistant.components.tcp.sensor.TcpSensor.update")
def test_config_valid_keys(self, *args):
"""Store valid keys in _config."""
sensor = tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
del TEST_CONFIG["sensor"]["platform"]
for key in TEST_CONFIG["sensor"]: async def test_state(hass, mock_socket, mock_select):
assert key in sensor._config """Return the contents of _state."""
assert await async_setup_component(hass, "sensor", TEST_CONFIG)
await hass.async_block_till_done()
def test_validate_config_valid_keys(self): state = hass.states.get(TEST_ENTITY)
"""Return True when provided with the correct keys."""
with assert_setup_component(0, "sensor"):
assert setup_component(self.hass, "sensor", TEST_CONFIG)
@patch("homeassistant.components.tcp.sensor.TcpSensor.update") assert state
def test_config_invalid_keys(self, mock_update): assert state.state == "test_value"
"""Shouldn't store invalid keys in _config.""" assert (
config = copy(TEST_CONFIG["sensor"]) state.attributes["unit_of_measurement"]
config.update({"a": "test_a", "b": "test_b", "c": "test_c"}) == SENSOR_TEST_CONFIG[tcp.CONF_UNIT_OF_MEASUREMENT]
sensor = tcp.TcpSensor(self.hass, config) )
for invalid_key in "abc": assert mock_socket.connect.called
assert invalid_key not in sensor._config assert mock_socket.connect.call_args == call(
(SENSOR_TEST_CONFIG["host"], SENSOR_TEST_CONFIG["port"])
)
assert mock_socket.send.called
assert mock_socket.send.call_args == call(SENSOR_TEST_CONFIG["payload"].encode())
assert mock_select.call_args == call(
[mock_socket], [], [], SENSOR_TEST_CONFIG[tcp.CONF_TIMEOUT]
)
assert mock_socket.recv.called
assert mock_socket.recv.call_args == call(SENSOR_TEST_CONFIG["buffer_size"])
def test_validate_config_invalid_keys(self):
"""Test with invalid keys plus some extra."""
config = copy(TEST_CONFIG["sensor"])
config.update({"a": "test_a", "b": "test_b", "c": "test_c"})
with assert_setup_component(0, "sensor"):
assert setup_component(self.hass, "sensor", {"tcp": config})
@patch("homeassistant.components.tcp.sensor.TcpSensor.update") async def test_config_uses_defaults(hass, mock_socket):
def test_config_uses_defaults(self, mock_update): """Check if defaults were set."""
"""Check if defaults were set.""" config = copy(SENSOR_TEST_CONFIG)
config = copy(TEST_CONFIG["sensor"])
for key in KEYS_AND_DEFAULTS: for key in KEYS_AND_DEFAULTS:
del config[key] del config[key]
with assert_setup_component(1) as result_config: with assert_setup_component(1) as result_config:
assert setup_component(self.hass, "sensor", {"sensor": config}) assert await async_setup_component(hass, "sensor", {"sensor": config})
await hass.async_block_till_done()
sensor = tcp.TcpSensor(self.hass, result_config["sensor"][0]) state = hass.states.get("sensor.tcp_sensor")
for key, default in KEYS_AND_DEFAULTS.items(): assert state
assert sensor._config[key] == default assert state.state == "value"
def test_validate_config_missing_defaults(self): for key, default in KEYS_AND_DEFAULTS.items():
"""Return True when defaulted keys are not provided.""" assert result_config["sensor"][0].get(key) == default
config = copy(TEST_CONFIG["sensor"])
for key in KEYS_AND_DEFAULTS:
del config[key]
with assert_setup_component(0, "sensor"): @pytest.mark.parametrize("sock_attr", ["connect", "send"])
assert setup_component(self.hass, "sensor", {"tcp": config}) async def test_update_socket_error(hass, mock_socket, sock_attr):
"""Test socket errors during update."""
socket_method = getattr(mock_socket, sock_attr)
socket_method.side_effect = OSError("Boom")
def test_validate_config_missing_required(self): assert await async_setup_component(hass, "sensor", TEST_CONFIG)
"""Return False when required config items are missing.""" await hass.async_block_till_done()
for key in TEST_CONFIG["sensor"]:
if key in KEYS_AND_DEFAULTS:
continue
config = copy(TEST_CONFIG["sensor"])
del config[key]
with assert_setup_component(0, "sensor"):
assert setup_component(self.hass, "sensor", {"tcp": config})
@patch("homeassistant.components.tcp.sensor.TcpSensor.update") state = hass.states.get(TEST_ENTITY)
def test_init_calls_update(self, mock_update):
"""Call update() method during __init__()."""
tcp.TcpSensor(self.hass, TEST_CONFIG)
assert mock_update.called
@patch("socket.socket") assert state
@patch("select.select", return_value=(True, False, False)) assert state.state == "unknown"
def test_update_connects_to_host_and_port(self, mock_select, mock_socket):
"""Connect to the configured host and port."""
tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
mock_socket = mock_socket().__enter__()
assert mock_socket.connect.mock_calls[0][1] == (
(
TEST_CONFIG["sensor"][tcp.CONF_HOST],
TEST_CONFIG["sensor"][tcp.CONF_PORT],
),
)
@patch("socket.socket.connect", side_effect=socket.error())
def test_update_returns_if_connecting_fails(self, *args):
"""Return if connecting to host fails."""
with patch("homeassistant.components.tcp.sensor.TcpSensor.update"):
sensor = tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
assert sensor.update() is None
@patch("socket.socket.connect") async def test_update_select_fails(hass, mock_socket, mock_select):
@patch("socket.socket.send", side_effect=socket.error()) """Test select fails to return a socket for reading."""
def test_update_returns_if_sending_fails(self, *args): mock_select.return_value = (False, False, False)
"""Return if sending fails."""
with patch("homeassistant.components.tcp.sensor.TcpSensor.update"):
sensor = tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
assert sensor.update() is None
@patch("socket.socket.connect") assert await async_setup_component(hass, "sensor", TEST_CONFIG)
@patch("socket.socket.send") await hass.async_block_till_done()
@patch("select.select", return_value=(False, False, False))
def test_update_returns_if_select_fails(self, *args):
"""Return if select fails to return a socket."""
with patch("homeassistant.components.tcp.sensor.TcpSensor.update"):
sensor = tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
assert sensor.update() is None
@patch("socket.socket") state = hass.states.get(TEST_ENTITY)
@patch("select.select", return_value=(True, False, False))
def test_update_sends_payload(self, mock_select, mock_socket):
"""Send the configured payload as bytes."""
tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
mock_socket = mock_socket().__enter__()
mock_socket.send.assert_called_with(
TEST_CONFIG["sensor"][tcp.CONF_PAYLOAD].encode()
)
@patch("socket.socket") assert state
@patch("select.select", return_value=(True, False, False)) assert state.state == "unknown"
def test_update_calls_select_with_timeout(self, mock_select, mock_socket):
"""Provide the timeout argument to select."""
tcp.TcpSensor(self.hass, TEST_CONFIG["sensor"])
mock_socket = mock_socket().__enter__()
mock_select.assert_called_with(
[mock_socket], [], [], TEST_CONFIG["sensor"][tcp.CONF_TIMEOUT]
)
@patch("socket.socket")
@patch("select.select", return_value=(True, False, False))
def test_update_receives_packet_and_sets_as_state(self, mock_select, mock_socket):
"""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()
config = copy(TEST_CONFIG["sensor"])
del config[tcp.CONF_VALUE_TEMPLATE]
sensor = tcp.TcpSensor(self.hass, config)
assert sensor._state == test_value
@patch("socket.socket") async def test_update_returns_if_template_render_fails(hass, mock_socket):
@patch("select.select", return_value=(True, False, False)) """Return None if rendering the template fails."""
def test_update_renders_value_in_template(self, mock_select, mock_socket): config = copy(SENSOR_TEST_CONFIG)
"""Render the value in the provided template.""" config[tcp.CONF_VALUE_TEMPLATE] = "{{ value / 0 }}"
test_value = "test_value"
mock_socket = mock_socket().__enter__()
mock_socket.recv.return_value = test_value.encode()
config = copy(TEST_CONFIG["sensor"])
config[tcp.CONF_VALUE_TEMPLATE] = Template("{{ value }} {{ 1+1 }}")
sensor = tcp.TcpSensor(self.hass, config)
assert sensor._state == "%s 2" % test_value
@patch("socket.socket") assert await async_setup_component(hass, "sensor", {"sensor": config})
@patch("select.select", return_value=(True, False, False)) await hass.async_block_till_done()
def test_update_returns_if_template_render_fails(self, mock_select, mock_socket):
"""Return None if rendering the template fails.""" state = hass.states.get(TEST_ENTITY)
test_value = "test_value"
mock_socket = mock_socket().__enter__() assert state
mock_socket.recv.return_value = test_value.encode() assert state.state == "unknown"
config = copy(TEST_CONFIG["sensor"])
config[tcp.CONF_VALUE_TEMPLATE] = Template("{{ this won't work")
sensor = tcp.TcpSensor(self.hass, config)
assert sensor.update() is None