Skip to content

Commit

Permalink
Remove loop argument (#433)
Browse files Browse the repository at this point in the history
* remove loop argument from XKNX()

use asyncio.get_running_loop() and asyncio.create_task()
(or non-async method in knxip_interface)

* test without loop argument

* Review: Fix date time tests. By the time you add the tasks to asyncio the loop is not yet running thus the error occurs.

Simply add the tasks to the created (but not yet started) loop and wait for it to run.

Co-authored-by: Marvin Wichmann <marvin.wichmann@unic.com>
  • Loading branch information
farmio and Marvin Wichmann committed Sep 27, 2020
1 parent 3fcbe6c commit 3c2d3e0
Show file tree
Hide file tree
Showing 68 changed files with 613 additions and 618 deletions.
1 change: 0 additions & 1 deletion home-assistant-plugin/custom_components/xknx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def init_xknx(self):
"""Initialize of KNX object."""
self.xknx = XKNX(
config=self.config_file(),
loop=self.hass.loop,
own_address=self.config[DOMAIN][CONF_XKNX_INDIVIDUAL_ADDRESS],
rate_limit=self.config[DOMAIN][CONF_XKNX_RATE_LIMIT],
multicast_group=self.config[DOMAIN][CONF_XKNX_MCAST_GRP],
Expand Down
6 changes: 3 additions & 3 deletions test/core_tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def setUpClass(cls):
)
cls.datetime_patcher.start()

cls.xknx = XKNX(config="xknx.yaml", loop=cls.loop)
cls.xknx = XKNX(config="xknx.yaml")

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -553,7 +553,7 @@ def test_config_weather_expose_sensor(self):
def test_config_file_not_found(self):
"""Test error message when reading a non exisiting config file."""
with patch("logging.Logger.error") as mock_err:
XKNX(config="xknx_does_not_exist.yaml", loop=self.loop)
XKNX(config="xknx_does_not_exist.yaml")
self.assertEqual(mock_err.call_count, 1)

def test_config_file_error(self):
Expand All @@ -562,5 +562,5 @@ def test_config_file_error(self):
"xknx.core.Config.parse_group_light"
) as mock_parse:
mock_parse.side_effect = XKNXException()
XKNX(config="xknx.yaml", loop=self.loop)
XKNX(config="xknx.yaml")
self.assertEqual(mock_err.call_count, 1)
8 changes: 4 additions & 4 deletions test/core_tests/state_updater_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def tearDown(self):

def test_register_unregister(self):
"""Test register and unregister."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
self.assertEqual(len(xknx.state_updater._workers), 0)
# register when state address and sync_state is set
remote_value_1 = RemoteValue(
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_register_unregister(self):

def test_tracker_parser(self):
"""Test parsing tracker options."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()

def _get_only_tracker() -> _StateTracker:
# _workers is unordered so it just works with 1 item
Expand Down Expand Up @@ -104,7 +104,7 @@ def _get_only_tracker() -> _StateTracker:
@patch("logging.Logger.warning")
def test_tracker_parser_invalid_options(self, logging_warning_mock):
"""Test parsing invalid tracker options."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()

def _get_only_tracker() -> _StateTracker:
# _workers is unordered so it just works with 1 item
Expand Down Expand Up @@ -152,7 +152,7 @@ def _get_only_tracker() -> _StateTracker:

def test_state_updater_start_update_stop(self):
"""Test start, update_received and stop of StateUpdater."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
remote_value_1 = RemoteValue(
xknx, sync_state=True, group_address_state=GroupAddress("1/1/1")
)
Expand Down
24 changes: 12 additions & 12 deletions test/core_tests/telegram_queue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def tearDown(self):
def test_start(self):
"""Test start, run and stop."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

telegram_in = Telegram(
direction=TelegramDirection.INCOMING,
Expand Down Expand Up @@ -61,7 +61,7 @@ async def async_none():

async_sleep_mock.return_value = asyncio.ensure_future(async_none())

xknx = XKNX(loop=self.loop)
xknx = XKNX()
xknx.rate_limit = 20 # 50 ms per outgoing telegram
sleep_time = 0.05 # 1 / 20

Expand Down Expand Up @@ -99,7 +99,7 @@ async def async_none():
def test_register(self):
"""Test telegram_received_callback after state of switch was changed."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

telegram_received_callback = Mock()

Expand All @@ -125,7 +125,7 @@ async def async_telegram_received_cb(device):
def test_unregister(self):
"""Test telegram_received_callback after state of switch was changed."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

telegram_received_callback = Mock()

Expand Down Expand Up @@ -155,7 +155,7 @@ async def async_telegram_received_cb(device):
def test_process_to_device(self, devices_by_ga_mock):
"""Test process_telegram_incoming for forwarding telegram to a device."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

test_device = Mock()
async_device_process = asyncio.Future()
Expand All @@ -180,7 +180,7 @@ def test_process_to_device(self, devices_by_ga_mock):
def test_process_to_callback(self, devices_by_ga_mock):
"""Test process_telegram_incoming for returning after processing callback."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

telegram_received_callback = Mock()

Expand All @@ -207,7 +207,7 @@ async def async_telegram_received_cb(device):
def test_outgoing(self, logger_warning_mock, if_mock):
"""Test outgoing telegrams in telegram queue."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

async_if_send_telegram = asyncio.Future()
async_if_send_telegram.set_result(None)
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_outgoing(self, logger_warning_mock, if_mock):
def test_process_exception(self, process_tg_in_mock, logging_error_mock):
"""Test process_telegram exception handling."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

async def process_exception():
raise CouldNotParseTelegram(
Expand Down Expand Up @@ -270,7 +270,7 @@ def test_process_all_telegrams(
):
"""Test _process_all_telegrams for clearing the queue."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

async_process_mock = asyncio.Future()
async_process_mock.set_result(None)
Expand Down Expand Up @@ -302,7 +302,7 @@ def test_process_all_telegrams(
def test_no_filters(self):
"""Test telegram_received_callback after state of switch was changed."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

telegram_received_callback = Mock()

Expand All @@ -328,7 +328,7 @@ async def async_telegram_received_cb(device):
def test_positive_filters(self):
"""Test telegram_received_callback after state of switch was changed."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

telegram_received_callback = Mock()

Expand Down Expand Up @@ -357,7 +357,7 @@ async def async_telegram_received_cb(device):
def test_negative_filters(self):
"""Test telegram_received_callback after state of switch was changed."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()

telegram_received_callback = Mock()

Expand Down
8 changes: 4 additions & 4 deletions test/core_tests/value_reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def tearDown(self):
@patch("xknx.core.ValueReader.timeout")
def test_value_reader_read_success(self, timeout_mock):
"""Test value reader: successfull read."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
test_group_address = GroupAddress("0/0/0")
response_telegram = Telegram(
group_address=test_group_address,
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_value_reader_read_success(self, timeout_mock):
@patch("logging.Logger.warning")
def test_value_reader_read_timeout(self, logger_warning_mock):
"""Test value reader: read timeout."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
value_reader = ValueReader(xknx, GroupAddress("0/0/0"), timeout_in_seconds=0)

timed_out_read = self.loop.run_until_complete(value_reader.read())
Expand All @@ -85,7 +85,7 @@ def test_value_reader_read_timeout(self, logger_warning_mock):

def test_value_reader_send_group_read(self):
"""Test value reader: send_group_read."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
value_reader = ValueReader(xknx, GroupAddress("0/0/0"))

self.loop.run_until_complete(value_reader.send_group_read())
Expand All @@ -101,7 +101,7 @@ def test_value_reader_send_group_read(self):

def test_value_reader_telegram_received(self):
"""Test value reader: telegram_received."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
test_group_address = GroupAddress("0/0/0")
expected_telegram_1 = Telegram(
group_address=test_group_address,
Expand Down
16 changes: 8 additions & 8 deletions test/devices_tests/action_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def tearDown(self):
#
def test_counter(self):
"""Test counter method."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
action = ActionBase(xknx, counter=2)
self.assertTrue(action.test_counter(None))
self.assertFalse(action.test_counter(1))
Expand All @@ -33,7 +33,7 @@ def test_counter(self):

def test_no_counter(self):
"""Test counter method with no counter set."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
action = ActionBase(xknx, counter=None)
self.assertTrue(action.test_counter(None))
self.assertTrue(action.test_counter(1))
Expand All @@ -45,15 +45,15 @@ def test_no_counter(self):
#
def test_if_applicable_hook_on(self):
"""Test test_if_applicable method with hook set to 'on'."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
action = ActionBase(xknx, counter=2, hook="on")
self.assertTrue(action.test_if_applicable(True, 2))
self.assertFalse(action.test_if_applicable(True, 3))
self.assertFalse(action.test_if_applicable(False, 2))

def test_if_applicable_hook_off(self):
"""Test test_if_applicable method with hook set to 'off'."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
action = ActionBase(xknx, counter=2, hook="off")
self.assertTrue(action.test_if_applicable(False, 2))
self.assertFalse(action.test_if_applicable(False, 3))
Expand All @@ -64,15 +64,15 @@ def test_if_applicable_hook_off(self):
#
def test_execute_base_action(self):
"""Test if execute method of BaseAction shows correct info message."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
action = ActionBase(xknx)
with patch("logging.Logger.info") as mock_info:
self.loop.run_until_complete(action.execute())
mock_info.assert_called_with("Execute not implemented for %s", "ActionBase")

def test_execute_action(self):
"""Test if execute method of Action calls correct do method of device."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
light = Light(xknx, "Light1", group_address_switch="1/6/4")
action = Action(xknx, target=light.name, method="on")
with patch("xknx.devices.Light.do") as mock_do:
Expand All @@ -84,7 +84,7 @@ def test_execute_action(self):

def test_execute_action_callback(self):
"""Test if execute method of ActionCallback calls correct callback method."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
callback = Mock()

async def async_callback():
Expand All @@ -97,7 +97,7 @@ async def async_callback():

def test_execute_unknown_device(self):
"""Test if execute method of Action calls correct do method of device."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()

action = Action(xknx, target="Light1", method="on")
with patch("logging.Logger.warning") as logger_warning_mock:
Expand Down
18 changes: 9 additions & 9 deletions test/devices_tests/binary_sensor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def tearDown(self):
#
def test_process(self):
"""Test process / reading telegrams from telegram queue."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")

self.assertEqual(binaryinput.state, None)
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_process(self):

def test_process_reset_after(self):
"""Test process / reading telegrams from telegram queue."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
reset_after_ms = 0.01
binaryinput = BinarySensor(
xknx, "TestInput", "1/2/3", reset_after=reset_after_ms
Expand All @@ -76,7 +76,7 @@ def test_process_reset_after(self):

def test_process_action(self):
"""Test process / reading telegrams from telegram queue. Test if action is executed."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
switch = Switch(xknx, "TestOutlet", group_address="1/2/3")

binary_sensor = BinarySensor(xknx, "TestInput", group_address_state="1/2/3")
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_process_action(self):

def test_process_action_ignore_internal_state(self):
"""Test process / reading telegrams from telegram queue. Test if action is executed."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
switch = Switch(xknx, "TestOutlet", group_address="5/5/5")

binary_sensor = BinarySensor(
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_process_action_ignore_internal_state(self):

def test_process_wrong_payload(self):
"""Test process wrong telegram (wrong payload type)."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
binary_sensor = BinarySensor(xknx, "Warning", group_address_state="1/2/3")
telegram = Telegram(GroupAddress("1/2/3"), payload=DPTArray((0x1, 0x2, 0x3)))
with self.assertRaises(CouldNotParseTelegram):
Expand All @@ -169,7 +169,7 @@ def test_process_wrong_payload(self):
#
def test_is_on(self):
"""Test is_on() and is_off() of a BinarySensor with state 'on'."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")
self.assertFalse(binaryinput.is_on())
self.assertTrue(binaryinput.is_off())
Expand All @@ -184,7 +184,7 @@ def test_is_on(self):
#
def test_is_off(self):
"""Test is_on() and is_off() of a BinarySensor with state 'off'."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
binaryinput = BinarySensor(xknx, "TestInput", "1/2/3")
# pylint: disable=protected-access
self.loop.run_until_complete(binaryinput._set_internal_state(False))
Expand All @@ -198,7 +198,7 @@ def test_is_off(self):
def test_process_callback(self):
"""Test after_update_callback after state of switch was changed."""
# pylint: disable=no-self-use
xknx = XKNX(loop=self.loop)
xknx = XKNX()
switch = BinarySensor(xknx, "TestInput", group_address_state="1/2/3")

after_update_callback = Mock()
Expand All @@ -219,7 +219,7 @@ async def async_after_update_callback(device):
#
def test_counter(self):
"""Test counter functionality."""
xknx = XKNX(loop=self.loop)
xknx = XKNX()
switch = BinarySensor(xknx, "TestInput", group_address_state="1/2/3")
with patch("time.time") as mock_time:
mock_time.return_value = 1517000000.0
Expand Down

0 comments on commit 3c2d3e0

Please sign in to comment.