Switch rfxtrx to integration level config (#37742)

* Switch to integration level config

* Switch to per device config rather than per entity type

* All roller shutters should be added as covers

(there are non lighting types)

* Fixup tests that used invalid packets for platforms

* Avoid variable re-use

* Allow control events on sensors too

That way we get signal level sensors for these too

* Lint correction

* Don't filter sensors from config

Disable sensors from GUI if the entities are not wanted

* Correct usage of ATTR_ instead of CONF_

* Make sure the logging when a new entity is added includes the event
This commit is contained in:
Joakim Plate authored and GitHub committed 2020-07-12 22:03:22 +02:00
1 parent 16a947aa5f
commit 53844488d8
13 files changed
+430 -900

No files matched your search

+4 -67
View File
@@ -1,75 +1,12 @@
"""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():
@pytest.fixture(autouse=True, name="rfxtrx")
async def rfxtrx(hass):
"""Fixture that cleans up threads from integration."""
with mock.patch("RFXtrx._dummySerial", new=FixedDummySerial), mock.patch(
"RFXtrx.DummyTransport", new=FixedDummyTransport
):
yield
@pytest.fixture(name="rfxtrx")
async def rfxtrx_fixture(hass):
"""Stub out core rfxtrx to test platform."""
mock_component(hass, "rfxtrx")
rfxobject = mock.MagicMock()
hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxobject
yield rfxobject
# 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()
with mock.patch("RFXtrx.Connect") as connect, mock.patch("RFXtrx.DummyTransport2"):
yield connect.return_value
+69 -107
View File
@@ -9,47 +9,38 @@ from . import _signal_event
from tests.common import async_fire_time_changed
async def test_default_config(hass, rfxtrx):
"""Test with 0 sensor."""
await async_setup_component(
hass, "binary_sensor", {"binary_sensor": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
async def test_one(hass, rfxtrx):
"""Test with 1 sensor."""
await async_setup_component(
assert await async_setup_component(
hass,
"binary_sensor",
"rfxtrx",
{
"binary_sensor": {
"platform": "rfxtrx",
"devices": {"0a52080705020095220269": {"name": "Test"}},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {"0b1100cd0213c7f230010f71": {}},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
state = hass.states.get("binary_sensor.ac_213c7f2_48")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "AC 213c7f2:48"
async def test_one_pt2262(hass, rfxtrx):
"""Test with 1 sensor."""
await async_setup_component(
assert await async_setup_component(
hass,
"binary_sensor",
"rfxtrx",
{
"binary_sensor": {
"platform": "rfxtrx",
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0913000022670e013970": {
"name": "Test",
"data_bits": 4,
"command_on": 0xE,
"command_off": 0x7,
@@ -60,136 +51,107 @@ async def test_one_pt2262(hass, rfxtrx):
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
state = hass.states.get("binary_sensor.pt2262_22670e")
assert state
assert state.state == "off" # probably aught to be unknown
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "PT2262 22670e"
await _signal_event(hass, "0913000022670e013970")
state = hass.states.get("binary_sensor.test")
assert state
state = hass.states.get("binary_sensor.pt2262_22670e")
assert state.state == "on"
assert state.attributes.get("friendly_name") == "Test"
await _signal_event(hass, "09130000226707013d70")
state = hass.states.get("binary_sensor.test")
assert state
state = hass.states.get("binary_sensor.pt2262_22670e")
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Test"
async def test_several(hass, rfxtrx):
"""Test with 3."""
await async_setup_component(
assert await async_setup_component(
hass,
"binary_sensor",
"rfxtrx",
{
"binary_sensor": {
"platform": "rfxtrx",
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0b1100cd0213c7f230010f71": {"name": "Test"},
"0b1100100118cdea02010f70": {"name": "Bath"},
"0b1100101118cdea02010f70": {"name": "Living"},
"0b1100cd0213c7f230010f71": {},
"0b1100100118cdea02010f70": {},
"0b1100101118cdea02010f70": {},
},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
state = hass.states.get("binary_sensor.ac_213c7f2_48")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "AC 213c7f2:48"
state = hass.states.get("binary_sensor.bath")
state = hass.states.get("binary_sensor.ac_118cdea_2")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Bath"
assert state.attributes.get("friendly_name") == "AC 118cdea:2"
state = hass.states.get("binary_sensor.living")
state = hass.states.get("binary_sensor.ac_1118cdea_2")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Living"
assert state.attributes.get("friendly_name") == "AC 1118cdea:2"
async def test_discover(hass, rfxtrx):
"""Test with discovery."""
await async_setup_component(
assert await async_setup_component(
hass,
"binary_sensor",
{"binary_sensor": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
)
await hass.async_block_till_done()
await _signal_event(hass, "0b1100100118cdea02010f70")
state = hass.states.get("binary_sensor.0b1100100118cdea02010f70")
assert state
assert state.state == "on"
await _signal_event(hass, "0b1100100118cdeb02010f70")
state = hass.states.get("binary_sensor.0b1100100118cdeb02010f70")
assert state
assert state.state == "on"
# Trying to add a sensor
await _signal_event(hass, "0a52085e070100b31b0279")
state = hass.states.get("sensor.0a52085e070100b31b0279")
assert state is None
# Trying to add a light
await _signal_event(hass, "0b1100100118cdea02010f70")
state = hass.states.get("light.0b1100100118cdea02010f70")
assert state is None
# Trying to add a rollershutter
await _signal_event(hass, "0a1400adf394ab020e0060")
state = hass.states.get("cover.0a1400adf394ab020e0060")
assert state is None
async def test_discover_noautoadd(hass, rfxtrx):
"""Test with discovery of switch when auto add is False."""
await async_setup_component(
hass,
"binary_sensor",
"rfxtrx",
{
"binary_sensor": {
"platform": "rfxtrx",
"automatic_add": False,
"devices": {},
}
},
)
await hass.async_block_till_done()
# Trying to add switch
await _signal_event(hass, "0b1100100118cdea02010f70")
assert hass.states.async_all() == []
async def test_off_delay(hass, rfxtrx):
"""Test with discovery."""
await async_setup_component(
hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "rfxtrx",
"rfxtrx": {
"device": "abcd",
"dummy": True,
"automatic_add": True,
"devices": {
"0b1100100118cdea02010f70": {"name": "Test", "off_delay": 5}
"0b1100cd0213c7f230010f71": {},
"0b1100100118cdea02010f70": {},
"0b1100101118cdea02010f70": {},
},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
await _signal_event(hass, "0b1100100118cdea02010f70")
state = hass.states.get("binary_sensor.ac_118cdea_2")
assert state
assert state.state == "on"
await _signal_event(hass, "0b1100100118cdeb02010f70")
state = hass.states.get("binary_sensor.ac_118cdeb_2")
assert state
assert state.state == "on"
async def test_off_delay(hass, rfxtrx):
"""Test with discovery."""
assert await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {"0b1100100118cdea02010f70": {"off_delay": 5}},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.ac_118cdea_2")
assert state
assert state.state == "off"
await _signal_event(hass, "0b1100100118cdea02010f70")
state = hass.states.get("binary_sensor.test")
state = hass.states.get("binary_sensor.ac_118cdea_2")
assert state
assert state.state == "on"
@@ -197,12 +159,12 @@ async def test_off_delay(hass, rfxtrx):
async_fire_time_changed(hass, base_time + timedelta(seconds=4))
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
state = hass.states.get("binary_sensor.ac_118cdea_2")
assert state
assert state.state == "on"
async_fire_time_changed(hass, base_time + timedelta(seconds=6))
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
state = hass.states.get("binary_sensor.ac_118cdea_2")
assert state
assert state.state == "off"
+40 -86
View File
@@ -1,72 +1,48 @@
"""The tests for the Rfxtrx cover platform."""
from unittest.mock import call
from homeassistant.components import rfxtrx as rfxtrx_core
from homeassistant.setup import async_setup_component
from . import _signal_event
from tests.common import assert_setup_component
async def test_valid_config(hass, rfxtrx):
"""Test configuration."""
with assert_setup_component(1):
assert await async_setup_component(
hass,
"cover",
{
"cover": {
"platform": "rfxtrx",
"automatic_add": True,
"devices": {
"0b1100cd0213c7f210010f51": {
"name": "Test",
rfxtrx_core.ATTR_FIRE_EVENT: True,
}
},
}
},
)
await hass.async_block_till_done()
async def test_default_config(hass, rfxtrx):
"""Test with 0 cover."""
assert await async_setup_component(
hass, "cover", {"cover": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
async def test_one_cover(hass, rfxtrx):
"""Test with 1 cover."""
assert await async_setup_component(
hass,
"cover",
"rfxtrx",
{
"cover": {
"platform": "rfxtrx",
"devices": {"0b1400cd0213c7f210010f51": {"name": "Test"}},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {"0b1400cd0213c7f20d010f51": {}},
}
},
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1
state = hass.states.get("cover.lightwaverf_siemens_0213c7_242")
assert state
await hass.services.async_call(
"cover", "open_cover", {"entity_id": "cover.test"}, blocking=True
"cover",
"open_cover",
{"entity_id": "cover.lightwaverf_siemens_0213c7_242"},
blocking=True,
)
await hass.services.async_call(
"cover", "close_cover", {"entity_id": "cover.test"}, blocking=True
"cover",
"close_cover",
{"entity_id": "cover.lightwaverf_siemens_0213c7_242"},
blocking=True,
)
await hass.services.async_call(
"cover", "stop_cover", {"entity_id": "cover.test"}, blocking=True
"cover",
"stop_cover",
{"entity_id": "cover.lightwaverf_siemens_0213c7_242"},
blocking=True,
)
assert rfxtrx.transport.send.mock_calls == [
@@ -80,74 +56,52 @@ async def test_several_covers(hass, rfxtrx):
"""Test with 3 covers."""
assert await async_setup_component(
hass,
"cover",
"rfxtrx",
{
"cover": {
"platform": "rfxtrx",
"signal_repetitions": 3,
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0b1100cd0213c7f230010f71": {"name": "Test"},
"0b1100100118cdea02010f70": {"name": "Bath"},
"0b1100101118cdea02010f70": {"name": "Living"},
"0b1400cd0213c7f20d010f51": {},
"0A1400ADF394AB010D0060": {},
"09190000009ba8010100": {},
},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("cover.test")
state = hass.states.get("cover.lightwaverf_siemens_0213c7_242")
assert state
assert state.state == "closed"
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "LightwaveRF, Siemens 0213c7:242"
state = hass.states.get("cover.bath")
state = hass.states.get("cover.lightwaverf_siemens_f394ab_1")
assert state
assert state.state == "closed"
assert state.attributes.get("friendly_name") == "Bath"
assert state.attributes.get("friendly_name") == "LightwaveRF, Siemens f394ab:1"
state = hass.states.get("cover.living")
state = hass.states.get("cover.rollertrol_009ba8_1")
assert state
assert state.state == "closed"
assert state.attributes.get("friendly_name") == "Living"
assert len(hass.states.async_all()) == 3
assert state.attributes.get("friendly_name") == "RollerTrol 009ba8:1"
async def test_discover_covers(hass, rfxtrx):
"""Test with discovery of covers."""
assert await async_setup_component(
hass,
"cover",
{"cover": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
"rfxtrx",
{"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}},
)
await hass.async_block_till_done()
await _signal_event(hass, "0a140002f38cae010f0070")
assert len(hass.states.async_all()) == 1
state = hass.states.get("cover.lightwaverf_siemens_f38cae_1")
assert state
assert state.state == "open"
await _signal_event(hass, "0a1400adf394ab020e0060")
assert len(hass.states.async_all()) == 2
# Trying to add a sensor
await _signal_event(hass, "0a52085e070100b31b0279")
assert len(hass.states.async_all()) == 2
# Trying to add a light
await _signal_event(hass, "0b1100100118cdea02010f70")
assert len(hass.states.async_all()) == 2
async def test_discover_cover_noautoadd(hass, rfxtrx):
"""Test with discovery of cover when auto add is False."""
assert await async_setup_component(
hass,
"cover",
{"cover": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
await _signal_event(hass, "0a1400adf394ab010d0060")
assert len(hass.states.async_all()) == 0
await _signal_event(hass, "0a1400adf394ab020e0060")
assert len(hass.states.async_all()) == 0
state = hass.states.get("cover.lightwaverf_siemens_f394ab_2")
assert state
assert state.state == "open"
+14 -72
View File
@@ -1,8 +1,4 @@
"""The tests for the Rfxtrx component."""
# pylint: disable=protected-access
import asyncio
from async_timeout import timeout
from homeassistant.components import rfxtrx
from homeassistant.core import callback
@@ -10,37 +6,6 @@ from homeassistant.setup import async_setup_component
from . import _signal_event
from tests.common import assert_setup_component
async def test_default_config(hass):
"""Test configuration."""
assert await async_setup_component(
hass,
"rfxtrx",
{
"rfxtrx": {
"device": "/dev/serial/by-id/usb"
+ "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0",
"dummy": True,
}
},
)
with assert_setup_component(1, "sensor"):
await async_setup_component(
hass,
"sensor",
{"sensor": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
)
# Dummy startup is slow
async with timeout(10):
while len(hass.data[rfxtrx.DATA_RFXOBJECT].sensors()) < 2:
await asyncio.sleep(0.1)
assert len(hass.data[rfxtrx.DATA_RFXOBJECT].sensors()) == 2
async def test_valid_config(hass):
"""Test configuration."""
@@ -100,23 +65,8 @@ async def test_fire_event(hass):
"device": "/dev/serial/by-id/usb"
+ "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0",
"dummy": True,
}
},
)
assert await async_setup_component(
hass,
"switch",
{
"switch": {
"platform": "rfxtrx",
"automatic_add": True,
"devices": {
"0b1100cd0213c7f210010f51": {
"name": "Test",
rfxtrx.ATTR_FIRE_EVENT: True,
}
},
"devices": {"0b1100cd0213c7f210010f51": {rfxtrx.ATTR_FIRE_EVENT: True}},
}
},
)
@@ -131,18 +81,20 @@ async def test_fire_event(hass):
hass.bus.async_listen(rfxtrx.EVENT_BUTTON_PRESSED, record_event)
state = hass.states.get("switch.test")
state = hass.states.get("switch.ac_213c7f2_16")
assert state
assert state.state == "off"
await _signal_event(hass, "0b1100cd0213c7f210010f51")
state = hass.states.get("switch.test")
state = hass.states.get("switch.ac_213c7f2_16")
assert state
assert state.state == "on"
assert len(calls) == 1
assert calls[0].data == {"entity_id": "switch.test", "state": "on"}
assert any(
call.data == {"entity_id": "switch.ac_213c7f2_16", "state": "on"}
for call in calls
)
async def test_fire_event_sensor(hass):
@@ -155,26 +107,12 @@ async def test_fire_event_sensor(hass):
"device": "/dev/serial/by-id/usb"
+ "-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0",
"dummy": True,
"automatic_add": True,
"devices": {"0a520802060100ff0e0269": {rfxtrx.ATTR_FIRE_EVENT: True}},
}
},
)
await async_setup_component(
hass,
"sensor",
{
"sensor": {
"platform": "rfxtrx",
"automatic_add": True,
"devices": {
"0a520802060100ff0e0269": {
"name": "Test",
rfxtrx.ATTR_FIRE_EVENT: True,
}
},
}
},
)
await hass.async_block_till_done()
calls = []
@@ -188,4 +126,8 @@ async def test_fire_event_sensor(hass):
await _signal_event(hass, "0a520802060101ff0f0269")
assert len(calls) == 5
assert any(call.data == {"entity_id": "sensor.test_temperature"} for call in calls)
assert any(
call.data
== {"entity_id": "sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_temperature"}
for call in calls
)
+53 -122
View File
@@ -3,127 +3,79 @@ from unittest.mock import call
import pytest
from homeassistant.components import rfxtrx as rfxtrx_core
from homeassistant.setup import async_setup_component
from . import _signal_event
from tests.common import assert_setup_component
async def test_valid_config(hass, rfxtrx):
"""Test configuration."""
with assert_setup_component(1):
assert await async_setup_component(
hass,
"light",
{
"light": {
"platform": "rfxtrx",
"automatic_add": True,
"devices": {
"0b1100cd0213c7f210010f51": {
"name": "Test",
rfxtrx_core.ATTR_FIRE_EVENT: True,
}
},
}
},
)
assert await async_setup_component(
hass,
"light",
{
"light": {
"platform": "rfxtrx",
"automatic_add": True,
"devices": {
"0b1100cd0213c7f210010f51": {
"name": "Test",
"signal_repetitions": 3,
}
},
}
},
)
async def test_default_config(hass, rfxtrx):
"""Test with 0 switches."""
with assert_setup_component(1):
await async_setup_component(
hass, "light", {"light": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
async def test_one_light(hass, rfxtrx):
"""Test with 1 light."""
await async_setup_component(
assert await async_setup_component(
hass,
"light",
"rfxtrx",
{
"light": {
"platform": "rfxtrx",
"devices": {"0b1100cd0213c7f210010f51": {"name": "Test"}},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {"0b1100cd0213c7f210020f51": {}},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_16")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "AC 213c7f2:16"
await hass.services.async_call(
"light", "turn_on", {"entity_id": "light.test"}, blocking=True
"light", "turn_on", {"entity_id": "light.ac_213c7f2_16"}, blocking=True
)
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_16")
assert state.state == "on"
assert state.attributes.get("brightness") == 255
await hass.services.async_call(
"light", "turn_off", {"entity_id": "light.test"}, blocking=True
"light", "turn_off", {"entity_id": "light.ac_213c7f2_16"}, blocking=True
)
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_16")
assert state.state == "off"
assert state.attributes.get("brightness") is None
await hass.services.async_call(
"light",
"turn_on",
{"entity_id": "light.test", "brightness": 100},
{"entity_id": "light.ac_213c7f2_16", "brightness": 100},
blocking=True,
)
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_16")
assert state.state == "on"
assert state.attributes.get("brightness") == 100
await hass.services.async_call(
"light", "turn_on", {"entity_id": "light.test", "brightness": 10}, blocking=True
"light",
"turn_on",
{"entity_id": "light.ac_213c7f2_16", "brightness": 10},
blocking=True,
)
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_16")
assert state.state == "on"
assert state.attributes.get("brightness") == 10
await hass.services.async_call(
"light",
"turn_on",
{"entity_id": "light.test", "brightness": 255},
{"entity_id": "light.ac_213c7f2_16", "brightness": 255},
blocking=True,
)
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_16")
assert state.state == "on"
assert state.attributes.get("brightness") == 255
await hass.services.async_call(
"light", "turn_off", {"entity_id": "light.test"}, blocking=True
"light", "turn_off", {"entity_id": "light.ac_213c7f2_16"}, blocking=True
)
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_16")
assert state.state == "off"
assert state.attributes.get("brightness") is None
@@ -139,61 +91,59 @@ async def test_one_light(hass, rfxtrx):
async def test_several_lights(hass, rfxtrx):
"""Test with 3 lights."""
await async_setup_component(
assert await async_setup_component(
hass,
"light",
"rfxtrx",
{
"light": {
"platform": "rfxtrx",
"signal_repetitions": 3,
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0b1100cd0213c7f230010f71": {"name": "Test"},
"0b1100100118cdea02010f70": {"name": "Bath"},
"0b1100101118cdea02010f70": {"name": "Living"},
"0b1100cd0213c7f230020f71": {},
"0b1100100118cdea02020f70": {},
"0b1100101118cdea02050f70": {},
},
}
},
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 3
state = hass.states.get("light.test")
state = hass.states.get("light.ac_213c7f2_48")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "AC 213c7f2:48"
state = hass.states.get("light.bath")
state = hass.states.get("light.ac_118cdea_2")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Bath"
assert state.attributes.get("friendly_name") == "AC 118cdea:2"
state = hass.states.get("light.living")
state = hass.states.get("light.ac_1118cdea_2")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Living"
assert len(hass.states.async_all()) == 3
assert state.attributes.get("friendly_name") == "AC 1118cdea:2"
@pytest.mark.parametrize("repetitions", [1, 3])
async def test_repetitions(hass, rfxtrx, repetitions):
"""Test signal repetitions."""
await async_setup_component(
assert await async_setup_component(
hass,
"light",
"rfxtrx",
{
"light": {
"platform": "rfxtrx",
"signal_repetitions": repetitions,
"devices": {"0b1100cd0213c7f230010f71": {"name": "Test"}},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0b1100cd0213c7f230020f71": {"signal_repetitions": repetitions}
},
}
},
)
await hass.async_block_till_done()
await hass.services.async_call(
"light", "turn_on", {"entity_id": "light.test"}, blocking=True
"light", "turn_on", {"entity_id": "light.ac_213c7f2_48"}, blocking=True
)
await hass.async_block_till_done()
@@ -202,40 +152,21 @@ async def test_repetitions(hass, rfxtrx, repetitions):
async def test_discover_light(hass, rfxtrx):
"""Test with discovery of lights."""
await async_setup_component(
assert await async_setup_component(
hass,
"light",
{"light": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
"rfxtrx",
{"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}},
)
await hass.async_block_till_done()
await _signal_event(hass, "0b11009e00e6116202020070")
state = hass.states.get("light.0b11009e00e6116202020070")
state = hass.states.get("light.ac_0e61162_2")
assert state
assert state.state == "on"
assert state.attributes.get("friendly_name") == "0b11009e00e6116202020070"
assert state.attributes.get("friendly_name") == "AC 0e61162:2"
await _signal_event(hass, "0b1100120118cdea02020070")
state = hass.states.get("light.0b1100120118cdea02020070")
state = hass.states.get("light.ac_118cdea_2")
assert state
assert state.state == "on"
assert state.attributes.get("friendly_name") == "0b1100120118cdea02020070"
async def test_discover_light_noautoadd(hass, rfxtrx):
"""Test with discover of light when auto add is False."""
await async_setup_component(
hass,
"light",
{"light": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
await _signal_event(hass, "0b1100120118cdea02020070")
assert hass.states.async_all() == []
await _signal_event(hass, "0b1100120118cdea02010070")
assert hass.states.async_all() == []
await _signal_event(hass, "0b1100120118cdea02020070")
assert hass.states.async_all() == []
assert state.attributes.get("friendly_name") == "AC 118cdea:2"
+61 -84
View File
@@ -17,46 +17,46 @@ async def test_default_config(hass, rfxtrx):
async def test_one_sensor(hass, rfxtrx):
"""Test with 1 sensor."""
await async_setup_component(
assert await async_setup_component(
hass,
"sensor",
"rfxtrx",
{
"sensor": {
"platform": "rfxtrx",
"devices": {
"0a52080705020095220269": {
"name": "Test",
"data_type": "Temperature",
}
},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {"0a52080705020095220269": {}},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("sensor.test_temperature")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == "Test Temperature"
assert (
state.attributes.get("friendly_name")
== "WT260,WT260H,WT440H,WT450,WT450H 05:02 Temperature"
)
assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS
async def test_one_sensor_no_datatype(hass, rfxtrx):
"""Test with 1 sensor."""
await async_setup_component(
assert await async_setup_component(
hass,
"sensor",
"rfxtrx",
{
"sensor": {
"platform": "rfxtrx",
"devices": {"0a52080705020095220269": {"name": "Test"}},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {"0a52080705020095220269": {}},
}
},
)
await hass.async_block_till_done()
base_id = "sensor.test"
base_name = "Test"
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02"
base_name = "WT260,WT260H,WT440H,WT450,WT450H 05:02"
state = hass.states.get(f"{base_id}_temperature")
assert state
@@ -91,58 +91,63 @@ async def test_one_sensor_no_datatype(hass, rfxtrx):
async def test_several_sensors(hass, rfxtrx):
"""Test with 3 sensors."""
await async_setup_component(
assert await async_setup_component(
hass,
"sensor",
"rfxtrx",
{
"sensor": {
"platform": "rfxtrx",
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0a52080705020095220269": {
"name": "Test",
"data_type": "Temperature",
},
"0a520802060100ff0e0269": {
"name": "Bath",
"data_type": ["Temperature", "Humidity"],
},
"0a52080705020095220269": {},
"0a520802060100ff0e0269": {},
},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("sensor.test_temperature")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == "Test Temperature"
assert (
state.attributes.get("friendly_name")
== "WT260,WT260H,WT440H,WT450,WT450H 05:02 Temperature"
)
assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS
state = hass.states.get("sensor.bath_temperature")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_temperature")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == "Bath Temperature"
assert (
state.attributes.get("friendly_name")
== "WT260,WT260H,WT440H,WT450,WT450H 06:01 Temperature"
)
assert state.attributes.get("unit_of_measurement") == TEMP_CELSIUS
state = hass.states.get("sensor.bath_humidity")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_humidity")
assert state
assert state.state == "unknown"
assert state.attributes.get("friendly_name") == "Bath Humidity"
assert (
state.attributes.get("friendly_name")
== "WT260,WT260H,WT440H,WT450,WT450H 06:01 Humidity"
)
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
async def test_discover_sensor(hass, rfxtrx):
"""Test with discovery of sensor."""
await async_setup_component(
assert await async_setup_component(
hass,
"sensor",
{"sensor": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
"rfxtrx",
{"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}},
)
await hass.async_block_till_done()
# 1
await _signal_event(hass, "0a520801070100b81b0279")
base_id = "sensor.0a520801070100b81b0279"
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01"
state = hass.states.get(f"{base_id}_humidity")
assert state
@@ -171,7 +176,7 @@ async def test_discover_sensor(hass, rfxtrx):
# 2
await _signal_event(hass, "0a52080405020095240279")
base_id = "sensor.0a52080405020095240279"
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02"
state = hass.states.get(f"{base_id}_humidity")
assert state
@@ -200,7 +205,7 @@ async def test_discover_sensor(hass, rfxtrx):
# 1 Update
await _signal_event(hass, "0a52085e070100b31b0279")
base_id = "sensor.0a520801070100b81b0279"
base_id = "sensor.wt260_wt260h_wt440h_wt450_wt450h_07_01"
state = hass.states.get(f"{base_id}_humidity")
assert state
@@ -230,75 +235,47 @@ async def test_discover_sensor(hass, rfxtrx):
assert len(hass.states.async_all()) == 10
async def test_discover_sensor_noautoadd(hass, rfxtrx):
"""Test with discover of sensor when auto add is False."""
await async_setup_component(
hass,
"sensor",
{"sensor": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
await _signal_event(hass, "0a520801070100b81b0279")
assert len(hass.states.async_all()) == 0
await _signal_event(hass, "0a52080405020095240279")
assert len(hass.states.async_all()) == 0
await _signal_event(hass, "0a52085e070100b31b0279")
assert len(hass.states.async_all()) == 0
async def test_update_of_sensors(hass, rfxtrx):
"""Test with 3 sensors."""
await async_setup_component(
assert await async_setup_component(
hass,
"sensor",
"rfxtrx",
{
"sensor": {
"platform": "rfxtrx",
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0a52080705020095220269": {
"name": "Test",
"data_type": "Temperature",
},
"0a520802060100ff0e0269": {
"name": "Bath",
"data_type": ["Temperature", "Humidity"],
},
"0a52080705020095220269": {},
"0a520802060100ff0e0269": {},
},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("sensor.test_temperature")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature")
assert state
assert state.state == "unknown"
state = hass.states.get("sensor.bath_temperature")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_temperature")
assert state
assert state.state == "unknown"
state = hass.states.get("sensor.bath_humidity")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_humidity")
assert state
assert state.state == "unknown"
assert len(hass.states.async_all()) == 3
await _signal_event(hass, "0a520802060101ff0f0269")
await _signal_event(hass, "0a52080705020085220269")
state = hass.states.get("sensor.test_temperature")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_05_02_temperature")
assert state
assert state.state == "13.3"
state = hass.states.get("sensor.bath_temperature")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_temperature")
assert state
assert state.state == "51.1"
state = hass.states.get("sensor.bath_humidity")
state = hass.states.get("sensor.wt260_wt260h_wt440h_wt450_wt450h_06_01_humidity")
assert state
assert state.state == "15"
assert len(hass.states.async_all()) == 3
+40 -145
View File
@@ -3,121 +3,43 @@ from unittest.mock import call
import pytest
from homeassistant.components import rfxtrx as rfxtrx_core
from homeassistant.setup import async_setup_component
from . import _signal_event
from tests.common import assert_setup_component
async def test_valid_config(hass, rfxtrx):
"""Test configuration."""
with assert_setup_component(1):
await async_setup_component(
hass,
"switch",
{
"switch": {
"platform": "rfxtrx",
"automatic_add": True,
"devices": {
"0b1100cd0213c7f210010f51": {
"name": "Test",
rfxtrx_core.ATTR_FIRE_EVENT: True,
}
},
}
},
)
await hass.async_block_till_done()
async def test_valid_config_int_device_id(hass, rfxtrx):
"""Test configuration."""
with assert_setup_component(1):
await async_setup_component(
hass,
"switch",
{
"switch": {
"platform": "rfxtrx",
"automatic_add": True,
"devices": {
710000141010170: {
"name": "Test",
rfxtrx_core.ATTR_FIRE_EVENT: True,
}
},
}
},
)
await hass.async_block_till_done()
async def test_invalid_config2(hass, rfxtrx):
"""Test invalid configuration."""
with assert_setup_component(0):
await async_setup_component(
hass,
"switch",
{
"switch": {
"platform": "rfxtrx",
"automatic_add": True,
"invalid_key": "afda",
"devices": {
"0b1100cd0213c7f210010f51": {
"name": "Test",
rfxtrx_core.ATTR_FIRE_EVENT: True,
}
},
}
},
)
await hass.async_block_till_done()
async def test_default_config(hass, rfxtrx):
"""Test with 0 switches."""
await async_setup_component(
hass, "switch", {"switch": {"platform": "rfxtrx", "devices": {}}}
)
await hass.async_block_till_done()
assert hass.states.async_all() == []
async def test_one_switch(hass, rfxtrx):
"""Test with 1 switch."""
await async_setup_component(
assert await async_setup_component(
hass,
"switch",
"rfxtrx",
{
"switch": {
"platform": "rfxtrx",
"devices": {"0b1100cd0213c7f210010f51": {"name": "Test"}},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {"0b1100cd0213c7f210010f51": {}},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("switch.test")
state = hass.states.get("switch.ac_213c7f2_16")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "AC 213c7f2:16"
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.test"}, blocking=True
"switch", "turn_on", {"entity_id": "switch.ac_213c7f2_16"}, blocking=True
)
state = hass.states.get("switch.test")
state = hass.states.get("switch.ac_213c7f2_16")
assert state.state == "on"
await hass.services.async_call(
"switch", "turn_off", {"entity_id": "switch.test"}, blocking=True
"switch", "turn_off", {"entity_id": "switch.ac_213c7f2_16"}, blocking=True
)
state = hass.states.get("switch.test")
state = hass.states.get("switch.ac_213c7f2_16")
assert state.state == "off"
assert rfxtrx.transport.send.mock_calls == [
@@ -128,57 +50,59 @@ async def test_one_switch(hass, rfxtrx):
async def test_several_switches(hass, rfxtrx):
"""Test with 3 switches."""
await async_setup_component(
assert await async_setup_component(
hass,
"switch",
"rfxtrx",
{
"switch": {
"platform": "rfxtrx",
"signal_repetitions": 3,
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0b1100cd0213c7f230010f71": {"name": "Test"},
"0b1100100118cdea02010f70": {"name": "Bath"},
"0b1100101118cdea02010f70": {"name": "Living"},
"0b1100cd0213c7f230010f71": {},
"0b1100100118cdea02010f70": {},
"0b1100101118cdea02010f70": {},
},
}
},
)
await hass.async_block_till_done()
state = hass.states.get("switch.test")
state = hass.states.get("switch.ac_213c7f2_48")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Test"
assert state.attributes.get("friendly_name") == "AC 213c7f2:48"
state = hass.states.get("switch.bath")
state = hass.states.get("switch.ac_118cdea_2")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Bath"
assert state.attributes.get("friendly_name") == "AC 118cdea:2"
state = hass.states.get("switch.living")
state = hass.states.get("switch.ac_1118cdea_2")
assert state
assert state.state == "off"
assert state.attributes.get("friendly_name") == "Living"
assert state.attributes.get("friendly_name") == "AC 1118cdea:2"
@pytest.mark.parametrize("repetitions", [1, 3])
async def test_repetitions(hass, rfxtrx, repetitions):
"""Test signal repetitions."""
await async_setup_component(
assert await async_setup_component(
hass,
"switch",
"rfxtrx",
{
"switch": {
"platform": "rfxtrx",
"signal_repetitions": repetitions,
"devices": {"0b1100cd0213c7f230010f71": {"name": "Test"}},
"rfxtrx": {
"device": "abcd",
"dummy": True,
"devices": {
"0b1100cd0213c7f230010f71": {"signal_repetitions": repetitions}
},
}
},
)
await hass.async_block_till_done()
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.test"}, blocking=True
"switch", "turn_on", {"entity_id": "switch.ac_213c7f2_48"}, blocking=True
)
await hass.async_block_till_done()
@@ -187,48 +111,19 @@ async def test_repetitions(hass, rfxtrx, repetitions):
async def test_discover_switch(hass, rfxtrx):
"""Test with discovery of switches."""
await async_setup_component(
assert await async_setup_component(
hass,
"switch",
{"switch": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
"rfxtrx",
{"rfxtrx": {"device": "abcd", "dummy": True, "automatic_add": True}},
)
await hass.async_block_till_done()
await _signal_event(hass, "0b1100100118cdea02010f70")
state = hass.states.get("switch.0b1100100118cdea02010f70")
state = hass.states.get("switch.ac_118cdea_2")
assert state
assert state.state == "on"
await _signal_event(hass, "0b1100100118cdeb02010f70")
state = hass.states.get("switch.0b1100100118cdeb02010f70")
state = hass.states.get("switch.ac_118cdeb_2")
assert state
assert state.state == "on"
# Trying to add a sensor
await _signal_event(hass, "0a52085e070100b31b0279")
state = hass.states.get("sensor.0a52085e070100b31b0279")
assert state is None
# Trying to add a light
await _signal_event(hass, "0b1100100118cdea02010f70")
state = hass.states.get("light.0b1100100118cdea02010f70")
assert state is None
# Trying to add a rollershutter
await _signal_event(hass, "0a1400adf394ab020e0060")
state = hass.states.get("cover.0a1400adf394ab020e0060")
assert state is None
async def test_discover_switch_noautoadd(hass, rfxtrx):
"""Test with discovery of switch when auto add is False."""
await async_setup_component(
hass,
"switch",
{"switch": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
)
await hass.async_block_till_done()
# Trying to add switch
await _signal_event(hass, "0b1100100118cdea02010f70")
assert hass.states.async_all() == []