2016-10-25 04:49:49 +00:00
|
|
|
"""The tests for the mochad switch platform."""
|
|
|
|
import unittest
|
|
|
|
import unittest.mock as mock
|
|
|
|
|
2017-05-14 04:25:54 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-10-25 04:49:49 +00:00
|
|
|
from homeassistant.components import switch
|
2019-02-02 15:13:16 +00:00
|
|
|
from homeassistant.components.mochad import switch as mochad
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
|
|
|
2017-05-14 04:25:54 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def pymochad_mock():
|
|
|
|
"""Mock pymochad."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with mock.patch.dict(
|
|
|
|
"sys.modules",
|
|
|
|
{"pymochad": mock.MagicMock(), "pymochad.exceptions": mock.MagicMock()},
|
|
|
|
):
|
2017-05-14 04:25:54 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2016-10-25 04:49:49 +00:00
|
|
|
class TestMochadSwitchSetup(unittest.TestCase):
|
|
|
|
"""Test the mochad switch."""
|
|
|
|
|
|
|
|
PLATFORM = mochad
|
|
|
|
COMPONENT = switch
|
2019-07-31 19:25:30 +00:00
|
|
|
THING = "switch"
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-25 04:49:49 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Stop everything that was started."""
|
2016-10-25 04:49:49 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@mock.patch("homeassistant.components.mochad.switch.MochadSwitch")
|
2017-05-14 04:25:54 +00:00
|
|
|
def test_setup_adds_proper_devices(self, mock_switch):
|
2016-10-25 04:49:49 +00:00
|
|
|
"""Test if setup adds devices."""
|
|
|
|
good_config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"mochad": {},
|
|
|
|
"switch": {
|
|
|
|
"platform": "mochad",
|
|
|
|
"devices": [{"name": "Switch1", "address": "a1"}],
|
|
|
|
},
|
2016-10-25 04:49:49 +00:00
|
|
|
}
|
2018-10-24 10:10:05 +00:00
|
|
|
assert setup_component(self.hass, switch.DOMAIN, good_config)
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMochadSwitch(unittest.TestCase):
|
|
|
|
"""Test for mochad switch platform."""
|
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-25 04:49:49 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
controller_mock = mock.MagicMock()
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_dict = {"address": "a1", "name": "fake_switch"}
|
|
|
|
self.switch = mochad.MochadSwitch(self.hass, controller_mock, dev_dict)
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_name(self):
|
|
|
|
"""Test the name."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "fake_switch" == self.switch.name
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
def test_turn_on(self):
|
|
|
|
"""Test turn_on."""
|
|
|
|
self.switch.turn_on()
|
2019-07-31 19:25:30 +00:00
|
|
|
self.switch.switch.send_cmd.assert_called_once_with("on")
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
def test_turn_off(self):
|
|
|
|
"""Test turn_off."""
|
|
|
|
self.switch.turn_off()
|
2019-07-31 19:25:30 +00:00
|
|
|
self.switch.switch.send_cmd.assert_called_once_with("off")
|