LiteJet: Unit tests and new trigger options held_more_than and held_less_than. (#4473)
* LiteJet: Unit tests and new trigger options held_more_than and held_less_than. * Unit tests for the LiteJet component and associated platforms. Coverage is almost 100% -- just misses one line. * The automation LiteJet trigger returns an empty "removal" function to ensure the automation base is happy with it. The pylitejet library doesn't actually support a real removal. * The automation LiteJet trigger can detect hold time and act appropriately to support things like short tap or long hold. * LiteJet: Fix indent in unit test source code. * LiteJet: Fix test_include_switches_* unit tests on Python 3.5 * LiteJet: Remove wait for state existence from unit tests. Recent fixes to discovery make this no longer necessary.
This commit is contained in:
1 parent
84040892df
commit
2a6c0cfc17
7 files changed
+727
-6
No files matched your search
@@ -0,0 +1,168 @@
|
||||
"""The tests for the litejet component."""
|
||||
import logging
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant import bootstrap
|
||||
from homeassistant.components import litejet
|
||||
from tests.common import get_test_home_assistant
|
||||
import homeassistant.components.light as light
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ENTITY_LIGHT = 'light.mock_load_1'
|
||||
ENTITY_LIGHT_NUMBER = 1
|
||||
ENTITY_OTHER_LIGHT = 'light.mock_load_2'
|
||||
ENTITY_OTHER_LIGHT_NUMBER = 2
|
||||
|
||||
|
||||
class TestLiteJetLight(unittest.TestCase):
|
||||
"""Test the litejet component."""
|
||||
|
||||
@mock.patch('pylitejet.LiteJet')
|
||||
def setup_method(self, method, mock_pylitejet):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.start()
|
||||
|
||||
self.load_activated_callbacks = {}
|
||||
self.load_deactivated_callbacks = {}
|
||||
|
||||
def get_load_name(number):
|
||||
return "Mock Load #"+str(number)
|
||||
|
||||
def on_load_activated(number, callback):
|
||||
self.load_activated_callbacks[number] = callback
|
||||
|
||||
def on_load_deactivated(number, callback):
|
||||
self.load_deactivated_callbacks[number] = callback
|
||||
|
||||
self.mock_lj = mock_pylitejet.return_value
|
||||
self.mock_lj.loads.return_value = range(1, 3)
|
||||
self.mock_lj.button_switches.return_value = range(0)
|
||||
self.mock_lj.all_switches.return_value = range(0)
|
||||
self.mock_lj.scenes.return_value = range(0)
|
||||
self.mock_lj.get_load_level.return_value = 0
|
||||
self.mock_lj.get_load_name.side_effect = get_load_name
|
||||
self.mock_lj.on_load_activated.side_effect = on_load_activated
|
||||
self.mock_lj.on_load_deactivated.side_effect = on_load_deactivated
|
||||
|
||||
assert bootstrap.setup_component(
|
||||
self.hass,
|
||||
litejet.DOMAIN,
|
||||
{
|
||||
'litejet': {
|
||||
'port': '/tmp/this_will_be_mocked'
|
||||
}
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.mock_lj.get_load_level.reset_mock()
|
||||
|
||||
def light(self):
|
||||
return self.hass.states.get(ENTITY_LIGHT)
|
||||
|
||||
def other_light(self):
|
||||
return self.hass.states.get(ENTITY_OTHER_LIGHT)
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_on_brightness(self):
|
||||
"""Test turning the light on with brightness."""
|
||||
|
||||
assert self.light().state == 'off'
|
||||
assert self.other_light().state == 'off'
|
||||
|
||||
assert not light.is_on(self.hass, ENTITY_LIGHT)
|
||||
|
||||
light.turn_on(self.hass, ENTITY_LIGHT, brightness=102)
|
||||
self.hass.block_till_done()
|
||||
self.mock_lj.activate_load_at.assert_called_with(
|
||||
ENTITY_LIGHT_NUMBER, 39, 0)
|
||||
|
||||
def test_on_off(self):
|
||||
"""Test turning the light on and off."""
|
||||
|
||||
assert self.light().state == 'off'
|
||||
assert self.other_light().state == 'off'
|
||||
|
||||
assert not light.is_on(self.hass, ENTITY_LIGHT)
|
||||
|
||||
light.turn_on(self.hass, ENTITY_LIGHT)
|
||||
self.hass.block_till_done()
|
||||
self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
|
||||
|
||||
light.turn_off(self.hass, ENTITY_LIGHT)
|
||||
self.hass.block_till_done()
|
||||
self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
|
||||
|
||||
def test_activated_event(self):
|
||||
"""Test handling an event from LiteJet."""
|
||||
|
||||
self.mock_lj.get_load_level.return_value = 99
|
||||
|
||||
# Light 1
|
||||
|
||||
_LOGGER.info(self.load_activated_callbacks[ENTITY_LIGHT_NUMBER])
|
||||
self.load_activated_callbacks[ENTITY_LIGHT_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.mock_lj.get_load_level.assert_called_once_with(
|
||||
ENTITY_LIGHT_NUMBER)
|
||||
|
||||
assert light.is_on(self.hass, ENTITY_LIGHT)
|
||||
assert not light.is_on(self.hass, ENTITY_OTHER_LIGHT)
|
||||
assert self.light().state == 'on'
|
||||
assert self.other_light().state == 'off'
|
||||
assert self.light().attributes.get(light.ATTR_BRIGHTNESS) == 255
|
||||
|
||||
# Light 2
|
||||
|
||||
self.mock_lj.get_load_level.return_value = 40
|
||||
|
||||
self.mock_lj.get_load_level.reset_mock()
|
||||
|
||||
self.load_activated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.mock_lj.get_load_level.assert_called_once_with(
|
||||
ENTITY_OTHER_LIGHT_NUMBER)
|
||||
|
||||
assert light.is_on(self.hass, ENTITY_OTHER_LIGHT)
|
||||
assert light.is_on(self.hass, ENTITY_LIGHT)
|
||||
assert self.light().state == 'on'
|
||||
assert self.other_light().state == 'on'
|
||||
assert int(self.other_light().attributes[light.ATTR_BRIGHTNESS]) == 103
|
||||
|
||||
def test_deactivated_event(self):
|
||||
"""Test handling an event from LiteJet."""
|
||||
|
||||
# Initial state is on.
|
||||
|
||||
self.mock_lj.get_load_level.return_value = 99
|
||||
|
||||
self.load_activated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert light.is_on(self.hass, ENTITY_OTHER_LIGHT)
|
||||
|
||||
# Event indicates it is off now.
|
||||
|
||||
self.mock_lj.get_load_level.reset_mock()
|
||||
self.mock_lj.get_load_level.return_value = 0
|
||||
|
||||
self.load_deactivated_callbacks[ENTITY_OTHER_LIGHT_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
# (Requesting the level is not strictly needed with a deactivated
|
||||
# event but the implementation happens to do it. This could be
|
||||
# changed to a assert_not_called in the future.)
|
||||
self.mock_lj.get_load_level.assert_called_with(
|
||||
ENTITY_OTHER_LIGHT_NUMBER)
|
||||
|
||||
assert not light.is_on(self.hass, ENTITY_OTHER_LIGHT)
|
||||
assert not light.is_on(self.hass, ENTITY_LIGHT)
|
||||
assert self.light().state == 'off'
|
||||
assert self.other_light().state == 'off'
|
||||
Reference in new issue
Block a user