2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Rfxtrx light platform."""
|
2020-07-05 20:41:11 +00:00
|
|
|
from unittest.mock import call
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-07-21 22:01:31 +00:00
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS
|
|
|
|
from homeassistant.core import State
|
2020-07-03 08:22:02 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2020-07-03 08:22:02 +00:00
|
|
|
from . import _signal_event
|
2016-03-02 19:36:41 +00:00
|
|
|
|
2020-07-21 22:01:31 +00:00
|
|
|
from tests.common import mock_restore_cache
|
|
|
|
|
2020-07-03 08:22:02 +00:00
|
|
|
|
|
|
|
async def test_one_light(hass, rfxtrx):
|
|
|
|
"""Test with 1 light."""
|
2020-07-12 20:03:22 +00:00
|
|
|
assert await async_setup_component(
|
2020-07-03 08:22:02 +00:00
|
|
|
hass,
|
2020-07-12 20:03:22 +00:00
|
|
|
"rfxtrx",
|
2020-07-13 01:40:45 +00:00
|
|
|
{"rfxtrx": {"device": "abcd", "devices": {"0b1100cd0213c7f210020f51": {}}}},
|
2020-07-03 08:22:02 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_16")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state
|
|
|
|
assert state.state == "off"
|
2020-07-12 20:03:22 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "AC 213c7f2:16"
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.services.async_call(
|
2020-07-12 20:03:22 +00:00
|
|
|
"light", "turn_on", {"entity_id": "light.ac_213c7f2_16"}, blocking=True
|
2020-07-03 08:22:02 +00:00
|
|
|
)
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_16")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes.get("brightness") == 255
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.services.async_call(
|
2020-07-12 20:03:22 +00:00
|
|
|
"light", "turn_off", {"entity_id": "light.ac_213c7f2_16"}, blocking=True
|
2020-07-05 20:41:11 +00:00
|
|
|
)
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_16")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state.state == "off"
|
|
|
|
assert state.attributes.get("brightness") is None
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
2020-07-12 20:03:22 +00:00
|
|
|
{"entity_id": "light.ac_213c7f2_16", "brightness": 100},
|
2020-07-05 20:41:11 +00:00
|
|
|
blocking=True,
|
|
|
|
)
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_16")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes.get("brightness") == 100
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.services.async_call(
|
2020-07-12 20:03:22 +00:00
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": "light.ac_213c7f2_16", "brightness": 10},
|
|
|
|
blocking=True,
|
2020-07-05 20:41:11 +00:00
|
|
|
)
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_16")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes.get("brightness") == 10
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
2020-07-12 20:03:22 +00:00
|
|
|
{"entity_id": "light.ac_213c7f2_16", "brightness": 255},
|
2020-07-05 20:41:11 +00:00
|
|
|
blocking=True,
|
|
|
|
)
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_16")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes.get("brightness") == 255
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.services.async_call(
|
2020-07-12 20:03:22 +00:00
|
|
|
"light", "turn_off", {"entity_id": "light.ac_213c7f2_16"}, blocking=True
|
2020-07-05 20:41:11 +00:00
|
|
|
)
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_16")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state.state == "off"
|
|
|
|
assert state.attributes.get("brightness") is None
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
assert rfxtrx.transport.send.mock_calls == [
|
|
|
|
call(bytearray(b"\x0b\x11\x00\x00\x02\x13\xc7\xf2\x10\x01\x00\x00")),
|
|
|
|
call(bytearray(b"\x0b\x11\x00\x00\x02\x13\xc7\xf2\x10\x00\x00\x00")),
|
|
|
|
call(bytearray(b"\x0b\x11\x00\x00\x02\x13\xc7\xf2\x10\x02\x06\x00")),
|
|
|
|
call(bytearray(b"\x0b\x11\x00\x00\x02\x13\xc7\xf2\x10\x02\x00\x00")),
|
|
|
|
call(bytearray(b"\x0b\x11\x00\x00\x02\x13\xc7\xf2\x10\x02\x0f\x00")),
|
|
|
|
call(bytearray(b"\x0b\x11\x00\x00\x02\x13\xc7\xf2\x10\x00\x00\x00")),
|
|
|
|
]
|
2020-07-03 08:22:02 +00:00
|
|
|
|
|
|
|
|
2020-07-21 22:01:31 +00:00
|
|
|
@pytest.mark.parametrize("state,brightness", [["on", 100], ["on", 50], ["off", None]])
|
|
|
|
async def test_state_restore(hass, rfxtrx, state, brightness):
|
|
|
|
"""State restoration."""
|
|
|
|
|
|
|
|
entity_id = "light.ac_213c7f2_16"
|
|
|
|
|
|
|
|
mock_restore_cache(
|
|
|
|
hass, [State(entity_id, state, attributes={ATTR_BRIGHTNESS: brightness})]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"rfxtrx",
|
|
|
|
{"rfxtrx": {"device": "abcd", "devices": {"0b1100cd0213c7f210020f51": {}}}},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(entity_id).state == state
|
|
|
|
assert hass.states.get(entity_id).attributes.get(ATTR_BRIGHTNESS) == brightness
|
|
|
|
|
|
|
|
|
2020-07-03 08:22:02 +00:00
|
|
|
async def test_several_lights(hass, rfxtrx):
|
|
|
|
"""Test with 3 lights."""
|
2020-07-12 20:03:22 +00:00
|
|
|
assert await async_setup_component(
|
2020-07-03 08:22:02 +00:00
|
|
|
hass,
|
2020-07-12 20:03:22 +00:00
|
|
|
"rfxtrx",
|
2020-07-03 08:22:02 +00:00
|
|
|
{
|
2020-07-12 20:03:22 +00:00
|
|
|
"rfxtrx": {
|
|
|
|
"device": "abcd",
|
2020-07-03 08:22:02 +00:00
|
|
|
"devices": {
|
2020-07-12 20:03:22 +00:00
|
|
|
"0b1100cd0213c7f230020f71": {},
|
|
|
|
"0b1100100118cdea02020f70": {},
|
|
|
|
"0b1100101118cdea02050f70": {},
|
2020-07-03 08:22:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_213c7f2_48")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state
|
|
|
|
assert state.state == "off"
|
2020-07-12 20:03:22 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "AC 213c7f2:48"
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_118cdea_2")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state
|
|
|
|
assert state.state == "off"
|
2020-07-12 20:03:22 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "AC 118cdea:2"
|
2020-07-05 20:41:11 +00:00
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_1118cdea_2")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state
|
|
|
|
assert state.state == "off"
|
2020-07-12 20:03:22 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "AC 1118cdea:2"
|
2020-07-05 20:41:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("repetitions", [1, 3])
|
|
|
|
async def test_repetitions(hass, rfxtrx, repetitions):
|
|
|
|
"""Test signal repetitions."""
|
2020-07-12 20:03:22 +00:00
|
|
|
assert await async_setup_component(
|
2020-07-03 08:22:02 +00:00
|
|
|
hass,
|
2020-07-12 20:03:22 +00:00
|
|
|
"rfxtrx",
|
2020-07-05 20:41:11 +00:00
|
|
|
{
|
2020-07-12 20:03:22 +00:00
|
|
|
"rfxtrx": {
|
|
|
|
"device": "abcd",
|
|
|
|
"devices": {
|
|
|
|
"0b1100cd0213c7f230020f71": {"signal_repetitions": repetitions}
|
|
|
|
},
|
2020-07-05 20:41:11 +00:00
|
|
|
}
|
|
|
|
},
|
2020-07-03 08:22:02 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.services.async_call(
|
2020-07-12 20:03:22 +00:00
|
|
|
"light", "turn_on", {"entity_id": "light.ac_213c7f2_48"}, blocking=True
|
2020-07-05 20:41:11 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
assert rfxtrx.transport.send.call_count == repetitions
|
2020-07-03 08:22:02 +00:00
|
|
|
|
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
async def test_discover_light(hass, rfxtrx):
|
|
|
|
"""Test with discovery of lights."""
|
2020-07-12 20:03:22 +00:00
|
|
|
assert await async_setup_component(
|
2020-07-13 01:40:45 +00:00
|
|
|
hass, "rfxtrx", {"rfxtrx": {"device": "abcd", "automatic_add": True}},
|
2020-07-03 08:22:02 +00:00
|
|
|
)
|
2020-07-05 20:41:11 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-07-13 00:57:19 +00:00
|
|
|
await hass.async_start()
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await _signal_event(hass, "0b11009e00e6116202020070")
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_0e61162_2")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state
|
|
|
|
assert state.state == "on"
|
2020-07-12 20:03:22 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "AC 0e61162:2"
|
2020-07-03 08:22:02 +00:00
|
|
|
|
2020-07-05 20:41:11 +00:00
|
|
|
await _signal_event(hass, "0b1100120118cdea02020070")
|
2020-07-12 20:03:22 +00:00
|
|
|
state = hass.states.get("light.ac_118cdea_2")
|
2020-07-05 20:41:11 +00:00
|
|
|
assert state
|
|
|
|
assert state.state == "on"
|
2020-07-12 20:03:22 +00:00
|
|
|
assert state.attributes.get("friendly_name") == "AC 118cdea:2"
|