core/tests/components/mfi/test_sensor.py

185 lines
6.8 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the mFi sensor platform."""
import unittest
import unittest.mock as mock
2019-12-05 06:47:40 +00:00
from mficlient.client import FailedToLogin
import requests
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
import homeassistant.components.mfi.sensor as mfi
import homeassistant.components.sensor as sensor
2016-04-20 03:30:44 +00:00
from homeassistant.const import TEMP_CELSIUS
from homeassistant.setup import setup_component
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
class TestMfiSensorSetup(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the mFi sensor platform."""
PLATFORM = mfi
COMPONENT = sensor
2019-07-31 19:25:30 +00:00
THING = "sensor"
GOOD_CONFIG = {
2019-07-31 19:25:30 +00:00
"sensor": {
"platform": "mfi",
"host": "foo",
"port": 6123,
"username": "user",
"password": "pass",
"ssl": True,
"verify_ssl": True,
}
}
def setup_method(self, method):
2018-08-19 20:29:08 +00:00
"""Set up things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
def teardown_method(self, method):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
self.hass.stop()
2019-12-05 06:47:40 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MFiClient")
def test_setup_missing_config(self, mock_client):
2016-03-09 09:25:50 +00:00
"""Test setup with missing configuration."""
2019-07-31 19:25:30 +00:00
config = {"sensor": {"platform": "mfi"}}
assert setup_component(self.hass, "sensor", config)
assert not mock_client.called
2019-12-05 06:47:40 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MFiClient")
def test_setup_failed_login(self, mock_client):
2016-03-09 09:25:50 +00:00
"""Test setup with login failure."""
mock_client.side_effect = FailedToLogin
2019-07-31 19:25:30 +00:00
assert not self.PLATFORM.setup_platform(self.hass, dict(self.GOOD_CONFIG), None)
2019-12-05 06:47:40 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MFiClient")
def test_setup_failed_connect(self, mock_client):
"""Test setup with connection failure."""
mock_client.side_effect = requests.exceptions.ConnectionError
2019-07-31 19:25:30 +00:00
assert not self.PLATFORM.setup_platform(self.hass, dict(self.GOOD_CONFIG), None)
2019-12-05 06:47:40 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MFiClient")
def test_setup_minimum(self, mock_client):
2016-03-09 09:25:50 +00:00
"""Test setup with minimum configuration."""
config = dict(self.GOOD_CONFIG)
2019-07-31 19:25:30 +00:00
del config[self.THING]["port"]
assert setup_component(self.hass, self.COMPONENT.DOMAIN, config)
assert mock_client.call_count == 1
2019-07-31 19:25:30 +00:00
assert mock_client.call_args == mock.call(
"foo", "user", "pass", port=6443, use_tls=True, verify=True
)
2019-12-05 06:47:40 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MFiClient")
def test_setup_with_port(self, mock_client):
2016-03-09 09:25:50 +00:00
"""Test setup with port."""
config = dict(self.GOOD_CONFIG)
2019-07-31 19:25:30 +00:00
config[self.THING]["port"] = 6123
assert setup_component(self.hass, self.COMPONENT.DOMAIN, config)
assert mock_client.call_count == 1
2019-07-31 19:25:30 +00:00
assert mock_client.call_args == mock.call(
"foo", "user", "pass", port=6123, use_tls=True, verify=True
)
2019-12-05 06:47:40 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MFiClient")
def test_setup_with_tls_disabled(self, mock_client):
2016-03-09 09:25:50 +00:00
"""Test setup without TLS."""
config = dict(self.GOOD_CONFIG)
2019-07-31 19:25:30 +00:00
del config[self.THING]["port"]
config[self.THING]["ssl"] = False
config[self.THING]["verify_ssl"] = False
assert setup_component(self.hass, self.COMPONENT.DOMAIN, config)
assert mock_client.call_count == 1
2019-07-31 19:25:30 +00:00
assert mock_client.call_args == mock.call(
"foo", "user", "pass", port=6080, use_tls=False, verify=False
)
2019-12-05 06:47:40 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MFiClient")
2019-07-31 19:25:30 +00:00
@mock.patch("homeassistant.components.mfi.sensor.MfiSensor")
def test_setup_adds_proper_devices(self, mock_sensor, mock_client):
2016-03-09 09:25:50 +00:00
"""Test if setup adds devices."""
2019-07-31 19:25:30 +00:00
ports = {
i: mock.MagicMock(model=model) for i, model in enumerate(mfi.SENSOR_MODELS)
}
ports["bad"] = mock.MagicMock(model="notasensor")
mock_client.return_value.get_devices.return_value = [
mock.MagicMock(ports=ports)
]
assert setup_component(self.hass, sensor.DOMAIN, self.GOOD_CONFIG)
for ident, port in ports.items():
2019-07-31 19:25:30 +00:00
if ident != "bad":
mock_sensor.assert_any_call(port, self.hass)
2019-07-31 19:25:30 +00:00
assert mock.call(ports["bad"], self.hass) not in mock_sensor.mock_calls
class TestMfiSensor(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test for mFi sensor platform."""
def setup_method(self, method):
2018-08-19 20:29:08 +00:00
"""Set up things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
self.port = mock.MagicMock()
self.sensor = mfi.MfiSensor(self.port, self.hass)
def teardown_method(self, method):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
self.hass.stop()
def test_name(self):
2016-03-09 09:25:50 +00:00
"""Test the name."""
assert self.port.label == self.sensor.name
def test_uom_temp(self):
2016-03-09 09:25:50 +00:00
"""Test the UOM temperature."""
2019-07-31 19:25:30 +00:00
self.port.tag = "temperature"
assert TEMP_CELSIUS == self.sensor.unit_of_measurement
def test_uom_power(self):
2016-03-09 09:25:50 +00:00
"""Test the UOEM power."""
2019-07-31 19:25:30 +00:00
self.port.tag = "active_pwr"
assert "Watts" == self.sensor.unit_of_measurement
def test_uom_digital(self):
2016-03-09 09:25:50 +00:00
"""Test the UOM digital input."""
2019-07-31 19:25:30 +00:00
self.port.model = "Input Digital"
assert "State" == self.sensor.unit_of_measurement
def test_uom_unknown(self):
2016-03-09 09:25:50 +00:00
"""Test the UOM."""
2019-07-31 19:25:30 +00:00
self.port.tag = "balloons"
assert "balloons" == self.sensor.unit_of_measurement
def test_uom_uninitialized(self):
"""Test that the UOM defaults if not initialized."""
type(self.port).tag = mock.PropertyMock(side_effect=ValueError)
2019-07-31 19:25:30 +00:00
assert "State" == self.sensor.unit_of_measurement
def test_state_digital(self):
2016-03-09 09:25:50 +00:00
"""Test the digital input."""
2019-07-31 19:25:30 +00:00
self.port.model = "Input Digital"
self.port.value = 0
assert mfi.STATE_OFF == self.sensor.state
self.port.value = 1
assert mfi.STATE_ON == self.sensor.state
self.port.value = 2
assert mfi.STATE_ON == self.sensor.state
def test_state_digits(self):
2016-03-09 09:25:50 +00:00
"""Test the state of digits."""
2019-07-31 19:25:30 +00:00
self.port.tag = "didyoucheckthedict?"
self.port.value = 1.25
2019-07-31 19:25:30 +00:00
with mock.patch.dict(mfi.DIGITS, {"didyoucheckthedict?": 1}):
assert 1.2 == self.sensor.state
with mock.patch.dict(mfi.DIGITS, {}):
assert 1.0 == self.sensor.state
def test_state_uninitialized(self):
"""Test the state of uninitialized sensors."""
type(self.port).tag = mock.PropertyMock(side_effect=ValueError)
assert mfi.STATE_OFF == self.sensor.state
def test_update(self):
2016-03-09 09:25:50 +00:00
"""Test the update."""
self.sensor.update()
assert self.port.refresh.call_count == 1
assert self.port.refresh.call_args == mock.call()