2019-10-14 15:38:34 +00:00
|
|
|
"""The tests for the RFXtrx switch platform."""
|
2016-03-02 19:36:41 +00:00
|
|
|
import unittest
|
2019-10-14 15:38:34 +00:00
|
|
|
|
2019-10-12 19:37:59 +00:00
|
|
|
import RFXtrx as rfxtrxmod
|
2019-10-14 15:38:34 +00:00
|
|
|
import pytest
|
2016-09-28 07:05:38 +00:00
|
|
|
|
2016-03-02 19:36:41 +00:00
|
|
|
from homeassistant.components import rfxtrx as rfxtrx_core
|
2019-10-14 15:38:34 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2020-06-28 04:54:27 +00:00
|
|
|
from tests.common import assert_setup_component, get_test_home_assistant, mock_component
|
2016-03-02 19:36:41 +00:00
|
|
|
|
|
|
|
|
2016-10-01 20:57:10 +00:00
|
|
|
@pytest.mark.skipif("os.environ.get('RFXTRX') != 'RUN'")
|
2016-03-02 19:36:41 +00:00
|
|
|
class TestSwitchRfxtrx(unittest.TestCase):
|
2019-10-14 15:38:34 +00:00
|
|
|
"""Test the RFXtrx switch platform."""
|
2016-03-02 19:36:41 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-31 15:47:29 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_component(self.hass, "rfxtrx")
|
2020-06-08 19:26:40 +00:00
|
|
|
self.addCleanup(self.tear_down_cleanup)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2020-06-08 19:26:40 +00:00
|
|
|
def tear_down_cleanup(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2020-06-28 04:54:27 +00:00
|
|
|
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()
|
2016-03-02 19:36:41 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-04-09 03:55:31 +00:00
|
|
|
def test_valid_config(self):
|
|
|
|
"""Test configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "rfxtrx",
|
|
|
|
"automatic_add": True,
|
|
|
|
"devices": {
|
|
|
|
"0b1100cd0213c7f210010f51": {
|
|
|
|
"name": "Test",
|
2020-06-28 04:54:27 +00:00
|
|
|
rfxtrx_core.ATTR_FIRE_EVENT: True,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
2016-07-12 08:45:22 +00:00
|
|
|
def test_valid_config_int_device_id(self):
|
|
|
|
"""Test configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "rfxtrx",
|
|
|
|
"automatic_add": True,
|
|
|
|
"devices": {
|
|
|
|
710000141010170: {
|
|
|
|
"name": "Test",
|
2020-06-28 04:54:27 +00:00
|
|
|
rfxtrx_core.ATTR_FIRE_EVENT: True,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
|
|
|
def test_invalid_config2(self):
|
2016-12-02 05:45:19 +00:00
|
|
|
"""Test invalid configuration."""
|
2020-06-28 04:54:27 +00:00
|
|
|
with assert_setup_component(0):
|
|
|
|
setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "rfxtrx",
|
|
|
|
"automatic_add": True,
|
|
|
|
"invalid_key": "afda",
|
|
|
|
"devices": {
|
|
|
|
"0b1100cd0213c7f210010f51": {
|
|
|
|
"name": "Test",
|
|
|
|
rfxtrx_core.ATTR_FIRE_EVENT: True,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
2016-03-02 19:36:41 +00:00
|
|
|
def test_default_config(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with 0 switches."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass, "switch", {"switch": {"platform": "rfxtrx", "devices": {}}}
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-04-23 17:57:36 +00:00
|
|
|
def test_one_switch(self):
|
|
|
|
"""Test with 1 switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "rfxtrx",
|
|
|
|
"devices": {"0b1100cd0213c7f210010f51": {"name": "Test"}},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2016-04-23 17:57:36 +00:00
|
|
|
|
2020-06-28 04:54:27 +00:00
|
|
|
self.hass.data[rfxtrx_core.DATA_RFXOBJECT] = rfxtrxmod.Core(
|
2019-07-31 19:25:30 +00:00
|
|
|
"", transport_protocol=rfxtrxmod.DummyTransport
|
|
|
|
)
|
2016-04-23 17:57:36 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(rfxtrx_core.RFX_DEVICES)
|
2020-06-28 04:54:27 +00:00
|
|
|
entity = rfxtrx_core.RFX_DEVICES["213c7f2_16"]
|
|
|
|
entity.hass = self.hass
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "Test" == entity.name
|
|
|
|
assert "off" == entity.state
|
2018-10-24 10:10:05 +00:00
|
|
|
assert entity.assumed_state
|
|
|
|
assert entity.signal_repetitions == 1
|
|
|
|
assert not entity.should_fire_event
|
|
|
|
assert not entity.should_poll
|
|
|
|
|
|
|
|
assert not entity.is_on
|
2016-04-09 03:55:31 +00:00
|
|
|
entity.turn_on()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert entity.is_on
|
2016-04-09 03:55:31 +00:00
|
|
|
entity.turn_off()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not entity.is_on
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2020-06-28 04:54:27 +00:00
|
|
|
assert "Test" == entity.name
|
|
|
|
assert "off" == entity.state
|
2016-07-17 08:27:27 +00:00
|
|
|
entity.turn_on()
|
2020-06-28 04:54:27 +00:00
|
|
|
assert "on" == entity.state
|
2016-07-17 08:27:27 +00:00
|
|
|
entity.turn_off()
|
2020-06-28 04:54:27 +00:00
|
|
|
assert "off" == entity.state
|
2016-07-17 08:27:27 +00:00
|
|
|
|
2016-04-09 03:55:31 +00:00
|
|
|
def test_several_switches(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with 3 switches."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"switch": {
|
|
|
|
"platform": "rfxtrx",
|
|
|
|
"signal_repetitions": 3,
|
|
|
|
"devices": {
|
|
|
|
"0b1100cd0213c7f230010f71": {"name": "Test"},
|
|
|
|
"0b1100100118cdea02010f70": {"name": "Bath"},
|
|
|
|
"0b1100101118cdea02010f70": {"name": "Living"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 3 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
device_num = 0
|
2016-04-09 03:55:31 +00:00
|
|
|
for id in rfxtrx_core.RFX_DEVICES:
|
|
|
|
entity = rfxtrx_core.RFX_DEVICES[id]
|
2018-10-24 10:10:05 +00:00
|
|
|
assert entity.signal_repetitions == 3
|
2019-07-31 19:25:30 +00:00
|
|
|
if entity.name == "Living":
|
2016-03-02 19:36:41 +00:00
|
|
|
device_num = device_num + 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "off" == entity.state
|
|
|
|
assert "<Entity Living: off>" == entity.__str__()
|
|
|
|
elif entity.name == "Bath":
|
2016-03-02 19:36:41 +00:00
|
|
|
device_num = device_num + 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "off" == entity.state
|
|
|
|
assert "<Entity Bath: off>" == entity.__str__()
|
|
|
|
elif entity.name == "Test":
|
2016-03-02 19:36:41 +00:00
|
|
|
device_num = device_num + 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "off" == entity.state
|
|
|
|
assert "<Entity Test: off>" == entity.__str__()
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 3 == device_num
|
2016-03-02 19:36:41 +00:00
|
|
|
|
|
|
|
def test_discover_switch(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with discovery of switches."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{"switch": {"platform": "rfxtrx", "automatic_add": True, "devices": {}}},
|
|
|
|
)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0b1100100118cdea02010f70")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0B, 0x11, 0x00, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x01, 0x0F, 0x70]
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2020-06-28 04:54:27 +00:00
|
|
|
entity = rfxtrx_core.RFX_DEVICES["118cdea_2"]
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(rfxtrx_core.RFX_DEVICES)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "<Entity 0b1100100118cdea01010f70: on>" == entity.__str__()
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-04-09 03:55:31 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 1 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0b1100100118cdeb02010f70")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x00, 0x00, 0x70]
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2020-06-28 04:54:27 +00:00
|
|
|
entity = rfxtrx_core.RFX_DEVICES["118cdeb_2"]
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == len(rfxtrx_core.RFX_DEVICES)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "<Entity 0b1100120118cdea02000070: on>" == entity.__str__()
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Trying to add a sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
|
|
|
|
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
|
2016-03-02 19:36:41 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Trying to add a light
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0b1100100118cdea02010f70")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0B, 0x11, 0x11, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x02, 0x0F, 0x70]
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-06-02 07:39:58 +00:00
|
|
|
# Trying to add a rollershutter
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0a1400adf394ab020e0060")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
|
|
|
|
)
|
2016-06-02 07:39:58 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == len(rfxtrx_core.RFX_DEVICES)
|
2016-06-02 07:39:58 +00:00
|
|
|
|
2016-03-02 19:36:41 +00:00
|
|
|
def test_discover_switch_noautoadd(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test with discovery of switch when auto add is False."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"switch",
|
|
|
|
{"switch": {"platform": "rfxtrx", "automatic_add": False, "devices": {}}},
|
|
|
|
)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0b1100100118cdea02010f70")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0B, 0x11, 0x00, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x01, 0x0F, 0x70]
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-04-09 03:55:31 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0b1100100118cdeb02010f70")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0B, 0x11, 0x00, 0x12, 0x01, 0x18, 0xCD, 0xEA, 0x02, 0x00, 0x00, 0x70]
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Trying to add a sensor
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0a52085e070100b31b0279")
|
|
|
|
event.data = bytearray(b"\nR\x08^\x07\x01\x00\xb3\x1b\x02y")
|
2016-03-02 19:36:41 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Trying to add a light
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0b1100100118cdea02010f70")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0B, 0x11, 0x11, 0x10, 0x01, 0x18, 0xCD, 0xEA, 0x01, 0x02, 0x0F, 0x70]
|
|
|
|
)
|
2016-04-09 03:55:31 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|
2016-06-02 07:39:58 +00:00
|
|
|
|
|
|
|
# Trying to add a rollershutter
|
2019-07-31 19:25:30 +00:00
|
|
|
event = rfxtrx_core.get_rfx_object("0a1400adf394ab020e0060")
|
|
|
|
event.data = bytearray(
|
|
|
|
[0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]
|
|
|
|
)
|
2016-06-02 07:39:58 +00:00
|
|
|
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 0 == len(rfxtrx_core.RFX_DEVICES)
|