mirror of
https://github.com/home-assistant/core.git
synced 2025-08-12 06:50:00 +00:00
.github
docs
homeassistant
script
tests
auth
components
alarm_control_panel
alexa
auth
automation
binary_sensor
calendar
camera
cast
climate
cloud
config
counter
cover
deconz
device_tracker
dialogflow
emulated_hue
fan
frontend
geo_location
geofency
google_assistant
group
hangouts
hassio
homekit
homematicip_cloud
http
hue
ifttt
image_processing
ios
light
lock
lovelace
luftdaten
mailbox
mailgun
media_player
mqtt
nest
notify
onboarding
openuv
persistent_notification
point
rainmachine
recorder
remote
scene
sensor
simplisafe
smhi
sonos
switch
__init__.py
common.py
test_command_line.py
test_deconz.py
test_flux.py
test_init.py
test_litejet.py
test_mfi.py
test_mochad.py
test_mqtt.py
test_rest.py
test_rflink.py
test_rfxtrx.py
test_template.py
test_unifi.py
test_vultr.py
test_wake_on_lan.py
test_zwave.py
timer
tradfri
tts
twilio
unifi
upnp
vacuum
water_heater
weather
websocket_api
zone
zwave
__init__.py
conftest.py
test_alert.py
test_api.py
test_canary.py
test_configurator.py
test_conversation.py
test_datadog.py
test_demo.py
test_device_sun_light_trigger.py
test_discovery.py
test_duckdns.py
test_dyson.py
test_feedreader.py
test_ffmpeg.py
test_folder_watcher.py
test_freedns.py
test_google.py
test_google_domains.py
test_graphite.py
test_history.py
test_history_graph.py
test_huawei_lte.py
test_influxdb.py
test_init.py
test_input_boolean.py
test_input_datetime.py
test_input_number.py
test_input_select.py
test_input_text.py
test_intent_script.py
test_introduction.py
test_kira.py
test_litejet.py
test_logbook.py
test_logentries.py
test_logger.py
test_melissa.py
test_microsoft_face.py
test_mqtt_eventstream.py
test_mqtt_statestream.py
test_namecheapdns.py
test_no_ip.py
test_nuheat.py
test_panel_custom.py
test_panel_iframe.py
test_pilight.py
test_plant.py
test_prometheus.py
test_proximity.py
test_python_script.py
test_qwikswitch.py
test_remember_the_milk.py
test_rest_command.py
test_rflink.py
test_rfxtrx.py
test_ring.py
test_rss_feed_template.py
test_script.py
test_shell_command.py
test_shopping_list.py
test_sleepiq.py
test_snips.py
test_spaceapi.py
test_spc.py
test_splunk.py
test_statsd.py
test_sun.py
test_system_log.py
test_updater.py
test_vultr.py
test_wake_on_lan.py
test_webhook.py
test_weblink.py
fixtures
helpers
mock
resources
scripts
test_util
testing_config
util
__init__.py
common.py
conftest.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_loader.py
test_main.py
test_requirements.py
test_setup.py
virtualization
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hound.yml
.ignore
.isort.cfg
.readthedocs.yml
.travis.yml
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE.md
MANIFEST.in
README.rst
mypy.ini
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini

* Add test cases and fix for device_defaults fire_event option. * Also for light. * Change docstring mood.
313 lines
8.7 KiB
Python
313 lines
8.7 KiB
Python
"""Test for RFlink switch components.
|
|
|
|
Test setup of rflink switch component/platform. State tracking and
|
|
control of Rflink switch devices.
|
|
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
|
|
from homeassistant.const import (
|
|
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON)
|
|
from homeassistant.core import callback
|
|
|
|
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',
|
|
'aliases': ['test_alias_0_0'],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
@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
|
|
# incoming events for its name and aliases
|
|
|
|
# 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
|
|
assert not switch_after_first_command.attributes.get('assumed_state')
|
|
|
|
# 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'
|
|
|
|
# test following aliases
|
|
# 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
|
|
# events because every new unknown device is added as a light by default.
|
|
|
|
# test changing state from HA propagates to Rflink
|
|
hass.async_add_job(
|
|
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
|
|
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
|
|
yield from hass.async_block_till_done()
|
|
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
|
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,
|
|
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
|
|
yield from hass.async_block_till_done()
|
|
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
|
assert protocol.send_command_ack.call_args_list[1][0][1] == 'on'
|
|
|
|
|
|
@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',
|
|
'group_aliases': ['test_group_0_0'],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
# 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',
|
|
'nogroup_aliases': ['test_nogroup_0_0'],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
# 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'
|
|
|
|
|
|
@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'
|