hass-core/tests/components/light/test_mochad.py
Matthew Treinish 640d58f0a8 Fix X10 commands for mochad light turn on (#11146)
* Fix X10 commands for mochad light turn on

This commit attempts to address issues that a lot of people are having
with the x10 light component. Originally this was written to use the
xdim (extended dim) X10 command. However, not every X10 dimmer device
supports the xdim command. Additionally, it turns out the number of
dim/brighness levels the X10 device supports is device specific and
there is no way to detect this (given the mostly 1 way nature of X10)

To address these issues, this commit removes the usage of xdim and
instead relies on using the 'on' command and the 'dim' command. This
should work on all x10 light devices. In an attempt to address the
different dim/brightness levels supported by different devices this
commit also adds a new optional config value, 'brightness_levels', to
specify if it's either 32, 64, or 256. By default 32 levels are used
as this is the normal case and what is documented by mochad.

Fixes #8943

* make code more readable

* fix style

* fix lint

* fix tests
2017-12-17 00:52:40 +01:00

154 lines
4.8 KiB
Python

"""The tests for the mochad light platform."""
import unittest
import unittest.mock as mock
import pytest
from homeassistant.components import light
from homeassistant.components.light import mochad
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant
@pytest.fixture(autouse=True)
def pymochad_mock():
"""Mock pymochad."""
with mock.patch.dict('sys.modules', {
'pymochad': mock.MagicMock(),
}):
yield
class TestMochadSwitchSetup(unittest.TestCase):
"""Test the mochad light."""
PLATFORM = mochad
COMPONENT = light
THING = 'light'
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
@mock.patch('homeassistant.components.light.mochad.MochadLight')
def test_setup_adds_proper_devices(self, mock_light):
"""Test if setup adds devices."""
good_config = {
'mochad': {},
'light': {
'platform': 'mochad',
'devices': [
{
'name': 'Light1',
'address': 'a1',
},
],
}
}
self.assertTrue(setup_component(self.hass, light.DOMAIN, good_config))
class TestMochadLight(unittest.TestCase):
"""Test for mochad light platform."""
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
controller_mock = mock.MagicMock()
dev_dict = {'address': 'a1', 'name': 'fake_light',
'brightness_levels': 32}
self.light = mochad.MochadLight(self.hass, controller_mock,
dev_dict)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_name(self):
"""Test the name."""
self.assertEqual('fake_light', self.light.name)
def test_turn_on_with_no_brightness(self):
"""Test turn_on."""
self.light.turn_on()
self.light.device.send_cmd.assert_called_once_with('on')
def test_turn_on_with_brightness(self):
"""Test turn_on."""
self.light.turn_on(brightness=45)
self.light.device.send_cmd.assert_has_calls(
[mock.call('on'), mock.call('dim 25')])
def test_turn_off(self):
"""Test turn_off."""
self.light.turn_off()
self.light.device.send_cmd.assert_called_once_with('off')
class TestMochadLight256Levels(unittest.TestCase):
"""Test for mochad light platform."""
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
controller_mock = mock.MagicMock()
dev_dict = {'address': 'a1', 'name': 'fake_light',
'brightness_levels': 256}
self.light = mochad.MochadLight(self.hass, controller_mock,
dev_dict)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_turn_on_with_no_brightness(self):
"""Test turn_on."""
self.light.turn_on()
self.light.device.send_cmd.assert_called_once_with('xdim 255')
def test_turn_on_with_brightness(self):
"""Test turn_on."""
self.light.turn_on(brightness=45)
self.light.device.send_cmd.assert_called_once_with('xdim 45')
def test_turn_off(self):
"""Test turn_off."""
self.light.turn_off()
self.light.device.send_cmd.assert_called_once_with('off')
class TestMochadLight64Levels(unittest.TestCase):
"""Test for mochad light platform."""
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
controller_mock = mock.MagicMock()
dev_dict = {'address': 'a1', 'name': 'fake_light',
'brightness_levels': 64}
self.light = mochad.MochadLight(self.hass, controller_mock,
dev_dict)
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_turn_on_with_no_brightness(self):
"""Test turn_on."""
self.light.turn_on()
self.light.device.send_cmd.assert_called_once_with('xdim 63')
def test_turn_on_with_brightness(self):
"""Test turn_on."""
self.light.turn_on(brightness=45)
self.light.device.send_cmd.assert_called_once_with('xdim 11')
def test_turn_off(self):
"""Test turn_off."""
self.light.turn_off()
self.light.device.send_cmd.assert_called_once_with('off')