mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
LiteJet is now configured using config_flow (#44409)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
"""The tests for the litejet component."""
|
||||
import logging
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant import setup
|
||||
from homeassistant.components import litejet
|
||||
import homeassistant.components.switch as switch
|
||||
from homeassistant.components import switch
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.components.switch import common
|
||||
from . import async_init_integration
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -18,117 +14,67 @@ ENTITY_OTHER_SWITCH = "switch.mock_switch_2"
|
||||
ENTITY_OTHER_SWITCH_NUMBER = 2
|
||||
|
||||
|
||||
class TestLiteJetSwitch(unittest.TestCase):
|
||||
"""Test the litejet component."""
|
||||
async def test_on_off(hass, mock_litejet):
|
||||
"""Test turning the switch on and off."""
|
||||
|
||||
@mock.patch("homeassistant.components.litejet.LiteJet")
|
||||
def setup_method(self, method, mock_pylitejet):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.start()
|
||||
await async_init_integration(hass, use_switch=True)
|
||||
|
||||
self.switch_pressed_callbacks = {}
|
||||
self.switch_released_callbacks = {}
|
||||
assert hass.states.get(ENTITY_SWITCH).state == "off"
|
||||
assert hass.states.get(ENTITY_OTHER_SWITCH).state == "off"
|
||||
|
||||
def get_switch_name(number):
|
||||
return f"Mock Switch #{number}"
|
||||
assert not switch.is_on(hass, ENTITY_SWITCH)
|
||||
|
||||
def on_switch_pressed(number, callback):
|
||||
self.switch_pressed_callbacks[number] = callback
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_SWITCH}, blocking=True
|
||||
)
|
||||
mock_litejet.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
|
||||
|
||||
def on_switch_released(number, callback):
|
||||
self.switch_released_callbacks[number] = callback
|
||||
await hass.services.async_call(
|
||||
switch.DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_SWITCH}, blocking=True
|
||||
)
|
||||
mock_litejet.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
|
||||
|
||||
self.mock_lj = mock_pylitejet.return_value
|
||||
self.mock_lj.loads.return_value = range(0)
|
||||
self.mock_lj.button_switches.return_value = range(1, 3)
|
||||
self.mock_lj.all_switches.return_value = range(1, 6)
|
||||
self.mock_lj.scenes.return_value = range(0)
|
||||
self.mock_lj.get_switch_name.side_effect = get_switch_name
|
||||
self.mock_lj.on_switch_pressed.side_effect = on_switch_pressed
|
||||
self.mock_lj.on_switch_released.side_effect = on_switch_released
|
||||
|
||||
config = {"litejet": {"port": "/dev/serial/by-id/mock-litejet"}}
|
||||
if method == self.test_include_switches_False:
|
||||
config["litejet"]["include_switches"] = False
|
||||
elif method != self.test_include_switches_unspecified:
|
||||
config["litejet"]["include_switches"] = True
|
||||
async def test_pressed_event(hass, mock_litejet):
|
||||
"""Test handling an event from LiteJet."""
|
||||
|
||||
assert setup.setup_component(self.hass, litejet.DOMAIN, config)
|
||||
self.hass.block_till_done()
|
||||
await async_init_integration(hass, use_switch=True)
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
# Switch 1
|
||||
mock_litejet.switch_pressed_callbacks[ENTITY_SWITCH_NUMBER]()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
def switch(self):
|
||||
"""Return the switch state."""
|
||||
return self.hass.states.get(ENTITY_SWITCH)
|
||||
assert switch.is_on(hass, ENTITY_SWITCH)
|
||||
assert not switch.is_on(hass, ENTITY_OTHER_SWITCH)
|
||||
assert hass.states.get(ENTITY_SWITCH).state == "on"
|
||||
assert hass.states.get(ENTITY_OTHER_SWITCH).state == "off"
|
||||
|
||||
def other_switch(self):
|
||||
"""Return the other switch state."""
|
||||
return self.hass.states.get(ENTITY_OTHER_SWITCH)
|
||||
# Switch 2
|
||||
mock_litejet.switch_pressed_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
def test_include_switches_unspecified(self):
|
||||
"""Test that switches are ignored by default."""
|
||||
self.mock_lj.button_switches.assert_not_called()
|
||||
self.mock_lj.all_switches.assert_not_called()
|
||||
assert switch.is_on(hass, ENTITY_OTHER_SWITCH)
|
||||
assert switch.is_on(hass, ENTITY_SWITCH)
|
||||
assert hass.states.get(ENTITY_SWITCH).state == "on"
|
||||
assert hass.states.get(ENTITY_OTHER_SWITCH).state == "on"
|
||||
|
||||
def test_include_switches_False(self):
|
||||
"""Test that switches can be explicitly ignored."""
|
||||
self.mock_lj.button_switches.assert_not_called()
|
||||
self.mock_lj.all_switches.assert_not_called()
|
||||
|
||||
def test_on_off(self):
|
||||
"""Test turning the switch on and off."""
|
||||
assert self.switch().state == "off"
|
||||
assert self.other_switch().state == "off"
|
||||
async def test_released_event(hass, mock_litejet):
|
||||
"""Test handling an event from LiteJet."""
|
||||
|
||||
assert not switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
await async_init_integration(hass, use_switch=True)
|
||||
|
||||
common.turn_on(self.hass, ENTITY_SWITCH)
|
||||
self.hass.block_till_done()
|
||||
self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
|
||||
# Initial state is on.
|
||||
mock_litejet.switch_pressed_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
common.turn_off(self.hass, ENTITY_SWITCH)
|
||||
self.hass.block_till_done()
|
||||
self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
|
||||
assert switch.is_on(hass, ENTITY_OTHER_SWITCH)
|
||||
|
||||
def test_pressed_event(self):
|
||||
"""Test handling an event from LiteJet."""
|
||||
# Switch 1
|
||||
_LOGGER.info(self.switch_pressed_callbacks[ENTITY_SWITCH_NUMBER])
|
||||
self.switch_pressed_callbacks[ENTITY_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
# Event indicates it is off now.
|
||||
mock_litejet.switch_released_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
assert not switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
assert self.switch().state == "on"
|
||||
assert self.other_switch().state == "off"
|
||||
|
||||
# Switch 2
|
||||
self.switch_pressed_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
assert switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
assert self.other_switch().state == "on"
|
||||
assert self.switch().state == "on"
|
||||
|
||||
def test_released_event(self):
|
||||
"""Test handling an event from LiteJet."""
|
||||
# Initial state is on.
|
||||
self.switch_pressed_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
|
||||
# Event indicates it is off now.
|
||||
|
||||
self.switch_released_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert not switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
assert not switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
assert self.other_switch().state == "off"
|
||||
assert self.switch().state == "off"
|
||||
assert not switch.is_on(hass, ENTITY_OTHER_SWITCH)
|
||||
assert not switch.is_on(hass, ENTITY_SWITCH)
|
||||
assert hass.states.get(ENTITY_SWITCH).state == "off"
|
||||
assert hass.states.get(ENTITY_OTHER_SWITCH).state == "off"
|
||||
|
||||
Reference in New Issue
Block a user