diff --git a/tests/components/rfxtrx/__init__.py b/tests/components/rfxtrx/__init__.py index db7a592877e..81b2db8f4df 100644 --- a/tests/components/rfxtrx/__init__.py +++ b/tests/components/rfxtrx/__init__.py @@ -1,14 +1 @@ """Tests for the rfxtrx component.""" -from homeassistant.components import rfxtrx - - -async def _signal_event(hass, packet_id): - event = rfxtrx.get_rfx_object(packet_id) - - await hass.async_add_executor_job( - hass.data[rfxtrx.DOMAIN][rfxtrx.DATA_RFXOBJECT].event_callback, event, - ) - - await hass.async_block_till_done() - await hass.async_block_till_done() - return event diff --git a/tests/components/rfxtrx/conftest.py b/tests/components/rfxtrx/conftest.py index eba322de5a3..9ce7ec07f4b 100644 --- a/tests/components/rfxtrx/conftest.py +++ b/tests/components/rfxtrx/conftest.py @@ -3,10 +3,26 @@ from unittest import mock import pytest +from homeassistant.components import rfxtrx + @pytest.fixture(autouse=True, name="rfxtrx") -async def rfxtrx(hass): +async def rfxtrx_fixture(hass): """Fixture that cleans up threads from integration.""" with mock.patch("RFXtrx.Connect") as connect, mock.patch("RFXtrx.DummyTransport2"): - yield connect.return_value + rfx = connect.return_value + + async def _signal_event(packet_id): + event = rfxtrx.get_rfx_object(packet_id) + await hass.async_add_executor_job( + rfx.event_callback, event, + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + return event + + rfx.signal = _signal_event + + yield rfx diff --git a/tests/components/rfxtrx/test_binary_sensor.py b/tests/components/rfxtrx/test_binary_sensor.py index ad04a0763f2..50f9ccf0eca 100644 --- a/tests/components/rfxtrx/test_binary_sensor.py +++ b/tests/components/rfxtrx/test_binary_sensor.py @@ -8,8 +8,6 @@ from homeassistant.core import State from homeassistant.setup import async_setup_component from homeassistant.util.dt import utcnow -from . import _signal_event - from tests.common import async_fire_time_changed, mock_restore_cache @@ -54,11 +52,11 @@ async def test_one_pt2262(hass, rfxtrx): assert state.state == "off" # probably aught to be unknown assert state.attributes.get("friendly_name") == "PT2262 22670e" - await _signal_event(hass, "0913000022670e013970") + await rfxtrx.signal("0913000022670e013970") state = hass.states.get("binary_sensor.pt2262_22670e") assert state.state == "on" - await _signal_event(hass, "09130000226707013d70") + await rfxtrx.signal("09130000226707013d70") state = hass.states.get("binary_sensor.pt2262_22670e") assert state.state == "off" @@ -138,12 +136,12 @@ async def test_discover(hass, rfxtrx): await hass.async_block_till_done() await hass.async_start() - await _signal_event(hass, "0b1100100118cdea02010f70") + await rfxtrx.signal("0b1100100118cdea02010f70") state = hass.states.get("binary_sensor.ac_118cdea_2") assert state assert state.state == "on" - await _signal_event(hass, "0b1100100118cdeb02010f70") + await rfxtrx.signal("0b1100100118cdeb02010f70") state = hass.states.get("binary_sensor.ac_118cdeb_2") assert state assert state.state == "on" @@ -168,7 +166,7 @@ async def test_off_delay(hass, rfxtrx): assert state assert state.state == "off" - await _signal_event(hass, "0b1100100118cdea02010f70") + await rfxtrx.signal("0b1100100118cdea02010f70") state = hass.states.get("binary_sensor.ac_118cdea_2") assert state assert state.state == "on" diff --git a/tests/components/rfxtrx/test_cover.py b/tests/components/rfxtrx/test_cover.py index 73c3cb9cc27..ffbef93daec 100644 --- a/tests/components/rfxtrx/test_cover.py +++ b/tests/components/rfxtrx/test_cover.py @@ -6,8 +6,6 @@ import pytest from homeassistant.core import State from homeassistant.setup import async_setup_component -from . import _signal_event - from tests.common import mock_restore_cache @@ -111,12 +109,12 @@ async def test_discover_covers(hass, rfxtrx): await hass.async_block_till_done() await hass.async_start() - await _signal_event(hass, "0a140002f38cae010f0070") + await rfxtrx.signal("0a140002f38cae010f0070") state = hass.states.get("cover.lightwaverf_siemens_f38cae_1") assert state assert state.state == "open" - await _signal_event(hass, "0a1400adf394ab020e0060") + await rfxtrx.signal("0a1400adf394ab020e0060") state = hass.states.get("cover.lightwaverf_siemens_f394ab_2") assert state assert state.state == "open" diff --git a/tests/components/rfxtrx/test_init.py b/tests/components/rfxtrx/test_init.py index 2a091525328..abe7c3c0441 100644 --- a/tests/components/rfxtrx/test_init.py +++ b/tests/components/rfxtrx/test_init.py @@ -1,11 +1,9 @@ """The tests for the Rfxtrx component.""" -from homeassistant.components import rfxtrx +from homeassistant.components.rfxtrx.const import EVENT_RFXTRX_EVENT from homeassistant.core import callback from homeassistant.setup import async_setup_component -from . import _signal_event - from tests.async_mock import call @@ -55,7 +53,7 @@ async def test_invalid_config(hass): ) -async def test_fire_event(hass): +async def test_fire_event(hass, rfxtrx): """Test fire event.""" assert await async_setup_component( hass, @@ -66,8 +64,8 @@ async def test_fire_event(hass): + "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0", "automatic_add": True, "devices": { - "0b1100cd0213c7f210010f51": {rfxtrx.CONF_FIRE_EVENT: True}, - "0716000100900970": {rfxtrx.CONF_FIRE_EVENT: True}, + "0b1100cd0213c7f210010f51": {"fire_event": True}, + "0716000100900970": {"fire_event": True}, }, } }, @@ -83,10 +81,10 @@ async def test_fire_event(hass): assert event.event_type == "rfxtrx_event" calls.append(event.data) - hass.bus.async_listen(rfxtrx.const.EVENT_RFXTRX_EVENT, record_event) + hass.bus.async_listen(EVENT_RFXTRX_EVENT, record_event) - await _signal_event(hass, "0b1100cd0213c7f210010f51") - await _signal_event(hass, "0716000100900970") + await rfxtrx.signal("0b1100cd0213c7f210010f51") + await rfxtrx.signal("0716000100900970") assert calls == [ { diff --git a/tests/components/rfxtrx/test_light.py b/tests/components/rfxtrx/test_light.py index b96dec95e6e..ead6d638841 100644 --- a/tests/components/rfxtrx/test_light.py +++ b/tests/components/rfxtrx/test_light.py @@ -7,8 +7,6 @@ from homeassistant.components.light import ATTR_BRIGHTNESS from homeassistant.core import State from homeassistant.setup import async_setup_component -from . import _signal_event - from tests.common import mock_restore_cache @@ -175,13 +173,13 @@ async def test_discover_light(hass, rfxtrx): await hass.async_block_till_done() await hass.async_start() - await _signal_event(hass, "0b11009e00e6116202020070") + await rfxtrx.signal("0b11009e00e6116202020070") state = hass.states.get("light.ac_0e61162_2") assert state assert state.state == "on" assert state.attributes.get("friendly_name") == "AC 0e61162:2" - await _signal_event(hass, "0b1100120118cdea02020070") + await rfxtrx.signal("0b1100120118cdea02020070") state = hass.states.get("light.ac_118cdea_2") assert state assert state.state == "on" diff --git a/tests/components/rfxtrx/test_sensor.py b/tests/components/rfxtrx/test_sensor.py index 3a797f4168d..a8cabcd401e 100644 --- a/tests/components/rfxtrx/test_sensor.py +++ b/tests/components/rfxtrx/test_sensor.py @@ -6,8 +6,6 @@ from homeassistant.const import TEMP_CELSIUS, UNIT_PERCENTAGE from homeassistant.core import State from homeassistant.setup import async_setup_component -from . import _signal_event - from tests.common import mock_restore_cache @@ -159,7 +157,7 @@ async def test_discover_sensor(hass, rfxtrx): await hass.async_start() # 1 - await _signal_event(hass, "0a520801070100b81b0279") + await rfxtrx.signal("0a520801070100b81b0279") base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01" state = hass.states.get(f"{base_id}_humidity") @@ -188,7 +186,7 @@ async def test_discover_sensor(hass, rfxtrx): assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE # 2 - await _signal_event(hass, "0a52080405020095240279") + await rfxtrx.signal("0a52080405020095240279") base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02" state = hass.states.get(f"{base_id}_humidity") @@ -217,7 +215,7 @@ async def test_discover_sensor(hass, rfxtrx): assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE # 1 Update - await _signal_event(hass, "0a52085e070100b31b0279") + await rfxtrx.signal("0a52085e070100b31b0279") base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01" state = hass.states.get(f"{base_id}_humidity") @@ -278,8 +276,8 @@ async def test_update_of_sensors(hass, rfxtrx): assert state assert state.state == "unknown" - await _signal_event(hass, "0a520802060101ff0f0269") - await _signal_event(hass, "0a52080705020085220269") + await rfxtrx.signal("0a520802060101ff0f0269") + await rfxtrx.signal("0a52080705020085220269") state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature") assert state diff --git a/tests/components/rfxtrx/test_switch.py b/tests/components/rfxtrx/test_switch.py index 22f7a73c77c..e4f7763c299 100644 --- a/tests/components/rfxtrx/test_switch.py +++ b/tests/components/rfxtrx/test_switch.py @@ -6,8 +6,6 @@ import pytest from homeassistant.core import State from homeassistant.setup import async_setup_component -from . import _signal_event - from tests.common import mock_restore_cache @@ -130,12 +128,12 @@ async def test_discover_switch(hass, rfxtrx): await hass.async_block_till_done() await hass.async_start() - await _signal_event(hass, "0b1100100118cdea02010f70") + await rfxtrx.signal("0b1100100118cdea02010f70") state = hass.states.get("switch.ac_118cdea_2") assert state assert state.state == "on" - await _signal_event(hass, "0b1100100118cdeb02010f70") + await rfxtrx.signal("0b1100100118cdeb02010f70") state = hass.states.get("switch.ac_118cdeb_2") assert state assert state.state == "on"