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
|
|
|
|
|
2020-10-30 08:13:33 +00:00
|
|
|
import pytest
|
2016-02-09 02:58:09 +00:00
|
|
|
|
2019-03-19 06:07:39 +00:00
|
|
|
import homeassistant.components.mfi.switch as mfi
|
2020-10-30 08:13:33 +00:00
|
|
|
import homeassistant.components.switch as switch_component
|
|
|
|
from homeassistant.setup import async_setup_component
|
2016-02-09 02:58:09 +00:00
|
|
|
|
2020-10-30 08:13:33 +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
|
|
|
|
|
|
|
|
2020-10-30 08:13:33 +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
|
2020-10-30 08:13:33 +00:00
|
|
|
) 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)
|
|
|
|
]
|
2020-10-30 08:13:33 +00:00
|
|
|
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)
|
2020-10-30 08:13:33 +00:00
|
|
|
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
|