1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
"""Unit test for devices container within XKNX."""
from unittest.mock import AsyncMock, Mock, patch
import pytest
from xknx import XKNX
from xknx.devices import BinarySensor, Device, Light, Switch
from xknx.telegram import GroupAddress
class TestDevices:
"""Test class for devices container within XKNX."""
#
# XKNX Config
#
def test_get_item(self) -> None:
"""Test get item by name or by index."""
xknx = XKNX()
light1 = Light(xknx, "Living-Room.Light_1", group_address_switch="1/6/7")
xknx.devices.async_add(light1)
switch1 = Switch(xknx, "TestOutlet_1", group_address="1/2/3")
xknx.devices.async_add(switch1)
light2 = Light(xknx, "Living-Room.Light_2", group_address_switch="1/6/8")
xknx.devices.async_add(light2)
switch2 = Switch(xknx, "TestOutlet_2", group_address="1/2/4")
xknx.devices.async_add(switch2)
assert xknx.devices["Living-Room.Light_1"] == light1
assert xknx.devices["TestOutlet_1"] == switch1
assert xknx.devices["Living-Room.Light_2"] == light2
assert xknx.devices["TestOutlet_2"] == switch2
with pytest.raises(KeyError):
# pylint: disable=pointless-statement
xknx.devices["TestOutlet_2sdds"]
assert xknx.devices[0] == light1
assert xknx.devices[1] == switch1
assert xknx.devices[2] == light2
assert xknx.devices[3] == switch2
with pytest.raises(IndexError):
# pylint: disable=pointless-statement
xknx.devices[4]
def test_device_by_group_address(self) -> None:
"""Test get devices by group address."""
xknx = XKNX()
light1 = Light(xknx, "Livingroom", group_address_switch="1/6/7")
sensor1 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
sensor2 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
light2 = Light(xknx, "Livingroom", group_address_switch="1/6/8")
xknx.devices.async_add(light1)
xknx.devices.async_add(sensor1)
xknx.devices.async_add(sensor2)
xknx.devices.async_add(light2)
assert tuple(xknx.devices.devices_by_group_address(GroupAddress("1/6/7"))) == (
light1,
)
assert tuple(xknx.devices.devices_by_group_address(GroupAddress("1/6/8"))) == (
light2,
)
assert tuple(xknx.devices.devices_by_group_address(GroupAddress("3/0/1"))) == (
sensor1,
sensor2,
)
def test_iter(self) -> None:
"""Test __iter__() function."""
xknx = XKNX()
light1 = Light(xknx, "Livingroom", group_address_switch="1/6/7")
sensor1 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
sensor2 = BinarySensor(xknx, "Diningroom", group_address_state="3/0/1")
light2 = Light(xknx, "Livingroom", group_address_switch="1/6/8")
xknx.devices.async_add(light1)
xknx.devices.async_add(sensor1)
xknx.devices.async_add(sensor2)
xknx.devices.async_add(light2)
assert tuple(iter(xknx.devices)) == (light1, sensor1, sensor2, light2)
def test_len(self) -> None:
"""Test len() function."""
xknx = XKNX()
assert len(xknx.devices) == 0
light = Light(xknx, "Living-Room.Light_1", group_address_switch="1/6/7")
xknx.devices.async_add(light)
assert len(xknx.devices) == 1
binary_sensor = BinarySensor(
xknx, "DiningRoom.Motion.Sensor", group_address_state="3/0/1"
)
xknx.devices.async_add(binary_sensor)
assert len(xknx.devices) == 2
xknx.devices.async_remove(light)
assert len(xknx.devices) == 1
xknx.devices.async_add(light)
assert len(xknx.devices) == 2
def test_contains(self) -> None:
"""Test __contains__() function."""
xknx = XKNX()
xknx.devices.async_add(
Light(xknx, "Living-Room.Light_1", group_address_switch="1/6/7")
)
xknx.devices.async_add(
Light(xknx, "Living-Room.Light_2", group_address_switch="1/6/8")
)
assert "Living-Room.Light_1" in xknx.devices
assert "Living-Room.Light_2" in xknx.devices
assert "Living-Room.Light_3" not in xknx.devices
@patch.multiple(Device, __abstractmethods__=set())
def test_add_remove(self) -> None:
"""Tesst add and remove functions."""
xknx = XKNX()
device1 = Device(xknx, "TestDevice1")
device2 = Device(xknx, "TestDevice2")
xknx.devices.async_add(device1)
xknx.devices.async_add(device2)
assert len(xknx.devices) == 2
xknx.devices.async_remove(device1)
assert len(xknx.devices) == 1
assert "TestDevice1" not in xknx.devices
xknx.devices.async_remove(device2)
assert len(xknx.devices) == 0
async def test_modification_of_device(self) -> None:
"""Test if devices object does store references and not copies of objects."""
xknx = XKNX()
light1 = Light(xknx, "Living-Room.Light_1", group_address_switch="1/6/7")
xknx.devices.async_add(light1)
for device in xknx.devices:
await device.set_on()
xknx.devices.process(xknx.telegrams.get_nowait())
assert light1.state
device2 = xknx.devices["Living-Room.Light_1"]
await device2.set_off()
xknx.devices.process(xknx.telegrams.get_nowait())
assert not light1.state
for device in xknx.devices.devices_by_group_address(GroupAddress("1/6/7")):
await device.set_on()
xknx.devices.process(xknx.telegrams.get_nowait())
assert light1.state
#
# TEST SYNC
#
@patch.multiple(Device, __abstractmethods__=set())
async def test_sync(self) -> None:
"""Test sync function."""
xknx = XKNX()
xknx.devices.async_add(Device(xknx, "TestDevice1"))
xknx.devices.async_add(Device(xknx, "TestDevice2"))
with patch("xknx.devices.Device.sync", new_callable=AsyncMock) as mock_sync:
await xknx.devices.sync()
assert mock_sync.call_count == 2
#
# TEST CALLBACK
#
@patch.multiple(Device, __abstractmethods__=set())
def test_device_updated_callback(self) -> None:
"""Test if device updated callback is called correctly if device was updated."""
xknx = XKNX()
device1 = Device(xknx, "TestDevice1")
device2 = Device(xknx, "TestDevice2")
xknx.devices.async_add(device1)
xknx.devices.async_add(device2)
after_update_callback1 = Mock()
after_update_callback2 = Mock()
# Registering both callbacks
xknx.devices.register_device_updated_cb(after_update_callback1)
xknx.devices.register_device_updated_cb(after_update_callback2)
# Triggering first device. Both callbacks to be called
device1.after_update()
after_update_callback1.assert_called_with(device1)
after_update_callback2.assert_called_with(device1)
after_update_callback1.reset_mock()
after_update_callback2.reset_mock()
# Triggering 2nd device. Both callbacks have to be called
device2.after_update()
after_update_callback1.assert_called_with(device2)
after_update_callback2.assert_called_with(device2)
after_update_callback1.reset_mock()
after_update_callback2.reset_mock()
# Unregistering first callback
xknx.devices.unregister_device_updated_cb(after_update_callback1)
# Triggering first device. Only second callback has to be called
device1.after_update()
after_update_callback1.assert_not_called()
after_update_callback2.assert_called_with(device1)
after_update_callback1.reset_mock()
after_update_callback2.reset_mock()
# Unregistering second callback
xknx.devices.unregister_device_updated_cb(after_update_callback2)
# Triggering second device. No callback should be called
device2.after_update()
after_update_callback1.assert_not_called()
after_update_callback2.assert_not_called()
after_update_callback1.reset_mock()
after_update_callback2.reset_mock()
|