Convert rfxtrx tests to pytest async tests and re-enable (#37206)

* Rework and re-enable rfxtrx tests

* Add missed change to _signal_event

* Fixup the dummy serial that causes max cpu

* Make sure we cleanup thread here too

* Make sure we always wait for tasks before we check state

* Some more places we need to wait before checking
This commit is contained in:
Joakim Plate 2020-07-03 10:22:02 +02:00 committed by GitHub
parent b859be8cea
commit ab4687d914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1079 additions and 1032 deletions

View File

@ -1 +1,6 @@
"""Tests for the rfxtrx component."""
from homeassistant.components import rfxtrx
async def _signal_event(hass, event):
await hass.async_add_executor_job(rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0], event)

View File

@ -0,0 +1,75 @@
"""Common test tools."""
import threading
from unittest import mock
import RFXtrx
import pytest
from homeassistant.components import rfxtrx as rfxtrx_core
from tests.common import mock_component
class FixedDummySerial(RFXtrx._dummySerial): # pylint: disable=protected-access
"""Fixed dummy serial that doesn't cause max CPU usage."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self._close_event = threading.Event()
def read(self, data=None):
"""Read."""
res = super().read(data)
if not res and not data:
self._close_event.wait(0.1)
return res
def close(self):
"""Close."""
self._close_event.set()
class FixedDummyTransport(RFXtrx.DummyTransport):
"""Fixed dummy transport that maxes CPU."""
def __init__(self, device="", debug=True):
"""Init."""
super().__init__(device, debug)
self._close_event = threading.Event()
def receive_blocking(self, data=None):
"""Read."""
res = super().receive_blocking(data)
if not res:
self._close_event.wait(0.1)
return res
def close(self):
"""Close."""
self._close_event.set()
@pytest.fixture(autouse=True)
async def rfxtrx_cleanup():
"""Fixture that cleans up threads from integration."""
with mock.patch("RFXtrx._dummySerial", new=FixedDummySerial), mock.patch(
"RFXtrx.DummyTransport", new=FixedDummyTransport
):
yield
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS.clear()
rfxtrx_core.RFX_DEVICES.clear()
@pytest.fixture(name="rfxtrx")
async def rfxtrx_fixture(hass):
"""Stub out core rfxtrx to test platform."""
mock_component(hass, "rfxtrx")
yield
# These test don't listen for stop to do cleanup.
if rfxtrx_core.DATA_RFXOBJECT in hass.data:
hass.data[rfxtrx_core.DATA_RFXOBJECT].close_connection()

View File

@ -1,37 +1,19 @@
"""The tests for the Rfxtrx cover platform."""
import unittest
import RFXtrx as rfxtrxmod
import pytest
from homeassistant.components import rfxtrx as rfxtrx_core
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant, mock_component
from . import _signal_event
from tests.common import assert_setup_component
@pytest.mark.skipif("os.environ.get('RFXTRX') != 'RUN'")
class TestCoverRfxtrx(unittest.TestCase):
"""Test the Rfxtrx cover platform."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_component(self.hass, "rfxtrx")
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS.clear()
rfxtrx_core.RFX_DEVICES.clear()
if rfxtrx_core.DATA_RFXOBJECT in self.hass.data:
self.hass.data[rfxtrx_core.DATA_RFXOBJECT].close_connection()
self.hass.stop()
def test_valid_config(self):
async def test_valid_config(hass, rfxtrx):
"""Test configuration."""
assert setup_component(
self.hass,
with assert_setup_component(1):
assert await async_setup_component(
hass,
"cover",
{
"cover": {
@ -46,18 +28,23 @@ class TestCoverRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
def test_default_config(self):
async def test_default_config(hass, rfxtrx):
"""Test with 0 cover."""
assert setup_component(
self.hass, "cover", {"cover": {"platform": "rfxtrx", "devices": {}}}
assert await async_setup_component(
hass, "cover", {"cover": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert 0 == len(rfxtrx_core.RFX_DEVICES)
def test_one_cover(self):
async def test_one_cover(hass, rfxtrx):
"""Test with 1 cover."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"cover",
{
"cover": {
@ -66,14 +53,16 @@ class TestCoverRfxtrx(unittest.TestCase):
}
},
)
self.hass.block_till_done()
self.hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxtrxmod.Core(
await hass.async_block_till_done()
hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxtrxmod.Core(
"", transport_protocol=rfxtrxmod.DummyTransport
)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
for id in rfxtrx_core.RFX_DEVICES:
entity = rfxtrx_core.RFX_DEVICES[id]
entity.hass = hass
assert entity.signal_repetitions == 1
assert not entity.should_fire_event
assert not entity.should_poll
@ -81,10 +70,11 @@ class TestCoverRfxtrx(unittest.TestCase):
entity.close_cover()
entity.stop_cover()
def test_several_covers(self):
async def test_several_covers(hass, rfxtrx):
"""Test with 3 covers."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"cover",
{
"cover": {
@ -98,6 +88,7 @@ class TestCoverRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
assert 3 == len(rfxtrx_core.RFX_DEVICES)
device_num = 0
@ -113,21 +104,22 @@ class TestCoverRfxtrx(unittest.TestCase):
assert 3 == device_num
def test_discover_covers(self):
async def test_discover_covers(hass, rfxtrx):
"""Test with discovery of covers."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"cover",
{"cover": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
)
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0a140002f38cae010f0070")
event.data = bytearray(
[0x0A, 0x14, 0x00, 0x02, 0xF3, 0x8C, 0xAE, 0x01, 0x0F, 0x00, 0x70]
)
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0a1400adf394ab020e0060")
@ -135,15 +127,13 @@ class TestCoverRfxtrx(unittest.TestCase):
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
)
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a sensor
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a light
@ -151,40 +141,38 @@ class TestCoverRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0B, 0x11, 0x11, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x02, 0x0F, 0x70]
)
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
def test_discover_cover_noautoadd(self):
async def test_discover_cover_noautoadd(hass, rfxtrx):
"""Test with discovery of cover when auto add is False."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"cover",
{"cover": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0a1400adf394ab010d0060")
event.data = bytearray(
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x01, 0x0D, 0x00, 0x60]
)
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0a1400adf394ab020e0060")
event.data = bytearray(
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
)
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a sensor
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a light
@ -192,6 +180,5 @@ class TestCoverRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0B, 0x11, 0x11, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x02, 0x0F, 0x70]
)
for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS:
evt_sub(event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)

View File

@ -1,38 +1,22 @@
"""The tests for the Rfxtrx component."""
# pylint: disable=protected-access
import time
import unittest
import asyncio
import pytest
from async_timeout import timeout
from homeassistant.components import rfxtrx
from homeassistant.core import callback
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
from . import _signal_event
from tests.common import assert_setup_component
@pytest.mark.skipif("os.environ.get('RFXTRX') != 'RUN'")
class TestRFXTRX(unittest.TestCase):
"""Test the Rfxtrx component."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.clear()
rfxtrx.RFX_DEVICES.clear()
if rfxtrx.DATA_RFXOBJECT in self.hass.data:
self.hass.data[rfxtrx.DATA_RFXOBJECT].close_connection()
self.hass.stop()
def test_default_config(self):
async def test_default_config(hass):
"""Test configuration."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
@ -43,20 +27,25 @@ class TestRFXTRX(unittest.TestCase):
},
)
assert setup_component(
self.hass,
with assert_setup_component(1, "sensor"):
await async_setup_component(
hass,
"sensor",
{"sensor": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
)
time.sleep(1) # Dummy startup is slow
# Dummy startup is slow
async with timeout(10):
while len(hass.data[rfxtrx.DATA_RFXOBJECT].sensors()) < 2:
await asyncio.sleep(0.1)
assert len(self.hass.data[rfxtrx.DATA_RFXOBJECT].sensors()) == 2
assert len(hass.data[rfxtrx.DATA_RFXOBJECT].sensors()) == 2
def test_valid_config(self):
async def test_valid_config(hass):
"""Test configuration."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
@ -67,10 +56,11 @@ class TestRFXTRX(unittest.TestCase):
},
)
def test_valid_config2(self):
async def test_valid_config2(hass):
"""Test configuration."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
@ -82,12 +72,13 @@ class TestRFXTRX(unittest.TestCase):
},
)
def test_invalid_config(self):
"""Test configuration."""
assert not setup_component(self.hass, "rfxtrx", {"rfxtrx": {}})
assert not setup_component(
self.hass,
async def test_invalid_config(hass):
"""Test configuration."""
assert not await async_setup_component(hass, "rfxtrx", {"rfxtrx": {}})
assert not await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
@ -98,10 +89,11 @@ class TestRFXTRX(unittest.TestCase):
},
)
def test_fire_event(self):
async def test_fire_event(hass):
"""Test fire event."""
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
@ -111,8 +103,9 @@ class TestRFXTRX(unittest.TestCase):
}
},
)
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"switch",
{
"switch": {
@ -127,6 +120,7 @@ class TestRFXTRX(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
calls = []
@ -135,8 +129,8 @@ class TestRFXTRX(unittest.TestCase):
"""Add recorded event to set."""
calls.append(event)
self.hass.bus.listen(rfxtrx.EVENT_BUTTON_PRESSED, record_event)
self.hass.block_till_done()
hass.bus.async_listen(rfxtrx.EVENT_BUTTON_PRESSED, record_event)
await hass.async_block_till_done()
entity = rfxtrx.RFX_DEVICES["213c7f2_16"]
entity.update_state(False, 0)
assert "Test" == entity.name
@ -147,19 +141,20 @@ class TestRFXTRX(unittest.TestCase):
event.data = bytearray(
[0x0B, 0x11, 0x00, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x01, 0x0F, 0x70]
)
rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event)
self.hass.block_till_done()
await _signal_event(hass, event)
await hass.async_block_till_done()
assert event.values["Command"] == "On"
assert "on" == entity.state
assert self.hass.states.get("switch.test").state == "on"
assert hass.states.get("switch.test").state == "on"
assert 1 == len(calls)
assert calls[0].data == {"entity_id": "switch.test", "state": "on"}
def test_fire_event_sensor(self):
async def test_fire_event_sensor(hass):
"""Test fire event."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
@ -169,8 +164,9 @@ class TestRFXTRX(unittest.TestCase):
}
},
)
assert setup_component(
self.hass,
await async_setup_component(
hass,
"sensor",
{
"sensor": {
@ -185,6 +181,7 @@ class TestRFXTRX(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
calls = []
@ -193,12 +190,12 @@ class TestRFXTRX(unittest.TestCase):
"""Add recorded event to set."""
calls.append(event)
self.hass.bus.listen("signal_received", record_event)
self.hass.block_till_done()
hass.bus.async_listen("signal_received", record_event)
await hass.async_block_till_done()
event = rfxtrx.get_rfx_object("0a520802060101ff0f0269")
event.data = bytearray(b"\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y")
rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
self.hass.block_till_done()
await hass.async_block_till_done()
assert 1 == len(calls)
assert calls[0].data == {"entity_id": "sensor.test_temperature"}

View File

@ -1,36 +1,17 @@
"""The tests for the Rfxtrx light platform."""
import unittest
import pytest
from homeassistant.components import rfxtrx as rfxtrx_core
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant, mock_component
from . import _signal_event
from tests.common import assert_setup_component
@pytest.mark.skipif("os.environ.get('RFXTRX') != 'RUN'")
class TestLightRfxtrx(unittest.TestCase):
"""Test the Rfxtrx light platform."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_component(self.hass, "rfxtrx")
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS.clear()
rfxtrx_core.RFX_DEVICES.clear()
if rfxtrx_core.DATA_RFXOBJECT in self.hass.data:
self.hass.data[rfxtrx_core.DATA_RFXOBJECT].close_connection()
self.hass.stop()
def test_valid_config(self):
async def test_valid_config(hass, rfxtrx):
"""Test configuration."""
assert setup_component(
self.hass,
with assert_setup_component(1):
assert await async_setup_component(
hass,
"light",
{
"light": {
@ -46,8 +27,8 @@ class TestLightRfxtrx(unittest.TestCase):
},
)
assert setup_component(
self.hass,
assert await async_setup_component(
hass,
"light",
{
"light": {
@ -63,17 +44,22 @@ class TestLightRfxtrx(unittest.TestCase):
},
)
def test_default_config(self):
async def test_default_config(hass, rfxtrx):
"""Test with 0 switches."""
assert setup_component(
self.hass, "light", {"light": {"platform": "rfxtrx", "devices": {}}}
with assert_setup_component(1):
await async_setup_component(
hass, "light", {"light": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert 0 == len(rfxtrx_core.RFX_DEVICES)
def test_one_light(self):
async def test_one_light(hass, rfxtrx):
"""Test with 1 light."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"light",
{
"light": {
@ -82,16 +68,17 @@ class TestLightRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
import RFXtrx as rfxtrxmod
self.hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxtrxmod.Core(
hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxtrxmod.Core(
"", transport_protocol=rfxtrxmod.DummyTransport
)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
entity = rfxtrx_core.RFX_DEVICES["213c7f2_16"]
entity.hass = self.hass
entity.hass = hass
assert "Test" == entity.name
assert "off" == entity.state
assert entity.assumed_state
@ -140,10 +127,11 @@ class TestLightRfxtrx(unittest.TestCase):
entity.turn_on(brightness=255)
assert "on" == entity.state
def test_several_lights(self):
async def test_several_lights(hass, rfxtrx):
"""Test with 3 lights."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"light",
{
"light": {
@ -157,6 +145,7 @@ class TestLightRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
assert 3 == len(rfxtrx_core.RFX_DEVICES)
device_num = 0
@ -178,18 +167,20 @@ class TestLightRfxtrx(unittest.TestCase):
assert 3 == device_num
def test_discover_light(self):
async def test_discover_light(hass, rfxtrx):
"""Test with discovery of lights."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"light",
{"light": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
)
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0b11009e00e6116202020070")
event.data = bytearray(b"\x0b\x11\x00\x9e\x00\xe6\x11b\x02\x02\x00p")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
entity = rfxtrx_core.RFX_DEVICES["0e61162_2"]
assert 1 == len(rfxtrx_core.RFX_DEVICES)
assert "<Entity 0b11009e00e6116202020070: on>" == entity.__str__()
@ -197,7 +188,7 @@ class TestLightRfxtrx(unittest.TestCase):
event = rfxtrx_core.get_rfx_object("0b11009e00e6116201010070")
event.data = bytearray(b"\x0b\x11\x00\x9e\x00\xe6\x11b\x01\x01\x00p")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0b1100120118cdea02020070")
@ -205,7 +196,7 @@ class TestLightRfxtrx(unittest.TestCase):
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x02, 0x00, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
entity = rfxtrx_core.RFX_DEVICES["118cdea_2"]
assert 2 == len(rfxtrx_core.RFX_DEVICES)
assert "<Entity 0b1100120118cdea02020070: on>" == entity.__str__()
@ -213,7 +204,7 @@ class TestLightRfxtrx(unittest.TestCase):
# trying to add a sensor
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# trying to add a switch
@ -222,7 +213,7 @@ class TestLightRfxtrx(unittest.TestCase):
[0x0B, 0x11, 0x00, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x01, 0x0F, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a rollershutter
@ -230,23 +221,25 @@ class TestLightRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
def test_discover_light_noautoadd(self):
async def test_discover_light_noautoadd(hass, rfxtrx):
"""Test with discover of light when auto add is False."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"light",
{"light": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0b1100120118cdea02020070")
event.data = bytearray(
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x02, 0x00, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0b1100120118cdea02010070")
@ -254,7 +247,7 @@ class TestLightRfxtrx(unittest.TestCase):
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x01, 0x00, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0b1100120118cdea02020070")
@ -262,13 +255,13 @@ class TestLightRfxtrx(unittest.TestCase):
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x02, 0x00, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a sensor
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a switch
@ -276,7 +269,7 @@ class TestLightRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0B, 0x11, 0x00, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x01, 0x0F, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a rollershutter
@ -284,5 +277,5 @@ class TestLightRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)

View File

@ -1,44 +1,25 @@
"""The tests for the Rfxtrx sensor platform."""
import unittest
import pytest
from homeassistant.components import rfxtrx as rfxtrx_core
from homeassistant.const import TEMP_CELSIUS, UNIT_PERCENTAGE
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant, mock_component
from . import _signal_event
@pytest.mark.skipif("os.environ.get('RFXTRX') != 'RUN'")
class TestSensorRfxtrx(unittest.TestCase):
"""Test the Rfxtrx sensor platform."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_component(self.hass, "rfxtrx")
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS.clear()
rfxtrx_core.RFX_DEVICES.clear()
if rfxtrx_core.DATA_RFXOBJECT in self.hass.data:
self.hass.data[rfxtrx_core.DATA_RFXOBJECT].close_connection()
self.hass.stop()
def test_default_config(self):
async def test_default_config(hass, rfxtrx):
"""Test with 0 sensor."""
assert setup_component(
self.hass, "sensor", {"sensor": {"platform": "rfxtrx", "devices": {}}}
await async_setup_component(
hass, "sensor", {"sensor": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert 0 == len(rfxtrx_core.RFX_DEVICES)
def test_one_sensor(self):
async def test_one_sensor(hass, rfxtrx):
"""Test with 1 sensor."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"sensor",
{
"sensor": {
@ -52,6 +33,7 @@ class TestSensorRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
assert 1 == len(rfxtrx_core.RFX_DEVICES)
entity = rfxtrx_core.RFX_DEVICES["sensor_05_02"]["Temperature"]
@ -59,10 +41,11 @@ class TestSensorRfxtrx(unittest.TestCase):
assert TEMP_CELSIUS == entity.unit_of_measurement
assert entity.state is None
def test_one_sensor_no_datatype(self):
async def test_one_sensor_no_datatype(hass, rfxtrx):
"""Test with 1 sensor."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"sensor",
{
"sensor": {
@ -71,6 +54,7 @@ class TestSensorRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
assert 1 == len(rfxtrx_core.RFX_DEVICES)
entity = rfxtrx_core.RFX_DEVICES["sensor_05_02"]["Temperature"]
@ -78,10 +62,11 @@ class TestSensorRfxtrx(unittest.TestCase):
assert TEMP_CELSIUS == entity.unit_of_measurement
assert entity.state is None
def test_several_sensors(self):
async def test_several_sensors(hass, rfxtrx):
"""Test with 3 sensors."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"sensor",
{
"sensor": {
@ -99,6 +84,7 @@ class TestSensorRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
assert 2 == len(rfxtrx_core.RFX_DEVICES)
device_num = 0
@ -122,18 +108,19 @@ class TestSensorRfxtrx(unittest.TestCase):
assert 2 == device_num
def test_discover_sensor(self):
async def test_discover_sensor(hass, rfxtrx):
"""Test with discovery of sensor."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"sensor",
{"sensor": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
)
self.hass.block_till_done()
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0a520801070100b81b0279")
event.data = bytearray(b"\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
entity = rfxtrx_core.RFX_DEVICES["sensor_07_01"]["Temperature"]
assert 1 == len(rfxtrx_core.RFX_DEVICES)
@ -147,12 +134,12 @@ class TestSensorRfxtrx(unittest.TestCase):
} == entity.device_state_attributes
assert "0a520801070100b81b0279" == entity.__str__()
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0a52080405020095240279")
event.data = bytearray(b"\nR\x08\x04\x05\x02\x00\x95$\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
entity = rfxtrx_core.RFX_DEVICES["sensor_05_02"]["Temperature"]
assert 2 == len(rfxtrx_core.RFX_DEVICES)
assert {
@ -167,7 +154,7 @@ class TestSensorRfxtrx(unittest.TestCase):
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
entity = rfxtrx_core.RFX_DEVICES["sensor_07_01"]["Temperature"]
assert 2 == len(rfxtrx_core.RFX_DEVICES)
assert {
@ -182,41 +169,44 @@ class TestSensorRfxtrx(unittest.TestCase):
# trying to add a switch
event = rfxtrx_core.get_rfx_object("0b1100cd0213c7f210010f70")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
def test_discover_sensor_noautoadd(self):
async def test_discover_sensor_noautoadd(hass, rfxtrx):
"""Test with discover of sensor when auto add is False."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"sensor",
{"sensor": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0a520801070100b81b0279")
event.data = bytearray(b"\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y")
assert 0 == len(rfxtrx_core.RFX_DEVICES)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0a52080405020095240279")
event.data = bytearray(b"\nR\x08\x04\x05\x02\x00\x95$\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
def test_update_of_sensors(self):
async def test_update_of_sensors(hass, rfxtrx):
"""Test with 3 sensors."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"sensor",
{
"sensor": {
@ -234,6 +224,7 @@ class TestSensorRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
assert 2 == len(rfxtrx_core.RFX_DEVICES)
device_num = 0
@ -259,12 +250,12 @@ class TestSensorRfxtrx(unittest.TestCase):
event = rfxtrx_core.get_rfx_object("0a520802060101ff0f0269")
event.data = bytearray(b"\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
event = rfxtrx_core.get_rfx_object("0a52080705020085220269")
event.data = bytearray(b"\nR\x08\x04\x05\x02\x00\x95$\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)

View File

@ -1,37 +1,19 @@
"""The tests for the RFXtrx switch platform."""
import unittest
import RFXtrx as rfxtrxmod
import pytest
from homeassistant.components import rfxtrx as rfxtrx_core
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.common import assert_setup_component, get_test_home_assistant, mock_component
from . import _signal_event
from tests.common import assert_setup_component
@pytest.mark.skipif("os.environ.get('RFXTRX') != 'RUN'")
class TestSwitchRfxtrx(unittest.TestCase):
"""Test the RFXtrx switch platform."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_component(self.hass, "rfxtrx")
self.addCleanup(self.tear_down_cleanup)
def tear_down_cleanup(self):
"""Stop everything that was started."""
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS.clear()
rfxtrx_core.RFX_DEVICES.clear()
if rfxtrx_core.DATA_RFXOBJECT in self.hass.data:
self.hass.data[rfxtrx_core.DATA_RFXOBJECT].close_connection()
self.hass.stop()
def test_valid_config(self):
async def test_valid_config(hass, rfxtrx):
"""Test configuration."""
assert setup_component(
self.hass,
with assert_setup_component(1):
await async_setup_component(
hass,
"switch",
{
"switch": {
@ -46,11 +28,14 @@ class TestSwitchRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
def test_valid_config_int_device_id(self):
async def test_valid_config_int_device_id(hass, rfxtrx):
"""Test configuration."""
assert setup_component(
self.hass,
with assert_setup_component(1):
await async_setup_component(
hass,
"switch",
{
"switch": {
@ -65,12 +50,14 @@ class TestSwitchRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
def test_invalid_config2(self):
async def test_invalid_config2(hass, rfxtrx):
"""Test invalid configuration."""
with assert_setup_component(0):
setup_component(
self.hass,
await async_setup_component(
hass,
"switch",
{
"switch": {
@ -86,18 +73,23 @@ class TestSwitchRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
def test_default_config(self):
async def test_default_config(hass, rfxtrx):
"""Test with 0 switches."""
assert setup_component(
self.hass, "switch", {"switch": {"platform": "rfxtrx", "devices": {}}}
await async_setup_component(
hass, "switch", {"switch": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert 0 == len(rfxtrx_core.RFX_DEVICES)
def test_one_switch(self):
async def test_one_switch(hass, rfxtrx):
"""Test with 1 switch."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"switch",
{
"switch": {
@ -106,14 +98,15 @@ class TestSwitchRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
self.hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxtrxmod.Core(
hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxtrxmod.Core(
"", transport_protocol=rfxtrxmod.DummyTransport
)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
entity = rfxtrx_core.RFX_DEVICES["213c7f2_16"]
entity.hass = self.hass
entity.hass = hass
assert "Test" == entity.name
assert "off" == entity.state
assert entity.assumed_state
@ -134,10 +127,11 @@ class TestSwitchRfxtrx(unittest.TestCase):
entity.turn_off()
assert "off" == entity.state
def test_several_switches(self):
async def test_several_switches(hass, rfxtrx):
"""Test with 3 switches."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"switch",
{
"switch": {
@ -151,6 +145,7 @@ class TestSwitchRfxtrx(unittest.TestCase):
}
},
)
await hass.async_block_till_done()
assert 3 == len(rfxtrx_core.RFX_DEVICES)
device_num = 0
@ -172,25 +167,27 @@ class TestSwitchRfxtrx(unittest.TestCase):
assert 3 == device_num
def test_discover_switch(self):
async def test_discover_switch(hass, rfxtrx):
"""Test with discovery of switches."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"switch",
{"switch": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
)
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0b1100100118cdea02010f70")
event.data = bytearray(
[0x0B, 0x11, 0x00, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x01, 0x0F, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
entity = rfxtrx_core.RFX_DEVICES["118cdea_2"]
assert 1 == len(rfxtrx_core.RFX_DEVICES)
assert "<Entity 0b1100100118cdea01010f70: on>" == entity.__str__()
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0b1100100118cdeb02010f70")
@ -198,7 +195,7 @@ class TestSwitchRfxtrx(unittest.TestCase):
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x00, 0x00, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
entity = rfxtrx_core.RFX_DEVICES["118cdeb_2"]
assert 2 == len(rfxtrx_core.RFX_DEVICES)
assert "<Entity 0b1100120118cdea02000070: on>" == entity.__str__()
@ -206,7 +203,7 @@ class TestSwitchRfxtrx(unittest.TestCase):
# Trying to add a sensor
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a light
@ -214,7 +211,7 @@ class TestSwitchRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0B, 0x11, 0x11, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x02, 0x0F, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a rollershutter
@ -222,40 +219,42 @@ class TestSwitchRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
def test_discover_switch_noautoadd(self):
async def test_discover_switch_noautoadd(hass, rfxtrx):
"""Test with discovery of switch when auto add is False."""
assert setup_component(
self.hass,
await async_setup_component(
hass,
"switch",
{"switch": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
event = rfxtrx_core.get_rfx_object("0b1100100118cdea02010f70")
event.data = bytearray(
[0x0B, 0x11, 0x00, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x01, 0x0F, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object("0b1100100118cdeb02010f70")
event.data = bytearray(
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x00, 0x00, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a sensor
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a light
@ -263,7 +262,7 @@ class TestSwitchRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0B, 0x11, 0x11, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x02, 0x0F, 0x70]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a rollershutter
@ -271,5 +270,5 @@ class TestSwitchRfxtrx(unittest.TestCase):
event.data = bytearray(
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
await _signal_event(hass, event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)