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

+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() == []