mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Clean up remaining rflink tests (#19551)
* some minor tests refactor * unused import * async/await refactor * Correct tests failures
This commit is contained in:
parent
dc11e41cf0
commit
70fff26383
@ -1,6 +1,5 @@
|
|||||||
"""Common functions for Rflink component tests and generic platform tests."""
|
"""Common functions for RFLink component tests and generic platform tests."""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from unittest.mock import Mock
|
from unittest.mock import Mock
|
||||||
|
|
||||||
from homeassistant.bootstrap import async_setup_component
|
from homeassistant.bootstrap import async_setup_component
|
||||||
@ -10,23 +9,19 @@ from homeassistant.const import (
|
|||||||
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_STOP_COVER)
|
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_STOP_COVER)
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def mock_rflink(hass, config, domain, monkeypatch, failures=None):
|
||||||
def mock_rflink(hass, config, domain, monkeypatch, failures=None):
|
"""Create mock RFLink asyncio protocol, test component setup."""
|
||||||
"""Create mock Rflink asyncio protocol, test component setup."""
|
|
||||||
transport, protocol = (Mock(), Mock())
|
transport, protocol = (Mock(), Mock())
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def send_command_ack(*command):
|
||||||
def send_command_ack(*command):
|
|
||||||
return True
|
return True
|
||||||
protocol.send_command_ack = Mock(wraps=send_command_ack)
|
protocol.send_command_ack = Mock(wraps=send_command_ack)
|
||||||
|
|
||||||
@asyncio.coroutine
|
|
||||||
def send_command(*command):
|
def send_command(*command):
|
||||||
return True
|
return True
|
||||||
protocol.send_command = Mock(wraps=send_command)
|
protocol.send_command = Mock(wraps=send_command)
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def create_rflink_connection(*args, **kwargs):
|
||||||
def create_rflink_connection(*args, **kwargs):
|
|
||||||
"""Return mocked transport and protocol."""
|
"""Return mocked transport and protocol."""
|
||||||
# failures can be a list of booleans indicating in which sequence
|
# failures can be a list of booleans indicating in which sequence
|
||||||
# creating a connection should success or fail
|
# creating a connection should success or fail
|
||||||
@ -45,7 +40,7 @@ def mock_rflink(hass, config, domain, monkeypatch, failures=None):
|
|||||||
'rflink.protocol.create_rflink_connection',
|
'rflink.protocol.create_rflink_connection',
|
||||||
mock_create)
|
mock_create)
|
||||||
|
|
||||||
yield from async_setup_component(hass, domain, config)
|
await async_setup_component(hass, domain, config)
|
||||||
|
|
||||||
# hook into mock config for injecting events
|
# hook into mock config for injecting events
|
||||||
event_callback = mock_create.call_args_list[0][1]['event_callback']
|
event_callback = mock_create.call_args_list[0][1]['event_callback']
|
||||||
@ -57,8 +52,7 @@ def mock_rflink(hass, config, domain, monkeypatch, failures=None):
|
|||||||
return event_callback, mock_create, protocol, disconnect_callback
|
return event_callback, mock_create, protocol, disconnect_callback
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_version_banner(hass, monkeypatch):
|
||||||
def test_version_banner(hass, monkeypatch):
|
|
||||||
"""Test sending unknown commands doesn't cause issues."""
|
"""Test sending unknown commands doesn't cause issues."""
|
||||||
# use sensor domain during testing main platform
|
# use sensor domain during testing main platform
|
||||||
domain = 'sensor'
|
domain = 'sensor'
|
||||||
@ -73,7 +67,7 @@ def test_version_banner(hass, monkeypatch):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
event_callback, _, _, _ = yield from mock_rflink(
|
event_callback, _, _, _ = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch)
|
hass, config, domain, monkeypatch)
|
||||||
|
|
||||||
event_callback({
|
event_callback({
|
||||||
@ -84,8 +78,7 @@ def test_version_banner(hass, monkeypatch):
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_send_no_wait(hass, monkeypatch):
|
||||||
def test_send_no_wait(hass, monkeypatch):
|
|
||||||
"""Test command sending without ack."""
|
"""Test command sending without ack."""
|
||||||
domain = 'switch'
|
domain = 'switch'
|
||||||
config = {
|
config = {
|
||||||
@ -105,19 +98,18 @@ def test_send_no_wait(hass, monkeypatch):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
_, _, protocol, _ = yield from mock_rflink(
|
_, _, protocol, _ = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch)
|
hass, config, domain, monkeypatch)
|
||||||
|
|
||||||
hass.async_add_job(
|
hass.async_create_task(
|
||||||
hass.services.async_call(domain, SERVICE_TURN_OFF,
|
hass.services.async_call(domain, SERVICE_TURN_OFF,
|
||||||
{ATTR_ENTITY_ID: 'switch.test'}))
|
{ATTR_ENTITY_ID: 'switch.test'}))
|
||||||
yield from hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert protocol.send_command.call_args_list[0][0][0] == 'protocol_0_0'
|
assert protocol.send_command.call_args_list[0][0][0] == 'protocol_0_0'
|
||||||
assert protocol.send_command.call_args_list[0][0][1] == 'off'
|
assert protocol.send_command.call_args_list[0][0][1] == 'off'
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_cover_send_no_wait(hass, monkeypatch):
|
||||||
def test_cover_send_no_wait(hass, monkeypatch):
|
|
||||||
"""Test command sending to a cover device without ack."""
|
"""Test command sending to a cover device without ack."""
|
||||||
domain = 'cover'
|
domain = 'cover'
|
||||||
config = {
|
config = {
|
||||||
@ -137,19 +129,18 @@ def test_cover_send_no_wait(hass, monkeypatch):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
_, _, protocol, _ = yield from mock_rflink(
|
_, _, protocol, _ = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch)
|
hass, config, domain, monkeypatch)
|
||||||
|
|
||||||
hass.async_add_job(
|
hass.async_create_task(
|
||||||
hass.services.async_call(domain, SERVICE_STOP_COVER,
|
hass.services.async_call(domain, SERVICE_STOP_COVER,
|
||||||
{ATTR_ENTITY_ID: 'cover.test'}))
|
{ATTR_ENTITY_ID: 'cover.test'}))
|
||||||
yield from hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert protocol.send_command.call_args_list[0][0][0] == 'RTS_0100F2_0'
|
assert protocol.send_command.call_args_list[0][0][0] == 'RTS_0100F2_0'
|
||||||
assert protocol.send_command.call_args_list[0][0][1] == 'STOP'
|
assert protocol.send_command.call_args_list[0][0][1] == 'STOP'
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_send_command(hass, monkeypatch):
|
||||||
def test_send_command(hass, monkeypatch):
|
|
||||||
"""Test send_command service."""
|
"""Test send_command service."""
|
||||||
domain = 'rflink'
|
domain = 'rflink'
|
||||||
config = {
|
config = {
|
||||||
@ -159,21 +150,20 @@ def test_send_command(hass, monkeypatch):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
_, _, protocol, _ = yield from mock_rflink(
|
_, _, protocol, _ = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch)
|
hass, config, domain, monkeypatch)
|
||||||
|
|
||||||
hass.async_add_job(
|
hass.async_create_task(
|
||||||
hass.services.async_call(domain, SERVICE_SEND_COMMAND,
|
hass.services.async_call(domain, SERVICE_SEND_COMMAND,
|
||||||
{'device_id': 'newkaku_0000c6c2_1',
|
{'device_id': 'newkaku_0000c6c2_1',
|
||||||
'command': 'on'}))
|
'command': 'on'}))
|
||||||
yield from hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert (protocol.send_command_ack.call_args_list[0][0][0]
|
assert (protocol.send_command_ack.call_args_list[0][0][0]
|
||||||
== 'newkaku_0000c6c2_1')
|
== 'newkaku_0000c6c2_1')
|
||||||
assert protocol.send_command_ack.call_args_list[0][0][1] == 'on'
|
assert protocol.send_command_ack.call_args_list[0][0][1] == 'on'
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_send_command_invalid_arguments(hass, monkeypatch):
|
||||||
def test_send_command_invalid_arguments(hass, monkeypatch):
|
|
||||||
"""Test send_command service."""
|
"""Test send_command service."""
|
||||||
domain = 'rflink'
|
domain = 'rflink'
|
||||||
config = {
|
config = {
|
||||||
@ -183,25 +173,24 @@ def test_send_command_invalid_arguments(hass, monkeypatch):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
_, _, protocol, _ = yield from mock_rflink(
|
_, _, protocol, _ = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch)
|
hass, config, domain, monkeypatch)
|
||||||
|
|
||||||
# one argument missing
|
# one argument missing
|
||||||
hass.async_add_job(
|
hass.async_create_task(
|
||||||
hass.services.async_call(domain, SERVICE_SEND_COMMAND,
|
hass.services.async_call(domain, SERVICE_SEND_COMMAND,
|
||||||
{'command': 'on'}))
|
{'command': 'on'}))
|
||||||
hass.async_add_job(
|
hass.async_create_task(
|
||||||
hass.services.async_call(domain, SERVICE_SEND_COMMAND,
|
hass.services.async_call(domain, SERVICE_SEND_COMMAND,
|
||||||
{'device_id': 'newkaku_0000c6c2_1'}))
|
{'device_id': 'newkaku_0000c6c2_1'}))
|
||||||
# no arguments
|
# no arguments
|
||||||
hass.async_add_job(
|
hass.async_create_task(
|
||||||
hass.services.async_call(domain, SERVICE_SEND_COMMAND, {}))
|
hass.services.async_call(domain, SERVICE_SEND_COMMAND, {}))
|
||||||
yield from hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert protocol.send_command_ack.call_args_list == []
|
assert protocol.send_command_ack.call_args_list == []
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_reconnecting_after_disconnect(hass, monkeypatch):
|
||||||
def test_reconnecting_after_disconnect(hass, monkeypatch):
|
|
||||||
"""An unexpected disconnect should cause a reconnect."""
|
"""An unexpected disconnect should cause a reconnect."""
|
||||||
domain = 'sensor'
|
domain = 'sensor'
|
||||||
config = {
|
config = {
|
||||||
@ -215,7 +204,7 @@ def test_reconnecting_after_disconnect(hass, monkeypatch):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
_, mock_create, _, disconnect_callback = yield from mock_rflink(
|
_, mock_create, _, disconnect_callback = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch)
|
hass, config, domain, monkeypatch)
|
||||||
|
|
||||||
assert disconnect_callback, 'disconnect callback not passed to rflink'
|
assert disconnect_callback, 'disconnect callback not passed to rflink'
|
||||||
@ -223,14 +212,13 @@ def test_reconnecting_after_disconnect(hass, monkeypatch):
|
|||||||
# rflink initiated disconnect
|
# rflink initiated disconnect
|
||||||
disconnect_callback(None)
|
disconnect_callback(None)
|
||||||
|
|
||||||
yield from hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# we expect 2 call, the initial and reconnect
|
# we expect 2 call, the initial and reconnect
|
||||||
assert mock_create.call_count == 2
|
assert mock_create.call_count == 2
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_reconnecting_after_failure(hass, monkeypatch):
|
||||||
def test_reconnecting_after_failure(hass, monkeypatch):
|
|
||||||
"""A failure to reconnect should be retried."""
|
"""A failure to reconnect should be retried."""
|
||||||
domain = 'sensor'
|
domain = 'sensor'
|
||||||
config = {
|
config = {
|
||||||
@ -247,22 +235,21 @@ def test_reconnecting_after_failure(hass, monkeypatch):
|
|||||||
failures = [False, True, False]
|
failures = [False, True, False]
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
_, mock_create, _, disconnect_callback = yield from mock_rflink(
|
_, mock_create, _, disconnect_callback = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch, failures=failures)
|
hass, config, domain, monkeypatch, failures=failures)
|
||||||
|
|
||||||
# rflink initiated disconnect
|
# rflink initiated disconnect
|
||||||
disconnect_callback(None)
|
disconnect_callback(None)
|
||||||
|
|
||||||
# wait for reconnects to have happened
|
# wait for reconnects to have happened
|
||||||
yield from hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
yield from hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# we expect 3 calls, the initial and 2 reconnects
|
# we expect 3 calls, the initial and 2 reconnects
|
||||||
assert mock_create.call_count == 3
|
assert mock_create.call_count == 3
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def test_error_when_not_connected(hass, monkeypatch):
|
||||||
def test_error_when_not_connected(hass, monkeypatch):
|
|
||||||
"""Sending command should error when not connected."""
|
"""Sending command should error when not connected."""
|
||||||
domain = 'switch'
|
domain = 'switch'
|
||||||
config = {
|
config = {
|
||||||
@ -285,14 +272,12 @@ def test_error_when_not_connected(hass, monkeypatch):
|
|||||||
failures = [False, True, False]
|
failures = [False, True, False]
|
||||||
|
|
||||||
# setup mocking rflink module
|
# setup mocking rflink module
|
||||||
_, _, _, disconnect_callback = yield from mock_rflink(
|
_, _, _, disconnect_callback = await mock_rflink(
|
||||||
hass, config, domain, monkeypatch, failures=failures)
|
hass, config, domain, monkeypatch, failures=failures)
|
||||||
|
|
||||||
# rflink initiated disconnect
|
# rflink initiated disconnect
|
||||||
disconnect_callback(None)
|
disconnect_callback(None)
|
||||||
|
|
||||||
yield from asyncio.sleep(0, loop=hass.loop)
|
success = await hass.services.async_call(
|
||||||
|
|
||||||
success = yield from hass.services.async_call(
|
|
||||||
domain, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: 'switch.test'})
|
domain, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: 'switch.test'})
|
||||||
assert not success, 'changing state should not succeed when disconnected'
|
assert not success, 'changing state should not succeed when disconnected'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user