core/tests/components/mfi/test_switch.py

106 lines
2.9 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the mFi switch platform."""
2021-01-01 21:31:56 +00:00
import unittest.mock as mock
import pytest
2016-02-09 02:58:09 +00:00
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_component
from homeassistant.setup import async_setup_component
2016-02-09 02:58:09 +00:00
PLATFORM = mfi
COMPONENT = switch_component
THING = "switch"
GOOD_CONFIG = {
"switch": {
"platform": "mfi",
"host": "foo",
"port": 6123,
"username": "user",
"password": "pass",
"ssl": True,
"verify_ssl": True,
}
}
2019-12-05 06:47:40 +00:00
async def test_setup_adds_proper_devices(hass):
"""Test if setup adds devices."""
with mock.patch(
"homeassistant.components.mfi.switch.MFiClient"
) as mock_client, mock.patch(
2020-11-10 23:03:16 +00:00
"homeassistant.components.mfi.switch.MfiSwitch", side_effect=mfi.MfiSwitch
) as mock_switch:
2019-07-31 19:25:30 +00:00
ports = {
2020-11-13 09:51:27 +00:00
i: mock.MagicMock(
model=model, label=f"Port {i}", output=False, data={}, ident=f"abcd-{i}"
)
for i, model in enumerate(mfi.SWITCH_MODELS)
2019-07-31 19:25:30 +00:00
}
ports["bad"] = mock.MagicMock(model="notaswitch")
mock_client.return_value.get_devices.return_value = [
mock.MagicMock(ports=ports)
]
assert await async_setup_component(hass, COMPONENT.DOMAIN, GOOD_CONFIG)
await hass.async_block_till_done()
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)
assert mock.call(ports["bad"], hass) not in mock_switch.mock_calls
@pytest.fixture(name="port")
def port_fixture():
"""Port fixture."""
return mock.MagicMock()
@pytest.fixture(name="switch")
def switch_fixture(port):
"""Switch fixture."""
return mfi.MfiSwitch(port)
async def test_name(port, switch):
"""Test the name."""
assert port.label == switch.name
async def test_update(port, switch):
"""Test update."""
switch.update()
assert port.refresh.call_count == 1
assert port.refresh.call_args == mock.call()
async def test_update_with_target_state(port, switch):
"""Test update with target state."""
# pylint: disable=protected-access
switch._target_state = True
port.data = {}
port.data["output"] = "stale"
switch.update()
assert port.data["output"] == 1.0
# pylint: disable=protected-access
assert switch._target_state is None
port.data["output"] = "untouched"
switch.update()
assert port.data["output"] == "untouched"
async def test_turn_on(port, switch):
"""Test turn_on."""
switch.turn_on()
assert port.control.call_count == 1
assert port.control.call_args == mock.call(True)
# pylint: disable=protected-access
assert switch._target_state
async def test_turn_off(port, switch):
"""Test turn_off."""
switch.turn_off()
assert port.control.call_count == 1
assert port.control.call_args == mock.call(False)
# pylint: disable=protected-access
assert not switch._target_state