The default configuration of the mFi controller generates self-signed certificates which are valid for short periods of time, and which are regnerated on start or as needed. This makes requests mad. Since most people will be using the self-signed certificates anyway, add options to let them choose non-TLS, or unverified connections if they want/need. This bumps the mficlient requirement to 0.3.0 for proper handling of verify=False.
165 lines
5.9 KiB
Python
165 lines
5.9 KiB
Python
"""
|
|
tests.components.sensor.test_mfi
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Tests mFi sensor.
|
|
"""
|
|
import unittest
|
|
import unittest.mock as mock
|
|
|
|
import requests
|
|
|
|
import homeassistant.components.sensor as sensor
|
|
import homeassistant.components.sensor.mfi as mfi
|
|
from homeassistant.const import TEMP_CELCIUS
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
class TestMfiSensorSetup(unittest.TestCase):
|
|
PLATFORM = mfi
|
|
COMPONENT = sensor
|
|
THING = 'sensor'
|
|
GOOD_CONFIG = {
|
|
'sensor': {
|
|
'platform': 'mfi',
|
|
'host': 'foo',
|
|
'port': 6123,
|
|
'username': 'user',
|
|
'password': 'pass',
|
|
'use_tls': True,
|
|
'verify_tls': True,
|
|
}
|
|
}
|
|
|
|
def setup_method(self, method):
|
|
self.hass = get_test_home_assistant()
|
|
self.hass.config.latitude = 32.87336
|
|
self.hass.config.longitude = 117.22743
|
|
|
|
def teardown_method(self, method):
|
|
""" Stop down stuff we started. """
|
|
self.hass.stop()
|
|
|
|
def test_setup_missing_config(self):
|
|
config = {
|
|
'sensor': {
|
|
'platform': 'mfi',
|
|
}
|
|
}
|
|
self.assertFalse(self.PLATFORM.setup_platform(self.hass, config, None))
|
|
|
|
@mock.patch('mficlient.client')
|
|
def test_setup_failed_login(self, mock_client):
|
|
mock_client.FailedToLogin = Exception()
|
|
mock_client.MFiClient.side_effect = mock_client.FailedToLogin
|
|
self.assertFalse(
|
|
self.PLATFORM.setup_platform(self.hass,
|
|
dict(self.GOOD_CONFIG),
|
|
None))
|
|
|
|
@mock.patch('mficlient.client')
|
|
def test_setup_failed_connect(self, mock_client):
|
|
mock_client.FailedToLogin = Exception()
|
|
mock_client.MFiClient.side_effect = requests.exceptions.ConnectionError
|
|
self.assertFalse(
|
|
self.PLATFORM.setup_platform(self.hass,
|
|
dict(self.GOOD_CONFIG),
|
|
None))
|
|
|
|
@mock.patch('mficlient.client.MFiClient')
|
|
def test_setup_minimum(self, mock_client):
|
|
config = dict(self.GOOD_CONFIG)
|
|
del config[self.THING]['port']
|
|
assert self.COMPONENT.setup(self.hass, config)
|
|
mock_client.assert_called_once_with('foo', 'user', 'pass',
|
|
port=6443, use_tls=True,
|
|
verify=True)
|
|
|
|
@mock.patch('mficlient.client.MFiClient')
|
|
def test_setup_with_port(self, mock_client):
|
|
config = dict(self.GOOD_CONFIG)
|
|
config[self.THING]['port'] = 6123
|
|
assert self.COMPONENT.setup(self.hass, config)
|
|
mock_client.assert_called_once_with('foo', 'user', 'pass',
|
|
port=6123, use_tls=True,
|
|
verify=True)
|
|
|
|
@mock.patch('mficlient.client.MFiClient')
|
|
def test_setup_with_tls_disabled(self, mock_client):
|
|
config = dict(self.GOOD_CONFIG)
|
|
del config[self.THING]['port']
|
|
config[self.THING]['use_tls'] = False
|
|
config[self.THING]['verify_tls'] = False
|
|
assert self.COMPONENT.setup(self.hass, config)
|
|
mock_client.assert_called_once_with('foo', 'user', 'pass',
|
|
port=6080, use_tls=False,
|
|
verify=False)
|
|
|
|
@mock.patch('mficlient.client.MFiClient')
|
|
@mock.patch('homeassistant.components.sensor.mfi.MfiSensor')
|
|
def test_setup_adds_proper_devices(self, mock_sensor, mock_client):
|
|
ports = {i: mock.MagicMock(model=model)
|
|
for i, model in enumerate(mfi.SENSOR_MODELS)}
|
|
ports['bad'] = mock.MagicMock(model='notasensor')
|
|
print(ports['bad'].model)
|
|
mock_client.return_value.get_devices.return_value = \
|
|
[mock.MagicMock(ports=ports)]
|
|
assert sensor.setup(self.hass, self.GOOD_CONFIG)
|
|
for ident, port in ports.items():
|
|
if ident != 'bad':
|
|
mock_sensor.assert_any_call(port, self.hass)
|
|
assert mock.call(ports['bad'], self.hass) not in mock_sensor.mock_calls
|
|
|
|
|
|
class TestMfiSensor(unittest.TestCase):
|
|
def setup_method(self, method):
|
|
self.hass = get_test_home_assistant()
|
|
self.hass.config.latitude = 32.87336
|
|
self.hass.config.longitude = 117.22743
|
|
self.port = mock.MagicMock()
|
|
self.sensor = mfi.MfiSensor(self.port, self.hass)
|
|
|
|
def teardown_method(self, method):
|
|
""" Stop down stuff we started. """
|
|
self.hass.stop()
|
|
|
|
def test_name(self):
|
|
self.assertEqual(self.port.label, self.sensor.name)
|
|
|
|
def test_uom_temp(self):
|
|
self.port.tag = 'temperature'
|
|
self.assertEqual(TEMP_CELCIUS, self.sensor.unit_of_measurement)
|
|
|
|
def test_uom_power(self):
|
|
self.port.tag = 'active_pwr'
|
|
self.assertEqual('Watts', self.sensor.unit_of_measurement)
|
|
|
|
def test_uom_digital(self):
|
|
self.port.model = 'Input Digital'
|
|
self.assertEqual('State', self.sensor.unit_of_measurement)
|
|
|
|
def test_uom_unknown(self):
|
|
self.port.tag = 'balloons'
|
|
self.assertEqual('balloons', self.sensor.unit_of_measurement)
|
|
|
|
def test_state_digital(self):
|
|
self.port.model = 'Input Digital'
|
|
self.port.value = 0
|
|
self.assertEqual(mfi.STATE_OFF, self.sensor.state)
|
|
self.port.value = 1
|
|
self.assertEqual(mfi.STATE_ON, self.sensor.state)
|
|
self.port.value = 2
|
|
self.assertEqual(mfi.STATE_ON, self.sensor.state)
|
|
|
|
def test_state_digits(self):
|
|
self.port.tag = 'didyoucheckthedict?'
|
|
self.port.value = 1.25
|
|
with mock.patch.dict(mfi.DIGITS, {'didyoucheckthedict?': 1}):
|
|
self.assertEqual(1.2, self.sensor.state)
|
|
with mock.patch.dict(mfi.DIGITS, {}):
|
|
self.assertEqual(1.0, self.sensor.state)
|
|
|
|
def test_update(self):
|
|
self.sensor.update()
|
|
self.port.refresh.assert_called_once_with()
|