def test_negative_filters(self): """Test telegram_received_callback after state of switch was changed.""" # pylint: disable=no-self-use xknx = XKNX() telegram_received_callback = Mock() async def async_telegram_received_cb(device): """Async callback.""" telegram_received_callback(device) xknx.telegram_queue.register_telegram_received_cb( async_telegram_received_cb, [AddressFilter("2/4-8/*"), AddressFilter("1/2/8-")], ) telegram = Telegram( direction=TelegramDirection.INCOMING, payload=DPTBinary(1), group_address=GroupAddress("1/2/3"), ) xknx.telegrams.put_nowait(telegram) self.loop.run_until_complete( xknx.telegram_queue._process_all_telegrams()) telegram_received_callback.assert_not_called()
def test_address_filter_level3_1(self): """Test AddressFilter 1st part of level3 addresses.""" af1 = AddressFilter("4/2/3") assert af1.match("4/2/3") assert not af1.match("2/2/3") assert not af1.match("10/2/3") af2 = AddressFilter("2-/4/3") assert not af2.match("1/4/3") assert af2.match("2/4/3") assert af2.match("10/4/3") af3 = AddressFilter("*/5/5") assert af3.match("2/5/5") assert af3.match("8/5/5")
def test_address_filter_level3_2(self): """Test AddressFilter 2nd part of level3 addresses.""" af1 = AddressFilter("1/2/3") assert af1.match("1/2/3") assert not af1.match("1/3/3") assert not af1.match("1/1/3") af2 = AddressFilter("1/2-/3") assert not af2.match("1/1/3") assert af2.match("1/2/3") assert af2.match("1/5/3") af3 = AddressFilter("1/*/3") assert af3.match("1/4/3") assert af3.match("1/7/3")
def test_address_filter_level2_2(self): """Test AddressFilter 2nd part of level2 addresses.""" af1 = AddressFilter("2/3") assert af1.match("2/3") assert not af1.match("2/4") assert not af1.match("2/1") af2 = AddressFilter("2/3-4,7-") assert not af2.match("2/2") assert af2.match("2/3") assert not af2.match("2/6") assert af2.match("2/8") af3 = AddressFilter("2/*") assert af3.match("2/3") assert af3.match("2/5")
def test_address_filter_level3_3(self): """Test AddressFilter 3rd part of level3 addresses.""" af1 = AddressFilter("1/2/3") assert af1.match("1/2/3") assert not af1.match("1/2/4") assert not af1.match("1/2/1") af2 = AddressFilter("1/2/2-3,5-") assert not af2.match("1/2/1") assert af2.match("1/2/3") assert not af2.match("1/2/4") assert af2.match("1/2/6") af3 = AddressFilter("1/2/*") assert af3.match("1/2/3") assert af3.match("1/2/5")
def test_address_filter_level2_1(self): """Test AddressFilter 1st part of level2 addresses.""" af1 = AddressFilter("4/2") self.assertTrue(af1.match("4/2")) self.assertFalse(af1.match("2/2")) self.assertFalse(af1.match("10/2")) af2 = AddressFilter("2-3,8-/4") self.assertFalse(af2.match("1/4")) self.assertTrue(af2.match("2/4")) self.assertFalse(af2.match("7/4")) self.assertTrue(af2.match("10/4")) af3 = AddressFilter("*/5") self.assertTrue(af3.match("2/5")) self.assertTrue(af3.match("8/5"))
def test_internal_address_filter_wildcard(self): """Test AddressFilter with wildcards for InternalGroupAddress.""" af1 = AddressFilter("i-?") assert af1.match("i1") assert af1.match("i 2") assert af1.match("i-3") assert af1.match(InternalGroupAddress("i-4")) assert not af1.match("1") assert not af1.match(GroupAddress(1)) assert not af1.match("i11") assert not af1.match("i 11") assert not af1.match("i-11") assert not af1.match(InternalGroupAddress("i-11")) af2 = AddressFilter("i-t?st") assert af2.match("it1st") assert af2.match("i t2st") assert af2.match("i-test") assert af2.match(InternalGroupAddress("i-test")) assert not af2.match("1") assert not af2.match(GroupAddress(1)) assert not af2.match("i11") assert not af2.match("i tst") assert not af2.match("i-teest") assert not af2.match(InternalGroupAddress("i-tst")) af3 = AddressFilter("i-*") assert af3.match("i1") assert af3.match("i asdf") assert af3.match("i-3sdf") assert af3.match(InternalGroupAddress("i-4")) assert not af3.match("1") assert not af3.match(GroupAddress(1)) assert af3.match("i11") assert af3.match("i 11??") assert af3.match("i-11*") assert af3.match(InternalGroupAddress("i-11")) af4 = AddressFilter("i-t*t") assert af4.match("it1t") assert af4.match("i t22t") assert af4.match("i-testt") assert af4.match("i-tt") assert af4.match(InternalGroupAddress("i-t333t")) assert not af4.match("1") assert not af4.match(GroupAddress(1)) assert not af4.match("i testx") assert not af4.match("i-11test") assert not af4.match(InternalGroupAddress("i-11"))
def test_address_filter_free(self): """Test AddressFilter free format addresses.""" af1 = AddressFilter("4") assert af1.match("4") assert not af1.match("1") assert not af1.match("10") af2 = AddressFilter("1,4,7-") assert af2.match("1") assert not af2.match("2") assert af2.match("4") assert not af2.match("6") assert af2.match("60") af3 = AddressFilter("*") assert af3.match("2") assert af3.match("8")
def test_internal_address_filter_exact(self): """Test AddressFilter for InternalGroupAddress.""" af1 = AddressFilter("i-1") assert af1.match("i1") assert af1.match("i 1") assert af1.match("i-1") assert af1.match(InternalGroupAddress("i-1")) assert not af1.match("1") assert not af1.match(GroupAddress(1))
def test_address_combined(self): """Test AddressFilter with complex filters.""" af1 = AddressFilter("2-/2,3,5-/*") assert af1.match("2/3/8") assert af1.match("4/7/10") assert af1.match("2/7/10") assert not af1.match("1/7/10") assert not af1.match("2/4/10") assert not af1.match("2/1/10")
async def test_callback_negative_address_filters(self): """Test telegram_received_callback after state of switch was changed.""" xknx = XKNX() async_telegram_received_callback = AsyncMock() xknx.telegram_queue.register_telegram_received_cb( async_telegram_received_callback, address_filters=[AddressFilter("2/4-8/*"), AddressFilter("1/2/8-")], ) telegram = Telegram( destination_address=GroupAddress("1/2/3"), direction=TelegramDirection.INCOMING, payload=GroupValueWrite(DPTBinary(1)), ) xknx.telegrams.put_nowait(telegram) await xknx.telegram_queue._process_all_telegrams() async_telegram_received_callback.assert_not_called()
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) telegram_received_callback = Mock() async def async_telegram_received_cb(device): """Async callback.""" telegram_received_callback(device) xknx.telegram_queue.register_telegram_received_cb( async_telegram_received_cb, [AddressFilter("2/4-8/*"), AddressFilter("1/2/-8")]) telegram = Telegram(direction=TelegramDirection.INCOMING, payload=DPTBinary(1), group_address=GroupAddress("1/2/3")) self.loop.run_until_complete( asyncio.Task(xknx.telegram_queue.process_telegram(telegram))) telegram_received_callback.assert_called_with(telegram)
def test_initialize_wrong_format(self): """Test if wrong address format raises exception.""" with pytest.raises(ConversionError): AddressFilter("2-/2,3/4/5/1,5-/*")
async def test_binary_sensor_loop(self, value_type, test_payload, test_value): """Test binary_sensor and expose_sensor with binary values.""" xknx = XKNX() xknx.knxip_interface = AsyncMock() xknx.rate_limit = False telegram_callback = AsyncMock() xknx.telegram_queue.register_telegram_received_cb( telegram_callback, address_filters=[AddressFilter("i-test")], match_for_outgoing=True, ) await xknx.telegram_queue.start() expose = ExposeSensor( xknx, "TestExpose", group_address="i-test", value_type=value_type, ) assert expose.resolve_state() is None await expose.set(test_value) await xknx.telegrams.join() outgoing_telegram = Telegram( destination_address=InternalGroupAddress("i-test"), direction=TelegramDirection.OUTGOING, payload=GroupValueWrite(test_payload), ) # InternalGroupAddress isn't passed to knxip_interface xknx.knxip_interface.send_telegram.assert_not_called() telegram_callback.assert_called_with(outgoing_telegram) assert expose.resolve_state() == test_value bin_sensor = BinarySensor(xknx, "TestSensor", group_address_state="i-test") assert bin_sensor.state is None # read sensor state (from expose as it has the same GA) # wait_for_result so we don't have to await self.xknx.telegrams.join() await bin_sensor.sync(wait_for_result=True) read_telegram = Telegram( destination_address=InternalGroupAddress("i-test"), direction=TelegramDirection.OUTGOING, payload=GroupValueRead(), ) response_telegram = Telegram( destination_address=InternalGroupAddress("i-test"), direction=TelegramDirection.OUTGOING, payload=GroupValueResponse(test_payload), ) xknx.knxip_interface.send_telegram.assert_not_called() telegram_callback.assert_has_calls( [ call(read_telegram), call(response_telegram), ] ) # test if Sensor has successfully read from ExposeSensor assert bin_sensor.state == test_value assert expose.resolve_state() == bin_sensor.state await xknx.telegram_queue.stop()