コード例 #1
0
ファイル: disconnect_test.py プロジェクト: spacegaier/xknx
    def test_disconnect(self):
        """Test disconnecting from KNX bus."""
        xknx = XKNX()
        communication_channel_id = 23
        udp_client = UDPClient(xknx, ("192.168.1.1", 0), ("192.168.1.2", 1234))
        disconnect = Disconnect(xknx,
                                udp_client,
                                communication_channel_id,
                                route_back=False)
        disconnect.timeout_in_seconds = 0

        self.assertEqual(disconnect.awaited_response_class, DisconnectResponse)
        self.assertEqual(disconnect.communication_channel_id,
                         communication_channel_id)

        # Expected KNX/IP-Frame:
        exp_knxipframe = KNXIPFrame.init_from_body(
            DisconnectRequest(
                xknx,
                communication_channel_id=communication_channel_id,
                control_endpoint=HPAI(ip_addr="192.168.1.3", port=4321),
            ))
        with patch("xknx.io.UDPClient.send") as mock_udp_send, patch(
                "xknx.io.UDPClient.getsockname") as mock_udp_getsockname:
            mock_udp_getsockname.return_value = ("192.168.1.3", 4321)
            self.loop.run_until_complete(disconnect.start())
            mock_udp_send.assert_called_with(exp_knxipframe)

        # Response KNX/IP-Frame with wrong type
        wrong_knxipframe = KNXIPFrame(xknx)
        wrong_knxipframe.init(KNXIPServiceType.DISCONNECT_REQUEST)
        with patch("logging.Logger.warning") as mock_warning:
            disconnect.response_rec_callback(wrong_knxipframe, None)
            mock_warning.assert_called_with("Could not understand knxipframe")

        # Response KNX/IP-Frame with error:
        err_knxipframe = KNXIPFrame(xknx)
        err_knxipframe.init(KNXIPServiceType.DISCONNECT_RESPONSE)
        err_knxipframe.body.status_code = ErrorCode.E_CONNECTION_ID
        with patch("logging.Logger.debug") as mock_warning:
            disconnect.response_rec_callback(err_knxipframe, None)
            mock_warning.assert_called_with(
                "Error: KNX bus responded to request of type '%s' with error in '%s': %s",
                type(disconnect).__name__,
                type(err_knxipframe.body).__name__,
                ErrorCode.E_CONNECTION_ID,
            )

        # Correct Response KNX/IP-Frame:
        res_knxipframe = KNXIPFrame(xknx)
        res_knxipframe.init(KNXIPServiceType.DISCONNECT_RESPONSE)
        disconnect.response_rec_callback(res_knxipframe, None)
        self.assertTrue(disconnect.success)
コード例 #2
0
    def test_disconnect(self):
        """Test disconnecting from KNX bus."""
        xknx = XKNX(loop=self.loop)
        communication_channel_id = 23
        udp_client = UDPClient(xknx, ("192.168.1.1", 0), ("192.168.1.2", 1234))
        disconnect = Disconnect(xknx, udp_client, communication_channel_id)
        disconnect.timeout_in_seconds = 0

        self.assertEqual(disconnect.awaited_response_class, DisconnectResponse)
        self.assertEqual(disconnect.communication_channel_id,
                         communication_channel_id)

        # Expected KNX/IP-Frame:
        exp_knxipframe = KNXIPFrame(xknx)
        exp_knxipframe.init(KNXIPServiceType.DISCONNECT_REQUEST)
        exp_knxipframe.body.communication_channel_id = communication_channel_id
        exp_knxipframe.body.control_endpoint = HPAI(ip_addr='192.168.1.3',
                                                    port=4321)
        exp_knxipframe.normalize()
        with patch('xknx.io.UDPClient.send') as mock_udp_send, \
                patch('xknx.io.UDPClient.getsockname') as mock_udp_getsockname:
            mock_udp_getsockname.return_value = ("192.168.1.3", 4321)
            self.loop.run_until_complete(asyncio.Task(disconnect.start()))
            mock_udp_send.assert_called_with(exp_knxipframe)

        # Response KNX/IP-Frame with wrong type
        wrong_knxipframe = KNXIPFrame(xknx)
        wrong_knxipframe.init(KNXIPServiceType.DISCONNECT_REQUEST)
        with patch('logging.Logger.warning') as mock_warning:
            disconnect.response_rec_callback(wrong_knxipframe, None)
            mock_warning.assert_called_with('Cant understand knxipframe')

        # Response KNX/IP-Frame with error:
        err_knxipframe = KNXIPFrame(xknx)
        err_knxipframe.init(KNXIPServiceType.DISCONNECT_RESPONSE)
        err_knxipframe.body.status_code = ErrorCode.E_CONNECTION_ID
        with patch('logging.Logger.warning') as mock_warning:
            disconnect.response_rec_callback(err_knxipframe, None)
            mock_warning.assert_called_with(
                "Error: KNX bus responded to request of type '%s' with error in '%s': %s",
                type(disconnect).__name__,
                type(err_knxipframe.body).__name__, ErrorCode.E_CONNECTION_ID)

        # Correct Response KNX/IP-Frame:
        res_knxipframe = KNXIPFrame(xknx)
        res_knxipframe.init(KNXIPServiceType.DISCONNECT_RESPONSE)
        with patch('logging.Logger.debug') as mock_debug:
            disconnect.response_rec_callback(res_knxipframe, None)
            mock_debug.assert_called_with(
                'Success: received correct answer from KNX bus: %s',
                ErrorCode.E_NO_ERROR)
            self.assertTrue(disconnect.success)
コード例 #3
0
async def main():
    """Search for a Tunnelling device, walk through all possible channels and disconnect them."""
    xknx = XKNX()
    gatewayscanner = GatewayScanner(xknx)
    gateways = await gatewayscanner.scan()

    if not gateways:
        print("No Gateways found")
        return

    gateway = gateways[0]

    if not gateway.supports_tunnelling:
        print("Gateway does not support tunneling")
        return

    udp_client = UDPClient(xknx, (gateway.local_ip, 0), (gateway.ip_addr, gateway.port))

    await udp_client.connect()

    for i in range(0, 255):

        conn_state = ConnectionState(xknx, udp_client, communication_channel_id=i)

        await conn_state.start()

        if conn_state.success:
            print("Disconnecting ", i)
            disconnect = Disconnect(xknx, udp_client, communication_channel_id=i)

            await disconnect.start()

            if disconnect.success:
                print("Disconnected ", i)
コード例 #4
0
ファイル: disconnect_test.py プロジェクト: phbaer/xknx
    def test_disconnect(self):
        """Test disconnecting from KNX bus."""
        xknx = XKNX(loop=self.loop)
        communication_channel_id = 23
        udp_client = UDPClient(xknx, ("192.168.1.1", 0), ("192.168.1.2", 1234))
        disconnect = Disconnect(xknx, udp_client, communication_channel_id)
        disconnect.timeout_in_seconds = 0

        self.assertEqual(disconnect.awaited_response_class, DisconnectResponse)
        self.assertEqual(disconnect.communication_channel_id, communication_channel_id)

        # Expected KNX/IP-Frame:
        exp_knxipframe = KNXIPFrame(xknx)
        exp_knxipframe.init(KNXIPServiceType.DISCONNECT_REQUEST)
        exp_knxipframe.body.communication_channel_id = communication_channel_id
        exp_knxipframe.body.control_endpoint = HPAI(
            ip_addr='192.168.1.3', port=4321)
        exp_knxipframe.normalize()
        with patch('xknx.io.UDPClient.send') as mock_udp_send, \
                patch('xknx.io.UDPClient.getsockname') as mock_udp_getsockname:
            mock_udp_getsockname.return_value = ("192.168.1.3", 4321)
            self.loop.run_until_complete(asyncio.Task(disconnect.start()))
            mock_udp_send.assert_called_with(exp_knxipframe)

        # Response KNX/IP-Frame with wrong type
        wrong_knxipframe = KNXIPFrame(xknx)
        wrong_knxipframe.init(KNXIPServiceType.DISCONNECT_REQUEST)
        with patch('logging.Logger.warning') as mock_warning:
            disconnect.response_rec_callback(wrong_knxipframe, None)
            mock_warning.assert_called_with('Cant understand knxipframe')

        # Response KNX/IP-Frame with error:
        err_knxipframe = KNXIPFrame(xknx)
        err_knxipframe.init(KNXIPServiceType.DISCONNECT_RESPONSE)
        err_knxipframe.body.status_code = ErrorCode.E_CONNECTION_ID
        with patch('logging.Logger.warning') as mock_warning:
            disconnect.response_rec_callback(err_knxipframe, None)
            mock_warning.assert_called_with("Error: KNX bus responded to request of type '%s' with error in '%s': %s",
                                            type(disconnect).__name__,
                                            type(err_knxipframe.body).__name__, ErrorCode.E_CONNECTION_ID)

        # Correct Response KNX/IP-Frame:
        res_knxipframe = KNXIPFrame(xknx)
        res_knxipframe.init(KNXIPServiceType.DISCONNECT_RESPONSE)
        with patch('logging.Logger.debug') as mock_debug:
            disconnect.response_rec_callback(res_knxipframe, None)
            mock_debug.assert_called_with('Success: received correct answer from KNX bus: %s', ErrorCode.E_NO_ERROR)
            self.assertTrue(disconnect.success)
コード例 #5
0
print("Connecting to {}:{} from {}".format(gatewayscanner.found_ip_addr,
                                           gatewayscanner.found_port,
                                           gatewayscanner.found_local_ip))

own_ip = "192.168.42.1"
gateway_ip = "192.168.42.10"
gateway_port = 3671

udp_client = UDPClient(
    xknx, (gatewayscanner.found_local_ip, 0),
    (gatewayscanner.found_ip_addr, gatewayscanner.found_port))

task = asyncio.Task(udp_client.connect())

xknx.loop.run_until_complete(task)

for i in range(0, 255):

    conn_state = ConnectionState(xknx, udp_client, communication_channel_id=i)

    conn_state.start()

    if conn_state.success:
        print("Disconnecting ", i)
        disconnect = Disconnect(xknx, udp_client, communication_channel_id=i)

        disconnect.start()

        if disconnect.success:
            print("Disconnected ", i)