2017-01-31 16:11:52 +00:00
|
|
|
"""Test for RFlink switch components.
|
|
|
|
|
|
|
|
Test setup of rflink switch component/platform. State tracking and
|
|
|
|
control of Rflink switch devices.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
2017-09-24 22:47:59 +00:00
|
|
|
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
|
2017-01-31 16:11:52 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON)
|
2017-09-24 22:47:59 +00:00
|
|
|
from homeassistant.core import callback
|
2017-01-31 16:11:52 +00:00
|
|
|
|
|
|
|
from ..test_rflink import mock_rflink
|
|
|
|
|
|
|
|
DOMAIN = 'switch'
|
|
|
|
|
|
|
|
CONFIG = {
|
|
|
|
'rflink': {
|
|
|
|
'port': '/dev/ttyABC0',
|
|
|
|
'ignore_devices': ['ignore_wildcard_*', 'ignore_sensor'],
|
|
|
|
},
|
|
|
|
DOMAIN: {
|
|
|
|
'platform': 'rflink',
|
|
|
|
'devices': {
|
|
|
|
'protocol_0_0': {
|
|
|
|
'name': 'test',
|
2017-07-06 13:59:54 +00:00
|
|
|
'aliases': ['test_alias_0_0'],
|
2017-01-31 16:11:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_default_setup(hass, monkeypatch):
|
|
|
|
"""Test all basic functionality of the rflink switch component."""
|
|
|
|
# setup mocking rflink module
|
|
|
|
event_callback, create, protocol, _ = yield from mock_rflink(
|
|
|
|
hass, CONFIG, DOMAIN, monkeypatch)
|
|
|
|
|
|
|
|
# make sure arguments are passed
|
|
|
|
assert create.call_args_list[0][1]['ignore']
|
|
|
|
|
|
|
|
# test default state of switch loaded from config
|
|
|
|
switch_initial = hass.states.get('switch.test')
|
|
|
|
assert switch_initial.state == 'off'
|
|
|
|
assert switch_initial.attributes['assumed_state']
|
|
|
|
|
|
|
|
# switch should follow state of the hardware device by interpreting
|
2017-07-06 13:59:54 +00:00
|
|
|
# incoming events for its name and aliases
|
2017-01-31 16:11:52 +00:00
|
|
|
|
|
|
|
# mock incoming command event for this device
|
|
|
|
event_callback({
|
|
|
|
'id': 'protocol_0_0',
|
|
|
|
'command': 'on',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
switch_after_first_command = hass.states.get('switch.test')
|
|
|
|
assert switch_after_first_command.state == 'on'
|
|
|
|
# also after receiving first command state not longer has to be assumed
|
2017-06-17 17:03:49 +00:00
|
|
|
assert not switch_after_first_command.attributes.get('assumed_state')
|
2017-01-31 16:11:52 +00:00
|
|
|
|
|
|
|
# mock incoming command event for this device
|
|
|
|
event_callback({
|
|
|
|
'id': 'protocol_0_0',
|
|
|
|
'command': 'off',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get('switch.test').state == 'off'
|
|
|
|
|
2017-07-06 13:59:54 +00:00
|
|
|
# test following aliases
|
2017-01-31 16:11:52 +00:00
|
|
|
# mock incoming command event for this device alias
|
|
|
|
event_callback({
|
|
|
|
'id': 'test_alias_0_0',
|
|
|
|
'command': 'on',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get('switch.test').state == 'on'
|
|
|
|
|
|
|
|
# The switch component does not support adding new devices for incoming
|
2017-09-23 15:15:46 +00:00
|
|
|
# events because every new unknown device is added as a light by default.
|
2017-01-31 16:11:52 +00:00
|
|
|
|
|
|
|
# test changing state from HA propagates to Rflink
|
|
|
|
hass.async_add_job(
|
|
|
|
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
|
2017-06-02 07:05:34 +00:00
|
|
|
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
|
2017-01-31 16:11:52 +00:00
|
|
|
yield from hass.async_block_till_done()
|
2017-06-02 07:05:34 +00:00
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
2017-01-31 16:11:52 +00:00
|
|
|
assert protocol.send_command_ack.call_args_list[0][0][0] == 'protocol_0_0'
|
|
|
|
assert protocol.send_command_ack.call_args_list[0][0][1] == 'off'
|
|
|
|
|
|
|
|
hass.async_add_job(
|
|
|
|
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
|
2017-06-02 07:05:34 +00:00
|
|
|
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
|
2017-01-31 16:11:52 +00:00
|
|
|
yield from hass.async_block_till_done()
|
2017-06-02 07:05:34 +00:00
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
2017-01-31 16:11:52 +00:00
|
|
|
assert protocol.send_command_ack.call_args_list[1][0][1] == 'on'
|
2017-06-02 07:05:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_group_alias(hass, monkeypatch):
|
|
|
|
"""Group aliases should only respond to group commands (allon/alloff)."""
|
|
|
|
config = {
|
|
|
|
'rflink': {
|
|
|
|
'port': '/dev/ttyABC0',
|
|
|
|
},
|
|
|
|
DOMAIN: {
|
|
|
|
'platform': 'rflink',
|
|
|
|
'devices': {
|
|
|
|
'protocol_0_0': {
|
|
|
|
'name': 'test',
|
2017-07-06 13:59:54 +00:00
|
|
|
'group_aliases': ['test_group_0_0'],
|
2017-06-02 07:05:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup mocking rflink module
|
|
|
|
event_callback, _, _, _ = yield from mock_rflink(
|
|
|
|
hass, config, DOMAIN, monkeypatch)
|
|
|
|
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
|
|
|
|
|
|
|
# test sending group command to group alias
|
|
|
|
event_callback({
|
|
|
|
'id': 'test_group_0_0',
|
|
|
|
'command': 'allon',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
|
|
|
|
|
|
|
# test sending group command to group alias
|
|
|
|
event_callback({
|
|
|
|
'id': 'test_group_0_0',
|
|
|
|
'command': 'off',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_nogroup_alias(hass, monkeypatch):
|
|
|
|
"""Non group aliases should not respond to group commands."""
|
|
|
|
config = {
|
|
|
|
'rflink': {
|
|
|
|
'port': '/dev/ttyABC0',
|
|
|
|
},
|
|
|
|
DOMAIN: {
|
|
|
|
'platform': 'rflink',
|
|
|
|
'devices': {
|
|
|
|
'protocol_0_0': {
|
|
|
|
'name': 'test',
|
2017-07-06 13:59:54 +00:00
|
|
|
'nogroup_aliases': ['test_nogroup_0_0'],
|
2017-06-02 07:05:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup mocking rflink module
|
|
|
|
event_callback, _, _, _ = yield from mock_rflink(
|
|
|
|
hass, config, DOMAIN, monkeypatch)
|
|
|
|
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
|
|
|
|
|
|
|
# test sending group command to nogroup alias
|
|
|
|
event_callback({
|
|
|
|
'id': 'test_nogroup_0_0',
|
|
|
|
'command': 'allon',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
# should not affect state
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
|
|
|
|
|
|
|
# test sending group command to nogroup alias
|
|
|
|
event_callback({
|
|
|
|
'id': 'test_nogroup_0_0',
|
|
|
|
'command': 'on',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
# should affect state
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_nogroup_device_id(hass, monkeypatch):
|
|
|
|
"""Device id that do not respond to group commands (allon/alloff)."""
|
|
|
|
config = {
|
|
|
|
'rflink': {
|
|
|
|
'port': '/dev/ttyABC0',
|
|
|
|
},
|
|
|
|
DOMAIN: {
|
|
|
|
'platform': 'rflink',
|
|
|
|
'devices': {
|
|
|
|
'test_nogroup_0_0': {
|
|
|
|
'name': 'test',
|
|
|
|
'group': False,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup mocking rflink module
|
|
|
|
event_callback, _, _, _ = yield from mock_rflink(
|
|
|
|
hass, config, DOMAIN, monkeypatch)
|
|
|
|
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
|
|
|
|
|
|
|
# test sending group command to nogroup
|
|
|
|
event_callback({
|
|
|
|
'id': 'test_nogroup_0_0',
|
|
|
|
'command': 'allon',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
# should not affect state
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
|
|
|
|
|
|
|
# test sending group command to nogroup
|
|
|
|
event_callback({
|
|
|
|
'id': 'test_nogroup_0_0',
|
|
|
|
'command': 'on',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
# should affect state
|
|
|
|
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
2017-09-24 22:47:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_device_defaults(hass, monkeypatch):
|
|
|
|
"""Event should fire if device_defaults config says so."""
|
|
|
|
config = {
|
|
|
|
'rflink': {
|
|
|
|
'port': '/dev/ttyABC0',
|
|
|
|
},
|
|
|
|
DOMAIN: {
|
|
|
|
'platform': 'rflink',
|
|
|
|
'device_defaults': {
|
|
|
|
'fire_event': True,
|
|
|
|
},
|
|
|
|
'devices': {
|
|
|
|
'protocol_0_0': {
|
|
|
|
'name': 'test',
|
|
|
|
'aliases': ['test_alias_0_0'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup mocking rflink module
|
|
|
|
event_callback, _, _, _ = yield from mock_rflink(
|
|
|
|
hass, config, DOMAIN, monkeypatch)
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def listener(event):
|
|
|
|
calls.append(event)
|
|
|
|
hass.bus.async_listen_once(EVENT_BUTTON_PRESSED, listener)
|
|
|
|
|
|
|
|
# test event for new unconfigured sensor
|
|
|
|
event_callback({
|
|
|
|
'id': 'protocol_0_0',
|
|
|
|
'command': 'off',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert calls[0].data == {'state': 'off', 'entity_id': DOMAIN + '.test'}
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_not_firing_default(hass, monkeypatch):
|
|
|
|
"""By default no bus events should be fired."""
|
|
|
|
config = {
|
|
|
|
'rflink': {
|
|
|
|
'port': '/dev/ttyABC0',
|
|
|
|
},
|
|
|
|
DOMAIN: {
|
|
|
|
'platform': 'rflink',
|
|
|
|
'devices': {
|
|
|
|
'protocol_0_0': {
|
|
|
|
'name': 'test',
|
|
|
|
'aliases': ['test_alias_0_0'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup mocking rflink module
|
|
|
|
event_callback, _, _, _ = yield from mock_rflink(
|
|
|
|
hass, config, DOMAIN, monkeypatch)
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def listener(event):
|
|
|
|
calls.append(event)
|
|
|
|
hass.bus.async_listen_once(EVENT_BUTTON_PRESSED, listener)
|
|
|
|
|
|
|
|
# test event for new unconfigured sensor
|
|
|
|
event_callback({
|
|
|
|
'id': 'protocol_0_0',
|
|
|
|
'command': 'off',
|
|
|
|
})
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert not calls, 'an event has been fired'
|