core/tests/components/mfi/test_switch.py

120 lines
3.9 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the mFi switch platform."""
2016-02-09 02:58:09 +00:00
import unittest
import unittest.mock as mock
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.switch as mfi
import homeassistant.components.switch as switch
from homeassistant.setup import setup_component
2016-02-09 02:58:09 +00:00
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
2016-02-09 02:58:09 +00:00
2019-12-05 06:47:40 +00:00
class TestMfiSwitchSetup(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the mFi switch."""
2016-02-09 02:58:09 +00:00
PLATFORM = mfi
COMPONENT = switch
2019-07-31 19:25:30 +00:00
THING = "switch"
2016-02-09 02:58:09 +00:00
GOOD_CONFIG = {
2019-07-31 19:25:30 +00:00
"switch": {
"platform": "mfi",
"host": "foo",
"port": 6123,
"username": "user",
"password": "pass",
"ssl": True,
"verify_ssl": True,
2016-02-09 02:58:09 +00:00
}
}
2019-12-05 06:47:40 +00:00
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
@mock.patch("homeassistant.components.mfi.switch.MFiClient")
2019-07-31 19:25:30 +00:00
@mock.patch("homeassistant.components.mfi.switch.MfiSwitch")
2016-02-09 02:58:09 +00:00
def test_setup_adds_proper_devices(self, mock_switch, 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.SWITCH_MODELS)
}
ports["bad"] = mock.MagicMock(model="notaswitch")
print(ports["bad"].model)
mock_client.return_value.get_devices.return_value = [
mock.MagicMock(ports=ports)
]
assert setup_component(self.hass, switch.DOMAIN, self.GOOD_CONFIG)
2016-02-09 02:58:09 +00:00
for ident, port in ports.items():
2019-07-31 19:25:30 +00:00
if ident != "bad":
2016-02-09 02:58:09 +00:00
mock_switch.assert_any_call(port)
2019-07-31 19:25:30 +00:00
assert mock.call(ports["bad"], self.hass) not in mock_switch.mock_calls
2016-02-09 02:58:09 +00:00
class TestMfiSwitch(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test for mFi switch platform."""
2016-02-09 02:58:09 +00:00
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()
2016-02-09 02:58:09 +00:00
self.port = mock.MagicMock()
self.switch = mfi.MfiSwitch(self.port)
def teardown_method(self, method):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2016-02-09 02:58:09 +00:00
self.hass.stop()
def test_name(self):
2016-03-09 09:25:50 +00:00
"""Test the name."""
assert self.port.label == self.switch.name
2016-02-09 02:58:09 +00:00
def test_update(self):
2016-03-09 09:25:50 +00:00
"""Test update."""
2016-02-09 02:58:09 +00:00
self.switch.update()
assert self.port.refresh.call_count == 1
assert self.port.refresh.call_args == mock.call()
2016-02-09 02:58:09 +00:00
def test_update_with_target_state(self):
2016-03-09 09:25:50 +00:00
"""Test update with target state."""
2016-02-09 02:58:09 +00:00
self.switch._target_state = True
self.port.data = {}
2019-07-31 19:25:30 +00:00
self.port.data["output"] = "stale"
2016-02-09 02:58:09 +00:00
self.switch.update()
2019-07-31 19:25:30 +00:00
assert 1.0 == self.port.data["output"]
assert self.switch._target_state is None
2019-07-31 19:25:30 +00:00
self.port.data["output"] = "untouched"
2016-02-09 02:58:09 +00:00
self.switch.update()
2019-07-31 19:25:30 +00:00
assert "untouched" == self.port.data["output"]
2016-02-09 02:58:09 +00:00
def test_turn_on(self):
2016-03-09 09:25:50 +00:00
"""Test turn_on."""
2016-02-09 02:58:09 +00:00
self.switch.turn_on()
assert self.port.control.call_count == 1
assert self.port.control.call_args == mock.call(True)
assert self.switch._target_state
2016-02-09 02:58:09 +00:00
def test_turn_off(self):
2016-03-09 09:25:50 +00:00
"""Test turn_off."""
2016-02-09 02:58:09 +00:00
self.switch.turn_off()
assert self.port.control.call_count == 1
assert self.port.control.call_args == mock.call(False)
assert not self.switch._target_state
2016-02-09 02:58:09 +00:00
def test_current_power_w(self):
2016-03-09 09:25:50 +00:00
"""Test current power."""
2019-07-31 19:25:30 +00:00
self.port.data = {"active_pwr": 10}
assert 10 == self.switch.current_power_w
2016-02-09 02:58:09 +00:00
def test_current_power_w_no_data(self):
2016-03-09 09:25:50 +00:00
"""Test current power if there is no data."""
2019-07-31 19:25:30 +00:00
self.port.data = {"notpower": 123}
assert 0 == self.switch.current_power_w
2016-02-09 02:58:09 +00:00
def test_device_state_attributes(self):
2016-03-09 09:25:50 +00:00
"""Test the state attributes."""
2019-07-31 19:25:30 +00:00
self.port.data = {"v_rms": 1.25, "i_rms": 2.75}
assert {"volts": 1.2, "amps": 2.8} == self.switch.device_state_attributes