Move Broadlink services to component (#21465)
* Register services in broadlink domain * Add tests for broadlink services * Resolve review comments * One more review fix * Restore auth retry * Drop unused constants * Fix flake8 errors
This commit is contained in:
1 parent
f269135ae9
commit
0a3e11aa12
6 files changed
+250
-65
No files matched your search
@@ -0,0 +1 @@
|
||||
"""The tests for broadlink platforms."""
|
||||
@@ -0,0 +1,117 @@
|
||||
"""The tests for the broadlink component."""
|
||||
from datetime import timedelta
|
||||
from base64 import b64decode
|
||||
from unittest.mock import MagicMock, patch, call
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.util.dt import utcnow
|
||||
from homeassistant.components.broadlink import async_setup_service
|
||||
from homeassistant.components.broadlink.const import (
|
||||
DOMAIN, SERVICE_LEARN, SERVICE_SEND)
|
||||
|
||||
DUMMY_IR_PACKET = ("JgBGAJKVETkRORA6ERQRFBEUERQRFBE5ETkQOhAVEBUQFREUEBUQ"
|
||||
"OhEUERQRORE5EBURFBA6EBUQOhE5EBUQFRA6EDoRFBEADQUAAA==")
|
||||
DUMMY_HOST = "192.168.0.2"
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def dummy_broadlink():
|
||||
"""Mock broadlink module so we don't have that dependency on tests."""
|
||||
broadlink = MagicMock()
|
||||
with patch.dict('sys.modules', {
|
||||
'broadlink': broadlink,
|
||||
}):
|
||||
yield broadlink
|
||||
|
||||
|
||||
async def test_send(hass):
|
||||
"""Test send service."""
|
||||
mock_device = MagicMock()
|
||||
mock_device.send_data.return_value = None
|
||||
|
||||
async_setup_service(hass, DUMMY_HOST, mock_device)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_SEND, {
|
||||
"host": DUMMY_HOST,
|
||||
"packet": (DUMMY_IR_PACKET)
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_device.send_data.call_count == 1
|
||||
assert mock_device.send_data.call_args == call(
|
||||
b64decode(DUMMY_IR_PACKET))
|
||||
|
||||
|
||||
async def test_learn(hass):
|
||||
"""Test learn service."""
|
||||
mock_device = MagicMock()
|
||||
mock_device.enter_learning.return_value = None
|
||||
mock_device.check_data.return_value = b64decode(DUMMY_IR_PACKET)
|
||||
|
||||
with patch.object(hass.components.persistent_notification,
|
||||
'async_create') as mock_create:
|
||||
|
||||
async_setup_service(hass, DUMMY_HOST, mock_device)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_LEARN, {
|
||||
"host": DUMMY_HOST,
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_device.enter_learning.call_count == 1
|
||||
assert mock_device.enter_learning.call_args == call()
|
||||
|
||||
assert mock_create.call_count == 1
|
||||
assert mock_create.call_args == call(
|
||||
"Received packet is: {}".format(DUMMY_IR_PACKET),
|
||||
title='Broadlink switch')
|
||||
|
||||
|
||||
async def test_learn_timeout(hass):
|
||||
"""Test learn service."""
|
||||
mock_device = MagicMock()
|
||||
mock_device.enter_learning.return_value = None
|
||||
mock_device.check_data.return_value = None
|
||||
|
||||
async_setup_service(hass, DUMMY_HOST, mock_device)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
now = utcnow()
|
||||
|
||||
with patch.object(hass.components.persistent_notification,
|
||||
'async_create') as mock_create, \
|
||||
patch('homeassistant.components.broadlink.utcnow') as mock_utcnow:
|
||||
|
||||
mock_utcnow.side_effect = [now, now + timedelta(20)]
|
||||
|
||||
await hass.services.async_call(DOMAIN, SERVICE_LEARN, {
|
||||
"host": DUMMY_HOST,
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_device.enter_learning.call_count == 1
|
||||
assert mock_device.enter_learning.call_args == call()
|
||||
|
||||
assert mock_create.call_count == 1
|
||||
assert mock_create.call_args == call(
|
||||
"No signal was received",
|
||||
title='Broadlink switch')
|
||||
|
||||
|
||||
async def test_ipv4():
|
||||
"""Test ipv4 parsing."""
|
||||
from homeassistant.components.broadlink import ipv4_address
|
||||
|
||||
schema = vol.Schema(ipv4_address)
|
||||
|
||||
for value in ('invalid', '1', '192', '192.168',
|
||||
'192.168.0', '192.168.0.A'):
|
||||
with pytest.raises(vol.MultipleInvalid):
|
||||
schema(value)
|
||||
|
||||
for value in ('192.168.0.1', '10.0.0.1'):
|
||||
schema(value)
|
||||
Reference in new issue
Block a user