mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Avoid using implementation internal to trigger events (#38041)
This uses the moxking in fixture to trigger events.
This commit is contained in:
parent
945acb4e29
commit
7a3c6d6525
@ -1,14 +1 @@
|
|||||||
"""Tests for the rfxtrx component."""
|
"""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
|
|
||||||
|
@ -3,10 +3,26 @@ from unittest import mock
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components import rfxtrx
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, name="rfxtrx")
|
@pytest.fixture(autouse=True, name="rfxtrx")
|
||||||
async def rfxtrx(hass):
|
async def rfxtrx_fixture(hass):
|
||||||
"""Fixture that cleans up threads from integration."""
|
"""Fixture that cleans up threads from integration."""
|
||||||
|
|
||||||
with mock.patch("RFXtrx.Connect") as connect, mock.patch("RFXtrx.DummyTransport2"):
|
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
|
||||||
|
@ -8,8 +8,6 @@ from homeassistant.core import State
|
|||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from . import _signal_event
|
|
||||||
|
|
||||||
from tests.common import async_fire_time_changed, mock_restore_cache
|
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.state == "off" # probably aught to be unknown
|
||||||
assert state.attributes.get("friendly_name") == "PT2262 22670e"
|
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")
|
state = hass.states.get("binary_sensor.pt2262_22670e")
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
|
|
||||||
await _signal_event(hass, "09130000226707013d70")
|
await rfxtrx.signal("09130000226707013d70")
|
||||||
state = hass.states.get("binary_sensor.pt2262_22670e")
|
state = hass.states.get("binary_sensor.pt2262_22670e")
|
||||||
assert state.state == "off"
|
assert state.state == "off"
|
||||||
|
|
||||||
@ -138,12 +136,12 @@ async def test_discover(hass, rfxtrx):
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
await hass.async_start()
|
await hass.async_start()
|
||||||
|
|
||||||
await _signal_event(hass, "0b1100100118cdea02010f70")
|
await rfxtrx.signal("0b1100100118cdea02010f70")
|
||||||
state = hass.states.get("binary_sensor.ac_118cdea_2")
|
state = hass.states.get("binary_sensor.ac_118cdea_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
|
|
||||||
await _signal_event(hass, "0b1100100118cdeb02010f70")
|
await rfxtrx.signal("0b1100100118cdeb02010f70")
|
||||||
state = hass.states.get("binary_sensor.ac_118cdeb_2")
|
state = hass.states.get("binary_sensor.ac_118cdeb_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
@ -168,7 +166,7 @@ async def test_off_delay(hass, rfxtrx):
|
|||||||
assert state
|
assert state
|
||||||
assert state.state == "off"
|
assert state.state == "off"
|
||||||
|
|
||||||
await _signal_event(hass, "0b1100100118cdea02010f70")
|
await rfxtrx.signal("0b1100100118cdea02010f70")
|
||||||
state = hass.states.get("binary_sensor.ac_118cdea_2")
|
state = hass.states.get("binary_sensor.ac_118cdea_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
|
@ -6,8 +6,6 @@ import pytest
|
|||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from . import _signal_event
|
|
||||||
|
|
||||||
from tests.common import mock_restore_cache
|
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_block_till_done()
|
||||||
await hass.async_start()
|
await hass.async_start()
|
||||||
|
|
||||||
await _signal_event(hass, "0a140002f38cae010f0070")
|
await rfxtrx.signal("0a140002f38cae010f0070")
|
||||||
state = hass.states.get("cover.lightwaverf_siemens_f38cae_1")
|
state = hass.states.get("cover.lightwaverf_siemens_f38cae_1")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "open"
|
assert state.state == "open"
|
||||||
|
|
||||||
await _signal_event(hass, "0a1400adf394ab020e0060")
|
await rfxtrx.signal("0a1400adf394ab020e0060")
|
||||||
state = hass.states.get("cover.lightwaverf_siemens_f394ab_2")
|
state = hass.states.get("cover.lightwaverf_siemens_f394ab_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "open"
|
assert state.state == "open"
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
"""The tests for the Rfxtrx component."""
|
"""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.core import callback
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from . import _signal_event
|
|
||||||
|
|
||||||
from tests.async_mock import call
|
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."""
|
"""Test fire event."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
@ -66,8 +64,8 @@ async def test_fire_event(hass):
|
|||||||
+ "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0",
|
+ "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0",
|
||||||
"automatic_add": True,
|
"automatic_add": True,
|
||||||
"devices": {
|
"devices": {
|
||||||
"0b1100cd0213c7f210010f51": {rfxtrx.CONF_FIRE_EVENT: True},
|
"0b1100cd0213c7f210010f51": {"fire_event": True},
|
||||||
"0716000100900970": {rfxtrx.CONF_FIRE_EVENT: True},
|
"0716000100900970": {"fire_event": True},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -83,10 +81,10 @@ async def test_fire_event(hass):
|
|||||||
assert event.event_type == "rfxtrx_event"
|
assert event.event_type == "rfxtrx_event"
|
||||||
calls.append(event.data)
|
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 rfxtrx.signal("0b1100cd0213c7f210010f51")
|
||||||
await _signal_event(hass, "0716000100900970")
|
await rfxtrx.signal("0716000100900970")
|
||||||
|
|
||||||
assert calls == [
|
assert calls == [
|
||||||
{
|
{
|
||||||
|
@ -7,8 +7,6 @@ from homeassistant.components.light import ATTR_BRIGHTNESS
|
|||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from . import _signal_event
|
|
||||||
|
|
||||||
from tests.common import mock_restore_cache
|
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_block_till_done()
|
||||||
await hass.async_start()
|
await hass.async_start()
|
||||||
|
|
||||||
await _signal_event(hass, "0b11009e00e6116202020070")
|
await rfxtrx.signal("0b11009e00e6116202020070")
|
||||||
state = hass.states.get("light.ac_0e61162_2")
|
state = hass.states.get("light.ac_0e61162_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
assert state.attributes.get("friendly_name") == "AC 0e61162:2"
|
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")
|
state = hass.states.get("light.ac_118cdea_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
|
@ -6,8 +6,6 @@ from homeassistant.const import TEMP_CELSIUS, UNIT_PERCENTAGE
|
|||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from . import _signal_event
|
|
||||||
|
|
||||||
from tests.common import mock_restore_cache
|
from tests.common import mock_restore_cache
|
||||||
|
|
||||||
|
|
||||||
@ -159,7 +157,7 @@ async def test_discover_sensor(hass, rfxtrx):
|
|||||||
await hass.async_start()
|
await hass.async_start()
|
||||||
|
|
||||||
# 1
|
# 1
|
||||||
await _signal_event(hass, "0a520801070100b81b0279")
|
await rfxtrx.signal("0a520801070100b81b0279")
|
||||||
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01"
|
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01"
|
||||||
|
|
||||||
state = hass.states.get(f"{base_id}_humidity")
|
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
|
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||||
|
|
||||||
# 2
|
# 2
|
||||||
await _signal_event(hass, "0a52080405020095240279")
|
await rfxtrx.signal("0a52080405020095240279")
|
||||||
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02"
|
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02"
|
||||||
state = hass.states.get(f"{base_id}_humidity")
|
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
|
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
||||||
|
|
||||||
# 1 Update
|
# 1 Update
|
||||||
await _signal_event(hass, "0a52085e070100b31b0279")
|
await rfxtrx.signal("0a52085e070100b31b0279")
|
||||||
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01"
|
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01"
|
||||||
|
|
||||||
state = hass.states.get(f"{base_id}_humidity")
|
state = hass.states.get(f"{base_id}_humidity")
|
||||||
@ -278,8 +276,8 @@ async def test_update_of_sensors(hass, rfxtrx):
|
|||||||
assert state
|
assert state
|
||||||
assert state.state == "unknown"
|
assert state.state == "unknown"
|
||||||
|
|
||||||
await _signal_event(hass, "0a520802060101ff0f0269")
|
await rfxtrx.signal("0a520802060101ff0f0269")
|
||||||
await _signal_event(hass, "0a52080705020085220269")
|
await rfxtrx.signal("0a52080705020085220269")
|
||||||
|
|
||||||
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature")
|
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature")
|
||||||
assert state
|
assert state
|
||||||
|
@ -6,8 +6,6 @@ import pytest
|
|||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from . import _signal_event
|
|
||||||
|
|
||||||
from tests.common import mock_restore_cache
|
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_block_till_done()
|
||||||
await hass.async_start()
|
await hass.async_start()
|
||||||
|
|
||||||
await _signal_event(hass, "0b1100100118cdea02010f70")
|
await rfxtrx.signal("0b1100100118cdea02010f70")
|
||||||
state = hass.states.get("switch.ac_118cdea_2")
|
state = hass.states.get("switch.ac_118cdea_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
|
|
||||||
await _signal_event(hass, "0b1100100118cdeb02010f70")
|
await rfxtrx.signal("0b1100100118cdeb02010f70")
|
||||||
state = hass.states.get("switch.ac_118cdeb_2")
|
state = hass.states.get("switch.ac_118cdeb_2")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == "on"
|
assert state.state == "on"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user