Move tests to right folder (#23790)

* Move tests to right folder

* Fix test leaving files behind
This commit is contained in:
Paulus Schoutsen authored and Jason Hu committed 2019-05-10 13:20:50 -07:00
1 parent 4347a0f6b7
commit e9ea5c2ccb
8 files changed
+4

No files matched your search

-298
View File
@@ -1,298 +0,0 @@
"""The tests for the Rfxtrx switch platform."""
import unittest
import pytest
from homeassistant.setup import setup_component
from homeassistant.components import rfxtrx as rfxtrx_core
from tests.common import get_test_home_assistant, mock_component
@pytest.mark.skipif("os.environ.get('RFXTRX') != 'RUN'")
class TestSwitchRfxtrx(unittest.TestCase):
"""Test the Rfxtrx switch platform."""
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
mock_component(self.hass, 'rfxtrx')
def tearDown(self):
"""Stop everything that was started."""
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS = []
rfxtrx_core.RFX_DEVICES = {}
if rfxtrx_core.RFXOBJECT:
rfxtrx_core.RFXOBJECT.close_connection()
self.hass.stop()
def test_valid_config(self):
"""Test configuration."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': True,
'devices':
{'0b1100cd0213c7f210010f51': {
'name': 'Test',
rfxtrx_core.ATTR_FIREEVENT: True}
}}})
def test_valid_config_int_device_id(self):
"""Test configuration."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': True,
'devices':
{710000141010170: {
'name': 'Test',
rfxtrx_core.ATTR_FIREEVENT: True}
}}})
def test_invalid_config1(self):
"""Test invalid configuration."""
assert not setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': True,
'devices':
{'2FF7f216': {
'name': 'Test',
'packetid': '0b1100cd0213c7f210010f51',
'signal_repetitions': 3}
}}})
def test_invalid_config2(self):
"""Test invalid configuration."""
assert not setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': True,
'invalid_key': 'afda',
'devices':
{'213c7f216': {
'name': 'Test',
'packetid': '0b1100cd0213c7f210010f51',
rfxtrx_core.ATTR_FIREEVENT: True}
}}})
def test_invalid_config3(self):
"""Test invalid configuration."""
assert not setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': True,
'devices':
{'213c7f216': {
'name': 'Test',
'packetid': 'AA1100cd0213c7f210010f51',
rfxtrx_core.ATTR_FIREEVENT: True}
}}})
def test_invalid_config4(self):
"""Test configuration."""
assert not setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': True,
'devices':
{'213c7f216': {
'name': 'Test',
rfxtrx_core.ATTR_FIREEVENT: True}
}}})
def test_default_config(self):
"""Test with 0 switches."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'devices':
{}}})
assert 0 == len(rfxtrx_core.RFX_DEVICES)
def test_old_config(self):
"""Test with 1 switch."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'devices':
{'123efab1': {
'name': 'Test',
'packetid': '0b1100cd0213c7f210010f51'}}}})
import RFXtrx as rfxtrxmod
rfxtrx_core.RFXOBJECT =\
rfxtrxmod.Core("", transport_protocol=rfxtrxmod.DummyTransport)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
entity = rfxtrx_core.RFX_DEVICES['213c7f216']
assert 'Test' == entity.name
assert 'off' == entity.state
assert entity.assumed_state
assert entity.signal_repetitions == 1
assert not entity.should_fire_event
assert not entity.should_poll
assert not entity.is_on
entity.turn_on()
assert entity.is_on
entity.turn_off()
assert not entity.is_on
def test_one_switch(self):
"""Test with 1 switch."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'devices':
{'0b1100cd0213c7f210010f51': {
'name': 'Test'}}}})
import RFXtrx as rfxtrxmod
rfxtrx_core.RFXOBJECT =\
rfxtrxmod.Core("", transport_protocol=rfxtrxmod.DummyTransport)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
entity = rfxtrx_core.RFX_DEVICES['213c7f216']
assert 'Test' == entity.name
assert 'off' == entity.state
assert entity.assumed_state
assert entity.signal_repetitions == 1
assert not entity.should_fire_event
assert not entity.should_poll
assert not entity.is_on
entity.turn_on()
assert entity.is_on
entity.turn_off()
assert not entity.is_on
entity_id = rfxtrx_core.RFX_DEVICES['213c7f216'].entity_id
entity_hass = self.hass.states.get(entity_id)
assert 'Test' == entity_hass.name
assert 'off' == entity_hass.state
entity.turn_on()
entity_hass = self.hass.states.get(entity_id)
assert 'on' == entity_hass.state
entity.turn_off()
entity_hass = self.hass.states.get(entity_id)
assert 'off' == entity_hass.state
def test_several_switches(self):
"""Test with 3 switches."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'signal_repetitions': 3,
'devices':
{'0b1100cd0213c7f230010f71': {
'name': 'Test'},
'0b1100100118cdea02010f70': {
'name': 'Bath'},
'0b1100101118cdea02010f70': {
'name': 'Living'}}}})
assert 3 == len(rfxtrx_core.RFX_DEVICES)
device_num = 0
for id in rfxtrx_core.RFX_DEVICES:
entity = rfxtrx_core.RFX_DEVICES[id]
assert entity.signal_repetitions == 3
if entity.name == 'Living':
device_num = device_num + 1
assert 'off' == entity.state
assert '<Entity Living: off>' == entity.__str__()
elif entity.name == 'Bath':
device_num = device_num + 1
assert 'off' == entity.state
assert '<Entity Bath: off>' == entity.__str__()
elif entity.name == 'Test':
device_num = device_num + 1
assert 'off' == entity.state
assert '<Entity Test: off>' == entity.__str__()
assert 3 == device_num
def test_discover_switch(self):
"""Test with discovery of switches."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': True,
'devices': {}}})
event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
event.data = bytearray([0x0b, 0x11, 0x00, 0x10, 0x01, 0x18,
0xcd, 0xea, 0x01, 0x01, 0x0f, 0x70])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
entity = rfxtrx_core.RFX_DEVICES['118cdea2']
assert 1 == len(rfxtrx_core.RFX_DEVICES)
assert '<Entity 0b1100100118cdea01010f70: on>' == \
entity.__str__()
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 1 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object('0b1100100118cdeb02010f70')
event.data = bytearray([0x0b, 0x11, 0x00, 0x12, 0x01, 0x18,
0xcd, 0xea, 0x02, 0x00, 0x00, 0x70])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
entity = rfxtrx_core.RFX_DEVICES['118cdeb2']
assert 2 == len(rfxtrx_core.RFX_DEVICES)
assert '<Entity 0b1100120118cdea02000070: on>' == \
entity.__str__()
# Trying to add a sensor
event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a light
event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
event.data = bytearray([0x0b, 0x11, 0x11, 0x10, 0x01, 0x18,
0xcd, 0xea, 0x01, 0x02, 0x0f, 0x70])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a rollershutter
event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060')
event.data = bytearray([0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94,
0xAB, 0x02, 0x0E, 0x00, 0x60])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 2 == len(rfxtrx_core.RFX_DEVICES)
def test_discover_switch_noautoadd(self):
"""Test with discovery of switch when auto add is False."""
assert setup_component(self.hass, 'switch', {
'switch': {'platform': 'rfxtrx',
'automatic_add': False,
'devices': {}}})
event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
event.data = bytearray([0x0b, 0x11, 0x00, 0x10, 0x01, 0x18,
0xcd, 0xea, 0x01, 0x01, 0x0f, 0x70])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
event = rfxtrx_core.get_rfx_object('0b1100100118cdeb02010f70')
event.data = bytearray([0x0b, 0x11, 0x00, 0x12, 0x01, 0x18,
0xcd, 0xea, 0x02, 0x00, 0x00, 0x70])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a sensor
event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279')
event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y')
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a light
event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70')
event.data = bytearray([0x0b, 0x11, 0x11, 0x10, 0x01,
0x18, 0xcd, 0xea, 0x01, 0x02, 0x0f, 0x70])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
# Trying to add a rollershutter
event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060')
event.data = bytearray([0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94,
0xAB, 0x02, 0x0E, 0x00, 0x60])
rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS[0](event)
assert 0 == len(rfxtrx_core.RFX_DEVICES)
-369
View File
@@ -1,369 +0,0 @@
"""UniFi POE control platform tests."""
from collections import deque
from unittest.mock import Mock
import pytest
import aiounifi
from aiounifi.clients import Clients
from aiounifi.devices import Devices
from homeassistant import config_entries
from homeassistant.components import unifi
from homeassistant.setup import async_setup_component
import homeassistant.components.switch as switch
from tests.common import mock_coro
CLIENT_1 = {
'hostname': 'client_1',
'ip': '10.0.0.1',
'is_wired': True,
'mac': '00:00:00:00:00:01',
'name': 'POE Client 1',
'oui': 'Producer',
'sw_mac': '00:00:00:00:01:01',
'sw_port': 1,
'wired-rx_bytes': 1234000000,
'wired-tx_bytes': 5678000000
}
CLIENT_2 = {
'hostname': 'client_2',
'ip': '10.0.0.2',
'is_wired': True,
'mac': '00:00:00:00:00:02',
'name': 'POE Client 2',
'oui': 'Producer',
'sw_mac': '00:00:00:00:01:01',
'sw_port': 2,
'wired-rx_bytes': 1234000000,
'wired-tx_bytes': 5678000000
}
CLIENT_3 = {
'hostname': 'client_3',
'ip': '10.0.0.3',
'is_wired': True,
'mac': '00:00:00:00:00:03',
'name': 'Non-POE Client 3',
'oui': 'Producer',
'sw_mac': '00:00:00:00:01:01',
'sw_port': 3,
'wired-rx_bytes': 1234000000,
'wired-tx_bytes': 5678000000
}
CLIENT_4 = {
'hostname': 'client_4',
'ip': '10.0.0.4',
'is_wired': True,
'mac': '00:00:00:00:00:04',
'name': 'Non-POE Client 4',
'oui': 'Producer',
'sw_mac': '00:00:00:00:01:01',
'sw_port': 4,
'wired-rx_bytes': 1234000000,
'wired-tx_bytes': 5678000000
}
CLOUDKEY = {
'hostname': 'client_1',
'ip': 'mock-host',
'is_wired': True,
'mac': '10:00:00:00:00:01',
'name': 'Cloud key',
'oui': 'Producer',
'sw_mac': '00:00:00:00:01:01',
'sw_port': 1,
'wired-rx_bytes': 1234000000,
'wired-tx_bytes': 5678000000
}
POE_SWITCH_CLIENTS = [
{
'hostname': 'client_1',
'ip': '10.0.0.1',
'is_wired': True,
'mac': '00:00:00:00:00:01',
'name': 'POE Client 1',
'oui': 'Producer',
'sw_mac': '00:00:00:00:01:01',
'sw_port': 1,
'wired-rx_bytes': 1234000000,
'wired-tx_bytes': 5678000000
},
{
'hostname': 'client_2',
'ip': '10.0.0.2',
'is_wired': True,
'mac': '00:00:00:00:00:02',
'name': 'POE Client 2',
'oui': 'Producer',
'sw_mac': '00:00:00:00:01:01',
'sw_port': 1,
'wired-rx_bytes': 1234000000,
'wired-tx_bytes': 5678000000
}
]
DEVICE_1 = {
'device_id': 'mock-id',
'ip': '10.0.1.1',
'mac': '00:00:00:00:01:01',
'type': 'usw',
'name': 'mock-name',
'port_overrides': [],
'port_table': [
{
'media': 'GE',
'name': 'Port 1',
'port_idx': 1,
'poe_class': 'Class 4',
'poe_enable': True,
'poe_mode': 'auto',
'poe_power': '2.56',
'poe_voltage': '53.40',
'portconf_id': '1a1',
'port_poe': True,
'up': True
},
{
'media': 'GE',
'name': 'Port 2',
'port_idx': 2,
'poe_class': 'Class 4',
'poe_enable': True,
'poe_mode': 'auto',
'poe_power': '2.56',
'poe_voltage': '53.40',
'portconf_id': '1a2',
'port_poe': True,
'up': True
},
{
'media': 'GE',
'name': 'Port 3',
'port_idx': 3,
'poe_class': 'Unknown',
'poe_enable': False,
'poe_mode': 'off',
'poe_power': '0.00',
'poe_voltage': '0.00',
'portconf_id': '1a3',
'port_poe': False,
'up': True
},
{
'media': 'GE',
'name': 'Port 4',
'port_idx': 4,
'poe_class': 'Unknown',
'poe_enable': False,
'poe_mode': 'auto',
'poe_power': '0.00',
'poe_voltage': '0.00',
'portconf_id': '1a4',
'port_poe': True,
'up': True
}
]
}
CONTROLLER_DATA = {
unifi.CONF_HOST: 'mock-host',
unifi.CONF_USERNAME: 'mock-user',
unifi.CONF_PASSWORD: 'mock-pswd',
unifi.CONF_PORT: 1234,
unifi.CONF_SITE_ID: 'mock-site',
unifi.CONF_VERIFY_SSL: True
}
ENTRY_CONFIG = {
unifi.CONF_CONTROLLER: CONTROLLER_DATA,
unifi.CONF_POE_CONTROL: True
}
CONTROLLER_ID = unifi.CONTROLLER_ID.format(host='mock-host', site='mock-site')
@pytest.fixture
def mock_controller(hass):
"""Mock a UniFi Controller."""
controller = Mock(
available=True,
api=Mock(),
spec=unifi.UniFiController
)
controller.mac = '10:00:00:00:00:01'
controller.mock_requests = []
controller.mock_client_responses = deque()
controller.mock_device_responses = deque()
async def mock_request(method, path, **kwargs):
kwargs['method'] = method
kwargs['path'] = path
controller.mock_requests.append(kwargs)
if path == 's/{site}/stat/sta':
return controller.mock_client_responses.popleft()
if path == 's/{site}/stat/device':
return controller.mock_device_responses.popleft()
return None
controller.api.clients = Clients({}, mock_request)
controller.api.devices = Devices({}, mock_request)
return controller
async def setup_controller(hass, mock_controller):
"""Load the UniFi switch platform with the provided controller."""
hass.config.components.add(unifi.DOMAIN)
hass.data[unifi.DOMAIN] = {CONTROLLER_ID: mock_controller}
config_entry = config_entries.ConfigEntry(
1, unifi.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
config_entries.CONN_CLASS_LOCAL_POLL)
await hass.config_entries.async_forward_entry_setup(config_entry, 'switch')
# To flush out the service call to update the group
await hass.async_block_till_done()
async def test_platform_manually_configured(hass):
"""Test that we do not discover anything or try to set up a bridge."""
assert await async_setup_component(hass, switch.DOMAIN, {
'switch': {
'platform': 'unifi'
}
}) is True
assert unifi.DOMAIN not in hass.data
async def test_no_clients(hass, mock_controller):
"""Test the update_clients function when no clients are found."""
mock_controller.mock_client_responses.append({})
await setup_controller(hass, mock_controller)
assert len(mock_controller.mock_requests) == 2
assert not hass.states.async_all()
async def test_controller_not_client(hass, mock_controller):
"""Test that the controller doesn't become a switch."""
mock_controller.mock_client_responses.append([CLOUDKEY])
mock_controller.mock_device_responses.append([DEVICE_1])
await setup_controller(hass, mock_controller)
assert len(mock_controller.mock_requests) == 2
assert not hass.states.async_all()
cloudkey = hass.states.get('switch.cloud_key')
assert cloudkey is None
async def test_switches(hass, mock_controller):
"""Test the update_items function with some lights."""
mock_controller.mock_client_responses.append([CLIENT_1, CLIENT_4])
mock_controller.mock_device_responses.append([DEVICE_1])
await setup_controller(hass, mock_controller)
assert len(mock_controller.mock_requests) == 2
# 1 All Lights group, 2 lights
assert len(hass.states.async_all()) == 2
switch_1 = hass.states.get('switch.client_1')
assert switch_1 is not None
assert switch_1.state == 'on'
assert switch_1.attributes['power'] == '2.56'
assert switch_1.attributes['received'] == 1234
assert switch_1.attributes['sent'] == 5678
assert switch_1.attributes['switch'] == '00:00:00:00:01:01'
assert switch_1.attributes['port'] == 1
assert switch_1.attributes['poe_mode'] == 'auto'
switch = hass.states.get('switch.client_4')
assert switch is None
async def test_new_client_discovered(hass, mock_controller):
"""Test if 2nd update has a new client."""
mock_controller.mock_client_responses.append([CLIENT_1])
mock_controller.mock_device_responses.append([DEVICE_1])
await setup_controller(hass, mock_controller)
assert len(mock_controller.mock_requests) == 2
assert len(hass.states.async_all()) == 2
mock_controller.mock_client_responses.append([CLIENT_1, CLIENT_2])
mock_controller.mock_device_responses.append([DEVICE_1])
# Calling a service will trigger the updates to run
await hass.services.async_call('switch', 'turn_off', {
'entity_id': 'switch.client_1'
}, blocking=True)
# 2x light update, 1 turn on request
assert len(mock_controller.mock_requests) == 5
assert len(hass.states.async_all()) == 3
switch = hass.states.get('switch.client_2')
assert switch is not None
assert switch.state == 'on'
async def test_failed_update_successful_login(hass, mock_controller):
"""Running update can login when requested."""
mock_controller.available = False
mock_controller.api.clients.update = Mock()
mock_controller.api.clients.update.side_effect = aiounifi.LoginRequired
mock_controller.api.login = Mock()
mock_controller.api.login.return_value = mock_coro()
await setup_controller(hass, mock_controller)
assert len(mock_controller.mock_requests) == 0
assert mock_controller.available is True
async def test_failed_update_failed_login(hass, mock_controller):
"""Running update can handle a failed login."""
mock_controller.api.clients.update = Mock()
mock_controller.api.clients.update.side_effect = aiounifi.LoginRequired
mock_controller.api.login = Mock()
mock_controller.api.login.side_effect = aiounifi.AiounifiException
await setup_controller(hass, mock_controller)
assert len(mock_controller.mock_requests) == 0
assert mock_controller.available is False
async def test_failed_update_unreachable_controller(hass, mock_controller):
"""Running update can handle a unreachable controller."""
mock_controller.mock_client_responses.append([CLIENT_1, CLIENT_2])
mock_controller.mock_device_responses.append([DEVICE_1])
await setup_controller(hass, mock_controller)
mock_controller.api.clients.update = Mock()
mock_controller.api.clients.update.side_effect = aiounifi.AiounifiException
# Calling a service will trigger the updates to run
await hass.services.async_call('switch', 'turn_off', {
'entity_id': 'switch.client_1'
}, blocking=True)
# 2x light update, 1 turn on request
assert len(mock_controller.mock_requests) == 3
assert len(hass.states.async_all()) == 3
assert mock_controller.available is False
async def test_ignore_multiple_poe_clients_on_same_port(hass, mock_controller):
"""Ignore when there are multiple POE driven clients on same port.
If there is a non-UniFi switch powered by POE,
clients will be transparently marked as having POE as well.
"""
mock_controller.mock_client_responses.append(POE_SWITCH_CLIENTS)
mock_controller.mock_device_responses.append([DEVICE_1])
await setup_controller(hass, mock_controller)
assert len(mock_controller.mock_requests) == 2
# 1 All Lights group, 2 lights
assert len(hass.states.async_all()) == 0
switch_1 = hass.states.get('switch.client_1')
switch_2 = hass.states.get('switch.client_2')
assert switch_1 is None
assert switch_2 is None